diff --git a/src/_table_generation/make_tables.nr b/src/_table_generation/make_tables.nr index 4f96265..5c3ebc5 100644 --- a/src/_table_generation/make_tables.nr +++ b/src/_table_generation/make_tables.nr @@ -65,27 +65,27 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN // output is layer type // 11 tokens , 3 layers = 11 * 11 * 3 = 121 * 3 = 343 // object contexts - let no_change = ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 0 }; - let error_flags = - ValidationFlags { push_layer: 0x1000000, push_layer_type_of_root: 0, pop_layer: 0 }; + let no_change = + ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: false }; + let error_flags_field = 0x1000000; let begin_new_object_flags = ValidationFlags { - push_layer: 1, - push_layer_type_of_root: OBJECT_LAYER as Field, - pop_layer: 0, + push_layer: true, + push_layer_type_of_root: OBJECT_LAYER != 0, + pop_layer: false, }; let begin_new_array_flags = ValidationFlags { - push_layer: 1, - push_layer_type_of_root: ARRAY_LAYER as Field, - pop_layer: 0, + push_layer: true, + push_layer_type_of_root: ARRAY_LAYER != 0, + pop_layer: false, }; let end_object_or_array_flags: ValidationFlags = - ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 1 }; + ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: true }; let token_ids: [u32; NUM_TOKENS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags.to_field()); + let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags_field); let object_layer_begin_object_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == KEY_TOKEN) { result = no_change.to_field(); } @@ -98,13 +98,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN let object_layer_key_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { let mut result = no_change.to_field(); if (token != KEY_SEPARATOR_TOKEN) { - result = error_flags.to_field(); + result = error_flags_field; } result }); let object_layer_key_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) { result = no_change.to_field(); } @@ -118,7 +118,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let object_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -129,7 +129,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let object_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -144,7 +144,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let object_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == KEY_TOKEN) { result = no_change.to_field(); } @@ -157,7 +157,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN [[0; NUM_TOKENS]; NUM_TOKENS]; let no_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == NO_TOKEN) { result = no_change.to_field(); } @@ -177,7 +177,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN object_layer_flags[KEY_TOKEN] = object_layer_key_token_outcomes; let array_layer_begin_array_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token: u32| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) { result = no_change.to_field(); } @@ -194,7 +194,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -205,7 +205,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let array_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) { result = no_change.to_field(); } @@ -219,7 +219,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN }); let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -229,7 +229,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN result }); let array_layer_end_array_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -243,7 +243,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN result }); let array_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; if (token == VALUE_SEPARATOR_TOKEN) { result = no_change.to_field(); } @@ -266,7 +266,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN array_layer_flags[KEY_TOKEN] = error_token_outcomes; let single_value_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| { - let mut result = error_flags.to_field(); + let mut result = error_flags_field; // we have reached the end of json if (token == NO_TOKEN) { result = no_change.to_field(); diff --git a/src/json.nr b/src/json.nr index a39b7b7..6fa56f3 100644 --- a/src/json.nr +++ b/src/json.nr @@ -207,17 +207,17 @@ impl Field { - self.push_layer + self.push_layer_type_of_root * 0x100 + self.pop_layer * 0x10000 + self.push_layer as Field + + self.push_layer_type_of_root as Field * 0x100 + + self.pop_layer as Field * 0x10000 } unconstrained fn __from_field(f: Field) -> Self { let bytes: [u8; 4] = f.to_be_bytes(); - let mut push_layer = bytes[3] as Field; - let push_layer_type_of_root = bytes[2] as Field; - let pop_layer = bytes[1] as Field; + let mut push_layer = bytes[3] != 0; + let push_layer_type_of_root = bytes[2] != 0; + let pop_layer = bytes[1] != 0; let error = bytes[0] as Field; assert(error == 0, "ValidationFlags: grammar error"); @@ -28,7 +30,6 @@ impl ValidationFlags { // an out of bounds error will be triggered // n.b. reason for doing this is that by only having 3 flags stored in our lookup table, // we can extract them all with 1 add gate. combined with 2 bool checks = 3 gates instead of 5/6 gates if we had 4 flags - push_layer = push_layer + error * 0x1000000; ValidationFlags { push_layer, push_layer_type_of_root, pop_layer } } @@ -36,12 +37,8 @@ impl ValidationFlags { pub(crate) fn from_field(f: Field) -> Self { // Safety: check the comments below let r = unsafe { ValidationFlags::__from_field(f) }; - // checks pop_layer is a valid boolean - assert(r.pop_layer * r.pop_layer == r.pop_layer); - // checks push_layer_type_of_root is a valid boolean - assert(r.push_layer_type_of_root * r.push_layer_type_of_root == r.push_layer_type_of_root); - // checks the input field is a valid combination of the outputs of the decomposition - assert(r.pop_layer * 0x10000 + r.push_layer_type_of_root * 0x100 + r.push_layer == f); + // checks the input field is a valid combination of the outputs of the decomposition d dddddd + assert_eq(r.to_field(), f); r } } @@ -78,8 +75,7 @@ impl RawTranscriptEntry { result.length.assert_max_bit_size::<16>(); result.index.assert_max_bit_size::<16>(); result.encoded_ascii.assert_max_bit_size::<14>(); - - assert(result.encoded_ascii + result.index * 0x10000 + result.length * 0x100000000 == felt); + assert_eq(result.to_field(), felt); result } @@ -195,7 +191,7 @@ impl TranscriptEntry { // checks that token is in range result.token.assert_max_bit_size::<8>(); // checks that the input is a valid combination of the outputs of the decomposition - assert(result.token + result.index * 0x100 + result.length * 0x1000000 == felt); + assert_eq(result.to_field(), felt); result }