Skip to content

Commit 8a985b5

Browse files
committed
add literal validations
1 parent ef91dec commit 8a985b5

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

src/json.nr

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,32 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
759759
let index_valid: Field = range_valid[i] as Field;
760760
// 1 gate
761761
let entry = TranscriptEntry::to_field(TranscriptEntry { token, index, length });
762+
763+
if token == LITERAL_TOKEN as Field {
764+
let index_as_u32 = index as u32;
765+
index.assert_max_bit_size::<8>();
766+
if length == 5 {
767+
let is_false = (self.json[index_as_u32] == 102) // 'f'
768+
& (self.json[index_as_u32 + 1] == 97) // 'a'
769+
& (self.json[index_as_u32 + 2] == 108) // 'l'
770+
& (self.json[index_as_u32 + 3] == 115) // 's'
771+
& (self.json[index_as_u32 + 4] == 101); // 'e'
772+
assert(is_false, "invalid literal");
773+
} else if length == 4 {
774+
let is_true = (self.json[index_as_u32] == 116) // 't'
775+
& (self.json[index_as_u32 + 1] == 114) // 'r'
776+
& (self.json[index_as_u32 + 2] == 117) // 'u'
777+
& (self.json[index_as_u32 + 3] == 101); // 'e'
778+
779+
let is_null = (self.json[index_as_u32] == 110) // 'n'
780+
& (self.json[index_as_u32 + 1] == 117) // 'u'
781+
& (self.json[index_as_u32 + 2] == 108) // 'l'
782+
& (self.json[index_as_u32 + 3] == 108); // 'l'
783+
assert(is_null | is_true, "invalid literal");
784+
} else {
785+
assert(false, "invalid literal");
786+
}
787+
}
762788
// 2 gates
763789
let diff = updated_transcript[cast_num_to_u32(transcript_ptr)] - entry;
764790
std::as_witness(diff);
@@ -1075,13 +1101,6 @@ fn test_json_char_outside_of_string_fails() {
10751101
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
10761102
}
10771103

1078-
#[test(should_fail_with = "ValidationFlags: grammar error")]
1079-
fn test_json_char_outside_of_string_fails_2() {
1080-
// n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags
1081-
let text = "{ \"hello \", \"world\" n}";
1082-
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
1083-
}
1084-
10851104
#[test(should_fail_with = "ValidationFlags: grammar error")]
10861105
fn test_json_array_with_invalid_tokens_fails() {
10871106
// n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags
@@ -1133,3 +1152,27 @@ fn key_is_not_a_key() {
11331152
let json_string = "{1\n:0}";
11341153
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(json_string);
11351154
}
1155+
1156+
#[test(should_fail_with = "invalid literal")]
1157+
fn test_invalid_literal() {
1158+
let text = "{ \"name\":fal }";
1159+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1160+
}
1161+
1162+
#[test(should_fail_with = "invalid literal")]
1163+
fn test_invalid_literal_2() {
1164+
let text = "{ \"name\":treu}";
1165+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1166+
}
1167+
1168+
#[test(should_fail_with = "invalid literal")]
1169+
fn test_invalid_literal_3() {
1170+
let text = "{ \"name\":truea }";
1171+
let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text);
1172+
}
1173+
1174+
#[test(should_fail_with = "invalid literal")]
1175+
fn test_invalid_literal_4() {
1176+
let text = "{ \"hello \", \"world\" n}";
1177+
let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text);
1178+
}

0 commit comments

Comments
 (0)