-
Notifications
You must be signed in to change notification settings - Fork 4
fix: add validation for literals #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,6 +607,36 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max | |
let index_valid: Field = range_valid[i] as Field; | ||
// 1 gate | ||
let entry = TranscriptEntry::to_field(TranscriptEntry { token, index, length }); | ||
|
||
if token == LITERAL_TOKEN as Field { | ||
index.assert_max_bit_size::<8>(); | ||
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]; | ||
Comment on lines
+614
to
+617
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pulling these array reads out to be unconditional |
||
if length == 5 { | ||
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 { | ||
assert_eq(length, 4, "invalid literal"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just assert on the length here to avoid having a predicate in this branch. |
||
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 | ||
let diff = updated_transcript[cast_num_to_u32(transcript_ptr)] - entry; | ||
std::as_witness(diff); | ||
|
@@ -923,13 +953,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 +1004,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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've moved this range constraint to sit before the cast to u32 to ensure that we don't truncate.