Skip to content

Commit 8adc6a8

Browse files
fix: add validation for literals (#64)
Co-authored-by: jialinli <[email protected]>
1 parent b494199 commit 8adc6a8

File tree

1 file changed

+54
-7
lines changed

1 file changed

+54
-7
lines changed

src/json.nr

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,36 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
607607
let index_valid: Field = range_valid[i] as Field;
608608
// 1 gate
609609
let entry = TranscriptEntry::to_field(TranscriptEntry { token, index, length });
610+
611+
if token == LITERAL_TOKEN as Field {
612+
index.assert_max_bit_size::<8>();
613+
let index_as_u32 = index as u32;
614+
let first_char = self.json[index_as_u32];
615+
let second_char = self.json[index_as_u32 + 1];
616+
let third_char = self.json[index_as_u32 + 2];
617+
let fourth_char = self.json[index_as_u32 + 3];
618+
if length == 5 {
619+
let fifth_char = self.json[index_as_u32 + 4];
620+
let is_false = (first_char == 102) // 'f'
621+
& (second_char == 97) // 'a'
622+
& (third_char == 108) // 'l'
623+
& (fourth_char == 115) // 's'
624+
& (fifth_char == 101); // 'e'
625+
assert(is_false, "invalid literal");
626+
} else {
627+
assert_eq(length, 4, "invalid literal");
628+
let is_true = (first_char == 116) // 't'
629+
& (second_char == 114) // 'r'
630+
& (third_char == 117) // 'u'
631+
& (fourth_char == 101); // 'e'
632+
633+
let is_null = (first_char == 110) // 'n'
634+
& (second_char == 117) // 'u'
635+
& (third_char == 108) // 'l'
636+
& (fourth_char == 108); // 'l'
637+
assert(is_null | is_true, "invalid literal");
638+
}
639+
}
610640
// 2 gates
611641
let diff = updated_transcript[cast_num_to_u32(transcript_ptr)] - entry;
612642
std::as_witness(diff);
@@ -923,13 +953,6 @@ fn test_json_char_outside_of_string_fails() {
923953
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
924954
}
925955

926-
#[test(should_fail_with = "ValidationFlags: grammar error")]
927-
fn test_json_char_outside_of_string_fails_2() {
928-
// n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags
929-
let text = "{ \"hello \", \"world\" n}";
930-
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
931-
}
932-
933956
#[test(should_fail_with = "ValidationFlags: grammar error")]
934957
fn test_json_array_with_invalid_tokens_fails() {
935958
// n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags
@@ -981,3 +1004,27 @@ fn key_is_not_a_key() {
9811004
let json_string = "{1\n:0}";
9821005
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(json_string);
9831006
}
1007+
1008+
#[test(should_fail_with = "invalid literal")]
1009+
fn test_invalid_literal() {
1010+
let text = "{ \"name\":fal }";
1011+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1012+
}
1013+
1014+
#[test(should_fail_with = "invalid literal")]
1015+
fn test_invalid_literal_2() {
1016+
let text = "{ \"name\":treu}";
1017+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1018+
}
1019+
1020+
#[test(should_fail_with = "invalid literal")]
1021+
fn test_invalid_literal_3() {
1022+
let text = "{ \"name\":truea }";
1023+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1024+
}
1025+
1026+
#[test(should_fail_with = "invalid literal")]
1027+
fn test_invalid_literal_4() {
1028+
let text = "{ \"hello \", \"world\" n}";
1029+
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
1030+
}

0 commit comments

Comments
 (0)