Skip to content

Commit 1faa036

Browse files
authored
Merge branch 'main' into jl/documentation
2 parents b80d48a + c1e9bdf commit 1faa036

File tree

6 files changed

+110
-128
lines changed

6 files changed

+110
-128
lines changed

src/_string_tools/slice_packed_field.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ pub fn slice_fields<let InputFields: u32, let OutputFields: u32>(
756756
let use_previous_for_last_limb: Field = extra_head_section + bytes_fit_into_limb;
757757

758758
// 1, 132.5
759-
let mut index_of_overflow_limb = start_index + num_whole_limbs + 1;
759+
let index_of_overflow_limb = start_index + num_whole_limbs + 1;
760760
// 2, 134.5
761761
let last_limb_from_data = data[index_of_overflow_limb as u32];
762762
// 2, 136.5

src/_table_generation/make_tables.nr

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ unconstrained fn make_token_validation_table() -> [Field; NUM_TOKENS * NUM_TOKEN
288288
let mut flattened_flags: [Field; NUM_TOKENS * NUM_TOKENS * 3] =
289289
[0; NUM_TOKENS * NUM_TOKENS * 3];
290290
let NN = (NUM_TOKENS * NUM_TOKENS);
291-
for j in 0..NUM_TOKENS as u32 {
292-
for k in 0..NUM_TOKENS as u32 {
291+
for j in 0..NUM_TOKENS {
292+
for k in 0..NUM_TOKENS {
293293
flattened_flags[OBJECT_LAYER * NN + j * NUM_TOKENS + k] = object_layer_flags[j][k];
294294
flattened_flags[ARRAY_LAYER * NN + j * NUM_TOKENS + k] = array_layer_flags[j][k];
295295
flattened_flags[SINGLE_VALUE_LAYER * NN + j * NUM_TOKENS + k] =
@@ -359,94 +359,94 @@ unconstrained fn make_process_raw_transcript_table() -> [Field; 1024] {
359359
}
360360

361361
unconstrained fn generate_token_flags_table() -> [Field; NUM_TOKENS * 2] {
362-
let mut flags: [TokenFlags; NUM_TOKENS * 2] = [TokenFlags::default(); NUM_TOKENS * 2];
362+
let mut flags: [TokenFlags; NUM_TOKENS * 2] = std::mem::zeroed();
363363

364364
let mut no_token_flags: TokenFlags = TokenFlags {
365365
create_json_entry: false,
366-
is_end_of_object_or_array: 0,
367-
is_start_of_object_or_array: 0,
368-
new_context: OBJECT_LAYER as Field,
369-
is_key_token: 0,
370-
is_value_token: 0,
371-
preserve_num_entries: 1,
366+
is_end_of_object_or_array: false,
367+
is_start_of_object_or_array: false,
368+
new_context: OBJECT_LAYER != 0,
369+
is_key_token: false,
370+
is_value_token: false,
371+
preserve_num_entries: true,
372372
};
373373
let mut key_token_flags: TokenFlags = TokenFlags {
374374
create_json_entry: false,
375-
is_end_of_object_or_array: 0,
376-
is_start_of_object_or_array: 0,
377-
new_context: OBJECT_LAYER as Field,
378-
is_key_token: 1,
379-
is_value_token: 0,
380-
preserve_num_entries: 1,
375+
is_end_of_object_or_array: false,
376+
is_start_of_object_or_array: false,
377+
new_context: OBJECT_LAYER != 0,
378+
is_key_token: true,
379+
is_value_token: false,
380+
preserve_num_entries: true,
381381
};
382382
let begin_object_flags = TokenFlags {
383383
create_json_entry: false,
384-
is_end_of_object_or_array: 0,
385-
is_start_of_object_or_array: 1,
386-
new_context: OBJECT_LAYER as Field,
387-
is_key_token: 0,
388-
is_value_token: 0,
389-
preserve_num_entries: 0,
384+
is_end_of_object_or_array: false,
385+
is_start_of_object_or_array: true,
386+
new_context: OBJECT_LAYER != 0,
387+
is_key_token: false,
388+
is_value_token: false,
389+
preserve_num_entries: false,
390390
};
391391

392392
let begin_array_flags = TokenFlags {
393393
create_json_entry: false,
394-
is_end_of_object_or_array: 0,
395-
is_start_of_object_or_array: 1,
396-
new_context: ARRAY_LAYER as Field,
397-
is_key_token: 0,
398-
is_value_token: 0,
399-
preserve_num_entries: 0,
394+
is_end_of_object_or_array: false,
395+
is_start_of_object_or_array: true,
396+
new_context: ARRAY_LAYER != 0,
397+
is_key_token: false,
398+
is_value_token: false,
399+
preserve_num_entries: false,
400400
};
401401

402402
let mut end_object_flags = TokenFlags {
403403
create_json_entry: true,
404-
is_end_of_object_or_array: 1,
405-
is_start_of_object_or_array: 0,
406-
new_context: 0,
407-
is_key_token: 0,
408-
is_value_token: 0,
409-
preserve_num_entries: 0,
404+
is_end_of_object_or_array: true,
405+
is_start_of_object_or_array: false,
406+
new_context: false,
407+
is_key_token: false,
408+
is_value_token: false,
409+
preserve_num_entries: false,
410410
};
411411

412412
let mut end_array_flags = TokenFlags {
413413
create_json_entry: true,
414-
is_end_of_object_or_array: 1,
415-
is_start_of_object_or_array: 0,
416-
new_context: 0,
417-
is_key_token: 0,
418-
is_value_token: 0,
419-
preserve_num_entries: 0,
414+
is_end_of_object_or_array: true,
415+
is_start_of_object_or_array: false,
416+
new_context: false,
417+
is_key_token: false,
418+
is_value_token: false,
419+
preserve_num_entries: false,
420420
};
421421

422422
let mut string_flags = TokenFlags {
423423
create_json_entry: true,
424-
is_end_of_object_or_array: 0,
425-
is_start_of_object_or_array: 0,
426-
new_context: OBJECT_LAYER as Field,
427-
is_key_token: 0,
428-
is_value_token: 1,
429-
preserve_num_entries: 1,
424+
is_end_of_object_or_array: false,
425+
is_start_of_object_or_array: false,
426+
new_context: OBJECT_LAYER != 0,
427+
is_key_token: false,
428+
is_value_token: true,
429+
preserve_num_entries: true,
430430
};
431431

432432
let mut numeric_flags = TokenFlags {
433433
create_json_entry: true,
434-
is_end_of_object_or_array: 0,
435-
is_start_of_object_or_array: 0,
436-
new_context: OBJECT_LAYER as Field,
437-
is_key_token: 0,
438-
is_value_token: 1,
439-
preserve_num_entries: 1,
434+
is_end_of_object_or_array: false,
435+
is_start_of_object_or_array: false,
436+
new_context: OBJECT_LAYER != 0,
437+
is_key_token: false,
438+
is_value_token: true,
439+
preserve_num_entries: true,
440440
};
441441

442442
let mut literal_flags = TokenFlags {
443443
create_json_entry: true,
444-
is_end_of_object_or_array: 0,
445-
is_start_of_object_or_array: 0,
446-
new_context: OBJECT_LAYER as Field,
447-
is_key_token: 0,
448-
is_value_token: 1,
449-
preserve_num_entries: 1,
444+
is_end_of_object_or_array: false,
445+
is_start_of_object_or_array: false,
446+
new_context: OBJECT_LAYER != 0,
447+
is_key_token: false,
448+
is_value_token: true,
449+
preserve_num_entries: true,
450450
};
451451

452452
flags[NO_TOKEN] = no_token_flags;
@@ -461,26 +461,26 @@ unconstrained fn generate_token_flags_table() -> [Field; NUM_TOKENS * 2] {
461461
flags[LITERAL_TOKEN] = literal_flags;
462462
flags[KEY_TOKEN] = key_token_flags;
463463

464-
no_token_flags.new_context = ARRAY_LAYER as Field;
465-
key_token_flags.new_context = ARRAY_LAYER as Field;
466-
string_flags.new_context = ARRAY_LAYER as Field;
467-
numeric_flags.new_context = ARRAY_LAYER as Field;
468-
literal_flags.new_context = ARRAY_LAYER as Field;
469-
470-
flags[NUM_TOKENS + (NO_TOKEN as u32)] = no_token_flags;
471-
flags[NUM_TOKENS + (BEGIN_OBJECT_TOKEN as u32)] = begin_object_flags;
472-
flags[NUM_TOKENS + (END_OBJECT_TOKEN as u32)] = end_object_flags;
473-
flags[NUM_TOKENS + (BEGIN_ARRAY_TOKEN as u32)] = begin_array_flags;
474-
flags[NUM_TOKENS + (END_ARRAY_TOKEN as u32)] = end_array_flags;
475-
flags[NUM_TOKENS + (KEY_SEPARATOR_TOKEN as u32)] = no_token_flags;
476-
flags[NUM_TOKENS + (VALUE_SEPARATOR_TOKEN as u32)] = no_token_flags;
477-
flags[NUM_TOKENS + (STRING_TOKEN as u32)] = string_flags;
478-
flags[NUM_TOKENS + (NUMERIC_TOKEN as u32)] = numeric_flags;
479-
flags[NUM_TOKENS + (LITERAL_TOKEN as u32)] = literal_flags;
480-
flags[NUM_TOKENS + (KEY_TOKEN as u32)] = key_token_flags;
464+
no_token_flags.new_context = ARRAY_LAYER != 0;
465+
key_token_flags.new_context = ARRAY_LAYER != 0;
466+
string_flags.new_context = ARRAY_LAYER != 0;
467+
numeric_flags.new_context = ARRAY_LAYER != 0;
468+
literal_flags.new_context = ARRAY_LAYER != 0;
469+
470+
flags[NUM_TOKENS + NO_TOKEN] = no_token_flags;
471+
flags[NUM_TOKENS + BEGIN_OBJECT_TOKEN] = begin_object_flags;
472+
flags[NUM_TOKENS + END_OBJECT_TOKEN] = end_object_flags;
473+
flags[NUM_TOKENS + BEGIN_ARRAY_TOKEN] = begin_array_flags;
474+
flags[NUM_TOKENS + END_ARRAY_TOKEN] = end_array_flags;
475+
flags[NUM_TOKENS + KEY_SEPARATOR_TOKEN] = no_token_flags;
476+
flags[NUM_TOKENS + VALUE_SEPARATOR_TOKEN] = no_token_flags;
477+
flags[NUM_TOKENS + STRING_TOKEN] = string_flags;
478+
flags[NUM_TOKENS + NUMERIC_TOKEN] = numeric_flags;
479+
flags[NUM_TOKENS + LITERAL_TOKEN] = literal_flags;
480+
flags[NUM_TOKENS + KEY_TOKEN] = key_token_flags;
481481

482482
let mut result: [Field; NUM_TOKENS * 2] = [0; NUM_TOKENS * 2];
483-
for i in 0..(NUM_TOKENS as u32 * 2) {
483+
for i in 0..(NUM_TOKENS * 2) {
484484
result[i] = flags[i].to_field();
485485
}
486486
result

src/getters.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
9494
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
9595

9696
let hasher: ByteHasher<MaxKeyFields> = ByteHasher {};
97-
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len() as u32);
97+
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len());
9898
let two_pow_216 = 0x100000000000000000000000000000000000000000000000000000000;
9999

100100
let keyhash = keyhash + self.root_id * two_pow_216;
@@ -147,7 +147,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
147147
assert(self.layer_type_of_root != ARRAY_LAYER, "cannot extract array elements via a key");
148148

149149
let hasher: ByteHasher<MaxKeyFields> = ByteHasher {};
150-
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len() as u32);
150+
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len());
151151
let two_pow_216 = 0x100000000000000000000000000000000000000000000000000000000;
152152

153153
let keyhash = keyhash + self.root_id * two_pow_216;
@@ -395,7 +395,7 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
395395
case 3: entry A > keyhash > entryB
396396
*/
397397
let hasher: ByteHasher<MaxKeyFields> = ByteHasher {};
398-
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len() as u32);
398+
let keyhash = hasher.get_keyhash_var(key.storage(), 0, key.len());
399399

400400
let HASH_MAXIMUM = 0x1000000000000000000000000000000000000000000000000000000000000 - 1;
401401
let two_pow_216 = 0x100000000000000000000000000000000000000000000000000000000;

src/json.nr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,20 @@ impl<let NumBytes: u32, let NumPackedFields: u32, let MaxNumTokens: u32, let Max
321321
TOKEN_FLAGS_TABLE[cast_num_to_u32(token) + context * NUM_TOKENS],
322322
);
323323

324+
// We convert these booleans into Fields so that we can use them in arithmetic operations.
325+
let create_json_entry = create_json_entry as Field;
326+
let is_end_of_object_or_array = is_end_of_object_or_array as Field;
327+
let is_start_of_object_or_array = is_start_of_object_or_array as Field;
328+
let new_context = new_context as Field;
329+
let update_key = update_key as Field;
330+
let is_value_token = is_value_token as Field;
331+
let preserve_num_entries = preserve_num_entries as Field;
332+
324333
// Determine what the current key index is and the key size in bytes
325334
// (key index = byte location in the original JSON of the key)
326335
// Pseudocode equivalent:
327336
// current_key_index_and_length = update_key ? (index + length * 0x10000) : current_key_index_and_length
337+
328338
// 2 gates
329339
// only update current_key_index_and_length if the token is a key token
330340
let diff = (index + length * 0x10000) - current_key_index_and_length;

src/keyhash.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<let KeyFields: u32> FieldHasher<KeyFields> {
1818
key_length: Field,
1919
) -> Field {
2020
let key_fields: [Field; KeyFields] = slice_fields(packed_fields, body_index, key_length);
21-
let hashed = poseidon::poseidon2::Poseidon2::hash(key_fields, KeyFields as u32);
21+
let hashed = poseidon::poseidon2::Poseidon2::hash(key_fields, KeyFields);
2222
slice_200_bits_from_field(hashed)
2323
}
2424
}
@@ -60,7 +60,7 @@ impl<let KeyFields: u32> ByteHasher<KeyFields> {
6060
key_fields[j] = limb;
6161
}
6262

63-
let hashed_full = poseidon::poseidon2::Poseidon2::hash(key_fields, KeyFields as u32);
63+
let hashed_full = poseidon::poseidon2::Poseidon2::hash(key_fields, KeyFields);
6464

6565
let mut r = slice_200_bits_from_field(hashed_full);
6666
r

src/token_flags.nr

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
/// - when `preserve_num_entries == 1` implies the current parent object has not changed (could do with renaming this variable)
1515
pub(crate) struct TokenFlags {
1616
pub(crate) create_json_entry: bool,
17-
pub(crate) is_end_of_object_or_array: Field,
18-
pub(crate) is_start_of_object_or_array: Field,
19-
pub(crate) new_context: Field,
20-
pub(crate) is_key_token: Field,
21-
pub(crate) is_value_token: Field,
22-
pub(crate) preserve_num_entries: Field,
17+
pub(crate) is_end_of_object_or_array: bool,
18+
pub(crate) is_start_of_object_or_array: bool,
19+
pub(crate) new_context: bool,
20+
pub(crate) is_key_token: bool,
21+
pub(crate) is_value_token: bool,
22+
pub(crate) preserve_num_entries: bool,
2323
}
2424

2525
impl TokenFlags {
@@ -33,12 +33,12 @@ impl TokenFlags {
3333
unconstrained fn __from_field(f: Field) -> Self {
3434
let bytes: [u8; 7] = f.to_be_bytes();
3535
let create_json_entry = bytes[0] != 0;
36-
let is_end_of_object_or_array = bytes[1] as Field;
37-
let is_start_of_object_or_array = bytes[2] as Field;
38-
let new_context = bytes[3] as Field;
39-
let is_key_token = bytes[4] as Field;
40-
let is_value_token = bytes[5] as Field;
41-
let preserve_num_entries = bytes[6] as Field;
36+
let is_end_of_object_or_array = bytes[1] != 0;
37+
let is_start_of_object_or_array = bytes[2] != 0;
38+
let new_context = bytes[3] != 0;
39+
let is_key_token = bytes[4] != 0;
40+
let is_value_token = bytes[5] != 0;
41+
let preserve_num_entries = bytes[6] != 0;
4242

4343
TokenFlags {
4444
create_json_entry,
@@ -56,21 +56,6 @@ impl TokenFlags {
5656
// 10 gates
5757
// Safety: check the comments below
5858
let r = unsafe { TokenFlags::__from_field(f) };
59-
60-
// checks that the flags are binary
61-
assert(
62-
r.is_end_of_object_or_array * r.is_end_of_object_or_array
63-
== r.is_end_of_object_or_array,
64-
);
65-
assert(
66-
r.is_start_of_object_or_array * r.is_start_of_object_or_array
67-
== r.is_start_of_object_or_array,
68-
);
69-
assert(r.new_context * r.new_context == r.new_context);
70-
assert(r.is_key_token * r.is_key_token == r.is_key_token);
71-
assert(r.is_value_token * r.is_value_token == r.is_value_token);
72-
assert(r.preserve_num_entries * r.preserve_num_entries == r.preserve_num_entries);
73-
7459
// asserts the relation of r and f
7560
assert(r.to_field() == f);
7661
r
@@ -79,25 +64,12 @@ impl TokenFlags {
7964
/// Pack a TokenFlags object into a Field element
8065
/// 4 gates
8166
pub(crate) fn to_field(self) -> Field {
82-
self.preserve_num_entries
83-
+ self.is_value_token * 0x100
84-
+ self.is_key_token * 0x10000
85-
+ self.new_context * 0x1000000
86-
+ self.is_start_of_object_or_array * 0x100000000
87-
+ self.is_end_of_object_or_array * 0x10000000000
88-
+ self.create_json_entry as Field * 0x1000000000000
89-
}
90-
91-
/// Default constructor
92-
pub(crate) fn default() -> Self {
93-
TokenFlags {
94-
create_json_entry: false,
95-
is_end_of_object_or_array: 0,
96-
is_start_of_object_or_array: 0,
97-
new_context: 0,
98-
is_key_token: 0,
99-
preserve_num_entries: 0,
100-
is_value_token: 0,
101-
}
67+
(self.preserve_num_entries as Field)
68+
+ (self.is_value_token as Field) * 0x100
69+
+ (self.is_key_token as Field) * 0x10000
70+
+ (self.new_context as Field) * 0x1000000
71+
+ (self.is_start_of_object_or_array as Field) * 0x100000000
72+
+ (self.is_end_of_object_or_array as Field) * 0x10000000000
73+
+ (self.create_json_entry as Field) * 0x1000000000000
10274
}
10375
}

0 commit comments

Comments
 (0)