Skip to content

Commit 8c68544

Browse files
wip
1 parent 12e600e commit 8c68544

File tree

8 files changed

+1899
-97
lines changed

8 files changed

+1899
-97
lines changed

out.txt

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

src/checkpoint

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

src/getters.nr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use crate::redux_tables::{
1010
BEGIN_ARRAY_TOKEN, ASCII_TO_NUMBER
1111
};
1212
use crate::keyhash::get_keyhash;
13+
use crate::keyhash::get_keyhash_old;
14+
1315
use crate::keymap::KeyLen; // todo make param
1416

1517
struct KeySearchResult {

src/keyhash.nr

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,15 @@ unconstrained fn __slice_200_bits_from_field(f: Field) -> (Field, Field, bool) {
164164
}
165165

166166
fn slice_200_bits_from_field(f: Field) -> Field {
167-
f * f
168-
// let (lo, hi, borrow) = __slice_200_bits_from_field(f);
169-
// assert(hi * TWO_POW_200 + lo == f);
170-
// lo.assert_max_bit_size(200);
171-
// hi.assert_max_bit_size(56);
172-
// let lo_diff = PLO_200_felt - lo + (borrow as Field * TWO_POW_200);
173-
// let hi_diff = PHI_54_felt - hi - borrow as Field;
174-
// println(f"{lo_diff}, {hi_diff}");
175-
// lo_diff.assert_max_bit_size(200);
176-
// hi_diff.assert_max_bit_size(56);
177-
// lo
167+
let (lo, hi, borrow) = __slice_200_bits_from_field(f);
168+
assert(hi * TWO_POW_200 + lo == f);
169+
lo.assert_max_bit_size(200);
170+
hi.assert_max_bit_size(56);
171+
let lo_diff = PLO_200_felt - lo + (borrow as Field * TWO_POW_200);
172+
let hi_diff = PHI_54_felt - hi - borrow as Field;
173+
lo_diff.assert_max_bit_size(200);
174+
hi_diff.assert_max_bit_size(56);
175+
lo
178176
}
179177
// TESTS PASS WITH THIS ONE, NEED TO CHANGE TEST EXPECTS TO MATCH NEW REORDERING
180178
fn get_keyhash_old<let N: u32>(body_text: [u8; N], body_index: u16, key_length: u16) -> Field {
@@ -374,29 +372,29 @@ fn sum_var_bytes_into_field<let N: u32>(
374372
chunks[0] = body_text[idx] as Field;
375373
idx += path_f[0];
376374

377-
// chunks[1] = body_text[idx] as Field * 0x100 + body_text[idx + 1] as Field;
378-
// idx += path_f[1] * 2;
379-
380-
// chunks[2] = body_text[idx] as Field * 0x1000000 + body_text[idx + 1] as Field * 0x10000
381-
// + body_text[idx + 2] as Field * 0x100 + body_text[idx + 3] as Field;
382-
// idx += path_f[2] * 4;
383-
384-
// chunks[3] = body_text[idx] as Field * 0x100000000000000 + body_text[idx + 1] as Field * 0x100000000000
385-
// + body_text[idx + 2] as Field * 0x10000000000 + body_text[idx + 3] as Field * 0x100000000
386-
// + body_text[idx + 4] as Field * 0x1000000 + body_text[idx + 5] as Field * 0x10000
387-
// + body_text[idx + 6] as Field * 0x100 + body_text[idx + 7] as Field;
388-
// idx += path_f[3] * 8;
389-
390-
// chunks[4] = body_text[idx] as Field * 0x1000000000000000000000000000000 + body_text[idx + 1] as Field * 0x1000000000000000000000000000
391-
// + body_text[idx + 2] as Field * 0x100000000000000000000000000 + body_text[idx + 3] as Field * 0x1000000000000000000000000
392-
// + body_text[idx + 4] as Field * 0x10000000000000000000000 + body_text[idx + 5] as Field * 0x100000000000000000000
393-
// + body_text[idx + 6] as Field * 0x1000000000000000000 + body_text[idx + 7] as Field * 0000000000000000
394-
// + body_text[idx + 8] as Field * 0x100000000000000 + body_text[idx + 9] as Field * 0x100000000000
395-
// + body_text[idx + 10] as Field * 0x10000000000 + body_text[idx + 11] as Field * 0x100000000
396-
// + body_text[idx + 12] as Field * 0x1000000 + body_text[idx + 13] as Field * 0x10000
397-
// + body_text[idx + 14] as Field * 0x100 + body_text[idx + 15] as Field;
398-
399-
// chunks[4] = chunks[3] * chunks[3];
375+
chunks[1] = body_text[idx] as Field * 0x100 + body_text[idx + 1] as Field;
376+
idx += path_f[1] * 2;
377+
378+
chunks[2] = body_text[idx] as Field * 0x1000000 + body_text[idx + 1] as Field * 0x10000
379+
+ body_text[idx + 2] as Field * 0x100 + body_text[idx + 3] as Field;
380+
idx += path_f[2] * 4;
381+
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;
386+
idx += path_f[3] * 8;
387+
388+
chunks[4] = body_text[idx] as Field * 0x1000000000000000000000000000000 + body_text[idx + 1] as Field * 0x1000000000000000000000000000
389+
+ body_text[idx + 2] as Field * 0x100000000000000000000000000 + body_text[idx + 3] as Field * 0x1000000000000000000000000
390+
+ body_text[idx + 4] as Field * 0x10000000000000000000000 + body_text[idx + 5] as Field * 0x100000000000000000000
391+
+ body_text[idx + 6] as Field * 0x1000000000000000000 + body_text[idx + 7] as Field * 0000000000000000
392+
+ body_text[idx + 8] as Field * 0x100000000000000 + body_text[idx + 9] as Field * 0x100000000000
393+
+ body_text[idx + 10] as Field * 0x10000000000 + body_text[idx + 11] as Field * 0x100000000
394+
+ body_text[idx + 12] as Field * 0x1000000 + body_text[idx + 13] as Field * 0x10000
395+
+ body_text[idx + 14] as Field * 0x100 + body_text[idx + 15] as Field;
396+
397+
//chunks[4] = chunks[3] * chunks[3];
400398
chunks[0] *= path_f[0];
401399
chunks[1] *= path_f[1];
402400
chunks[2] *= path_f[2];
@@ -486,15 +484,15 @@ fn get_keyhash_chunky<let N: u32>(body_text: [u8; N], body_index: u16, key_lengt
486484
// poseidon2 has t=4. 1 permutation should cover 3 fields
487485
// TGODO replace with cheaper hash!
488486
// println(f"KEYS BEING HASHED = {key_fields}");
489-
// let hashed_full = dep::std::hash::poseidon2::Poseidon2::hash(key_fields, num_limbs as u32);
490-
let hashed_full = key_fields[0] + key_fields[1];
487+
let hashed_full = dep::std::hash::poseidon2::Poseidon2::hash(key_fields, num_limbs as u32);
488+
//let hashed_full = key_fields[0] + key_fields[1];
491489
// println(f"{hashed_full}");
492490
// hashed_full
493491
// hashed_full
494-
let uv = hashed_full * hashed_full;
495-
println(f"{uv}");
496-
hashed_full
497-
// slice_200_bits_from_field(hashed_full)
492+
//let uv = hashed_full * hashed_full;
493+
//println(f"{uv}");
494+
//hashed_full
495+
slice_200_bits_from_field(hashed_full)
498496
// let hashed_bytes= dep::std::hash::blake2s(result);
499497
// let hashed = fakehash(result); // dep::std::hash::blake2s(result);
500498
//let hashed_full = limb;

src/keymap.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::lt::lte_field_240_bit;
55
use crate::lt::assert_lte_240_bit;
66
use crate::redux::BEGIN_OBJECT_TOKEN;
77
use crate::redux::BEGIN_ARRAY_TOKEN;
8+
use crate::keyhash::get_keyhash_old;
89
use crate::keyhash::get_keyhash;
910
use crate::keyhash::get_keyhash_chunky;
1011

@@ -93,7 +94,7 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
9394
// if I know that parent id 5 maps to idx 19 that is important
9495
// ok so now we have, for ONE child, the location of the parent
9596
//
96-
/*
97+
9798
let sort_result = noir_sort::sort_advanced(hashlist, lte_field_240_bit, assert_lte_240_bit);
9899
let mut sorted_entries: [Field; TranscriptEntries] = [0; TranscriptEntries];
99100

@@ -155,7 +156,6 @@ impl<let NumBytes: u32, let TranscriptEntries: u32> JSON<NumBytes, TranscriptEnt
155156
self.packed_json_entries = sorted_entries;
156157

157158
self.key_hashes = sort_result.sorted;
158-
*/
159159
}
160160
}
161161

src/main.nr

Lines changed: 217 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod getters;
2121
mod keyhash;
2222
mod slice_field;
2323
use crate::redux::JSON;
24-
use crate::slice_field::slice_field;
24+
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;
@@ -66,11 +66,208 @@ unconstrained fn preamble(_json: JSON<1024, 64>) -> JSON<1024, 64> {
6666
// diff = 15? that can't be right
6767
// wo range check 128. 5012 vs 5021 = 9
6868

69+
// slice limbs
70+
// 99 = 37955
71+
// 100 = 38283
72+
// delta = 283 + 45 = 328 lol fuck
73+
74+
// size 1 slice limbs
75+
// 99 = 28303
76+
// 100 = 28533
77+
// delta = 230 man painful
78+
79+
// size 2
80+
// 99 = 33227
81+
// 100 = 33508
82+
// delta = 285 pain
83+
84+
// size 5 slice limbs
85+
// 99 = 48103
86+
// 100 = 48533
87+
// delta = 430
88+
89+
// 607
90+
// 601 6
91+
// 595 6
92+
// 589 6
93+
94+
// 645
95+
// 640
96+
97+
// 606
98+
// 600
99+
100+
// 6006
101+
// 5145
102+
// 150?
103+
global TEST1: [Field; 35] = [
104+
0x000000000, // 0 0 0 0 0 0 0 0 0
105+
0x000000001, // 0 0 0 0 0 0 0 0 1
106+
0x000000002, // 0 0 0 0 0 0 0 1 0
107+
0x000000004, // 0 0 0 0 0 0 1 0 0
108+
0x000000008, // 0 0 0 0 0 1 0 0 0
109+
0x000000010, // 0 0 0 0 1 0 0 0 0
110+
0x000000020, // 0 0 0 1 0 0 0 0 0
111+
0x000000040, // 0 0 1 0 0 0 0 0 0
112+
0x000000080, // 0 1 0 0 0 0 0 0 0
113+
0x000000100, // 1 0 0 0 0 0 0 0 0
114+
0x000000000, // 0 0 0 0 0 0 0 0 0
115+
0x000000001, // 0 0 0 0 0 0 0 0 1
116+
0x000000002, // 0 0 0 0 0 0 0 1 0
117+
0x000000004, // 0 0 0 0 0 0 1 0 0
118+
0x000000008, // 0 0 0 0 0 1 0 0 0
119+
0x000000010, // 0 0 0 0 1 0 0 0 0
120+
0x000000020, // 0 0 0 1 0 0 0 0 0
121+
0x000000040, // 0 0 1 0 0 0 0 0 0
122+
0x000000080, // 0 1 0 0 0 0 0 0 0
123+
0x000000100, // 1 0 0 0 0 0 0 0 0
124+
0x000000000, // 0 0 0 0 0 0 0 0 0
125+
0x000000001, // 0 0 0 0 0 0 0 0 1
126+
0x000000002, // 0 0 0 0 0 0 0 1 0
127+
0x000000004, // 0 0 0 0 0 0 1 0 0
128+
0x000000008, // 0 0 0 0 0 1 0 0 0
129+
0x000000010, // 0 0 0 0 1 0 0 0 0
130+
0x000000020, // 0 0 0 1 0 0 0 0 0
131+
0x000000040, // 0 0 1 0 0 0 0 0 0
132+
0x000000080, // 0 1 0 0 0 0 0 0 0
133+
0x000000100, // 1 0 0 0 0 0 0 0 0
134+
0x000000010, // 0 0 0 0 1 0 0 0 0
135+
0x000000020, // 0 0 0 1 0 0 0 0 0
136+
0x000000040, // 0 0 1 0 0 0 0 0 0
137+
0x000000080, // 0 1 0 0 0 0 0 0 0
138+
0x000000100, // 1 0 0 0 0 0 0 0 0
139+
];
140+
141+
global FOO: [Field; 4] = [0, 1, 2, 3];
142+
fn maiff(p: Field) {
143+
// let k = LAST_LIMB_PATH[p[0]];
144+
// println(f"{k}");
145+
// let q = TEST1[p[0]];
146+
// println(f"{q}"); // 47
147+
// let k = LAST_LIMB_PATH[lhs[0]];
148+
// println(f"{k}");
149+
// let k = FOO[p];
150+
// println(f"{k}");
151+
// let k = LAST_LIMB_PATH[p];
152+
// let k: [Field; 2] = get_last_limb_path(p);
153+
// println(f"{k}");
154+
// reading array = 147 gates
155+
// get_last_limb_path =150 gates
156+
// init = 145 - 8 = 137
157+
// 35 * 3 = 105 . + 32?
158+
// 35 range check...
159+
// 6 bit = 64 / 3 values = 22 values
160+
// 4 3 3 3 3 3 3 - = 8 gates padding * 2 = 16 gates padding
161+
// hmm hmm hmm hmm
162+
// OK so we save 1 gate per call to keyfind.
163+
// We have 64 keys max per 1kb json = save 64. cost is quite a bit higher. blah
164+
// we save 3? gates if key len is 3 fields
165+
// 8 -> 17 = 9 gates
166+
// 4 gates to make
167+
// 2 gates to read
168+
// 17 gates = 2 9 gates init
169+
// 20 gate = size 3 12 gates init
170+
// 23 gates = size 4 hmm? 15 gates init
171+
// 3 gates + (3 * size) boo
172+
// 35 * 3 = 105 + 3 = 108 + 8 = 116 + 5 = 121
173+
// cost = 140 gates
174+
// where are the 3 gates coming from?
175+
// 122 -> 220 = 100. why not 70?
176+
// for i in 0..1 {
177+
// let k0: Field = (i as Field == p[i]) as Field;
178+
// let k1 = (i as Field == lhs[i]) as Field;
179+
// println(f"{k0}{k1}");
180+
// //let k: [Field; 2] = get_last_limb_path(p[i]);
181+
// // println(f"{k}");
182+
// // let is_last = (i as Field == p[i]) as Field; // 3 gates
183+
// // let new = (rhs[i] - lhs[i]) * is_last + lhs[i]; // 2 gates
184+
// // assert(new == r[i]); // 1 gate
185+
// }
186+
}
187+
// hmm something weird here
188+
fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100], start_bytes: [u16; 100]) {
189+
// 3,621 for 100
190+
// 3,613 for 99 = 8 which is expected
191+
// 54 bits
192+
193+
// 3,726 for 100
194+
// 3,723 for 99 = 3 gates = expected
195+
196+
// slice 200 bits
197+
// 100 = 6181
198+
// 99 = 6154
199+
// 27 gates ok expected
200+
// so something squiffy is happening
201+
// aaaah the cost of an extr arange table was glomming things
202+
203+
// 5,871
204+
// 28600 28055 26669 24689 24540
205+
// 28833 28283 26883 24883 24733
206+
207+
// current 27834 with path valid via lookups
208+
// now 27184 without "no data" (via field padding)
209+
// and 26987 after tweaking with path stuff
210+
// and 26837 after removing u16 cast
211+
212+
// more lookup messiness 28700 vs 28471 = 229 erm regression?
213+
// ok fixed. 24997 vs 24805 = 192? wow?
214+
215+
// 25349 vs 25153 = 150 + 46 = 196 hmmmm
216+
// 24749 vs 24559 = 190 lol a bit better
217+
// 45 * 3 = 135 => extra = 55 gates hmm
218+
219+
// 26987 vs 26773 delta = 214
220+
// 26900 vs 26687 delta = 213?
221+
// 26837 vs 26624 delta = 213? hmm
222+
// hmm
223+
// 27433 if we can remove no data
224+
// 26483 if we can remove slice_valid somehow = 10 gates hmm hmm
225+
// 233 hmmmmm this beats original version
226+
// 228
227+
// 214
228+
// 194 (we need to remove that damn write-at-arb-index)
229+
// 193
230+
// 45 * 3 = 135
231+
232+
// num_whole_limbs => path for slice valid
233+
// 0 = 0 0 0 0 0
234+
// 1 = 0 0 0 0 1
235+
// 2 = 0 0 0 1 1
236+
// 3 = 0 0 1 1 1
237+
// etc etc
238+
// nice! let's do that!
239+
240+
// e.g. if outputfields = 2
241+
// 3 gates to sort out
242+
// 2 gates to read
243+
// 5 gates total
244+
// 1: 18499 18630 -> 131
245+
// how to remove arb index
246+
// say we compute idx of final limb
247+
// we say if (i == idx) set value we write to last limb
248+
for i in 0..100 {
249+
let x: [Field; 2] = slice_fields(foo, start_bytes[i], num_bytes[i]);
250+
println(f"{x}");
251+
// assert(x.0 == x.1);
252+
}
253+
// for i in 0..99 {
254+
// let x = sum_var_bytes_into_field(bar, 10, 10);
255+
// println(f"{x}");
256+
// }
257+
}
258+
// 27983
259+
// 27759
260+
// 224
261+
// vs
262+
// 27883
263+
// 27659
264+
// 224
265+
// wat?
69266
// 10,285 (10,087)
70267
// 10,344 (10,144)
71268
// diff = 44 + 15 = 59 (diff = 57)
72269
// maybe 58 gates all in?
73-
fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100]) {
270+
fn mainddddold(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100]) {
74271
// 3,621 for 100
75272
// 3,613 for 99 = 8 which is expected
76273
println(f"{bar}");
@@ -105,11 +302,23 @@ fn main(foo: [Field; 100], bar: [u8; 1000], num_bytes: [u16; 100]) {
105302
// w/o range checks its 27,902 still crazy
106303
// 27965 with fn shell
107304
// ok something squiffy was getting masked away - constraining the input adds the gates
108-
fn makkkkin(body_text: [u8; 1024], body_indices: [u16; 64], key_lengths: [u16; 64], dd: Field) {
305+
306+
// 63 = 10953
307+
// 64 = 11010
308+
// diff = 57
309+
310+
// 63 = 12590
311+
// 64 = 12674
312+
// diff = 84 ? for 64 byte eh oh most commented out
313+
314+
// 34392
315+
// 34821
316+
// 429 per key painful (poseidon hash is squiffy though)
317+
fn mainkkkk(body_text: [u8; 1024], body_indices: [u16; 64], key_lengths: [u16; 64], dd: Field) {
109318
println(f"{body_text} {body_indices} {key_lengths}");
110319
let kk = slice_200_bits_from_field(dd);
111320
println(f"{kk}");
112-
for i in 0..63 {
321+
for i in 0..64 {
113322
let x = get_keyhash_chunky(body_text, body_indices[i], key_lengths[i]);
114323
println(f"{x}");
115324
}
@@ -226,11 +435,13 @@ expectations:
226435
227436
// chunks 1 removal cuts from 8.779 to 8.275 = 504 / 63 = 8
228437
*/
229-
fn maikn(body_text: [u8; 1024], body_indices: [u16; 64], key_lengths: [u16; 64], dd: Field) {
438+
// 63: 8275
439+
// 64: 8290
440+
fn mainkjghg(body_text: [u8; 1024], body_indices: [u16; 64], key_lengths: [u16; 64], dd: Field) {
230441
println(f"{body_text} {body_indices} {key_lengths}");
231442
let kk = slice_200_bits_from_field(dd);
232443
println(f"{kk}");
233-
for i in 0..63 {
444+
for i in 0..64 {
234445
let x = sum_var_bytes_into_field(body_text, body_indices[i] as Field, key_lengths[i] as Field);
235446
// let x = get_keyhash_chunky(body_text, body_indices[i], key_lengths[i]);
236447
println(f"{x}");

0 commit comments

Comments
 (0)