From 02af66edfecc910df165d4aad42b1266d7cf8f24 Mon Sep 17 00:00:00 2001 From: jialinli Date: Wed, 6 Aug 2025 00:36:31 -0700 Subject: [PATCH 1/4] add literal validations --- src/json.nr | 57 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/src/json.nr b/src/json.nr index 660598d..a6fff38 100644 --- a/src/json.nr +++ b/src/json.nr @@ -607,6 +607,32 @@ impl(); + if length == 5 { + let is_false = (self.json[index_as_u32] == 102) // 'f' + & (self.json[index_as_u32 + 1] == 97) // 'a' + & (self.json[index_as_u32 + 2] == 108) // 'l' + & (self.json[index_as_u32 + 3] == 115) // 's' + & (self.json[index_as_u32 + 4] == 101); // 'e' + assert(is_false, "invalid literal"); + } else if length == 4 { + let is_true = (self.json[index_as_u32] == 116) // 't' + & (self.json[index_as_u32 + 1] == 114) // 'r' + & (self.json[index_as_u32 + 2] == 117) // 'u' + & (self.json[index_as_u32 + 3] == 101); // 'e' + + let is_null = (self.json[index_as_u32] == 110) // 'n' + & (self.json[index_as_u32 + 1] == 117) // 'u' + & (self.json[index_as_u32 + 2] == 108) // 'l' + & (self.json[index_as_u32 + 3] == 108); // 'l' + assert(is_null | is_true, "invalid literal"); + } else { + assert(false, "invalid literal"); + } + } // 2 gates let diff = updated_transcript[cast_num_to_u32(transcript_ptr)] - entry; std::as_witness(diff); @@ -923,13 +949,6 @@ fn test_json_char_outside_of_string_fails() { let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text); } -#[test(should_fail_with = "ValidationFlags: grammar error")] -fn test_json_char_outside_of_string_fails_2() { - // n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags - let text = "{ \"hello \", \"world\" n}"; - let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text); -} - #[test(should_fail_with = "ValidationFlags: grammar error")] fn test_json_array_with_invalid_tokens_fails() { // n could be the start of the literal "null", so this passes the ScanData check but fails ValidationFlags @@ -981,3 +1000,27 @@ fn key_is_not_a_key() { let json_string = "{1\n:0}"; let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(json_string); } + +#[test(should_fail_with = "invalid literal")] +fn test_invalid_literal() { + let text = "{ \"name\":fal }"; + let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text); +} + +#[test(should_fail_with = "invalid literal")] +fn test_invalid_literal_2() { + let text = "{ \"name\":treu}"; + let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text); +} + +#[test(should_fail_with = "invalid literal")] +fn test_invalid_literal_3() { + let text = "{ \"name\":truea }"; + let _: JSON<153, 10, 60, 60, 2> = JSON::parse_json_from_string(text); +} + +#[test(should_fail_with = "invalid literal")] +fn test_invalid_literal_4() { + let text = "{ \"hello \", \"world\" n}"; + let _: JSON<26, 10, 20, 20, 2> = JSON::parse_json_from_string(text); +} From 96999c3ffc2f87e1170c83f51880240d054ace08 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:56:34 +0000 Subject: [PATCH 2/4] . --- src/json.nr | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/json.nr b/src/json.nr index a6fff38..0f118f3 100644 --- a/src/json.nr +++ b/src/json.nr @@ -609,28 +609,32 @@ impl(); + let index_as_u32 = index as u32; + let first_char = self.json[index_as_u32]; + let second_char = self.json[index_as_u32 + 1]; + let third_char = self.json[index_as_u32 + 2]; + let fourth_char = self.json[index_as_u32 + 3]; if length == 5 { - let is_false = (self.json[index_as_u32] == 102) // 'f' - & (self.json[index_as_u32 + 1] == 97) // 'a' - & (self.json[index_as_u32 + 2] == 108) // 'l' - & (self.json[index_as_u32 + 3] == 115) // 's' - & (self.json[index_as_u32 + 4] == 101); // 'e' + let fifth_char = self.json[index_as_u32 + 4]; + let is_false = (first_char == 102) // 'f' + & (second_char == 97) // 'a' + & (third_char == 108) // 'l' + & (fourth_char == 115) // 's' + & (fifth_char == 101); // 'e' assert(is_false, "invalid literal"); - } else if length == 4 { - let is_true = (self.json[index_as_u32] == 116) // 't' - & (self.json[index_as_u32 + 1] == 114) // 'r' - & (self.json[index_as_u32 + 2] == 117) // 'u' - & (self.json[index_as_u32 + 3] == 101); // 'e' - - let is_null = (self.json[index_as_u32] == 110) // 'n' - & (self.json[index_as_u32 + 1] == 117) // 'u' - & (self.json[index_as_u32 + 2] == 108) // 'l' - & (self.json[index_as_u32 + 3] == 108); // 'l' - assert(is_null | is_true, "invalid literal"); } else { - assert(false, "invalid literal"); + assert_eq(length, 4, "invalid literal length"); + let is_true = (first_char == 116) // 't' + & (second_char == 114) // 'r' + & (third_char == 117) // 'u' + & (fourth_char == 101); // 'e' + + let is_null = (first_char == 110) // 'n' + & (second_char == 117) // 'u' + & (third_char == 108) // 'l' + & (fourth_char == 108); // 'l' + assert(is_null | is_true, "invalid literal"); } } // 2 gates From 74de2bee7f0496143e8b0d123c455ae8dbed0f5c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 6 Aug 2025 13:58:37 +0000 Subject: [PATCH 3/4] . --- src/json.nr | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/json.nr b/src/json.nr index 0f118f3..b90d2a2 100644 --- a/src/json.nr +++ b/src/json.nr @@ -266,13 +266,14 @@ impl Date: Wed, 6 Aug 2025 14:03:04 +0000 Subject: [PATCH 4/4] . --- src/json.nr | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/json.nr b/src/json.nr index b90d2a2..e362525 100644 --- a/src/json.nr +++ b/src/json.nr @@ -266,14 +266,13 @@ impl