Skip to content

Commit c7661ba

Browse files
committed
use bool for token validation fields
1 parent 69b1435 commit c7661ba

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

src/_table_generation/make_tables.nr

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,27 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
6565
// output is layer type
6666
// 11 tokens , 3 layers = 11 * 11 * 3 = 121 * 3 = 343
6767
// object contexts
68-
let no_change = ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 0 };
69-
let error_flags =
70-
ValidationFlags { push_layer: 0x1000000, push_layer_type_of_root: 0, pop_layer: 0 };
68+
let no_change =
69+
ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: false };
70+
let error_flags_field = 0x1000000;
7171
let begin_new_object_flags = ValidationFlags {
72-
push_layer: 1,
73-
push_layer_type_of_root: OBJECT_LAYER as Field,
74-
pop_layer: 0,
72+
push_layer: true,
73+
push_layer_type_of_root: OBJECT_LAYER != 0,
74+
pop_layer: false,
7575
};
7676
let begin_new_array_flags = ValidationFlags {
77-
push_layer: 1,
78-
push_layer_type_of_root: ARRAY_LAYER as Field,
79-
pop_layer: 0,
77+
push_layer: true,
78+
push_layer_type_of_root: ARRAY_LAYER != 0,
79+
pop_layer: false,
8080
};
8181
let end_object_or_array_flags: ValidationFlags =
82-
ValidationFlags { push_layer: 0, push_layer_type_of_root: 0, pop_layer: 1 };
82+
ValidationFlags { push_layer: false, push_layer_type_of_root: false, pop_layer: true };
8383

8484
let token_ids: [u32; NUM_TOKENS] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
8585

86-
let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags.to_field());
86+
let error_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|_| error_flags_field);
8787
let object_layer_begin_object_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
88-
let mut result = error_flags.to_field();
88+
let mut result = error_flags_field;
8989
if (token == KEY_TOKEN) {
9090
result = no_change.to_field();
9191
}
@@ -98,13 +98,13 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
9898
let object_layer_key_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
9999
let mut result = no_change.to_field();
100100
if (token != KEY_SEPARATOR_TOKEN) {
101-
result = error_flags.to_field();
101+
result = error_flags_field;
102102
}
103103
result
104104
});
105105

106106
let object_layer_key_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
107-
let mut result = error_flags.to_field();
107+
let mut result = error_flags_field;
108108
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
109109
result = no_change.to_field();
110110
}
@@ -118,7 +118,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
118118
});
119119

120120
let object_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
121-
let mut result = error_flags.to_field();
121+
let mut result = error_flags_field;
122122
if (token == VALUE_SEPARATOR_TOKEN) {
123123
result = no_change.to_field();
124124
}
@@ -129,7 +129,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
129129
});
130130

131131
let object_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
132-
let mut result = error_flags.to_field();
132+
let mut result = error_flags_field;
133133
if (token == VALUE_SEPARATOR_TOKEN) {
134134
result = no_change.to_field();
135135
}
@@ -144,7 +144,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
144144
});
145145

146146
let object_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
147-
let mut result = error_flags.to_field();
147+
let mut result = error_flags_field;
148148
if (token == KEY_TOKEN) {
149149
result = no_change.to_field();
150150
}
@@ -157,7 +157,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
157157
[[0; NUM_TOKENS]; NUM_TOKENS];
158158

159159
let no_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
160-
let mut result = error_flags.to_field();
160+
let mut result = error_flags_field;
161161
if (token == NO_TOKEN) {
162162
result = no_change.to_field();
163163
}
@@ -177,7 +177,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
177177
object_layer_flags[KEY_TOKEN] = object_layer_key_token_outcomes;
178178

179179
let array_layer_begin_array_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token: u32| {
180-
let mut result = error_flags.to_field();
180+
let mut result = error_flags_field;
181181
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
182182
result = no_change.to_field();
183183
}
@@ -194,7 +194,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
194194
});
195195

196196
let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
197-
let mut result = error_flags.to_field();
197+
let mut result = error_flags_field;
198198
if (token == VALUE_SEPARATOR_TOKEN) {
199199
result = no_change.to_field();
200200
}
@@ -205,7 +205,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
205205
});
206206

207207
let array_layer_value_separator_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
208-
let mut result = error_flags.to_field();
208+
let mut result = error_flags_field;
209209
if (token == STRING_TOKEN) | (token == LITERAL_TOKEN) | (token == NUMERIC_TOKEN) {
210210
result = no_change.to_field();
211211
}
@@ -219,7 +219,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
219219
});
220220

221221
let array_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
222-
let mut result = error_flags.to_field();
222+
let mut result = error_flags_field;
223223
if (token == VALUE_SEPARATOR_TOKEN) {
224224
result = no_change.to_field();
225225
}
@@ -229,7 +229,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
229229
result
230230
});
231231
let array_layer_end_array_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
232-
let mut result = error_flags.to_field();
232+
let mut result = error_flags_field;
233233
if (token == VALUE_SEPARATOR_TOKEN) {
234234
result = no_change.to_field();
235235
}
@@ -243,7 +243,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
243243
result
244244
});
245245
let array_layer_end_object_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
246-
let mut result = error_flags.to_field();
246+
let mut result = error_flags_field;
247247
if (token == VALUE_SEPARATOR_TOKEN) {
248248
result = no_change.to_field();
249249
}
@@ -266,7 +266,7 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
266266
array_layer_flags[KEY_TOKEN] = error_token_outcomes;
267267

268268
let single_value_layer_value_token_outcomes: [Field; NUM_TOKENS] = token_ids.map(|token| {
269-
let mut result = error_flags.to_field();
269+
let mut result = error_flags_field;
270270
// we have reached the end of json
271271
if (token == NO_TOKEN) {
272272
result = no_change.to_field();

src/json.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,17 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
207207

208208
// 1 gate
209209
// we encode an error flag into `push_layer` by making its value such that `depth` will exceed the size of `parent_layer_stack`
210-
depth += push_layer - pop_layer;
210+
depth += push_layer as Field - pop_layer as Field;
211211
std::as_witness(depth);
212212

213213
// 6.5 gates
214214
let parent_layer = parent_layer_stack[cast_num_to_u32(depth)];
215-
let mut updated_layer = (1 - pop_layer - push_layer);
215+
let mut updated_layer = (1 - pop_layer as Field - push_layer as Field);
216216
std::as_witness(updated_layer);
217217
updated_layer =
218218
updated_layer * current_layer as Field + push_layer_type_of_root as Field;
219219
std::as_witness(updated_layer);
220-
updated_layer = updated_layer + parent_layer * pop_layer;
220+
updated_layer = updated_layer + parent_layer as Field * pop_layer as Field;
221221
std::as_witness(updated_layer);
222222
current_layer = cast_num_to_u32(updated_layer);
223223

src/transcript_entry.nr

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ use crate::json_tables::ASCII_TO_TOKEN_TABLE;
22
use crate::utils::cast_num_to_u32;
33

44
pub(crate) struct ValidationFlags {
5-
pub(crate) push_layer: Field,
6-
pub(crate) push_layer_type_of_root: Field,
7-
pub(crate) pop_layer: Field,
5+
pub(crate) push_layer: bool,
6+
pub(crate) push_layer_type_of_root: bool,
7+
pub(crate) pop_layer: bool,
88
}
99

1010
impl ValidationFlags {
1111
pub(crate) fn to_field(self) -> Field {
12-
self.push_layer + self.push_layer_type_of_root * 0x100 + self.pop_layer * 0x10000
12+
self.push_layer as Field
13+
+ self.push_layer_type_of_root as Field * 0x100
14+
+ self.pop_layer as Field * 0x10000
1315
}
1416

1517
unconstrained fn __from_field(f: Field) -> Self {
1618
let bytes: [u8; 4] = f.to_be_bytes();
17-
let mut push_layer = bytes[3] as Field;
18-
let push_layer_type_of_root = bytes[2] as Field;
19-
let pop_layer = bytes[1] as Field;
19+
let mut push_layer = bytes[3] != 0;
20+
let push_layer_type_of_root = bytes[2] != 0;
21+
let pop_layer = bytes[1] != 0;
2022
let error = bytes[0] as Field;
2123

2224
assert(error == 0, "ValidationFlags: grammar error");
@@ -28,20 +30,20 @@ impl ValidationFlags {
2830
// an out of bounds error will be triggered
2931
// n.b. reason for doing this is that by only having 3 flags stored in our lookup table,
3032
// 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
31-
push_layer = push_layer + error * 0x1000000;
3233
ValidationFlags { push_layer, push_layer_type_of_root, pop_layer }
3334
}
3435

3536
// 3 gates
3637
pub(crate) fn from_field(f: Field) -> Self {
3738
// Safety: check the comments below
3839
let r = unsafe { ValidationFlags::__from_field(f) };
39-
// checks pop_layer is a valid boolean
40-
assert(r.pop_layer * r.pop_layer == r.pop_layer);
41-
// checks push_layer_type_of_root is a valid boolean
42-
assert(r.push_layer_type_of_root * r.push_layer_type_of_root == r.push_layer_type_of_root);
4340
// checks the input field is a valid combination of the outputs of the decomposition
44-
assert(r.pop_layer * 0x10000 + r.push_layer_type_of_root * 0x100 + r.push_layer == f);
41+
assert(
42+
r.pop_layer as Field * 0x10000
43+
+ r.push_layer_type_of_root as Field * 0x100
44+
+ r.push_layer as Field
45+
== f,
46+
);
4547
r
4648
}
4749
}

0 commit comments

Comments
 (0)