Skip to content

Commit 41bd86e

Browse files
tests working
1 parent 8c68544 commit 41bd86e

File tree

8 files changed

+149
-504
lines changed

8 files changed

+149
-504
lines changed

out.txt

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

src/getters.nr

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl JSONLiteral {
6060
}
6161
global MaxKeyBytes = 32;
6262

63-
impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEntries> {
63+
impl<let NumBytes: u32, let NumPackedFields: u16, let TranscriptEntries: u32> JSON<NumBytes,NumPackedFields,TranscriptEntries> {
6464

6565
unconstrained fn find_key_in_map(self, target: Field) -> Field {
6666
let mut found_index: Field = 0;
@@ -420,7 +420,7 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
420420

421421
assert(entry.entry_type == BEGIN_OBJECT_TOKEN, "key does not describe an object");
422422

423-
let mut result: Option<JSON<NumBytes, TranscriptEntries>> = Option::none();
423+
let mut result: Option<JSON<NumBytes, NumPackedFields, TranscriptEntries>> = Option::none();
424424
if (exists) {
425425
let mut r = self;
426426
r.layer_id = entry.parent_index;
@@ -439,7 +439,7 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
439439
// TODO: ADD A layer_context VARIABLE INTO JSON WHICH DESCRIBES WHETHER WE ARE AN OBJECT, ARRAY OR SINGLE VALUE
440440
assert(entry.entry_type == BEGIN_ARRAY_TOKEN, "key does not describe an object");
441441

442-
let mut result: Option<JSON<NumBytes, TranscriptEntries>> = Option::none();
442+
let mut result: Option<JSON<NumBytes, NumPackedFields, TranscriptEntries>> = Option::none();
443443
if (exists) {
444444
let mut r = self;
445445
r.layer_id = entry.parent_index;
@@ -573,6 +573,7 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
573573
*/
574574
let keyhash = get_keyhash(key, 0, key_length);
575575

576+
println(f"search keyhash {keyhash}");
576577
// let two_pow_200 = 0x10000000000000000000000000000000000000000000000000000;
577578
let two_pow_216 = 0x100000000000000000000000000000000000000000000000000000000;
578579

@@ -659,3 +660,15 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
659660
(search_result.found, search_result.lhs_index)
660661
}
661662
}
663+
664+
/*
665+
key_fields[0] = 0x706f0794666f6c696f
666+
0x706f7274666f6c696f
667+
key_fields[1] = 0x00
668+
chunky result 0x4dbc868fd1791e3bbeb76c2afe7ce959388b191891389e2b49
669+
*/
670+
/*
671+
OLD VERSION key_fields[0] = 0x706f7274666f6c696f
672+
OLD VERSION key_fields[1] = 0x00
673+
old result 0x6f40d45d5b9a5dbc875225e048bf4aa4900440adfe972b0e9d
674+
*/

src/keyhash.nr

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ unconstrained fn __slice_200_bits_from_field(f: Field) -> (Field, Field, bool) {
149149
borrow = PLO.hilo < res200.hilo;
150150
PLO.hihi -= borrow as u64;
151151
borrow = PLO.hihi < res200.hihi;
152-
let u = PLO.hihi as Field;
153-
let v = res200.hihi as Field;
154-
println(f"ptop vs rtop = {u} : {v}");
152+
// let u = PLO.hihi as Field;
153+
// let v = res200.hihi as Field;
154+
// println(f"ptop vs rtop = {u} : {v}");
155155
// 0x29b85045b6818158
156156
// 0xc7411d9450cce4
157-
let k = PLO_200_felt;
158-
println(f"lo vs p = {lo} : {k}");
157+
// let k = PLO_200_felt;
158+
// println(f"lo vs p = {lo} : {k}");
159159
let mut PHI = PHI_54 - borrow as u64;
160160
assert(PHI > res54, "slice failed? this shouldn't happen!");
161161
// 0xc7411d9450cce4f0881a2ac0a346f02e1aa1d499535e33336c
@@ -369,6 +369,7 @@ fn sum_var_bytes_into_field<let N: u32>(
369369

370370
let mut chunks: [Field; 5] = [0; 5];
371371

372+
println(f"PATH = {path_f}");
372373
chunks[0] = body_text[idx] as Field;
373374
idx += path_f[0];
374375

@@ -379,10 +380,10 @@ fn sum_var_bytes_into_field<let N: u32>(
379380
+ body_text[idx + 2] as Field * 0x100 + body_text[idx + 3] as Field;
380381
idx += path_f[2] * 4;
381382

382-
chunks[3] = body_text[idx] as Field * 0x100000000000000 + body_text[idx + 1] as Field * 0x100000000000
383-
+ body_text[idx + 2] as Field * 0x10000000000 + body_text[idx + 3] as Field * 0x100000000
384-
+ body_text[idx + 4] as Field * 0x1000000 + body_text[idx + 5] as Field * 0x10000
385-
+ body_text[idx + 6] as Field * 0x100 + body_text[idx + 7] as Field;
383+
chunks[3] = body_text[idx] as Field * 0x100000000000000 + body_text[idx + 1] as Field * 0x1000000000000
384+
+ body_text[idx + 2] as Field * 0x10000000000 + body_text[idx + 3] as Field * 0x100000000
385+
+ body_text[idx + 4] as Field * 0x1000000 + body_text[idx + 5] as Field * 0x10000
386+
+ body_text[idx + 6] as Field * 0x100 + body_text[idx + 7] as Field;
386387
idx += path_f[3] * 8;
387388

388389
chunks[4] = body_text[idx] as Field * 0x1000000000000000000000000000000 + body_text[idx + 1] as Field * 0x1000000000000000000000000000
@@ -492,13 +493,14 @@ fn get_keyhash_chunky<let N: u32>(body_text: [u8; N], body_index: u16, key_lengt
492493
//let uv = hashed_full * hashed_full;
493494
//println(f"{uv}");
494495
//hashed_full
495-
slice_200_bits_from_field(hashed_full)
496+
// slice_200_bits_from_field(hashed_full)
496497
// let hashed_bytes= dep::std::hash::blake2s(result);
497498
// let hashed = fakehash(result); // dep::std::hash::blake2s(result);
498499
//let hashed_full = limb;
499-
// let r = slice_200_bits_from_field(hashed_full);
500-
// println(f"chunky result {r}");
501-
// r
500+
let r = slice_200_bits_from_field(hashed_full);
501+
println(f"chunky result {r}");
502+
r
503+
// 0x6f40d45d5b9a5dbc875225e048bf4aa4900440adfe972b0e9d
502504
// let hashed_bytes = hashed_full.to_be_bytes(32);
503505
// let mut result: Field = 0;
504506
// // 200 bits
@@ -546,6 +548,7 @@ fn get_keyhash<let N: u32>(body_text: [u8; N], body_index: u16, key_length: u16)
546548
println(f"OLD VERSION key_fields[{j}] = {k}");
547549
}
548550

551+
// 0x706f7274666f6c696f
549552
// 57,980 without calling this fn
550553
// 79,103 without pos2
551554
// => 330 gates per element without hashing wtf
@@ -578,4 +581,5 @@ fn get_keyhash<let N: u32>(body_text: [u8; N], body_index: u16, key_length: u16)
578581
}
579582
// 0xc7411d9450cce4f0881a2ac0a346f02e1aa1d499535e33336c
580583
// 0xc7411d9450cce4f0881a2ac0a346f02e1aa1d499535e33336c
581-
// 0xc7411d9450cce4f0881a2ac0a346f02e1aa1d499535e33336c
584+
// 0xc7411d9450cce4f0881a2ac0a346f02e1aa1d499535e33336c
585+

src/keymap.nr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ global KeyLen = 32; // todo make param
6161
// 0x10000000000000
6262
// ]
6363

64-
impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEntries> {
64+
impl<let NumBytes: u32, let NumPackedFields: u16, let TranscriptEntries: u32> JSON<NumBytes, NumPackedFields, TranscriptEntries> {
6565
// 101,105
6666
// 56,876
6767
// 44,229 cost

src/main.nr

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::slice_field::{slice_fields, slice_field};
2525
use crate::keyhash::{slice_200_bits_from_field, get_keyhash_chunky, sum_var_bytes_into_field};
2626
use crate::lt::lt_field_16_bit;
2727
global LEN = 10000;
28-
unconstrained fn preamble(_json: JSON<1024, 64>) -> JSON<1024, 64> {
28+
unconstrained fn preamble(_json: JSON<1024, 64, 37>) -> JSON<1024, 64, 37> {
2929
let mut json = _json.build_transcript();
3030
let x = json.transcript[0];
3131
println(f"{x}");
@@ -184,8 +184,35 @@ fn maiff(p: Field) {
184184
// // assert(new == r[i]); // 1 gate
185185
// }
186186
}
187+
188+
fn main(bytes: [u8; 1024]) {
189+
let mut json = JSON {
190+
json: bytes,
191+
transcript: [0; 64],
192+
transcript_length: 0,
193+
key_data: [0; 64],
194+
key_hashes: [0; 64],
195+
layer_id: 0,
196+
layer_context: 0,
197+
layer_index_in_transcript: 0,
198+
packed_json_entries: [0; 64],
199+
packed_json: [0; 37]
200+
};
201+
202+
json.compute_packed_json();
203+
204+
for i in 0..37 {
205+
let kk = json.packed_json_entries[i];
206+
println(f"{kk}");
207+
}
208+
json.compute_packed_json();
209+
for i in 0..37 {
210+
let kk = json.packed_json_entries[i];
211+
println(f"{kk}");
212+
}
213+
}
187214
// hmm something weird here
188-
fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100], start_bytes: [u16; 100]) {
215+
fn mainjj(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100], start_bytes: [u16; 100]) {
189216
// 3,621 for 100
190217
// 3,613 for 99 = 8 which is expected
191218
// 54 bits
@@ -214,6 +241,8 @@ fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100], start_bytes:
214241

215242
// 25349 vs 25153 = 150 + 46 = 196 hmmmm
216243
// 24749 vs 24559 = 190 lol a bit better
244+
// 23767 vs 23949 = 140 + 42 = 182
245+
// 23569 = 178
217246
// 45 * 3 = 135 => extra = 55 gates hmm
218247

219248
// 26987 vs 26773 delta = 214
@@ -245,7 +274,7 @@ fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100], start_bytes:
245274
// how to remove arb index
246275
// say we compute idx of final limb
247276
// we say if (i == idx) set value we write to last limb
248-
for i in 0..100 {
277+
for i in 0..99 {
249278
let x: [Field; 2] = slice_fields(foo, start_bytes[i], num_bytes[i]);
250279
println(f"{x}");
251280
// assert(x.0 == x.1);
@@ -458,7 +487,8 @@ fn maink(text: str<1024>) {
458487
layer_id: 0,
459488
layer_context: 0,
460489
layer_index_in_transcript: 0,
461-
packed_json_entries: [0; 64]
490+
packed_json_entries: [0; 64],
491+
packed_json: [0; 37]
462492
};
463493
// json = preamble(json);
464494
let mut json = json.build_transcript();

src/redux.nr

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ global TokenFlagsTable: [Field; NUM_TOKENS * 2] = generate_token_flags_table();
408408
// }
409409
// global CAPTURE_TABLE_ENCODED = make_capture_table_full();
410410

411-
struct JSON<let NumBytes: u32, let TranscriptEntries: u32> {
411+
struct JSON<let NumBytes: u32, let NumPackedFields: u16, let TranscriptEntries: u32> {
412412
json: [u8; NumBytes],
413413
transcript: [Field; TranscriptEntries],
414414
transcript_length: u16,
@@ -417,16 +417,41 @@ struct JSON<let NumBytes: u32, let TranscriptEntries: u32> {
417417
layer_id: Field,
418418
layer_context: Field, // is the current layer an OBJECT_LAYER, ARRAY_LAYER or SINGLE_VALUE_LAYER
419419
layer_index_in_transcript: Field,
420-
packed_json_entries: [Field; TranscriptEntries]
420+
packed_json_entries: [Field; TranscriptEntries],
421+
packed_json: [Field; NumPackedFields]
421422
}
422423

423424
/*
424425
NEXT UP: HANDLE ARRAYS
425426
this requires adding an array index into the key data being hashed to form our list of objects
426427
427428
*/
428-
impl<let NumBytes: u16, let TranscriptEntries: u16> JSON<NumBytes, TranscriptEntries> {
429-
429+
impl<let NumBytes: u16, let NumPackedFields: u16, let TranscriptEntries: u16> JSON<NumBytes, NumPackedFields, TranscriptEntries> {
430+
431+
fn compute_packed_json(&mut self) {
432+
let NumWholeLimbs = NumBytes / 31;
433+
for i in 0..NumWholeLimbs {
434+
let mut limb: Field = 0;
435+
for j in 0..NumBytes {
436+
limb *= 0x100;
437+
limb += self.json[i * 31 + j] as Field;
438+
}
439+
std::as_witness(limb);
440+
self.packed_json[i] = limb;
441+
println(f"{limb}"); //limb == 0);
442+
}
443+
let NumRemainingBytes = NumBytes - NumWholeLimbs * 31;
444+
let mut limb: Field = 0;
445+
for j in 0..NumRemainingBytes {
446+
limb *= 0x100;
447+
limb += self.json[NumWholeLimbs * 31 + j] as Field;
448+
}
449+
for j in NumRemainingBytes..31 {
450+
limb *= 0x100;
451+
}
452+
std::as_witness(limb);
453+
self.packed_json[NumWholeLimbs + (NumRemainingBytes == 0) as u16] = limb;
454+
}
430455
fn capture_missing_tokens(&mut self) {
431456
let mut updated_transcript: [Field; TranscriptEntries] = [0; TranscriptEntries];
432457
let mut transcript_ptr: Field = 0;
@@ -687,7 +712,8 @@ impl<let NumBytes: u16, let TranscriptEntries: u16> JSON<NumBytes, TranscriptEnt
687712
layer_id: 0,
688713
layer_context: OBJECT_LAYER, // TODO support arrays and single values,
689714
layer_index_in_transcript: 0,
690-
packed_json_entries: self.packed_json_entries
715+
packed_json_entries: self.packed_json_entries,
716+
packed_json: self.packed_json
691717
}
692718
}
693719
}
@@ -776,7 +802,8 @@ fn test_redux() {
776802
layer_id: 0,
777803
layer_context: OBJECT_LAYER,
778804
layer_index_in_transcript: 0,
779-
packed_json_entries: [0; 30]
805+
packed_json_entries: [0; 30],
806+
packed_json: [0; 7]
780807
};
781808

782809
json = json.build_transcript();
@@ -1024,7 +1051,7 @@ struct JSONEntry {
10241051
}
10251052

10261053
assert(
1027-
json_entries[25] == JSONEntry {
1054+
json_entries[26] == JSONEntry {
10281055
array_pointer: 1,
10291056
entry_type: BEGIN_OBJECT_TOKEN,
10301057
child_pointer: 28, // first child of object is json entry 1 // ah fuck wot
@@ -1049,7 +1076,7 @@ struct JSONEntry {
10491076
);
10501077

10511078
assert(
1052-
json_entries[26] == JSONEntry {
1079+
json_entries[25] == JSONEntry {
10531080
array_pointer: 2,
10541081
entry_type: STRING_TOKEN,
10551082
child_pointer: 0, // first child of object is json entry 1
@@ -1131,7 +1158,8 @@ fn test_literal() {
11311158
layer_id: 0,
11321159
layer_context: OBJECT_LAYER,
11331160
layer_index_in_transcript: 0,
1134-
packed_json_entries: [0; 20]
1161+
packed_json_entries: [0; 20],
1162+
packed_json: [0, 10]
11351163
};
11361164

11371165
json = json.build_transcript();
@@ -1177,7 +1205,8 @@ fn test_arrays() {
11771205
layer_id: 0,
11781206
layer_context: OBJECT_LAYER,
11791207
layer_index_in_transcript: 0,
1180-
packed_json_entries: [0; 60]
1208+
packed_json_entries: [0; 60],
1209+
packed_json: [0, 10]
11811210
};
11821211

11831212
json = json.build_transcript();
@@ -1210,8 +1239,13 @@ fn test_arrays() {
12101239
let mut json_entries: [JSONEntry; 60] = [JSONEntry::new(); 60];
12111240
for i in 0..60 {
12121241
json_entries[i] = JSONEntry::from_field(json.packed_json_entries[i]);
1242+
let k = json_entries[i];
1243+
println(f"JSON ENTRY {k}");
12131244
}
1214-
assert(json_entries[57].entry_type == LITERAL_TOKEN);
1245+
assert(json_entries[56].entry_type == LITERAL_TOKEN);
1246+
assert(json_entries[56].parent_index == 2);
1247+
1248+
assert(json_entries[57].entry_type == BEGIN_ARRAY_TOKEN);
12151249
assert(json_entries[57].parent_index == 2);
12161250

12171251
assert(json_entries[58].entry_type == NUMERIC_TOKEN);

0 commit comments

Comments
 (0)