-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprove_source_goldilocks_zk.glass
More file actions
2330 lines (2295 loc) · 242 KB
/
Copy pathprove_source_goldilocks_zk.glass
File metadata and controls
2330 lines (2295 loc) · 242 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# =============================================================================
# prove_source_goldilocks_zk.glass — real Glass SOURCE -> a ZK proof over Goldilocks
#
# R1b proved a HAND-BUILT circuit over the production field. This drives the same
# cryptographic Goldilocks STARK from REAL GLASS SOURCE: prism parses the source,
# a `cgen` lowers it to Goldilocks gates, and the committed, F_{p^2}-challenged,
# query-verified, blinded STARK proves the result — the source front-end wired
# into the production-field backend.
#
# THE PATH. source text --prism--> Expr AST --unroll--> a call-free term (calls and
# higher-order arguments inlined) --cgen--> Goldilocks gates + a bignum witness
# --R1b STARK--> a succinct, zero-knowledge proof over Goldilocks (p = 2^64-2^32+1).
# The output wire IS the function's result, so claiming a wrong value breaks the
# gate that produced it.
#
# SCOPE: `+`, `-`, `*`, `let`, function calls (inlined), and now `==` / `if`. The
# comparison lowers to a real circuit gadget over the production field: a free
# inverse-hint wire (GHint) plus an assert (GEqZero) realise is-zero — d = a - b,
# inv = d^-1 (or 0), di = d*inv, z = 1 - di, assert z*d == 0, so z = (a==b), a 0/1
# wire; `if` is the mux f + c*(t - f). The trace domain is N=16 (the gadget pushes
# the gate count past 8). `match` / ADTs over bignum remain the next step.
# Imports prism. int64-safe (base-2^16 limbs); heavy interpreted -> run natively
# (examples/selfhost/run_native.sh); dogfoods byte-identical.
# =============================================================================
import "../selfhost/prism.glass"
fn lmod(x: Int) : Int = x % 65536
fn ldiv(x: Int) : Int = x / 65536
fn rev_go(xs: List<Int>, acc: List<Int>) : List<Int> = match xs { [] => acc; [h, ...t] => rev_go(t, [h] ++ acc) }
fn rev(xs: List<Int>) : List<Int> = rev_go(xs, [])
fn drop_zeros(xs: List<Int>) : List<Int> = match xs { [] => []; [h, ...t] => if h == 0 then drop_zeros(t) else xs }
fn trim(xs: List<Int>) : List<Int> = rev(drop_zeros(rev(xs)))
fn addc(a: List<Int>, b: List<Int>, carry: Int) : List<Int> =
match a {
[] => match b { [] => if carry == 0 then [] else [carry]; [hb, ...tb] => let s : Int = hb + carry in [lmod(s)] ++ addc([], tb, ldiv(s)) };
[ha, ...ta] => match b { [] => let s : Int = ha + carry in [lmod(s)] ++ addc(ta, [], ldiv(s)); [hb, ...tb] => let s : Int = ha + hb + carry in [lmod(s)] ++ addc(ta, tb, ldiv(s)) }
}
fn bn_add(a: List<Int>, b: List<Int>) : List<Int> = trim(addc(a, b, 0))
fn smul(xs: List<Int>, k: Int, carry: Int) : List<Int> =
match xs { [] => if carry == 0 then [] else [carry]; [h, ...t] => let p : Int = h * k + carry in [lmod(p)] ++ smul(t, k, ldiv(p)) }
fn shift(xs: List<Int>, n: Int) : List<Int> = if n <= 0 then xs else [0] ++ shift(xs, n - 1)
fn mul_go(a: List<Int>, b: List<Int>, i: Int) : List<Int> =
match b { [] => []; [hb, ...tb] => bn_add(shift(smul(a, hb, 0), i), mul_go(a, tb, i + 1)) }
fn bn_mul(a: List<Int>, b: List<Int>) : List<Int> = trim(mul_go(a, b, 0))
fn cmp_hi(a: List<Int>, b: List<Int>) : Int = match a { [] => 0; [ha, ...ta] => match b { [] => 0; [hb, ...tb] => if ha == hb then cmp_hi(ta, tb) else (if ha < hb then 0 - 1 else 1) } }
fn bn_cmp(a: List<Int>, b: List<Int>) : Int =
let ta : List<Int> = trim(a) in let tb : List<Int> = trim(b) in
if len(ta) == len(tb) then cmp_hi(rev(ta), rev(tb)) else (if len(ta) < len(tb) then 0 - 1 else 1)
fn subb(a: List<Int>, b: List<Int>, borrow: Int) : List<Int> =
match a { [] => [];
[ha, ...ta] => match b {
[] => let d : Int = ha - borrow in if d < 0 then [d + 65536] ++ subb(ta, [], 1) else [d] ++ subb(ta, [], 0);
[hb, ...tb] => let d : Int = ha - hb - borrow in if d < 0 then [d + 65536] ++ subb(ta, tb, 1) else [d] ++ subb(ta, tb, 0) } }
fn bn_sub(a: List<Int>, b: List<Int>) : List<Int> = trim(subb(a, b, 0))
fn take(xs: List<Int>, n: Int) : List<Int> = if n <= 0 then [] else match xs { [] => []; [h, ...t] => [h] ++ take(t, n - 1) }
fn drop(xs: List<Int>, n: Int) : List<Int> = if n <= 0 then xs else match xs { [] => []; [h, ...t] => drop(t, n - 1) }
fn pg(d: Int) : List<Int> = [1, 0, 65535, 65535]
fn fold_hi(d: Int) : List<Int> = [65535, 65535]
fn bn_mod_p(a: List<Int>) : List<Int> =
let t : List<Int> = trim(a) in
if len(t) > 4 then bn_mod_p(bn_add(take(t, 4), bn_mul(drop(t, 4), fold_hi(0))))
else if bn_cmp(t, pg(0)) >= 0 then bn_sub(t, pg(0)) else t
fn fadd(a: List<Int>, b: List<Int>) : List<Int> = gold_add(a, b)
fn fsub(a: List<Int>, b: List<Int>) : List<Int> = gold_sub(a, b)
fn fmul(a: List<Int>, b: List<Int>) : List<Int> = gold_mul(a, b)
fn bn_eq(a: List<Int>, b: List<Int>) : Bool = bn_cmp(a, b) == 0
fn bn_is_zero(e: List<Int>) : Bool = match trim(e) { [] => true; _ => false }
fn bn_is_odd(e: List<Int>) : Bool = match e { [] => false; [h, ...t] => h % 2 == 1 }
fn half_hl(xs: List<Int>, rem: Int) : List<Int> = match xs { [] => []; [h, ...t] => let cur : Int = rem * 65536 + h in [cur / 2] ++ half_hl(t, cur % 2) }
fn bn_half(e: List<Int>) : List<Int> = trim(rev(half_hl(rev(trim(e)), 0)))
fn fpow_bn(base: List<Int>, e: List<Int>) : List<Int> =
if bn_is_zero(e) then [1]
else let h : List<Int> = fpow_bn(fmul(base, base), bn_half(e)) in
if bn_is_odd(e) then fmul(base, h) else h
# CUT THE ROPE (v5.60.0): the field inverse was fpow_bn — ~64 list-rebuilding fmul/bn_half per
# call, and finv runs once PER quotient-codeword element / grand-product row (tens of thousands
# of times per proof). It was 100% of the >11-min proof. The inverse is now one unboxed Fermat
# inverse in C (`goldw_inv` = 64 __int128 squarings in registers, zero allocation); we round-trip
# limbs<->u64 at the boundary and `trim` to gold-canonical form. Soundness-neutral (same field,
# same value); difftested goldw_inv == limb inverse over large values + inv0 (examples/prove/goldw_difftest.glass).
fn finv(a: List<Int>) : List<Int> = trim(goldw_to_limbs(goldw_inv(limbs_to_goldw(a))))
fn halve_k(e: List<Int>, k: Int) : List<Int> = if k <= 0 then e else halve_k(bn_half(e), k - 1)
fn root_pow2(k: Int) : List<Int> = fpow_bn([7], halve_k(bn_sub(pg(0), [1]), k))
fn glit(c: Int) : List<Int> = trim([c % 65536, (c / 65536) % 65536, c / 4294967296])
# full-width literal: correct base-2^16 limbs for any c < 2^64 (glit caps near 2^48)
fn glit4(c: Int) : List<Int> = trim([c % 65536, (c / 65536) % 65536, (c / 4294967296) % 65536, (c / 281474976710656) % 65536])
# bignum -> decimal string (long division by 10), for displaying results > 2^31
fn bn_dm10_go(hi: List<Int>, q: List<Int>, rem: Int) : (List<Int>, Int) =
match hi { [] => (q, rem); [h, ...t] => let cur : Int = rem * 65536 + h in bn_dm10_go(t, q ++ [cur / 10], cur % 10) }
fn bn_dm10(x: List<Int>) : (List<Int>, Int) = match bn_dm10_go(rev(trim(x)), [], 0) { (qhi, r) => (trim(rev(qhi)), r) }
fn bn_dec_go(x: List<Int>, acc: String) : String = match trim(x) { [] => acc; _ => match bn_dm10(x) { (q, r) => bn_dec_go(q, int_to_string(r) ++ acc) } }
fn bn_dec(x: List<Int>) : String = match trim(x) { [] => "0"; _ => bn_dec_go(x, "") }
# Signed-aware decimal: a field element in the upper half (> p/2 = 9223372034707292160) is the
# canonical form of a NEGATIVE signed integer (a signed -k in [-2^31,2^31) is bound as p-k, far
# above p/2), so render it as "-(p-x)" = the negative it represents. Values <= p/2 print as-is.
# Used for the result/claim display so signed comparison/division/arithmetic show -3, not p-3.
# (A genuine large unsigned value in (p/2, p) would also show negative — rare; the field is the
# same element either way, and the Third Witness reports the reconciled value.)
fn bn_dec_signed(x: List<Int>) : String =
if bn_cmp(trim(x), glit4(9223372034707292160)) > 0 then "-" ++ bn_dec(fsub(glit4(0), x)) else bn_dec(x)
# --- list-of-bignum helpers ---------------------------------------------------
fn wnth(xs: List<List<Int>>, i: Int) : List<Int> = match xs { [] => []; [x, ...r] => if i == 0 then x else wnth(r, i - 1) }
fn lset(xs: List<List<Int>>, i: Int, v: List<Int>) : List<List<Int>> = match xs { [] => []; [x, ...r] => if i == 0 then [v] ++ r else [x] ++ lset(r, i - 1, v) }
fn llen(xs: List<List<Int>>) : Int = match xs { [] => 0; [x, ...r] => 1 + llen(r) }
fn lpad(xs: List<List<Int>>, n: Int) : List<List<Int>> = if llen(xs) >= n then xs else lpad(xs ++ [glit(0)], n)
# --- circuit: gates over Goldilocks (witness = list of field elements) --------
# GHint(o) — a FREE witness wire (no gate constrains it); the prover supplies its
# value. Soundness comes from the gates that CONSUME it.
# GEqZero(a) — assert w[a] == 0 (the `qe · l` constraint term). The is-zero gadget
# (for `==`) and the `if` mux are built from these two.
type Gate = | GConst(Int, List<Int>) | GAdd(Int, Int, Int) | GSub(Int, Int, Int) | GMul(Int, Int, Int) | GHint(Int) | GEqZero(Int)
type Build = | Build(Int, List<Gate>, List<List<Int>>)
fn mkc(c: List<Int>, b: Build) : (Int, Build) = match b { Build(n, gs, w) => (n, Build(n + 1, gs ++ [GConst(n, c)], w ++ [c])) }
fn mka(x: Int, y: Int, b: Build) : (Int, Build) = match b { Build(n, gs, w) => (n, Build(n + 1, gs ++ [GAdd(n, x, y)], w ++ [fadd(wnth(w, x), wnth(w, y))])) }
fn mks(x: Int, y: Int, b: Build) : (Int, Build) = match b { Build(n, gs, w) => (n, Build(n + 1, gs ++ [GSub(n, x, y)], w ++ [fsub(wnth(w, x), wnth(w, y))])) }
fn mkm(x: Int, y: Int, b: Build) : (Int, Build) = match b { Build(n, gs, w) => (n, Build(n + 1, gs ++ [GMul(n, x, y)], w ++ [fmul(wnth(w, x), wnth(w, y))])) }
fn mkh(v: List<Int>, b: Build) : (Int, Build) = match b { Build(n, gs, w) => (n, Build(n + 1, gs ++ [GHint(n)], w ++ [v])) }
fn mke(a: Int, b: Build) : Build = match b { Build(n, gs, w) => Build(n, gs ++ [GEqZero(a)], w) }
fn bwire(b: Build, i: Int) : List<Int> = match b { Build(n, gs, w) => wnth(w, i) }
# the nine trace columns (length = #gates, padded to N). A GHint row is unconstrained
# (all selectors 0); a GEqZero row fires the qe selector (its constraint is qe·l = 0).
fn sel_add(g: Gate) : List<Int> = match g { GAdd(o, a, b) => [1]; GConst(o, c) => []; GSub(o, a, b) => []; GMul(o, a, b) => []; GHint(o) => []; GEqZero(a) => [] }
fn sel_mul(g: Gate) : List<Int> = match g { GMul(o, a, b) => [1]; GConst(o, c) => []; GAdd(o, a, b) => []; GSub(o, a, b) => []; GHint(o) => []; GEqZero(a) => [] }
fn sel_sub(g: Gate) : List<Int> = match g { GSub(o, a, b) => [1]; GConst(o, c) => []; GAdd(o, a, b) => []; GMul(o, a, b) => []; GHint(o) => []; GEqZero(a) => [] }
fn sel_con(g: Gate) : List<Int> = match g { GConst(o, c) => [1]; GAdd(o, a, b) => []; GSub(o, a, b) => []; GMul(o, a, b) => []; GHint(o) => []; GEqZero(a) => [] }
fn sel_eq(g: Gate) : List<Int> = match g { GEqZero(a) => [1]; GConst(o, c) => []; GAdd(o, a, b) => []; GSub(o, a, b) => []; GMul(o, a, b) => []; GHint(o) => [] }
fn cv(g: Gate) : List<Int> = match g { GConst(o, c) => c; GAdd(o, a, b) => []; GSub(o, a, b) => []; GMul(o, a, b) => []; GHint(o) => []; GEqZero(a) => [] }
fn lv(g: Gate, w: List<List<Int>>) : List<Int> = match g { GConst(o, c) => []; GAdd(o, a, b) => wnth(w, a); GSub(o, a, b) => wnth(w, a); GMul(o, a, b) => wnth(w, a); GHint(o) => []; GEqZero(a) => wnth(w, a) }
fn rv(g: Gate, w: List<List<Int>>) : List<Int> = match g { GConst(o, c) => []; GAdd(o, a, b) => wnth(w, b); GSub(o, a, b) => wnth(w, b); GMul(o, a, b) => wnth(w, b); GHint(o) => []; GEqZero(a) => [] }
fn ov(g: Gate, w: List<List<Int>>) : List<Int> = match g { GConst(o, c) => wnth(w, o); GAdd(o, a, b) => wnth(w, o); GSub(o, a, b) => wnth(w, o); GMul(o, a, b) => wnth(w, o); GHint(o) => wnth(w, o); GEqZero(a) => [] }
fn c_qa(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [sel_add(g)] ++ c_qa(r) }
fn c_qm(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [sel_mul(g)] ++ c_qm(r) }
fn c_qs(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [sel_sub(g)] ++ c_qs(r) }
fn c_qc(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [sel_con(g)] ++ c_qc(r) }
fn c_qe(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [sel_eq(g)] ++ c_qe(r) }
fn c_c(gs: List<Gate>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [cv(g)] ++ c_c(r) }
fn c_l(gs: List<Gate>, w: List<List<Int>>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [lv(g, w)] ++ c_l(r, w) }
fn c_r(gs: List<Gate>, w: List<List<Int>>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [rv(g, w)] ++ c_r(r, w) }
fn c_o(gs: List<Gate>, w: List<List<Int>>) : List<List<Int>> = match gs { [] => []; [g, ...r] => [ov(g, w)] ++ c_o(r, w) }
# --- interpolation (inverse NTT) over the size-8 trace domain -----------------
fn poly_eval_b(coeffs: List<List<Int>>, x: List<Int>) : List<Int> = match coeffs { [] => []; [c, ...r] => fadd(c, fmul(x, poly_eval_b(r, x))) }
fn intt_sum(ev: List<List<Int>>, k: Int, j: Int, n: Int, wi: List<Int>) : List<Int> =
if j >= n then [] else fadd(fmul(wnth(ev, j), fpow_bn(wi, glit((j * k) % n))), intt_sum(ev, k, j + 1, n, wi))
fn interp_go(ev: List<List<Int>>, k: Int, n: Int, wi: List<Int>, ni: List<Int>) : List<List<Int>> =
if k >= n then [] else [fmul(ni, intt_sum(ev, k, 0, n, wi))] ++ interp_go(ev, k + 1, n, wi, ni)
fn interp16(ev: List<List<Int>>) : List<List<Int>> = interp_go(ev, 0, 16, finv(root_pow2(4)), finv(glit(16)))
# R1b: generic size-n inverse NTT (n a power of two) — the variable trace domain
fn ilog2(n: Int) : Int = if n <= 1 then 0 else 1 + ilog2(n / 2)
fn scale_cols(sc: List<Int>, xs: List<List<Int>>) : List<List<Int>> = match xs { [] => []; [h, ...t] => [fmul(sc, h)] ++ scale_cols(sc, t) }
# CUT THE ROPE 2: interpolation (inverse NTT) was O(n^2)+ (interp_go/intt_sum, fpow_bn per term).
# An iNTT is the forward NTT at omega^-1 with no coset shift, scaled by 1/n -> reuse the native ntt_lde.
fn interp_n(ev: List<List<Int>>, n: Int) : List<List<Int>> =
scale_cols(finv(glit(n)), w_to_limbs_list(ntt_lde(limbs_list_to_w(ev), n, 1, limbs_to_goldw(finv(root_pow2(ilog2(n)))))))
fn eval_on_dom(c: List<List<Int>>, dom: List<List<Int>>) : List<List<Int>> = match dom { [] => []; [x, ...r] => [poly_eval_b(c, x)] ++ eval_on_dom(c, r) }
# --- domains ------------------------------------------------------------------
fn dom_powers(cur: List<Int>, g: List<Int>, k: Int) : List<List<Int>> = if k <= 0 then [] else [cur] ++ dom_powers(fmul(cur, g), g, k - 1)
# --- the gate-constraint polynomial G(x) and its quotient Q = G / Z_H ---------
fn gate_G_at(qa: List<List<Int>>, qm: List<List<Int>>, qs: List<List<Int>>, qc: List<List<Int>>, qe: List<List<Int>>, l: List<List<Int>>, r: List<List<Int>>, o: List<List<Int>>, c: List<List<Int>>, x: List<Int>) : List<Int> =
let va : List<Int> = poly_eval_b(qa, x) in let vm : List<Int> = poly_eval_b(qm, x) in let vs : List<Int> = poly_eval_b(qs, x) in let vc : List<Int> = poly_eval_b(qc, x) in let vq : List<Int> = poly_eval_b(qe, x) in
let vl : List<Int> = poly_eval_b(l, x) in let vr : List<Int> = poly_eval_b(r, x) in let vo : List<Int> = poly_eval_b(o, x) in let vk : List<Int> = poly_eval_b(c, x) in
fadd(fmul(va, fsub(vo, fadd(vl, vr))),
fadd(fmul(vm, fsub(vo, fmul(vl, vr))),
fadd(fmul(vs, fsub(vo, fsub(vl, vr))),
fadd(fmul(vc, fsub(vo, vk)),
fmul(vq, vl)))))
# gate_G evaluated pointwise on ALREADY-evaluated column values (no per-point poly_eval_b).
fn gate_G_pt(va: List<Int>, vm: List<Int>, vs: List<Int>, vc: List<Int>, vq: List<Int>, vl: List<Int>, vr: List<Int>, vo: List<Int>, vk: List<Int>) : List<Int> =
fadd(fmul(va, fsub(vo, fadd(vl, vr))),
fadd(fmul(vm, fsub(vo, fmul(vl, vr))),
fadd(fmul(vs, fsub(vo, fsub(vl, vr))),
fadd(fmul(vc, fsub(vo, vk)),
fmul(vq, vl)))))
# CUT THE ROPE 2 (v5.61.0): the OLD quotient_cw evaluated 9 coefficient-polynomials at EVERY one of
# m=32n coset points via O(n) Horner (gate_G_at -> poly_eval_b) — O(9·n·m) = O(n²), the bottleneck the
# moment circuits grow (the comparison gadget, n≈1024, ran >18 min, ~all here). Now each column is
# low-degree-extended to the coset ONCE by the native NTT (`lde`, O(m log m)), and the quotient is a
# single pointwise lock-step walk. BYTE-IDENTICAL to the old quotient (lde == eval_on_dom, difftested
# in examples/prove/ntt_difftest.glass: NTT == naive coset-eval, interp == native).
fn limbs_list_to_w(xs: List<List<Int>>) : List<Int> = match xs { [] => []; [h, ...t] => [limbs_to_goldw(h)] ++ limbs_list_to_w(t) }
fn w_to_limbs_list(ws: List<Int>) : List<List<Int>> = match ws { [] => []; [h, ...t] => [goldw_to_limbs(h)] ++ w_to_limbs_list(t) }
# low-degree extension: evaluate the coeff-poly `c` on the size-(32n) coset {7·ω^i}, ω = root_pow2(fri_log n).
fn lde(c: List<List<Int>>, n: Int) : List<List<Int>> =
w_to_limbs_list(ntt_lde(limbs_list_to_w(c), fri_dsize(n), 7, limbs_to_goldw(root_pow2(fri_log(n)))))
fn quo_combine(eqa: List<List<Int>>, eqm: List<List<Int>>, eqs: List<List<Int>>, eqc: List<List<Int>>, eqe: List<List<Int>>, el: List<List<Int>>, er: List<List<Int>>, eo: List<List<Int>>, ec: List<List<Int>>, coset: List<List<Int>>, n: Int) : List<List<Int>> =
match coset { [] => []; [x, ...crest] =>
match eqa { [] => []; [va, ...qar] => match eqm { [] => []; [vm, ...qmr] => match eqs { [] => []; [vs, ...qsr] =>
match eqc { [] => []; [vc, ...qcr] => match eqe { [] => []; [vq, ...qer] => match el { [] => []; [vl, ...elr] =>
match er { [] => []; [vr, ...err] => match eo { [] => []; [vo, ...eor] => match ec { [] => []; [vk, ...ecr] =>
let g : List<Int> = gate_G_pt(va, vm, vs, vc, vq, vl, vr, vo, vk) in
let zh : List<Int> = fsub(fpow_bn(x, glit(n)), [1]) in
[fmul(g, finv(zh))] ++ quo_combine(qar, qmr, qsr, qcr, qer, elr, err, eor, ecr, crest, n) }}}}}}}}} }
fn quotient_cw(qa: List<List<Int>>, qm: List<List<Int>>, qs: List<List<Int>>, qc: List<List<Int>>, qe: List<List<Int>>, l: List<List<Int>>, r: List<List<Int>>, o: List<List<Int>>, c: List<List<Int>>, coset: List<List<Int>>, n: Int) : List<List<Int>> =
quo_combine(lde(qa, n), lde(qm, n), lde(qs, n), lde(qc, n), lde(qe, n), lde(l, n), lde(r, n), lde(o, n), lde(c, n), coset, n)
# --- FRI low-degree test (base-field fold; Stage B lifts to F_{p^2}) ----------
fn fold_at_b(cw: List<List<Int>>, dom: List<List<Int>>, beta: List<Int>, finv2: List<Int>, i: Int, half: Int) : List<List<Int>> =
if i >= half then []
else let fx : List<Int> = wnth(cw, i) in let fmx : List<Int> = wnth(cw, i + half) in let x : List<Int> = wnth(dom, i) in
let fe : List<Int> = fmul(fadd(fx, fmx), finv2) in let fo : List<Int> = fmul(fsub(fx, fmx), finv(fmul([2], x))) in
[fadd(fe, fmul(beta, fo))] ++ fold_at_b(cw, dom, beta, finv2, i + 1, half)
# CUT THE ROPE 3: was O(m^2) (wnth(dom,i) per element). Lock-step walk of the first `half` points.
fn sqd(dom: List<List<Int>>, i: Int, half: Int) : List<List<Int>> =
if i >= half then [] else match dom { [] => []; [h, ...t] => [fmul(h, h)] ++ sqd(t, i + 1, half) }
fn fold_all_b(cw: List<List<Int>>, dom: List<List<Int>>, finv2: List<Int>, round: Int) : List<List<Int>> =
if llen(cw) <= fri_final(0) then cw
else let half : Int = llen(cw) / 2 in fold_all_b(fold_at_b(cw, dom, glit(round * 7919 + 12345), finv2, 0, half), sqd(dom, 0, half), finv2, round + 1)
fn all_eqb(cw: List<List<Int>>, x: List<Int>) : Bool = match cw { [] => true; [y, ...r] => if bn_eq(y, x) then all_eqb(r, x) else false }
fn is_constb(cw: List<List<Int>>) : Bool = match cw { [] => true; [x, ...r] => all_eqb(r, x) }
# build all 9 column polynomials and form the quotient codeword Q on the coset.
# Trace domain N = 16 (the is-zero gadget + if-mux push the gate count past 8); the
# coset is a power-of-two blowup (FRI_DSIZE = 2^FRI_LOG points, the 2^FRI_LOG-th
# roots of unity shifted by the generator 7, disjoint from the trace domain H so
# Z_H never vanishes on it).
#
# THE RATE LEVER. The tested degree bound is FIXED at 32 = 2^5 by the circuit
# (deg Q = deg G - deg Z_H = 45 - 16 = 29 < 32), so the FRI fold runs a FIXED 5
# rounds and stops at length FRI_DSIZE/32 (a degree-<32 codeword has folded to a
# constant by then). The rate is therefore ρ = 32 / FRI_DSIZE, and GROWING the
# coset (the single source of truth below) lowers ρ toward a cryptographic target —
# 64 -> ρ=1/2, 128 -> 1/4, 256 -> 1/8, 512 -> 1/16 — WITHOUT changing what degree
# is tested. (Folding all the way to length 2 instead would test the useless bound
# DSIZE/2 and leave ρ pinned at 1/2 — no soundness gain. The stop-at-DSIZE/32 is
# the whole point.) Each halving of ρ halves the per-query survival probability, at
# the cost of ~linearly more quotient evaluations + bigger Merkle trees (the
# interpreter-dogfood gate). See docs/parameters.md §4.
# R1b: VARIABLE trace domain. d = N = ng(gates) = next pow2 of the gate count.
# deg Q = 3(N-1) - N = 2N-3 < 2N (the tested bound); coset = 16N gives ρ = 2N/16N
# = 1/8, and the fold stops at 16N/2N = 8 (constant) — the SAME validated relation
# the Baby Bear prover uses, now N-parametric so ADT circuits (N up to hundreds) prove.
fn glen(gs: List<Gate>) : Int = match gs { [] => 0; [g, ...r] => 1 + glen(r) }
fn ng(gates: List<Gate>) : Int = next_pow2(glen(gates), 1)
fn fri_log(d: Int) : Int = let r : Int = ilog2(d) + 5 in if r > 32 then error("glass prove: trace/coset size exceeds Goldilocks 2-adicity (2^32) — no primitive 2^k-th root of unity exists for k>32, so the NTT/LDE would silently compute on a wrong domain; ABSTAIN (defense-in-depth: unreachable for any constructible proof, which would need ~2^27 gates)") else r # coset = 32N (B3: log2(32N)); ρ=1/8 still (tested deg<4N / 32N)
fn fri_dsize(d: Int) : Int = 32 * d # coset = 32N -> ρ = 4N/32N = 1/8 (B3 perm-quotient is deg ~3N < 4N; was 16N/2N pre-B3)
fn fri_final(d: Int) : Int = 8 # fold to 8 over 32N (= 32N/4N): degree-<4N folded to a constant
fn fri_queries(d: Int) : Int = 82 # 82q -> ~80-bit provable FRI-query term (68 + 12 grind) / ~135 list-decode. The hash is now 4-lane (~128-bit binding) so this is no longer ~32-bit-capped. Queries are sampled WITHOUT replacement (sample_distinct dedups), so the 82 draws are negatively correlated => effective distinct queries >= the iid-82 model; the 0.83-bit/query closed-form is therefore conservative, not eroded (corrected v5.120; the old "with replacement / erosion" note was backward).
fn fri_coset(d: Int) : List<List<Int>> = dom_powers([7], root_pow2(fri_log(d)), fri_dsize(d))
# Column polynomials, factored so the (future) verifier recomputes the PUBLIC
# selectors/const by the EXACT same construction the prover uses (no drift).
fn icol(col: List<List<Int>>, n: Int) : List<List<Int>> = interp_n(lpad(col, n), n)
fn public_cols(gates: List<Gate>, n: Int) : List<List<List<Int>>> = # [qa, qm, qs, qc, qe, c] — derived from gates alone (public)
[icol(c_qa(gates), n), icol(c_qm(gates), n), icol(c_qs(gates), n), icol(c_qc(gates), n), icol(c_qe(gates), n), icol(c_c(gates), n)]
fn trace_cols(gates: List<Gate>, w: List<List<Int>>, n: Int) : List<List<List<Int>>> = # [l, r, o] — the witness columns (private)
[icol(c_l(gates, w), n), icol(c_r(gates, w), n), icol(c_o(gates, w), n)]
# --- B3: public copy-constraint (PLONK sigma) columns over the wire indices ----
# Cell ids: l-cell of row i = i, r-cell = n+i, o-cell = 2n+i. Active cells match
# lv/rv/ov EXACTLY (GAdd/Sub/Mul -> l,r,o; GConst/GHint -> o; GEqZero -> l); inactive
# cells are sigma FIXED POINTS so inert/padding cells cancel in the grand product.
# sigma links all cells sharing a wire index into one cycle. Verifier recomputes all 6
# columns (id1,id2,id3,S1,S2,S3) from the public gates, exactly like the selectors.
fn gate_cells(g: Gate, i: Int, n: Int) : List<Pair<Int, Int>> = match g {
GAdd(o, a, b) => [Pair(i, a), Pair(n + i, b), Pair(2 * n + i, o)];
GSub(o, a, b) => [Pair(i, a), Pair(n + i, b), Pair(2 * n + i, o)];
GMul(o, a, b) => [Pair(i, a), Pair(n + i, b), Pair(2 * n + i, o)];
GConst(o, c) => [Pair(2 * n + i, o)];
GHint(o) => [Pair(2 * n + i, o)];
GEqZero(a) => [Pair(i, a)] }
fn all_cells(gates: List<Gate>, i: Int, n: Int) : List<Pair<Int, Int>> = match gates { [] => []; [g, ...r] => gate_cells(g, i, n) ++ all_cells(r, i + 1, n) }
fn wire_of(cid: Int, cells: List<Pair<Int, Int>>) : Int = match cells { [] => 0 - 1; [Pair(c, w), ...rest] => if c == cid then w else wire_of(cid, rest) }
fn cells_for_wire(w: Int, cells: List<Pair<Int, Int>>) : List<Int> = match cells { [] => []; [Pair(c, ww), ...rest] => if ww == w then [c] ++ cells_for_wire(w, rest) else cells_for_wire(w, rest) }
fn next_in(cid: Int, lst: List<Int>, first: Int) : Int = match lst { [] => first; [c, ...rest] => if c == cid then (match rest { [] => first; [nx, ...more] => nx }) else next_in(cid, rest, first) }
fn sigma(cid: Int, cells: List<Pair<Int, Int>>) : Int =
let w : Int = wire_of(cid, cells) in
if w < 0 then cid else (let grp : List<Int> = cells_for_wire(w, cells) in match grp { [] => cid; [f, ...rest] => next_in(cid, grp, f) })
fn id_col(off: Int, i: Int, n: Int) : List<List<Int>> = if i >= n then [] else [glit(off + i)] ++ id_col(off, i + 1, n)
fn s_col(off: Int, cells: List<Pair<Int, Int>>, i: Int, n: Int) : List<List<Int>> = if i >= n then [] else [glit(sigma(off + i, cells))] ++ s_col(off, cells, i + 1, n)
fn perm_cols(gates: List<Gate>, n: Int) : List<List<List<Int>>> = # [id1, id2, id3, S1, S2, S3] interpolated, public
let cells : List<Pair<Int, Int>> = all_cells(gates, 0, n) in
[icol(id_col(0, 0, n), n), icol(id_col(n, 0, n), n), icol(id_col(2 * n, 0, n), n),
icol(s_col(0, cells, 0, n), n), icol(s_col(n, cells, 0, n), n), icol(s_col(2 * n, cells, 0, n), n)]
# (B3 grand-product Z helpers live below, after the G2/F_{p^2} declarations.)
fn q_cw(gates: List<Gate>, w: List<List<Int>>) : List<List<Int>> =
let n : Int = ng(gates) in
let pc : List<List<List<Int>>> = public_cols(gates, n) in
let tc : List<List<List<Int>>> = trace_cols(gates, w, n) in
quotient_cw(wnth3(pc, 0), wnth3(pc, 1), wnth3(pc, 2), wnth3(pc, 3), wnth3(pc, 4), wnth3(tc, 0), wnth3(tc, 1), wnth3(tc, 2), wnth3(pc, 5), fri_coset(n), n)
fn low_degree_quotient(gates: List<Gate>, w: List<List<Int>>) : Bool =
is_constb(fold_all_b(q_cw(gates, w), fri_coset(ng(gates)), finv([2]), 0))
# --- ZERO-KNOWLEDGE: blind Q with a random low-degree mask (Stage B) ----------
# A degree-<8 polynomial R, evaluated on the coset, is added to Q. Since deg R is
# below the fold-to-constant bound, Q+R stays low-degree (FRI fold is linear, so it
# still folds to the same kind of constant and ACCEPTs) — but every opened value is
# randomized by R. Two seeds give two different valid proofs of the same statement:
# the verifier learns "Q is low-degree", nothing about the witness. (the Poseidon hash over
# Goldilocks supplies the mask coefficients.)
# --- the in-STARK hash: the vetted Poseidon over Goldilocks (v5.44) ------------
# Replaces the educational MiMC. The Plonky2-exact Poseidon (t=12, R_F=8, R_P=22,
# x^7 S-box, 360 hadeshash constants) — byte-identical to Plonky2's published test
# vectors (frost_goldilocks_poseidon.glass). hashg(a,b) is a 2-to-1 sponge: absorb
# [a,b] into the rate, permute, squeeze lane 0. ~300x heavier than MiMC, so the demo
# runs reduced params (small coset / few queries / grind off) to dogfood; full
# strength (rho=1/8 + grind) runs native (examples/selfhost/run_native.sh).
fn inth(xs: List<Int>, i: Int) : Int = match xs { [] => 0; [h, ...t] => if i == 0 then h else inth(t, i - 1) }
fn pdrop1(st: List<List<Int>>) : List<List<Int>> = match st { [] => []; [h, ...t] => t }
# --- Poseidon over Goldilocks — byte-exact to Plonky2 (t=12, R_F=8, R_P=22) ----
# S-box x^7 (gcd(7, p-1)=1 over Goldilocks, so it is a permutation). Computed as
# Plonky2 does: x2=x^2, x4=x^4, x3=x^3, return x3*x4 = x^7.
fn sbox(x: List<Int>) : List<Int> = let x2 : List<Int> = fmul(x, x) in let x4 : List<Int> = fmul(x2, x2) in let x3 : List<Int> = fmul(x, x2) in fmul(x3, x4)
# constant_layer: lane i += ALL_ROUND_CONSTANTS[12*rc + i]
fn clayer(arc: List<List<Int>>, st: List<List<Int>>, rc: Int, i: Int) : List<List<Int>> =
if i >= 12 then [] else [fadd(wnth(st, i), wnth(arc, rc * 12 + i))] ++ clayer(arc, st, rc, i + 1)
# S-box on all 12 lanes (full round) / on lane 0 only (partial round)
fn sbox_all(st: List<List<Int>>, i: Int) : List<List<Int>> = if i >= 12 then [] else [sbox(wnth(st, i))] ++ sbox_all(st, i + 1)
fn sbox_lane0(st: List<List<Int>>) : List<List<Int>> = [sbox(wnth(st, 0))] ++ pdrop1(st)
# MDS: out[r] = sum_i st[(i+r) mod 12] * CIRC[i] (+ st[0]*8 for r=0, since DIAG=[8,0..])
fn mds_row(st: List<List<Int>>, circ: List<Int>, r: Int, i: Int, acc: List<Int>) : List<Int> =
if i >= 12 then acc else mds_row(st, circ, r, i + 1, fadd(acc, fmul(wnth(st, (i + r) % 12), glit(inth(circ, i)))))
fn mds_one(st: List<List<Int>>, circ: List<Int>, r: Int) : List<Int> =
let base : List<Int> = mds_row(st, circ, r, 0, []) in if r == 0 then fadd(base, fmul(wnth(st, 0), [8])) else base
fn mds_layer(st: List<List<Int>>, circ: List<Int>, r: Int) : List<List<Int>> =
if r >= 12 then [] else [mds_one(st, circ, r)] ++ mds_layer(st, circ, r + 1)
fn full_round(arc: List<List<Int>>, circ: List<Int>, st: List<List<Int>>, rc: Int) : List<List<Int>> =
mds_layer(sbox_all(clayer(arc, st, rc, 0), 0), circ, 0)
fn partial_round(arc: List<List<Int>>, circ: List<Int>, st: List<List<Int>>, rc: Int) : List<List<Int>> =
mds_layer(sbox_lane0(clayer(arc, st, rc, 0)), circ, 0)
fn do_full(arc: List<List<Int>>, circ: List<Int>, st: List<List<Int>>, rc: Int, stop: Int) : List<List<Int>> =
if rc >= stop then st else do_full(arc, circ, full_round(arc, circ, st, rc), rc + 1, stop)
fn do_partial(arc: List<List<Int>>, circ: List<Int>, st: List<List<Int>>, rc: Int, stop: Int) : List<List<Int>> =
if rc >= stop then st else do_partial(arc, circ, partial_round(arc, circ, st, rc), rc + 1, stop)
# the permutation: 4 full (rc 0..3), 22 partial (rc 4..25), 4 full (rc 26..29)
fn perm(arc: List<List<Int>>, circ: List<Int>, st: List<List<Int>>) : List<List<Int>> =
do_full(arc, circ, do_partial(arc, circ, do_full(arc, circ, st, 0, 4), 4, 26), 26, 30)
fn p_arc(d: Int) : List<List<Int>> = [[17413, 61972, 63334, 46469], [6871, 17298, 42335, 30534], [39348, 52967, 3377, 45819], [10199, 32820, 24740, 3943], [57362, 20724, 26214, 57613], [39921, 2000, 5323, 36014], [16031, 38390, 21404, 54328], [19517, 58203, 7292, 61304], [17446, 45252, 41529, 52676], [31743, 48947, 41480, 10111], [30881, 40357, 21410, 57718], [11382, 9691, 754, 50499], [51329, 63266, 30753, 34344], [36437, 16833, 6794, 22989], [50548, 18781, 6573, 50105], [1921, 61290, 50373, 42116], [5836, 56404, 48419, 12427], [49308, 36656, 16577, 28234], [36090, 3471, 60855, 39470], [28472, 44616, 50912, 58208], [18427, 49094, 29071, 54727], [65291, 6403, 44551, 50014], [19431, 38556, 9814, 33948], [48045, 2251, 11404, 49239], [130, 8670, 25418, 59898], [24589, 38298, 27976, 62831], [4453, 1593, 5096, 63447], [23983, 12930, 4907, 33431], [11402, 58123, 1504, 44392], [21342, 64760, 55797, 44113], [44423, 6338, 55260, 20522], [12353, 4363, 21772, 22433], [58755, 27872, 54030, 26299], [25678, 22685, 44015, 3490], [3425, 56085, 10063, 61537], [38451, 59842, 60474, 10424], [37907, 32299, 22246, 37543], [25990, 65257, 16875, 28903], [60444, 44930, 24290, 413], [25426, 29254, 11991, 28527], [19617, 59361, 5839, 31988], [25657, 34468, 20859, 25055], [31605, 4567, 18843, 34268], [1843, 47553, 39752, 19349], [15959, 43012, 15965, 59582], [34457, 59098, 48157, 62912], [29887, 2543, 11455, 16561], [54833, 52010, 2366, 42551], [62472, 6221, 63634, 15555], [12731, 22463, 40385, 11847], [17222, 42531, 56839, 28489], [36219, 56887, 59326, 8508], [60035, 23885, 12596, 23300], [54945, 836, 17784, 41694], [62216, 19067, 43769, 28931], [37633, 38695, 64525, 21286], [18247, 60418, 46927, 43470], [45475, 47905, 60552, 10232], [2195, 7661, 20442, 64747], [5749, 18084, 65299, 64198], [32140, 9869, 43589, 28977], [3999, 38243, 864, 37713], [27643, 44994, 23332, 44371], [17598, 39230, 62918, 17959], [52312, 47345, 63380, 25692], [5655, 2806, 28909, 9244], [1521, 25721, 57462, 44216], [18253, 19535, 59867, 14135], [65462, 59231, 24115, 59370], [49722, 51647, 58527, 37086], [40082, 27584, 60919, 53681], [50690, 42565, 18459, 2917], [10299, 2068, 6827, 39341], [51789, 54294, 31889, 17290], [29980, 50666, 58300, 46605], [22716, 61295, 43882, 51612], [58623, 42798, 60818, 27045], [44401, 7892, 12956, 24187], [18565, 20, 44040, 24512], [20170, 14711, 33426, 13019], [62224, 22576, 27036, 2782], [24353, 4161, 22587, 31941], [54863, 57702, 40658, 34271], [48305, 60978, 57167, 26116], [61256, 55894, 62984, 60292], [24637, 50190, 34868, 55904], [61827, 32865, 65088, 36759], [31625, 38643, 18524, 43327], [54627, 36632, 59630, 26372], [8473, 7687, 59820, 52963], [28865, 58036, 3685, 20749], [14576, 27536, 16200, 63026], [52975, 65445, 36062, 2896], [15549, 58619, 28809, 62017], [22320, 37073, 23592, 24807], [62108, 63072, 32139, 42529], [4494, 50092, 52528, 29017], [54592, 45034, 20111, 33691], [2780, 33426, 15966, 3391], [48040, 57063, 33725, 36733], [7430, 59911, 8771, 30735], [5684, 62430, 22597, 60305], [62342, 9910, 4621, 53662], [60614, 32351, 58682, 366], [58487, 31027, 54606, 52063], [17567, 31229, 16760, 44216], [18226, 59383, 6411, 39970], [13857, 41914, 15387, 23913], [40647, 49846, 1943, 56559], [31507, 55938, 37475, 15715], [53479, 7112, 64919, 57971], [32469, 11554, 624, 16783], [908, 46339, 64931, 35877], [49276, 44744, 60749, 11450], [56770, 52694, 59055, 24408], [3770, 24091, 20652, 10310], [55989, 59417, 13182, 25435], [54431, 54514, 878, 40858], [5902, 44636, 9740, 47422], [46957, 31197, 60136, 45223], [22284, 36006, 11452, 53366], [19445, 4784, 61368, 13510], [49426, 64020, 2741, 16575], [16595, 31831, 28924, 46773], [13396, 11747, 47360, 23079], [45778, 46802, 45413, 45477], [48674, 59857, 57516, 34594], [33019, 32342, 58291, 30862], [57988, 5457, 9830, 5287], [46105, 40691, 26415, 39095], [3642, 27571, 44663, 48019], [63568, 25472, 15108, 10493], [33671, 37669, 26629, 12452], [40161, 25021, 49164, 13181], [65309, 51108, 41540, 54764], [31165, 25810, 25474, 30562], [55143, 57071, 13502, 49636], [60450, 21432, 13594, 665], [7040, 44325, 22244, 45780], [59915, 42140, 53757, 16030], [48856, 42064, 43307, 10610], [37854, 31716, 28119, 8225], [40646, 10308, 59599, 44543], [53827, 19495, 47900, 7245], [54360, 33570, 27274, 5537], [42505, 32729, 4747, 14474], [61691, 47853, 58838, 8960], [20740, 18401, 43654, 12131], [49769, 28366, 27880, 61891], [28873, 6201, 4389, 10008], [43373, 28892, 659, 58756], [53170, 753, 48190, 19867], [63224, 26922, 48169, 59957], [16695, 60091, 6987, 6370], [21748, 9653, 40902, 7739], [10493, 27000, 17250, 9686], [38469, 50490, 7089, 23103], [36152, 27131, 568, 56191], [57852, 4564, 1625, 46178], [32816, 17277, 19172, 18882], [1382, 4395, 34348, 55187], [36843, 29453, 4358, 43741], [54632, 38832, 28174, 50235], [64674, 36582, 9409, 58000], [35942, 13752, 49781, 24144], [39559, 42239, 16160, 4152], [23288, 29092, 61085, 14568], [43449, 23537, 6199, 56657], [55287, 11636, 1432, 30544], [51376, 56806, 39348, 34475], [49280, 16209, 20320, 45344], [3791, 28709, 44164, 61281], [24639, 4908, 55567, 7100], [46423, 38477, 55997, 3281], [7881, 60317, 44635, 4515], [53533, 42373, 49134, 63317], [42199, 9870, 12880, 41912], [37807, 37500, 1780, 20835], [41434, 51695, 44105, 56756], [17432, 13981, 28140, 25787], [8140, 11084, 38338, 63948], [59894, 24394, 32629, 2259], [26459, 13432, 39777, 61124], [57520, 60709, 37690, 61763], [50722, 21983, 56706, 58565], [6542, 61843, 30550, 59309], [63947, 34815, 12683, 37570], [22893, 64883, 9720, 29596], [65232, 61805, 51913, 22070], [370, 37774, 37018, 56719], [16219, 5382, 8161, 50752], [5205, 61868, 31539, 35545], [34067, 45662, 13931, 3145], [33545, 61801, 54226, 1924], [39553, 41344, 46718, 21263], [8009, 39856, 37417, 16644], [47532, 29732, 16948, 5013], [4446, 41377, 48478, 40112], [18849, 50063, 62997, 739], [37359, 40018, 20298, 39005], [9959, 64973, 40938, 30555], [3885, 14415, 26022, 12354], [300, 38771, 25795, 22838], [62158, 45096, 11871, 20234], [29762, 193, 7952, 56673], [20432, 57322, 24474, 55320], [43806, 43427, 5020, 61319], [57651, 49998, 4918, 15271], [568, 22199, 17757, 32058], [9860, 12312, 13025, 26125], [8003, 18637, 34367, 10618], [60343, 42833, 42806, 37088], [20435, 21772, 32974, 21663], [48484, 12088, 45714, 3955], [39999, 64378, 8051, 5823], [60439, 91, 23129, 27935], [38852, 42479, 34671, 767], [22960, 42257, 29346, 50635], [36878, 11612, 62365, 33904], [52086, 54175, 46065, 9643], [17455, 45938, 36041, 9195], [25444, 50767, 47701, 54919], [61784, 64911, 40592, 55949], [24231, 12260, 56445, 58315], [8855, 44773, 51635, 47528], [3027, 4246, 35420, 49362], [6708, 26871, 44187, 17879], [59396, 28777, 28217, 61111], [58073, 5396, 51389, 15622], [1895, 31921, 38944, 40092], [46575, 44767, 2897, 25968], [37896, 14726, 17685, 37151], [3782, 48186, 33951, 31462], [65150, 41066, 16619, 15283], [42590, 26670, 40331, 46190], [17238, 41907, 8953, 36303], [22439, 22626, 44762, 30653], [53773, 20740, 16394, 61854], [38159, 58477, 41800, 50024], [62084, 59001, 52576, 40689], [7475, 54736, 55380, 59548], [33442, 35768, 30684, 23763], [59488, 33774, 64376, 42928], [38157, 50066, 16446, 30340], [15186, 20461, 61551, 24483], [18481, 7104, 31425, 36341], [6295, 7710, 7930, 11696], [40098, 43739, 28132, 21636], [51076, 22675, 17720, 47719], [51035, 17618, 18787, 21533], [34814, 29926, 26500, 59657], [49919, 63177, 39203, 57320], [56437, 3326, 42782, 60645], [58896, 54559, 36821, 24569], [17941, 6294, 37913, 33768], [20673, 18417, 1035, 22818], [17697, 15833, 29966, 63869], [22231, 47215, 54466, 20608], [36208, 22215, 4443, 42974], [33172, 34643, 17068, 27282], [15940, 63767, 28407, 63365], [56329, 65200, 64658, 8805], [35415, 63418, 51428, 6111], [7608, 2546, 42562, 36865], [37829, 29112, 19475, 36864], [21829, 29998, 47055, 47410], [23036, 46846, 57217, 41137], [62146, 30474, 56614, 36593], [60725, 53182, 42233, 1345], [50480, 30911, 4193, 40545], [35570, 37725, 32384, 45942], [44806, 8293, 54648, 152], [26311, 23572, 37325, 12753], [44189, 41753, 61359, 16655], [50347, 58134, 62018, 48632], [32464, 22371, 54619, 40588], [37736, 59811, 11243, 56850], [9510, 36608, 8149, 19712], [43512, 3764, 14080, 51814], [8056, 9465, 14806, 12067], [62744, 51213, 31000, 27930], [2281, 13347, 18745, 57242], [14732, 59080, 5406, 60354], [25877, 41233, 11176, 972], [53123, 59456, 53303, 54081], [45004, 23284, 46546, 14460], [40583, 8848, 20831, 48034], [36423, 1523, 65143, 29256], [8794, 21085, 58730, 19809], [23869, 36256, 38460, 9774], [3778, 19746, 39689, 23016], [40030, 46987, 23378, 1373], [61337, 13588, 32435, 33458], [52859, 38583, 38090, 54016], [5429, 52490, 45953, 53084], [23164, 37278, 19894, 65261], [58783, 30011, 16211, 16752], [35695, 64734, 43328, 24302], [518, 29968, 61873, 19665], [20160, 29765, 13701, 18976], [40876, 56255, 54113, 5240], [34652, 53569, 56327, 28418], [22178, 55525, 8238, 10602], [12005, 39923, 26521, 11005], [18717, 64149, 55662, 31439], [11373, 2747, 47628, 26520], [8482, 9929, 62843, 13510], [46558, 53766, 57786, 22326], [21019, 86, 32042, 8197], [35799, 53335, 23509, 15850], [13484, 32070, 3465, 5861], [42723, 47543, 62444, 10687], [56542, 23099, 54048, 18268], [32392, 49947, 8453, 6308], [12392, 44902, 29716, 574], [26583, 4633, 28936, 5396], [65273, 55254, 57329, 58531], [22327, 34824, 53669, 424], [48879, 55906, 51022, 4532], [13126, 27047, 52237, 58759], [10862, 6058, 12912, 8183], [53355, 9331, 10692, 22862], [4821, 35227, 7601, 63219], [54218, 29458, 50660, 49194], [30904, 24779, 489, 59138], [61704, 27237, 65339, 28560], [64132, 17895, 31346, 17012], [18866, 15031, 2020, 53749], [20685, 61783, 53861, 7302], [11292, 56707, 52851, 14742], [9405, 38962, 47618, 36479], [24020, 805, 60529, 47629], [64603, 5672, 48504, 40604], [54949, 38637, 25689, 56040], [54136, 45473, 2131, 57022], [17219, 19713, 10706, 42130], [24092, 55952, 47615, 31717], [43568, 49732, 24238, 41929], [1348, 62687, 48296, 560], [18630, 49121, 49854, 16693], [15474, 17290, 49356, 5743], [16122, 35560, 46490, 14178], [18256, 35089, 35404, 59538], [24293, 42132, 2897, 10820], [65411, 32153, 64811, 32974], [54826, 58911, 31174, 48024], [20532, 33830, 36634, 28284], [56776, 7102, 45790, 5707], [25915, 19804, 12261, 62401], [29154, 60823, 59682, 16569], [6208, 48763, 23311, 21791], [6161, 50379, 10919, 9475], [17222, 19222, 13319, 43757], [51229, 63945, 38587, 36861], [2140, 22839, 37355, 28924], [21568, 24465, 24106, 32633], [54219, 21622, 55775, 17731], [51469, 79, 55102, 61810], [4664, 48328, 50430, 57297], [22780, 32741, 64354, 48269]]
fn p_circ(d: Int) : List<Int> = [17, 15, 41, 16, 2, 28, 13, 13, 39, 18, 34, 20]
fn pzero(n: Int) : List<List<Int>> = if n <= 0 then [] else [glit(0)] ++ pzero(n - 1) # glit(0) trims to [] (field 0), but is typed List<Int> so HM infers List<List<Int>>
# --- WIDE hash: 4 Poseidon lanes (~256-bit -> ~128-bit collision binding), fixing the
# prior single-lane (~64-bit -> ~32-bit) Merkle/transcript cap (audit finding #1). hashg
# KEEPS its (List<Int>,List<Int>)->List<Int> signature: a "hash" is now 16 limbs (4 lanes
# x 4), a single field element is <=4 limbs; to_lanes splits either; lane0 extracts a
# field element where a challenge needs one. Opaque Merkle/transcript/digest uses are
# UNCHANGED. q_low/pow_ok read the first limb (= lane 0's first limb), also unchanged.
fn lcnt(x: List<Int>) : Int = match x { [] => 0; [h, ...t] => 1 + lcnt(t) }
fn pad4l(x: List<Int>) : List<Int> = if lcnt(x) >= 4 then x else pad4l(x ++ [0])
fn first4(x: List<Int>) : List<Int> = match x { [] => []; [a, ...t1] => match t1 { [] => [a]; [b, ...t2] => match t2 { [] => [a, b]; [c, ...t3] => match t3 { [] => [a, b, c]; [d, ...t4] => [a, b, c, d] } } } }
fn drop4(x: List<Int>) : List<Int> = match x { [] => []; [a, ...t1] => match t1 { [] => []; [b, ...t2] => match t2 { [] => []; [c, ...t3] => match t3 { [] => []; [d, ...t4] => t4 } } } }
fn to_lanes(x: List<Int>) : List<List<Int>> = if lcnt(x) <= 4 then [pad4l(x)] else [first4(x)] ++ to_lanes(drop4(x))
fn lanepad12(ls: List<List<Int>>) : List<List<Int>> = if llen(ls) >= 12 then ls else lanepad12(ls ++ [[0, 0, 0, 0]])
fn from4(st: List<List<Int>>) : List<Int> = pad4l(wnth(st, 0)) ++ pad4l(wnth(st, 1)) ++ pad4l(wnth(st, 2)) ++ pad4l(wnth(st, 3))
fn lane0(h: List<Int>) : List<Int> = first4(h)
# CUT THE ROPE (v5.60.0): the Poseidon MDS layer was THE proof bottleneck — 144 boxed fmul per
# round (each a wnth cons-walk + glit alloc + comb/split), x 30 rounds, x every Merkle/transcript
# hash. `poseidon_perm` is a single native intrinsic (q_poseidon_perm) running the whole
# permutation in registers over a u64[12] — zero alloc, O(1) indexing. We convert the 12 lane
# field-elements to unboxed words (limbs_to_goldw) at the boundary, permute, and expand the first
# 4 output lanes back to the SAME 16-limb digest (goldw_to_limbs + pad4l) — so hashg is BYTE-FOR-BYTE
# what `from4(perm(...))` produced, just ~100x faster. Validated: examples/prove/poseidon_difftest.glass
# (Plonky2 anchor + Glass goldw reference perm + interp==native byte-identical). The pure-Glass
# perm/full_round/mds_* below stay as the readable reference (and the difftest's spec).
fn lanes_to_w(ls: List<List<Int>>) : List<Int> = match ls { [] => []; [h, ...t] => [limbs_to_goldw(h)] ++ lanes_to_w(t) }
fn wnthw(xs: List<Int>, i: Int) : Int = match xs { [] => 0; [h, ...t] => if i == 0 then h else wnthw(t, i - 1) }
fn from4w(w: List<Int>) : List<Int> = pad4l(goldw_to_limbs(wnthw(w, 0))) ++ pad4l(goldw_to_limbs(wnthw(w, 1))) ++ pad4l(goldw_to_limbs(wnthw(w, 2))) ++ pad4l(goldw_to_limbs(wnthw(w, 3)))
# v5.70.0: the live hash is now Poseidon2 (Plonky3) — cheaper linear layers, the modern hash.
# The sponge wrappers (to_lanes/lanepad12/lanes_to_w 12->12 words/from4w 4-lane squeeze, rate 8 /
# cap 4) are permutation-agnostic and UNCHANGED; only the inner permutation changed (poseidon_perm
# -> poseidon2_perm, both byte-exact to their respective published vectors). pentecost/poseidon.py
# migrated to Poseidon2 in lock-step (the second verifier still checks the same hash). The pure-Glass
# Plonky2 perm/mds_* below remain ONLY as the readable Plonky2 reference + poseidon_difftest spec.
fn hashg(a: List<Int>, b: List<Int>) : List<Int> = from4w(poseidon2_perm(lanes_to_w(lanepad12(to_lanes(a) ++ to_lanes(b)))))
fn rand_coeffs(seed: Int, i: Int, k: Int) : List<List<Int>> = if i >= k then [] else [lane0(hashg(glit(seed), glit(i + 1)))] ++ rand_coeffs(seed, i + 1, k)
# --- ZK BLINDING (mechanism). T'(x) = T(x) + Z_H(x)*R(x): on H, Z_H=0 so T'=T (every gate
# constraint preserved); off H (the coset, where queries open) the Z_H*R mask randomizes the
# value, HIDING the witness. Full ZK = blind l,r,o,Z with deg(R) >= the ~166-eval leak surface,
# integrated into the staged FRI (the heavy part — design in docs/tier1-zk-design.md). Here we
# implement the blinding and demonstrate the HIDING property fast (no FRI).
fn zh_at(x: List<Int>, n: Int) : List<Int> = fsub(fpow_bn(x, glit(n)), [1])
fn blind_col(cw: List<List<Int>>, coset: List<List<Int>>, rco: List<List<Int>>, n: Int, i: Int, m: Int) : List<List<Int>> =
if i >= m then [] else [fadd(wnth(cw, i), fmul(zh_at(wnth(coset, i), n), poly_eval_b(rco, wnth(coset, i))))] ++ blind_col(cw, coset, rco, n, i + 1, m)
# Hiding smoke test: blind the SAME witness column with two mask seeds; the opened value at a
# coset point must (a) differ from the raw witness value (masked) and (b) differ between the two
# masks (randomized) — so the opening reveals the mask, not the witness.
fn zk_hiding(seed1: Int, seed2: Int) : Bool =
let n : Int = 8 in let coset : List<List<Int>> = fri_coset(n) in let m : Int = fri_dsize(n) in
let lvals : List<List<Int>> = [glit(2), glit(3), glit(5), glit(7), glit(11), glit(13), glit(17), glit(19)] in
let lcw : List<List<Int>> = lde(interp_n(lvals, n), n) in
let b1 : List<List<Int>> = blind_col(lcw, coset, rand_coeffs(seed1, 0, 16), n, 0, m) in
let b2 : List<List<Int>> = blind_col(lcw, coset, rand_coeffs(seed2, 0, 16), n, 0, m) in
let masked : Bool = if bn_eq(wnth(b1, 5), wnth(lcw, 5)) then false else true in
let diff_masks : Bool = if bn_eq(wnth(b1, 5), wnth(b2, 5)) then false else true in
masked && diff_masks
fn blind_cw(cw: List<List<Int>>, coeffs: List<List<Int>>, dom: List<List<Int>>, i: Int) : List<List<Int>> =
if i >= llen(cw) then [] else [fadd(wnth(cw, i), poly_eval_b(coeffs, wnth(dom, i)))] ++ blind_cw(cw, coeffs, dom, i + 1)
fn prove_zk(gates: List<Gate>, w: List<List<Int>>, seed: Int) : Bool =
let bcw : List<List<Int>> = blind_cw(q_cw(gates, w), rand_coeffs(seed, 0, 16), fri_coset(ng(gates)), 0) in
is_constb(fold_all_b(bcw, fri_coset(ng(gates)), finv([2]), 0))
fn opened5(gates: List<Gate>, w: List<List<Int>>, seed: Int) : List<Int> =
wnth(blind_cw(q_cw(gates, w), rand_coeffs(seed, 0, 16), fri_coset(ng(gates)), 0), 5)
# --- the cryptographic STARK: F_{p^2} fold challenge + Merkle commit + queries --
# Embed the quotient codeword into F_{p^2}, blind it, Merkle-commit each FRI layer,
# derive the fold challenge β ∈ F_{p^2} ≈ 2^128 from each layer's root (Fiat-Shamir,
# so it's unpredictable), and OPEN sampled positions against the commitment: a
# prover whose Q is not low-degree is caught at (almost) every query. This is the
# production STARK shape, on the production field. (FRI over F_{p^2} + Merkle from
# frost_goldilocks_zk.glass, here applied to the gate quotient.)
type G2 = | G2(List<Int>, List<Int>)
fn gadd2(a: G2, b: G2) : G2 = match a { G2(a0, a1) => match b { G2(b0, b1) => G2(fadd(a0, b0), fadd(a1, b1)) } }
fn gsub2(a: G2, b: G2) : G2 = match a { G2(a0, a1) => match b { G2(b0, b1) => G2(fsub(a0, b0), fsub(a1, b1)) } }
fn gscale2(a: G2, s: List<Int>) : G2 = match a { G2(a0, a1) => G2(fmul(a0, s), fmul(a1, s)) }
fn gmul2(a: G2, b: G2) : G2 = match a { G2(a0, a1) => match b { G2(b0, b1) => G2(fadd(fmul(a0, b0), fmul([7], fmul(a1, b1))), fadd(fmul(a0, b1), fmul(a1, b0))) } }
fn geq2(a: G2, b: G2) : Bool = match a { G2(a0, a1) => match b { G2(b0, b1) => bn_eq(a0, b0) && bn_eq(a1, b1) } }
# --- F_{p^2} ops for the TIER-1 OOD/DEEP soundness check (u^2 = 7) --------------
fn gconj(a: G2) : G2 = match a { G2(a0, a1) => G2(a0, fsub([], a1)) } # a0 - a1*u
fn gnorm(a: G2) : List<Int> = match a { G2(a0, a1) => fsub(fmul(a0, a0), fmul([7], fmul(a1, a1))) } # a*conj(a) = a0^2 - 7*a1^2 (base field)
fn ginv2(a: G2) : G2 = gscale2(gconj(a), finv(gnorm(a))) # a^-1 = conj(a)/norm
fn gpow2(a: G2, e: Int) : G2 = if e <= 0 then G2([1], []) else (if e % 2 == 0 then let h : G2 = gpow2(a, e / 2) in gmul2(h, h) else gmul2(a, gpow2(a, e - 1)))
fn poly_eval_g(coeffs: List<List<Int>>, z: G2) : G2 = match coeffs { [] => G2([], []); [c, ...r] => gadd2(emb(c), gmul2(z, poly_eval_g(r, z))) } # eval a base-field poly at z in F_{p^2}
# (an imaginary-part-zero "anti-smuggling" guard was removed here: it was never wired into
# verify, and it is unnecessary — the trace openings are base-field BY TYPE (List<Int>) and
# the OOD evals are DEEP-bound to the committed base-field columns, so nothing can be smuggled
# in an imaginary part. An auditor should still confirm this. (Was dead code; removed for honesty.))
# --- B3: the grand-product Z column (F_{p^2}; interpolation splits real/imag since it
# is linear). Z_0=1; Z_{i+1}=Z_i * num_i/den_i with num=prod(val+beta*id+gamma_p),
# den=prod(val+beta*sigma+gamma_p) over the 3 columns. Telescopes to 1 iff every wire
# carries a single value (the copy constraint). perm_factor takes BASE-field val+id.
fn reals(zs: List<G2>) : List<List<Int>> = match zs { [] => []; [G2(a0, a1), ...r] => [a0] ++ reals(r) }
fn imags(zs: List<G2>) : List<List<Int>> = match zs { [] => []; [G2(a0, a1), ...r] => [a1] ++ imags(r) }
fn poly_eval_g2(zr: List<List<Int>>, zi: List<List<Int>>, z: G2) : G2 =
match zr { [] => G2([], []); [cr, ...rr] => match zi { [] => G2([], []); [ci, ...ri] => gadd2(G2(cr, ci), gmul2(z, poly_eval_g2(rr, ri, z))) } }
# CUT THE ROPE 3: was O(n*m) — poly_eval_b (Horner) per coset point for both real and imag parts
# of the grand-product z-poly. Now two native NTT low-degree extensions, zipped into G2. (dom is
# always the size-32n coset, so n = |dom|/32.)
fn zip_g2(rs: List<List<Int>>, iss: List<List<Int>>) : List<G2> =
match rs { [] => []; [rh, ...rt] => match iss { [] => []; [ih, ...it] => [G2(rh, ih)] ++ zip_g2(rt, it) } }
fn eval_g2_on_dom(zr: List<List<Int>>, zi: List<List<Int>>, dom: List<List<Int>>) : List<G2> =
let nn : Int = lcnt(dom) / 32 in zip_g2(lde(zr, nn), lde(zi, nn))
fn perm_factor(val: List<Int>, idv: List<Int>, beta: G2, gam: G2) : G2 = gadd2(gadd2(emb(val), gmul2(beta, emb(idv))), gam)
fn perm_num(lv: List<Int>, rv: List<Int>, ov: List<Int>, i: Int, n: Int, beta: G2, gam: G2) : G2 =
gmul2(perm_factor(lv, glit(i), beta, gam), gmul2(perm_factor(rv, glit(n + i), beta, gam), perm_factor(ov, glit(2 * n + i), beta, gam)))
fn perm_den(lv: List<Int>, rv: List<Int>, ov: List<Int>, cells: List<Pair<Int, Int>>, i: Int, n: Int, beta: G2, gam: G2) : G2 =
gmul2(perm_factor(lv, glit(sigma(i, cells)), beta, gam), gmul2(perm_factor(rv, glit(sigma(n + i, cells)), beta, gam), perm_factor(ov, glit(sigma(2 * n + i, cells)), beta, gam)))
fn z_build(lvals: List<List<Int>>, rvals: List<List<Int>>, ovals: List<List<Int>>, cells: List<Pair<Int, Int>>, beta: G2, gam: G2, zcur: G2, i: Int, n: Int) : List<G2> =
if i >= n then [] else
let num : G2 = perm_num(wnth(lvals, i), wnth(rvals, i), wnth(ovals, i), i, n, beta, gam) in
let den : G2 = perm_den(wnth(lvals, i), wnth(rvals, i), wnth(ovals, i), cells, i, n, beta, gam) in
[zcur] ++ z_build(lvals, rvals, ovals, cells, beta, gam, gmul2(zcur, gmul2(num, ginv2(den))), i + 1, n)
fn z_final(lvals: List<List<Int>>, rvals: List<List<Int>>, ovals: List<List<Int>>, cells: List<Pair<Int, Int>>, beta: G2, gam: G2, zcur: G2, i: Int, n: Int) : G2 =
if i >= n then zcur else
let num : G2 = perm_num(wnth(lvals, i), wnth(rvals, i), wnth(ovals, i), i, n, beta, gam) in
let den : G2 = perm_den(wnth(lvals, i), wnth(rvals, i), wnth(ovals, i), cells, i, n, beta, gam) in
z_final(lvals, rvals, ovals, cells, beta, gam, gmul2(zcur, gmul2(num, ginv2(den))), i + 1, n)
fn pf(valz: G2, idz: G2, beta: G2, gam: G2) : G2 = gadd2(gadd2(valz, gmul2(beta, idz)), gam) # F_{p^2} factor (verifier, at z)
fn beta_p_of(ts: List<Int>) : G2 = G2(lane0(hashg(ts, glit(221))), lane0(hashg(ts, glit(222))))
fn gam_p_of(ts: List<Int>) : G2 = G2(lane0(hashg(ts, glit(223))), lane0(hashg(ts, glit(224))))
fn alpha_of(ts: List<Int>) : G2 = G2(lane0(hashg(ts, glit(231))), lane0(hashg(ts, glit(232))))
fn emb(x: List<Int>) : G2 = G2(x, [])
fn embed_cw(cw: List<List<Int>>) : List<G2> = match cw { [] => []; [x, ...r] => [emb(x)] ++ embed_cw(r) }
fn lhash(leaf: G2) : List<Int> = match leaf { G2(c0, c1) => hashg(hashg(glit(7), c0), c1) }
fn nth2(xs: List<G2>, i: Int) : G2 = match xs { [] => G2([], []); [h, ...t] => if i == 0 then h else nth2(t, i - 1) }
fn next_pow2(n: Int, p: Int) : Int = if p >= n then p else next_pow2(n, p * 2)
fn leaf_hashes_g(cw: List<G2>) : List<List<Int>> = match cw { [] => []; [x, ...r] => [lhash(x)] ++ leaf_hashes_g(r) }
fn append_zeros_g(xs: List<List<Int>>, k: Int) : List<List<Int>> = if k <= 0 then xs else append_zeros_g(xs ++ [glit(0)], k - 1)
fn pad_pow2_g(xs: List<List<Int>>) : List<List<Int>> = let n : Int = llen(xs) in append_zeros_g(xs, next_pow2(n, 1) - n)
fn level_up_g(xs: List<List<Int>>) : List<List<Int>> = match xs { [] => []; [a, ...rest] => match rest { [] => [a]; [b, ...r2] => [hashg(a, b)] ++ level_up_g(r2) } }
fn root_of_g(xs: List<List<Int>>) : List<Int> = match xs { [] => []; [a, ...rest] => match rest { [] => a; _ => root_of_g(level_up_g(xs)) } }
fn merkle_root_g(cw: List<G2>) : List<Int> = root_of_g(pad_pow2_g(leaf_hashes_g(cw)))
fn path_of_g(xs: List<List<Int>>, index: Int) : List<List<Int>> = match xs { [] => []; [a, ...rest] => match rest { [] => []; _ => let sib : List<Int> = if index % 2 == 0 then wnth(xs, index + 1) else wnth(xs, index - 1) in [sib] ++ path_of_g(level_up_g(xs), index / 2) } }
fn merkle_path_g(cw: List<G2>, index: Int) : List<List<Int>> = path_of_g(pad_pow2_g(leaf_hashes_g(cw)), index)
fn fold_path_g(h: List<Int>, index: Int, path: List<List<Int>>) : List<Int> = match path { [] => h; [s, ...rest] => let up : List<Int> = if index % 2 == 0 then hashg(h, s) else hashg(s, h) in fold_path_g(up, index / 2, rest) }
fn merkle_verify_g(leaf: G2, index: Int, path: List<List<Int>>, root: List<Int>) : Bool = bn_eq(fold_path_g(lhash(leaf), index, path), root)
fn fold_pair2(fx: G2, fmx: G2, x: List<Int>, beta: G2, finv2: List<Int>) : G2 =
let even : G2 = gscale2(gadd2(fx, fmx), finv2) in let odd : G2 = gscale2(gsub2(fx, fmx), finv(fmul([2], x))) in gadd2(even, gmul2(beta, odd))
# CUT THE ROPE 3: was O(m^2) — nth2(cw,i)/nth2(cw,i+half)/wnth(dom,i) per pair. to_vec once (O(m)), vget O(1).
fn fold_at2_go(cwv: List<G2>, domv: List<List<Int>>, beta: G2, finv2: List<Int>, i: Int, half: Int) : List<G2> =
if i >= half then [] else [fold_pair2(vget(cwv, i), vget(cwv, i + half), vget(domv, i), beta, finv2)] ++ fold_at2_go(cwv, domv, beta, finv2, i + 1, half)
fn fold_at2(cw: List<G2>, dom: List<List<Int>>, beta: G2, finv2: List<Int>, i: Int, half: Int) : List<G2> =
fold_at2_go(to_vec(cw), to_vec(dom), beta, finv2, 0, half)
# --- statement binding: a Poseidon digest over the gate list ------------------
# The FS transcript is SEEDED by this digest, so every FS challenge (the FRI fold
# betas and the query positions) is a deterministic function of the statement.
# gate_dig absorbs each gate's variant TAG (1..6, leading, for domain separation),
# its wire indices via glit (injective for indices < ~2^48 — true for every gate
# count), and — for GConst — the constant VALUE raw (full-width List<Int> field
# element, NOT routed through glit, so the claimed result R is captured at the full
# 2^64 width). The digest is WITNESS-INDEPENDENT: only public opcodes, wire indices,
# and constants are absorbed; GHint carries only its wire index (never the hint
# value), and the private witness `w` is never passed in. Under the current claim
# builders (build_claim / build_claim_m) the only constant in the gate list is the
# claimed result R (the last GConst), so the digest binds circuit structure + R
# without absorbing private CLI inputs (those stay in the witness, never as gates).
# glit(0) trims to [] (the field-zero element) — a valid hashg lane; the leading
# per-gate tag still separates distinct gate lists (Poseidon collision-resistance).
fn gate_dig(g: Gate, acc: List<Int>) : List<Int> = match g {
GConst(o, c) => hashg(hashg(hashg(acc, glit(1)), glit(o)), c);
GAdd(o, a, b) => hashg(hashg(hashg(hashg(acc, glit(2)), glit(o)), glit(a)), glit(b));
GSub(o, a, b) => hashg(hashg(hashg(hashg(acc, glit(3)), glit(o)), glit(a)), glit(b));
GMul(o, a, b) => hashg(hashg(hashg(hashg(acc, glit(4)), glit(o)), glit(a)), glit(b));
GHint(o) => hashg(hashg(acc, glit(5)), glit(o));
GEqZero(a) => hashg(hashg(acc, glit(6)), glit(a)) }
fn circuit_digest(gates: List<Gate>, acc: List<Int>) : List<Int> = match gates { [] => acc; [g, ...rest] => circuit_digest(rest, gate_dig(g, acc)) }
# 2718281 = a fixed domain-separation tag (distinct from the [] seed the FRI-root
# chain would otherwise start from). NOTE: protocol params (field, coset generator
# 7, blowup 16, fold-stop 8, query count 64, 12-bit grind) are NOT in the digest —
# they are compile-time, version-pinned constants the future TIER-1 independent
# verifier must agree on out of band; they are not transcript-bound.
# The digest seed now also binds the PROTOCOL PARAMETERS (coset generator 7, blowup
# fri_dsize(1)=32, fold-stop fri_final(0)=8, query count fri_queries(0)=82, grind bits
# 12) — so a verifier instantiated with mismatched params derives different challenges
# (parameter-substitution is transcript-enforced, closing one of the two FS-PARTIAL gaps).
fn stmt_seed_of(gates: List<Gate>) : List<Int> = circuit_digest(gates, hashg(hashg(hashg(hashg(hashg(glit(2718281), glit(7)), glit(fri_dsize(1))), glit(fri_final(0))), glit(fri_queries(0))), glit(12)))
# the FRI fold challenge beta, derived from the running transcript seed (statement +
# all preceding layer roots) — NOT from the bare layer root as before.
fn beta_of_seed_g(tseed: List<Int>) : G2 = G2(lane0(hashg(tseed, glit(101))), lane0(hashg(tseed, glit(102))))
# --- memoized Merkle tree: build every level ONCE, read roots/paths from it ----
# Query verification used to recompute pad_pow2_g(leaf_hashes_g(cw)) and rebuild
# every level per query (merkle_path_g) — O(queries · tree). Instead build the full
# tree (list of levels, level 0 = padded leaf hashes, last = [root]) once per FRI
# layer, store it in the Layer, and pull sibling paths straight out. Byte-identical
# (same hashes), just not recomputed. This unblocks raising the FRI query count.
fn wnth3(xs: List<List<List<Int>>>, i: Int) : List<List<Int>> = match xs { [] => []; [x, ...r] => if i == 0 then x else wnth3(r, i - 1) }
fn l3len(xs: List<List<List<Int>>>) : Int = match xs { [] => 0; [x, ...r] => 1 + l3len(r) }
fn build_levels_g(level: List<List<Int>>, acc: List<List<List<Int>>>) : List<List<List<Int>>> =
if llen(level) <= 1 then acc ++ [level] else build_levels_g(level_up_g(level), acc ++ [level])
fn merkle_levels_g(cw: List<G2>) : List<List<List<Int>>> = build_levels_g(pad_pow2_g(leaf_hashes_g(cw)), [])
fn root_of_levels(levels: List<List<List<Int>>>) : List<Int> = match wnth3(levels, l3len(levels) - 1) { [] => []; [r, ...t] => r }
fn path_of_levels(levels: List<List<List<Int>>>, li: Int, index: Int) : List<List<Int>> =
if li >= l3len(levels) - 1 then []
else let level : List<List<Int>> = wnth3(levels, li) in
let sib : List<Int> = if index % 2 == 0 then wnth(level, index + 1) else wnth(level, index - 1) in
[sib] ++ path_of_levels(levels, li + 1, index / 2)
fn merkle_path_levels(levels: List<List<List<Int>>>, index: Int) : List<List<Int>> = path_of_levels(levels, 0, index)
# --- Merkle batch-opening (v5.124, proof-size Stage 1): reconstruct a tree root from the opened leaves
# (at their indices) + a MINIMAL co-path node set, replacing the per-query independent sibling paths.
# Validated standalone in examples/prove/merkle_batch_difftest.glass + fuzz/merkle_batch_proto.py.
# Canonical bottom-up sweep over the memoized levels; even position = LEFT input to hashg (matching
# fold_path_g / level_up_g). The opened-index set is Fiat-Shamir-derived and recomputed by the verifier,
# so NO index tokens are transmitted (the co-path is consumed in the same canonical order both sides).
fn insI(a: Int, xs: List<Int>) : List<Int> = match xs { [] => [a]; [b, ...t] => if a <= b then [a] ++ xs else [b] ++ insI(a, t) }
fn sortI(xs: List<Int>) : List<Int> = match xs { [] => []; [a, ...t] => insI(a, sortI(t)) }
fn memI(xs: List<Int>, k: Int) : Bool = match xs { [] => false; [a, ...t] => if a == k then true else memI(t, k) }
fn unionI(xs: List<Int>, a: Int) : List<Int> = if memI(xs, a) then xs else xs ++ [a]
fn oidx_go(queries: List<Int>, half: Int, acc: List<Int>) : List<Int> =
match queries { [] => acc; [p, ...rest] => let j : Int = p % half in oidx_go(rest, half, unionI(unionI(acc, j), j + half)) }
# the canonical sorted set of opened leaf positions {j, j+half : query p}, recomputed by the verifier
fn oidx_of(queries: List<Int>, half: Int) : List<Int> = sortI(oidx_go(queries, half, []))
# PROVER: minimal co-path over the levels. `known` = positions reconstructible at this level; a sibling
# digest is emitted iff not itself known (i.e. not opened/reconstructible). Canonical: ascending position.
fn bcp_go(todo: List<Int>, known: List<Int>, nodes: List<List<Int>>, parents: List<Int>, cp: List<List<Int>>) : (List<Int>, List<List<Int>>) =
match todo { [] => (parents, cp);
[pos, ...rest] => let parent : Int = pos / 2 in
if memI(parents, parent) then bcp_go(rest, known, nodes, parents, cp)
else let sib : Int = if pos % 2 == 0 then pos + 1 else pos - 1 in
let cp2 : List<List<Int>> = if memI(known, sib) then cp else cp ++ [wnth(nodes, sib)] in
bcp_go(rest, known, nodes, parents ++ [parent], cp2) }
fn bcopath(levels: List<List<List<Int>>>, li: Int, known: List<Int>, cp: List<List<Int>>) : List<List<Int>> =
if li >= l3len(levels) - 1 then cp
else match bcp_go(sortI(known), known, wnth3(levels, li), [], []) { (parents, cpn) => bcopath(levels, li + 1, parents, cp ++ cpn) }
# VERIFIER: reconstruct the root. `known` = List<Pair<pos, digest>>; co-path siblings consumed in the
# same canonical order. An under-run uses [] (-> wrong root -> REJECT); an over-run leaves leftover cp.
fn khasL(m: List<Pair<Int, List<Int>>>, k: Int) : Bool = match m { [] => false; [Pair(kk, vv), ...t] => if kk == k then true else khasL(t, k) }
fn kgetL(m: List<Pair<Int, List<Int>>>, k: Int) : List<Int> = match m { [] => []; [Pair(kk, vv), ...t] => if kk == k then vv else kgetL(t, k) }
fn keysL(m: List<Pair<Int, List<Int>>>) : List<Int> = match m { [] => []; [Pair(k, v), ...t] => [k] ++ keysL(t) }
fn rec_go(known: List<Pair<Int, List<Int>>>, todo: List<Int>, cp: List<List<Int>>, acc: List<Pair<Int, List<Int>>>) : (List<Pair<Int, List<Int>>>, List<List<Int>>) =
match todo { [] => (acc, cp);
[pos, ...rest] => let parent : Int = pos / 2 in
if khasL(acc, parent) then rec_go(known, rest, cp, acc)
else let sib : Int = if pos % 2 == 0 then pos + 1 else pos - 1 in
match (if khasL(known, sib) then (kgetL(known, sib), cp) else (match cp { [] => ([], []); [s, ...cprest] => (s, cprest) })) { (sibh, cp2) =>
let me : List<Int> = kgetL(known, pos) in
let ph : List<Int> = if pos % 2 == 0 then hashg(me, sibh) else hashg(sibh, me) in
rec_go(known, rest, cp2, acc ++ [Pair(parent, ph)]) } }
fn recon_root(known: List<Pair<Int, List<Int>>>, cp: List<List<Int>>, height: Int) : (List<Int>, List<List<Int>>) =
if height <= 0 then (match known { [] => []; [Pair(k, v), ...t] => v }, cp)
else match rec_go(known, sortI(keysL(known)), cp, []) { (parents, cprest) => recon_root(parents, cprest, height - 1) }
# reconstruct + check: root must match AND the co-path must be exactly consumed (no over-run -> no malleability)
fn recon_ok(known: List<Pair<Int, List<Int>>>, cp: List<List<Int>>, height: Int, root: List<Int>) : Bool =
match recon_root(known, cp, height) { (rr, leftover) => bn_eq(rr, root) && (llen(leftover) == 0) }
type Layer = | Layer(List<G2>, List<List<Int>>, List<Int>, G2, List<List<List<Int>>>)
# tseed is threaded through the fold: tseed -> hashg(tseed, root_i) before each
# fold, and beta_i is derived from that updated seed. So beta_i depends on the
# statement seed AND every preceding layer root (fixing both the prior absence of
# statement-binding and the prior absence of cross-layer root chaining). The SAME
# beta value is stored in the Layer and used in fold_at2 (computed once), so the
# self-check (verify_query_g reads the stored beta) stays consistent by construction.
fn commit_g(cw: List<G2>, dom: List<List<Int>>, acc: List<Layer>, tseed: List<Int>, finv2: List<Int>) : (List<Layer>, List<G2>) =
if len(cw) <= fri_final(0) then (acc, cw)
else let levels : List<List<List<Int>>> = merkle_levels_g(cw) in let root : List<Int> = root_of_levels(levels) in
let tseed2 : List<Int> = hashg(tseed, root) in
let beta : G2 = beta_of_seed_g(tseed2) in let half : Int = len(cw) / 2 in
commit_g(fold_at2(cw, dom, beta, finv2, 0, half), sqd(dom, 0, half), acc ++ [Layer(cw, dom, root, beta, levels)], tseed2, finv2)
# LOAD-BEARING INVARIANT: transcript_seed_g(layers, stmt_seed) MUST equal the final
# tseed threaded through commit_g — identical hashg(.,root) chaining seeded by
# stmt_seed, in layer (= commit) order. This is the exact derivation the future
# TIER-1 independent verifier will re-derive the betas and query positions from; if
# these two ever diverge, the prover would sample queries from a different seed than
# it folded under. (Defensive alternative: have commit_g return the final tseed.)
fn transcript_seed_g(layers: List<Layer>, acc: List<Int>) : List<Int> = match layers { [] => acc; [Layer(cw, dom, root, beta, levels), ...rest] => transcript_seed_g(rest, hashg(acc, root)) }
fn q_low(h: List<Int>, dsize: Int) : Int = match h { [] => 0; [x, ...t] => x % dsize }
# queries WITHOUT replacement: re-draw on a collision (fresh counter) until `count`
# DISTINCT positions, so the FRI soundness gets the full `count` independent tests (no
# wasted repeats eroding the razor-thin 68+12 margin). Deterministic: prover + verifier
# derive the identical set. Terminates since count (82) << dsize (=32N).
fn imember(x: Int, xs: List<Int>) : Bool = match xs { [] => false; [h, ...t] => if h == x then true else imember(x, t) }
fn sample_distinct(seed: List<Int>, need: Int, ctr: Int, acc: List<Int>, dsize: Int) : List<Int> =
if need <= 0 then acc
else if lcnt(acc) >= dsize then acc # all dsize positions collected (tiny circuits where count > dsize) -> stop
else let q : Int = q_low(hashg(seed, glit(ctr)), dsize) in
if imember(q, acc) then sample_distinct(seed, need, ctr + 1, acc, dsize)
else sample_distinct(seed, need - 1, ctr + 1, acc ++ [q], dsize)
fn sample_queries_g(seed: List<Int>, count: Int, dsize: Int) : List<Int> = sample_distinct(seed, count, 1, [], dsize)
# --- grinding (proof-of-work on the Fiat-Shamir query seed) -------------------
# The prover must find a nonce whose hash with the transcript seed clears the low
# G_BITS bits (here 12). An adversary trying to grind a favorable set of query
# positions must therefore redo 2^12 hashing per attempt — adding 12 bits to the
# query-phase soundness (the standard STARK trick; production uses 20-30 bits).
# Two-level search keeps recursion depth bounded (~chunk + #chunks), so neither the
# interpreter's limit nor the native C stack is at risk.
# THE MEASURING REED (v5.63.0): the achieved bit-security is RE-DERIVED from the live proof
# parameters here — never asserted in prose. grind_modulus is the SINGLE source for the PoW
# target (pow_ok and grind_bits both read it, so the printed grind term can never drift from
# the grinding the prover actually does). provable_bits/listdecode_bits are pinned integer
# closed-forms at the construction's fixed rate ρ=4N/32N=1/8 (blowup = fri_dsize(1) = 32):
# ~0.83 bit/query unique-decoding, 1.5 bit/query list-decoding, + the grind bits.
fn grind_modulus(d: Int) : Int = 4096 # 2^12 proof-of-work target
fn grind_bits(d: Int) : Int = let g : Int = ilog2(grind_modulus(0)) in if g > 16 then 16 else g # = 12 today; CAPPED at 16: pow_ok only tests the first 16-bit limb (x in [0,2^16)), so grind_modulus > 2^16 would NOT raise the actual proof-of-work above 2^-16 — the cap keeps the PRINTED bit-security from overstating the grinding the prover does (v5.120 audit fix). Widening pow_ok to consume more limbs is the prerequisite for a genuinely higher grind.
fn blowup(d: Int) : Int = fri_dsize(1) # coset = 32N -> blowup 32 -> ρ = 1/8
fn provable_bits(gates: List<Gate>) : Int = (fri_queries(ng(gates)) * 83) / 100 + grind_bits(0)
fn listdecode_bits(gates: List<Gate>) : Int = (fri_queries(ng(gates)) * 3) / 2 + grind_bits(0)
fn measure_line(gates: List<Gate>) : String =
int_to_string(provable_bits(gates)) ++ " bits provable / " ++ int_to_string(listdecode_bits(gates))
++ " bits list-decoding (conjectural, proximity-gap) (" ++ int_to_string(fri_queries(ng(gates))) ++ " queries, blowup "
++ int_to_string(blowup(0)) ++ " => rate 1/8, " ++ int_to_string(grind_bits(0)) ++ "-bit grind, 4-lane ~128-bit hash)"
fn pow_ok(h: List<Int>) : Bool = match trim(h) { [] => true; [x, ...t] => x % grind_modulus(0) == 0 } # 12-bit proof-of-work: the FS query seed's low 12 bits must be 0
fn grind_in(seed: List<Int>, base: Int, i: Int) : Int =
if i >= 64 then 0 - 1 else (if pow_ok(hashg(seed, glit(base * 64 + i))) then base * 64 + i else grind_in(seed, base, i + 1))
fn grind_out(seed: List<Int>, base: Int) : Int = let r : Int = grind_in(seed, base, 0) in if r >= 0 then r else grind_out(seed, base + 1)
fn grind(seed: List<Int>) : Int = grind_out(seed, 0)
fn next_val2(rest: List<Layer>, final: List<G2>, j: Int) : G2 = match rest { [] => nth2(final, j); [Layer(cw, dom, root, beta, levels), ...more] => nth2(cw, j) }
fn verify_query_g(layers: List<Layer>, final: List<G2>, p: Int, finv2: List<Int>) : Bool =
match layers { [] => true;
[Layer(cw, dom, root, beta, levels), ...rest] =>
let half : Int = len(cw) / 2 in let j : Int = p % half in
let lx : G2 = nth2(cw, j) in let lmx : G2 = nth2(cw, j + half) in
let ok1 : Bool = merkle_verify_g(lx, j, merkle_path_levels(levels, j), root) in
let ok2 : Bool = merkle_verify_g(lmx, j + half, merkle_path_levels(levels, j + half), root) in
let folded : G2 = fold_pair2(lx, lmx, wnth(dom, j), beta, finv2) in
if ok1 then if ok2 then if geq2(folded, next_val2(rest, final, j)) then verify_query_g(rest, final, j, finv2) else false else false else false }
fn count_catches_g(layers: List<Layer>, final: List<G2>, qs: List<Int>, finv2: List<Int>) : Int =
match qs { [] => 0; [p, ...rest] => (if verify_query_g(layers, final, p, finv2) then 0 else 1) + count_catches_g(layers, final, rest, finv2) }
fn all_eq2(cw: List<G2>, x: G2) : Bool = match cw { [] => true; [y, ...r] => if geq2(y, x) then all_eq2(r, x) else false }
fn is_const2(cw: List<G2>) : Bool = match cw { [] => true; [x, ...r] => all_eq2(r, x) }
fn blind_g(cw: List<G2>, coeffs: List<List<Int>>, dom: List<List<Int>>, i: Int) : List<G2> =
if i >= len(cw) then [] else [gadd2(nth2(cw, i), emb(poly_eval_b(coeffs, wnth(dom, i))))] ++ blind_g(cw, coeffs, dom, i + 1)
fn prove_stark(gates: List<Gate>, w: List<List<Int>>, seed: Int) : Bool =
let stmt_seed : List<Int> = stmt_seed_of(gates) in # bind the statement (circuit + R) into the transcript
let ecw : List<G2> = blind_g(embed_cw(q_cw(gates, w)), rand_coeffs(seed, 0, 16), fri_coset(ng(gates)), 0) in
match commit_g(ecw, fri_coset(ng(gates)), [], stmt_seed, finv([2])) { (layers, final) =>
let base_seed : List<Int> = transcript_seed_g(layers, stmt_seed) in # == the final tseed from commit_g (see invariant above)
let nz : Int = grind(base_seed) in # proof-of-work: 12 bits
let ground : List<Int> = hashg(base_seed, glit(nz)) in # the PoW-cleared query seed
let faults : Int = count_catches_g(layers, final, sample_queries_g(ground, fri_queries(ng(gates)), fri_dsize(ng(gates))), finv([2])) in
is_const2(final) && faults == 0 && pow_ok(ground) }
fn stark_root(gates: List<Gate>, w: List<List<Int>>, seed: Int) : List<Int> =
merkle_root_g(blind_g(embed_cw(q_cw(gates, w)), rand_coeffs(seed, 0, 16), fri_coset(ng(gates)), 0))
# === TIER-1 (WIP, NOT YET SHIPPED): an INDEPENDENT verify(statement, proof) ====
# Splits the prover from the verifier. verify RE-DERIVES every Fiat-Shamir
# challenge — stmt_seed, the fold betas, the grind, the query positions — from the
# PUBLIC gates + the proof's Merkle roots; it never reads a stored beta and never
# sees the witness. This is the SCAFFOLDING (challenge re-derivation + opening
# checks). *** IT IS NOT YET SOUNDNESS ***: verify checks only that a low-degree
# codeword was committed and that the openings are Merkle/fold-consistent — it does
# NOT recompute G/Z_H from the gates, so a malicious prover can commit ANY low-degree
# codeword (e.g. P=0) and be ACCEPTed regardless of the statement (see prove_evil
# below — an executable demonstration of the gap). Closing it needs TIER-1 part 2:
# a trace commitment + the OOD/DEEP quotient identity + a copy/permutation argument.
type QOpen = | QOpen(G2, G2, List<List<Int>>, List<List<Int>>) # one query, one layer: lx, lmx, path_lx, path_lmx
type Proof = | Proof(List<List<Int>>, List<G2>, Int, List<List<QOpen>>) # roots, final, nonce, openings[query][layer]
# v5.125 Stage 2: the FRI openings (ProofB3 only) are BATCHED per LAYER. Per (query,layer) we keep only
# the opened VALUES (QVal: lx, lmx) — the fold-chain inputs — and each FRI layer's tree is opened ONCE as
# a multiproof: BFri carries qvals[query][layer] + one shared co-path per layer. The verifier reconstructs
# each layer root (vfri_recon, reusing recon_root) and runs the per-query fold chain (vq_b3) with NO
# per-query Merkle path. The FRI domains nest as powers of two, so a query's layer-L index is p % half_L.
type QVal = | QVal(G2, G2)
type BFri = | BFri(List<List<QVal>>, List<List<List<Int>>>) # qvals[query][layer], copath[layer]
# PROVER: per-query opened values (no paths) + one co-path per FRI layer over its opened-index set.
fn extract_qvals(layers: List<Layer>, p: Int) : List<QVal> =
match layers { [] => [];
[Layer(cw, dom, root, beta, levels), ...rest] =>
let half : Int = len(cw) / 2 in let j : Int = p % half in
[QVal(nth2(cw, j), nth2(cw, j + half))] ++ extract_qvals(rest, j) }
fn extract_qvals_all(layers: List<Layer>, queries: List<Int>) : List<List<QVal>> =
match queries { [] => []; [p, ...rest] => [extract_qvals(layers, p)] ++ extract_qvals_all(layers, rest) }
fn fri_copaths(layers: List<Layer>, queries: List<Int>) : List<List<List<Int>>> =
match layers { [] => [];
[Layer(cw, dom, root, beta, levels), ...rest] =>
let half : Int = len(cw) / 2 in
[bcopath(levels, 0, oidx_of(queries, half), [])] ++ fri_copaths(rest, queries) }
fn extract_bfri(layers: List<Layer>, queries: List<Int>) : BFri =
BFri(extract_qvals_all(layers, queries), fri_copaths(layers, queries))
# VERIFIER: reconstruct each FRI layer root from the opened values + co-path (FRI leaf = lhash(value)).
fn nth_qval(qv: List<QVal>, i: Int) : QVal = match qv { [] => QVal(G2([], []), G2([], [])); [x, ...r] => if i == 0 then x else nth_qval(r, i - 1) }
fn frimap(qvals: List<List<QVal>>, queries: List<Int>, lyr: Int, half: Int, acc: List<Pair<Int, List<Int>>>) : List<Pair<Int, List<Int>>> =
match qvals { [] => acc; [row, ...rr] => match queries { [] => acc; [p, ...qr] =>
match nth_qval(row, lyr) { QVal(lx, lmx) => let j : Int = p % half in
frimap(rr, qr, lyr, half, mapput(mapput(acc, j, lhash(lx)), j + half, lhash(lmx))) } } }
fn vfri_recon(qvals: List<List<QVal>>, queries: List<Int>, lcopaths: List<List<List<Int>>>, broots: List<List<Int>>, doms: List<List<List<Int>>>, lyr: Int, lyrs: Int) : Bool =
if lyr >= lyrs then true
else let domL : List<List<Int>> = wnth3(doms, lyr) in let half : Int = llen(domL) / 2 in let height : Int = ilog2(llen(domL)) in
if recon_ok(frimap(qvals, queries, lyr, half, []), wnth3(lcopaths, lyr), height, wnth(broots, lyr))
then vfri_recon(qvals, queries, lcopaths, broots, doms, lyr + 1, lyrs) else false
# per-query FRI fold chain (no Merkle — that is now done in bulk by vfri_recon); ties each layer's fold
# to the next layer's opened value, exactly as vq did, but reading QVal and skipping merkle_verify.
fn vq_b3(qv: List<QVal>, betas: List<G2>, doms: List<List<List<Int>>>, final: List<G2>, p: Int, finv2: List<Int>) : Bool =
match qv { [] => true;
[QVal(lx, lmx), ...orest] =>
match betas { [] => false; [beta, ...brest] =>
match doms { [] => false; [dom, ...drest] =>
let half : Int = llen(dom) / 2 in let j : Int = p % half in
let next_half : Int = match drest { [] => 0; [nd, ...nn] => llen(nd) / 2 } in
let nextv : G2 = match orest { [] => nth2(final, j); [QVal(nlx, nlmx), ...more] => if j < next_half then nlx else nlmx } in
let folded : G2 = fold_pair2(lx, lmx, wnth(dom, j), beta, finv2) in
if geq2(folded, nextv) then vq_b3(orest, brest, drest, final, j, finv2) else false } } }
fn qvlen(xs: List<List<QVal>>) : Int = match xs { [] => 0; [x, ...r] => 1 + qvlen(r) }
fn qval1len(xs: List<QVal>) : Int = match xs { [] => 0; [x, ...r] => 1 + qval1len(r) }
fn fri_layers(d: Int) : Int = ilog2(fri_dsize(d) / fri_final(0)) # #FRI layers = log2(16N/8) = log2(2N)
fn qopen_len(xs: List<QOpen>) : Int = match xs { [] => 0; [x, ...r] => 1 + qopen_len(r) }
fn openings_len(xs: List<List<QOpen>>) : Int = match xs { [] => 0; [x, ...r] => 1 + openings_len(r) }
fn roots_of(layers: List<Layer>) : List<List<Int>> = match layers { [] => []; [Layer(cw, dom, root, beta, levels), ...rest] => [root] ++ roots_of(rest) }
fn build_gates(b: Build) : List<Gate> = match b { Build(n, gs, w) => gs }
fn build_wit(b: Build) : List<List<Int>> = match b { Build(n, gs, w) => w }
# --- prover: emit a Proof (roots + per-query openings, paths from stored levels) --
fn extract_query(layers: List<Layer>, p: Int) : List<QOpen> =
match layers { [] => [];
[Layer(cw, dom, root, beta, levels), ...rest] =>
let half : Int = len(cw) / 2 in let j : Int = p % half in
[QOpen(nth2(cw, j), nth2(cw, j + half), merkle_path_levels(levels, j), merkle_path_levels(levels, j + half))] ++ extract_query(rest, j) }
fn extract_all(layers: List<Layer>, queries: List<Int>) : List<List<QOpen>> =
match queries { [] => []; [p, ...rest] => [extract_query(layers, p)] ++ extract_all(layers, rest) }
fn prove_to_proof(gates: List<Gate>, w: List<List<Int>>, seed: Int) : Proof =
let stmt_seed : List<Int> = stmt_seed_of(gates) in
let ecw : List<G2> = blind_g(embed_cw(q_cw(gates, w)), rand_coeffs(seed, 0, 16), fri_coset(ng(gates)), 0) in
match commit_g(ecw, fri_coset(ng(gates)), [], stmt_seed, finv([2])) { (layers, final) =>
let base_seed : List<Int> = transcript_seed_g(layers, stmt_seed) in
let nz : Int = grind(base_seed) in
let ground : List<Int> = hashg(base_seed, glit(nz)) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(ng(gates)), fri_dsize(ng(gates))) in
Proof(roots_of(layers), final, nz, extract_all(layers, queries)) }
# --- verifier: re-derive ALL challenges from public gates + proof.roots --------
fn rederive_betas(roots: List<List<Int>>, tseed: List<Int>) : List<G2> =
match roots { [] => []; [root, ...rest] => let ts2 : List<Int> = hashg(tseed, root) in [beta_of_seed_g(ts2)] ++ rederive_betas(rest, ts2) }
fn seed_from_roots(roots: List<List<Int>>, tseed: List<Int>) : List<Int> =
match roots { [] => tseed; [root, ...rest] => seed_from_roots(rest, hashg(tseed, root)) }
fn build_doms(dom: List<List<Int>>, count: Int) : List<List<List<Int>>> =
if count <= 0 then [] else [dom] ++ build_doms(sqd(dom, 0, llen(dom) / 2), count - 1)
# The fold target of layer i lands at index j_i in layer i+1's codeword, which is
# EITHER that layer's opened lx (if j_i < half_{i+1}) OR its lmx — and the SAME
# object is Merkle-verified at layer i+1, so the chain is airtight across the split.
fn vq(oq: List<QOpen>, roots: List<List<Int>>, betas: List<G2>, doms: List<List<List<Int>>>, final: List<G2>, p: Int, finv2: List<Int>) : Bool =
match oq { [] => true;
[QOpen(lx, lmx, plx, plmx), ...orest] =>
match roots { [] => false; [root, ...rrest] =>
match betas { [] => false; [beta, ...brest] =>
match doms { [] => false; [dom, ...drest] =>
let half : Int = llen(dom) / 2 in let j : Int = p % half in
let next_half : Int = match drest { [] => 0; [nd, ...nn] => llen(nd) / 2 } in
let nextv : G2 = match orest { [] => nth2(final, j); [QOpen(nlx, nlmx, np1, np2), ...more] => if j < next_half then nlx else nlmx } in
let ok1 : Bool = merkle_verify_g(lx, j, plx, root) in
let ok2 : Bool = merkle_verify_g(lmx, j + half, plmx, root) in
let folded : G2 = fold_pair2(lx, lmx, wnth(dom, j), beta, finv2) in
if ok1 then if ok2 then if geq2(folded, nextv) then vq(orest, rrest, brest, drest, final, j, finv2) else false else false else false } } } }
fn verify_queries(openings: List<List<QOpen>>, roots: List<List<Int>>, betas: List<G2>, doms: List<List<List<Int>>>, final: List<G2>, queries: List<Int>, lyrs: Int, finv2: List<Int>) : Bool =
match openings { [] => match queries { [] => true; _ => false };
[oq, ...orest] => match queries { [] => false; [p, ...qrest] =>
if qopen_len(oq) == lyrs then (if vq(oq, roots, betas, doms, final, p, finv2) then verify_queries(orest, roots, betas, doms, final, qrest, lyrs, finv2) else false) else false } }
fn verify(gates: List<Gate>, proof: Proof) : Bool =
match proof { Proof(roots, final, nonce, openings) =>
let n : Int = ng(gates) in let lyrs : Int = fri_layers(n) in
let stmt_seed : List<Int> = stmt_seed_of(gates) in
let betas : List<G2> = rederive_betas(roots, stmt_seed) in
let base_seed : List<Int> = seed_from_roots(roots, stmt_seed) in
let ground : List<Int> = hashg(base_seed, glit(nonce)) in
let doms : List<List<List<Int>>> = build_doms(fri_coset(n), lyrs) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(n), fri_dsize(n)) in
(llen(roots) == lyrs) && (len(final) == fri_final(0)) && (openings_len(openings) == fri_queries(n))
&& pow_ok(ground) && is_const2(final) && verify_queries(openings, roots, betas, doms, final, queries, lyrs, finv([2])) }
# --- the HONEST GAP, made executable: a malicious prover commits P=0 (low-degree,
# unrelated to the gates or to ANY witness); verify ACCEPTs it for any statement.
# THIS is why the scaffolding above is not yet soundness — TIER-1 part 2 closes it.
fn zeros_cw(k: Int) : List<G2> = if k <= 0 then [] else [emb(glit(0))] ++ zeros_cw(k - 1)
fn prove_evil(gates: List<Gate>) : Proof =
let stmt_seed : List<Int> = stmt_seed_of(gates) in
match commit_g(zeros_cw(fri_dsize(ng(gates))), fri_coset(ng(gates)), [], stmt_seed, finv([2])) { (layers, final) =>
let base_seed : List<Int> = transcript_seed_g(layers, stmt_seed) in
let nz : Int = grind(base_seed) in
let ground : List<Int> = hashg(base_seed, glit(nz)) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(ng(gates)), fri_dsize(ng(gates))) in
Proof(roots_of(layers), final, nz, extract_all(layers, queries)) }
fn tamper_nonce(proof: Proof) : Proof = match proof { Proof(roots, final, nonce, openings) => Proof(roots, final, nonce + 1, openings) }
# === TIER-1 PART 2 (WIP): SOUND prover/verifier — ties Q to the gate constraints
# Closes the P=0 gap. Commit the trace (l,r,o) AND the quotient Q (UNBLINDED — ZK is
# SUSPENDED on this path: witness opened in clear). Draw an out-of-domain z in F_{p^2}
# AFTER the trace commitment; prover sends l(z),r(z),o(z),Q(z); verifier checks the
# constraint identity Q(z)*Z_H(z) == G(z) in F_{p^2}; the DEEP quotients (T(x)-T(z))/(x-z)
# for T in {l,r,o,Q} are batched with an FS gamma and run through the EXISTING FRI, which
# degree-bounds all four columns AND binds the sent z-evals to the committed trees.
# WHAT THIS PROVES: every row of the committed trace satisfies its per-row gate, and R =
# the asserted output row. WHAT IT DOES NOT: inter-row WIRE consistency (B3, copy/perm,
# not built) — a prover can still thread inconsistent values across rows. NOT out of
# research grade (B3 + external audit remain); NOT zero-knowledge on this path.
type TOpen = | TOpen(List<Int>, List<Int>, List<Int>, List<Int>, List<List<Int>>, List<Int>, List<Int>, List<Int>, List<Int>, List<List<Int>>)
type ProofS = | ProofS(List<Int>, G2, G2, G2, G2, List<List<Int>>, List<G2>, Int, List<List<QOpen>>, List<TOpen>)
fn ood_of(ts: List<Int>) : G2 = G2(lane0(hashg(ts, glit(201))), lane0(hashg(ts, glit(202))))
fn gamma_of(ts: List<Int>) : G2 = G2(lane0(hashg(ts, glit(211))), lane0(hashg(ts, glit(212))))
fn absorb_g2(acc: List<Int>, g: G2) : List<Int> = match g { G2(a0, a1) => hashg(hashg(acc, a0), a1) }
fn ts_with_ood(ts1: List<Int>, lz: G2, rz: G2, oz: G2, qz: G2) : List<Int> = absorb_g2(absorb_g2(absorb_g2(absorb_g2(ts1, lz), rz), oz), qz)
fn zh_g(z: G2, n: Int) : G2 = gsub2(gpow2(z, n), G2([1], []))
fn g_at_z(pc: List<List<List<Int>>>, lz: G2, rz: G2, oz: G2, z: G2) : G2 =
let va : G2 = poly_eval_g(wnth3(pc, 0), z) in let vm : G2 = poly_eval_g(wnth3(pc, 1), z) in
let vs : G2 = poly_eval_g(wnth3(pc, 2), z) in let vc : G2 = poly_eval_g(wnth3(pc, 3), z) in
let vqe : G2 = poly_eval_g(wnth3(pc, 4), z) in let vk : G2 = poly_eval_g(wnth3(pc, 5), z) in
gadd2(gmul2(va, gsub2(oz, gadd2(lz, rz))), gadd2(gmul2(vm, gsub2(oz, gmul2(lz, rz))), gadd2(gmul2(vs, gsub2(oz, gsub2(lz, rz))), gadd2(gmul2(vc, gsub2(oz, vk)), gmul2(vqe, lz)))))
fn deep_batch(li: List<Int>, ri: List<Int>, oi: List<Int>, qi: List<Int>, xi: List<Int>, lz: G2, rz: G2, oz: G2, qz: G2, z: G2, gam: G2) : G2 =
let den : G2 = ginv2(gsub2(emb(xi), z)) in
let qdq : G2 = gmul2(gsub2(emb(qi), qz), den) in let qdl : G2 = gmul2(gsub2(emb(li), lz), den) in
let qdr : G2 = gmul2(gsub2(emb(ri), rz), den) in let qdo : G2 = gmul2(gsub2(emb(oi), oz), den) in
let g2 : G2 = gmul2(gam, gam) in let g3 : G2 = gmul2(g2, gam) in
gadd2(qdq, gadd2(gmul2(gam, qdl), gadd2(gmul2(g2, qdr), gmul2(g3, qdo))))
fn comb_leaf(li: List<Int>, ri: List<Int>, oi: List<Int>, qi: List<Int>) : List<Int> = hashg(hashg(hashg(li, ri), oi), qi)
fn trace_leaf_list(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<List<Int>>, i: Int, m: Int) : List<List<Int>> =
if i >= m then [] else [comb_leaf(wnth(lcw, i), wnth(rcw, i), wnth(ocw, i), wnth(qcw, i))] ++ trace_leaf_list(lcw, rcw, ocw, qcw, i + 1, m)
fn build_b(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<List<Int>>, coset: List<List<Int>>, lz: G2, rz: G2, oz: G2, qz: G2, z: G2, gam: G2, i: Int, m: Int) : List<G2> =
if i >= m then [] else [deep_batch(wnth(lcw, i), wnth(rcw, i), wnth(ocw, i), wnth(qcw, i), wnth(coset, i), lz, rz, oz, qz, z, gam)] ++ build_b(lcw, rcw, ocw, qcw, coset, lz, rz, oz, qz, z, gam, i + 1, m)
fn merkle_verify_h(leaf: List<Int>, index: Int, path: List<List<Int>>, root: List<Int>) : Bool = bn_eq(fold_path_g(leaf, index, path), root)
fn tlen(xs: List<TOpen>) : Int = match xs { [] => 0; [x, ...r] => 1 + tlen(r) }
fn extract_trace1(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<List<Int>>, tlevels: List<List<List<Int>>>, p: Int, m: Int) : TOpen =
let half : Int = m / 2 in let j : Int = p % half in
TOpen(wnth(lcw, j), wnth(rcw, j), wnth(ocw, j), wnth(qcw, j), merkle_path_levels(tlevels, j),
wnth(lcw, j + half), wnth(rcw, j + half), wnth(ocw, j + half), wnth(qcw, j + half), merkle_path_levels(tlevels, j + half))
fn extract_traces(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<List<Int>>, tlevels: List<List<List<Int>>>, queries: List<Int>, m: Int) : List<TOpen> =
match queries { [] => []; [p, ...rest] => [extract_trace1(lcw, rcw, ocw, qcw, tlevels, p, m)] ++ extract_traces(lcw, rcw, ocw, qcw, tlevels, rest, m) }
fn prove_sound(gates: List<Gate>, w: List<List<Int>>) : ProofS =
let n : Int = ng(gates) in let m : Int = fri_dsize(n) in let coset : List<List<Int>> = fri_coset(n) in
let stmt : List<Int> = stmt_seed_of(gates) in
let pc : List<List<List<Int>>> = public_cols(gates, n) in let tc : List<List<List<Int>>> = trace_cols(gates, w, n) in
let ll : List<List<Int>> = wnth3(tc, 0) in let rr : List<List<Int>> = wnth3(tc, 1) in let oo : List<List<Int>> = wnth3(tc, 2) in
let lcw : List<List<Int>> = lde(ll, n) in let rcw : List<List<Int>> = lde(rr, n) in let ocw : List<List<Int>> = lde(oo, n) in
let qcw : List<List<Int>> = q_cw(gates, w) in
let tlevels : List<List<List<Int>>> = build_levels_g(pad_pow2_g(trace_leaf_list(lcw, rcw, ocw, qcw, 0, m)), []) in
let trace_root : List<Int> = root_of_levels(tlevels) in
let ts1 : List<Int> = hashg(stmt, trace_root) in let z : G2 = ood_of(ts1) in
let lz : G2 = poly_eval_g(ll, z) in let rz : G2 = poly_eval_g(rr, z) in let oz : G2 = poly_eval_g(oo, z) in
let qz : G2 = gmul2(g_at_z(pc, lz, rz, oz, z), ginv2(zh_g(z, n))) in
let ts2 : List<Int> = ts_with_ood(ts1, lz, rz, oz, qz) in let gam : G2 = gamma_of(ts2) in
let bcw : List<G2> = build_b(lcw, rcw, ocw, qcw, coset, lz, rz, oz, qz, z, gam, 0, m) in
match commit_g(bcw, coset, [], ts2, finv([2])) { (blayers, bfinal) =>
let base_seed : List<Int> = transcript_seed_g(blayers, ts2) in
let nz : Int = grind(base_seed) in let ground : List<Int> = hashg(base_seed, glit(nz)) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(n), fri_dsize(n)) in
ProofS(trace_root, lz, rz, oz, qz, roots_of(blayers), bfinal, nz, extract_all(blayers, queries), extract_traces(lcw, rcw, ocw, qcw, tlevels, queries, fri_dsize(n))) }
fn vsq1(boq: List<QOpen>, toq: TOpen, broots: List<List<Int>>, betas: List<G2>, doms: List<List<List<Int>>>, bfinal: List<G2>, coset: List<List<Int>>, lz: G2, rz: G2, oz: G2, qz: G2, z: G2, gam: G2, trace_root: List<Int>, m: Int, p: Int, lyrs: Int, finv2: List<Int>) : Bool =
let half : Int = m / 2 in let j : Int = p % half in
match toq { TOpen(lj, rj, oj, qj, pj, ljh, rjh, ojh, qjh, pjh) =>
let mk1 : Bool = merkle_verify_h(comb_leaf(lj, rj, oj, qj), j, pj, trace_root) in
let mk2 : Bool = merkle_verify_h(comb_leaf(ljh, rjh, ojh, qjh), j + half, pjh, trace_root) in
let bj : G2 = deep_batch(lj, rj, oj, qj, wnth(coset, j), lz, rz, oz, qz, z, gam) in
let bjh : G2 = deep_batch(ljh, rjh, ojh, qjh, wnth(coset, j + half), lz, rz, oz, qz, z, gam) in
let recon : Bool = match boq { [] => false; [QOpen(blx, blmx, p1, p2), ...rest] => geq2(bj, blx) && geq2(bjh, blmx) } in
mk1 && mk2 && recon && (qopen_len(boq) == lyrs) && vq(boq, broots, betas, doms, bfinal, p, finv2) }
fn vsqs(bopen: List<List<QOpen>>, topen: List<TOpen>, broots: List<List<Int>>, betas: List<G2>, doms: List<List<List<Int>>>, bfinal: List<G2>, coset: List<List<Int>>, lz: G2, rz: G2, oz: G2, qz: G2, z: G2, gam: G2, trace_root: List<Int>, m: Int, queries: List<Int>, lyrs: Int, finv2: List<Int>) : Bool =
match bopen { [] => match queries { [] => match topen { [] => true; _ => false }; _ => false };
[boq, ...brest] => match topen { [] => false; [toq, ...trest] => match queries { [] => false; [p, ...qrest] =>
if vsq1(boq, toq, broots, betas, doms, bfinal, coset, lz, rz, oz, qz, z, gam, trace_root, m, p, lyrs, finv2)
then vsqs(brest, trest, broots, betas, doms, bfinal, coset, lz, rz, oz, qz, z, gam, trace_root, m, qrest, lyrs, finv2) else false } } }
fn verify_sound(gates: List<Gate>, proof: ProofS) : Bool =
match proof { ProofS(trace_root, lz, rz, oz, qz, broots, bfinal, nonce, bopen, topen) =>
let n : Int = ng(gates) in let m : Int = fri_dsize(n) in let coset : List<List<Int>> = fri_coset(n) in let lyrs : Int = fri_layers(n) in
let stmt : List<Int> = stmt_seed_of(gates) in let pc : List<List<List<Int>>> = public_cols(gates, n) in
let ts1 : List<Int> = hashg(stmt, trace_root) in let z : G2 = ood_of(ts1) in
let ts2 : List<Int> = ts_with_ood(ts1, lz, rz, oz, qz) in let gam : G2 = gamma_of(ts2) in
let betas : List<G2> = rederive_betas(broots, ts2) in let base_seed : List<Int> = seed_from_roots(broots, ts2) in
let ground : List<Int> = hashg(base_seed, glit(nonce)) in
let doms : List<List<List<Int>>> = build_doms(coset, lyrs) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(n), fri_dsize(n)) in
# OOD evals l(z),r(z),o(z),Q(z) are legitimately COMPLEX (base-field polys at a complex z); no real-only
# guard on them. The per-query trace openings are base-field by TYPE (List<Int>), and the DEEP-FRI binds
# the sent z-evals to the committed columns, so nothing can be smuggled. The identity is the whole check.
let id_ok : Bool = geq2(gmul2(qz, zh_g(z, n)), g_at_z(pc, lz, rz, oz, z)) in
let struct_ok : Bool = (llen(broots) == lyrs) && (len(bfinal) == fri_final(0)) && (openings_len(bopen) == fri_queries(n)) && (tlen(topen) == fri_queries(n)) && pow_ok(ground) && is_const2(bfinal) in
id_ok && struct_ok && vsqs(bopen, topen, broots, betas, doms, bfinal, coset, lz, rz, oz, qz, z, gam, trace_root, m, queries, lyrs, finv([2])) }
# P=0 attack against the SOUND path: commit all-zero l,r,o,Q and zero OOD evals; the
# DEEP-FRI/Merkle all pass, but the identity Q(z)*Z_H(z)==G(z) FAILS (0 != G_pub(z),
# nonzero off H because of the GConst(R) row) -> REJECT. The headline regression.
fn zeros_b(k: Int) : List<List<Int>> = if k <= 0 then [] else [glit(0)] ++ zeros_b(k - 1)
fn prove_sound_evil(gates: List<Gate>) : ProofS =
let n : Int = ng(gates) in let m : Int = fri_dsize(n) in let coset : List<List<Int>> = fri_coset(n) in
let stmt : List<Int> = stmt_seed_of(gates) in let z0 : List<List<Int>> = zeros_b(m) in
let tlevels : List<List<List<Int>>> = build_levels_g(pad_pow2_g(trace_leaf_list(z0, z0, z0, z0, 0, m)), []) in
let trace_root : List<Int> = root_of_levels(tlevels) in
let ts1 : List<Int> = hashg(stmt, trace_root) in let z : G2 = ood_of(ts1) in
let lz : G2 = G2([], []) in let rz : G2 = G2([], []) in let oz : G2 = G2([], []) in let qz : G2 = G2([], []) in
let ts2 : List<Int> = ts_with_ood(ts1, lz, rz, oz, qz) in let gam : G2 = gamma_of(ts2) in
let bcw : List<G2> = build_b(z0, z0, z0, z0, coset, lz, rz, oz, qz, z, gam, 0, m) in
match commit_g(bcw, coset, [], ts2, finv([2])) { (blayers, bfinal) =>
let base_seed : List<Int> = transcript_seed_g(blayers, ts2) in
let nz : Int = grind(base_seed) in let ground : List<Int> = hashg(base_seed, glit(nz)) in
let queries : List<Int> = sample_queries_g(ground, fri_queries(n), fri_dsize(n)) in
ProofS(trace_root, lz, rz, oz, qz, roots_of(blayers), bfinal, nz, extract_all(blayers, queries), extract_traces(z0, z0, z0, z0, tlevels, queries, fri_dsize(n))) }
# tamper a trace opening (flip the first query's l value): the Merkle leaf no longer
# matches trace_root AND the reconstructed B no longer matches the FRI opening -> REJECT.
# Pins the trace<->FRI index alignment (the synthesis's subtlest-bug guard).
fn tamper_topen(topen: List<TOpen>) : List<TOpen> = match topen { [] => []; [TOpen(lj, rj, oj, qj, pj, ljh, rjh, ojh, qjh, pjh), ...rest] => [TOpen(fadd(lj, [1]), rj, oj, qj, pj, ljh, rjh, ojh, qjh, pjh)] ++ rest }
fn tamper_strace(proof: ProofS) : ProofS = match proof { ProofS(tr, lz, rz, oz, qz, broots, bfinal, nonce, bopen, topen) => ProofS(tr, lz, rz, oz, qz, broots, bfinal, nonce, bopen, tamper_topen(topen)) }
# === TIER-1 PART 3 (WIP): SOUND prover/verifier WITH the copy/permutation argument
# Adds the PLONK grand-product so the committed trace is a CONSISTENT wire assignment
# (one value per wire), closing the inter-row gap. Staged 3-tree commit (commit-before-
# challenge): T1{l,r,o} -> beta,gamma_p -> T2{Z} -> alpha -> T3{Q} -> z(OOD). Q combines
# the gate, recurrence, and boundary constraints with powers of alpha. DEEP-FRI batches
# {l,r,o,Q,Z} (Z bound at z AND omega*z). Verifier checks qz == combined-quotient(z).
# v5.124 Stage 1: the trace opening is BATCHED. Per query we keep only the LEAF VALUES (TLeaf: lro+q+z
# at j and j+half) — the deep-check inputs, unchanged — and the 3 trace Merkle trees (root1-LRO,
# root3-quotient, root2-Z) are opened ONCE each as a batch multiproof: TBatch carries the per-query
# leaves + one shared co-path per tree (replacing the former 6 independent per-query paths). The verifier
# rebuilds each trace root from the opened leaves + its co-path (recon_ok); FRI `bopen` stays per-query.
type TLeaf = | TLeaf(List<Int>, List<Int>, List<Int>, G2, G2, List<Int>, List<Int>, List<Int>, G2, G2)
type TBatch = | TBatch(List<TLeaf>, List<List<Int>>, List<List<Int>>, List<List<Int>>)
type ProofB3 = | ProofB3(List<Int>, List<Int>, List<Int>, G2, G2, G2, G2, G2, G2, List<List<Int>>, List<G2>, Int, BFri, TBatch)
fn comb_lro(l: List<Int>, r: List<Int>, o: List<Int>) : List<Int> = hashg(hashg(l, r), o)
# CUT THE ROPE 3: was O(m²) — wnth(lcw,i)/wnth(rcw,i)/wnth(ocw,i) each an O(i) cons-walk per leaf,
# x m=32n leaves. Now a single lock-step walk of the three codewords (O(m), no indexing).
fn trace_leaf_lro(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, i: Int, m: Int) : List<List<Int>> =
match lcw { [] => []; [lh, ...lt] => match rcw { [] => []; [rh, ...rt] => match ocw { [] => []; [oh, ...ot] =>
[comb_lro(lh, rh, oh)] ++ trace_leaf_lro(lt, rt, ot, i + 1, m) } } }
fn absorb6(ts: List<Int>, a: G2, b: G2, c: G2, d: G2, e: G2, f: G2) : List<Int> = absorb_g2(absorb_g2(absorb_g2(absorb_g2(absorb_g2(absorb_g2(ts, a), b), c), d), e), f)
# absorb the final folded codeword into the transcript BEFORE sampling queries
# (standard BCS/ethSTARK; closes the second FS-PARTIAL gap — the query positions now
# depend on bfinal, so a prover cannot adapt the final codeword after seeing the queries).
fn absorb_final(acc: List<Int>, fin: List<G2>) : List<Int> = match fin { [] => acc; [G2(a0, a1), ...r] => absorb_final(hashg(hashg(acc, a0), a1), r) }
fn recur_at_z(pm: List<List<List<Int>>>, lz: G2, rz: G2, oz: G2, zz: G2, zwz: G2, beta: G2, gamp: G2, z: G2) : G2 =
let id1z : G2 = poly_eval_g(wnth3(pm, 0), z) in let id2z : G2 = poly_eval_g(wnth3(pm, 1), z) in let id3z : G2 = poly_eval_g(wnth3(pm, 2), z) in
let s1z : G2 = poly_eval_g(wnth3(pm, 3), z) in let s2z : G2 = poly_eval_g(wnth3(pm, 4), z) in let s3z : G2 = poly_eval_g(wnth3(pm, 5), z) in
let numz : G2 = gmul2(pf(lz, id1z, beta, gamp), gmul2(pf(rz, id2z, beta, gamp), pf(oz, id3z, beta, gamp))) in
let denz : G2 = gmul2(pf(lz, s1z, beta, gamp), gmul2(pf(rz, s2z, beta, gamp), pf(oz, s3z, beta, gamp))) in
gsub2(gmul2(zwz, denz), gmul2(zz, numz))
fn qcombined_z(pc: List<List<List<Int>>>, pm: List<List<List<Int>>>, lz: G2, rz: G2, oz: G2, zz: G2, zwz: G2, beta: G2, gamp: G2, alpha: G2, z: G2, n: Int) : G2 =
let izh : G2 = ginv2(zh_g(z, n)) in
let qgate : G2 = gmul2(g_at_z(pc, lz, rz, oz, z), izh) in
let qrec : G2 = gmul2(recur_at_z(pm, lz, rz, oz, zz, zwz, beta, gamp, z), izh) in
let qbnd : G2 = gmul2(gsub2(zz, G2([1], [])), ginv2(gsub2(z, G2([1], [])))) in
gadd2(qgate, gadd2(gmul2(alpha, qrec), gmul2(gmul2(alpha, alpha), qbnd)))
# CUT THE ROPE 3: was O(m^2) (wnth/nth2 per row, incl. zcw at (i+rot)%m) PLUS O(n*m) — six
# poly_eval_b of the perm columns per coset point. Now: LDE the 6 perm cols ONCE (native NTT),
# to_vec every codeword (incl. zcw for both i and (i+rot)%m), and vget O(1) in the row walk.
fn build_q_b3_go(qgv: List<List<Int>>, lv: List<List<Int>>, rv: List<List<Int>>, ov: List<List<Int>>, zv: List<G2>, cv: List<List<Int>>, s0: List<List<Int>>, s1: List<List<Int>>, s2: List<List<Int>>, s3: List<List<Int>>, s4: List<List<Int>>, s5: List<List<Int>>, alpha: G2, beta: G2, gamp: G2, n: Int, rot: Int, m: Int, i: Int) : List<G2> =
if i >= m then [] else
let x : List<Int> = vget(cv, i) in let zhx : List<Int> = fsub(fpow_bn(x, glit(n)), [1]) in
let lx : List<Int> = vget(lv, i) in let rx : List<Int> = vget(rv, i) in let ox : List<Int> = vget(ov, i) in
let zx : G2 = vget(zv, i) in let zwx : G2 = vget(zv, (i + rot) % m) in
let numx : G2 = gmul2(pf(emb(lx), emb(vget(s0, i)), beta, gamp), gmul2(pf(emb(rx), emb(vget(s1, i)), beta, gamp), pf(emb(ox), emb(vget(s2, i)), beta, gamp))) in
let denx : G2 = gmul2(pf(emb(lx), emb(vget(s3, i)), beta, gamp), gmul2(pf(emb(rx), emb(vget(s4, i)), beta, gamp), pf(emb(ox), emb(vget(s5, i)), beta, gamp))) in
let qrecur : G2 = gscale2(gsub2(gmul2(zwx, denx), gmul2(zx, numx)), finv(zhx)) in
let qbound : G2 = gmul2(gsub2(zx, G2([1], [])), ginv2(gsub2(emb(x), G2([1], [])))) in
[gadd2(emb(vget(qgv, i)), gadd2(gmul2(alpha, qrecur), gmul2(gmul2(alpha, alpha), qbound)))] ++ build_q_b3_go(qgv, lv, rv, ov, zv, cv, s0, s1, s2, s3, s4, s5, alpha, beta, gamp, n, rot, m, i + 1)
fn build_q_b3(pc: List<List<List<Int>>>, pm: List<List<List<Int>>>, qgatecw: List<List<Int>>, lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, zcw: List<G2>, coset: List<List<Int>>, alpha: G2, beta: G2, gamp: G2, n: Int, rot: Int, m: Int, i: Int) : List<G2> =
build_q_b3_go(to_vec(qgatecw), to_vec(lcw), to_vec(rcw), to_vec(ocw), to_vec(zcw), to_vec(coset),
to_vec(lde(wnth3(pm, 0), n)), to_vec(lde(wnth3(pm, 1), n)), to_vec(lde(wnth3(pm, 2), n)),
to_vec(lde(wnth3(pm, 3), n)), to_vec(lde(wnth3(pm, 4), n)), to_vec(lde(wnth3(pm, 5), n)),
alpha, beta, gamp, n, rot, m, 0)
fn deep_batch_b3(lj: List<Int>, rj: List<Int>, oj: List<Int>, qj: G2, zj: G2, xi: List<Int>, lz: G2, rz: G2, oz: G2, qz: G2, zz: G2, zwz: G2, z: G2, wz: G2, gam: G2) : G2 =
let invz : G2 = ginv2(gsub2(emb(xi), z)) in let invwz : G2 = ginv2(gsub2(emb(xi), wz)) in
let dq : G2 = gmul2(gsub2(qj, qz), invz) in let dl : G2 = gmul2(gsub2(emb(lj), lz), invz) in let dr : G2 = gmul2(gsub2(emb(rj), rz), invz) in
let dlo : G2 = gmul2(gsub2(emb(oj), oz), invz) in let dz : G2 = gmul2(gsub2(zj, zz), invz) in let dzw : G2 = gmul2(gsub2(zj, zwz), invwz) in
let g2 : G2 = gmul2(gam, gam) in let g3 : G2 = gmul2(g2, gam) in let g4 : G2 = gmul2(g3, gam) in let g5 : G2 = gmul2(g4, gam) in
gadd2(dq, gadd2(gmul2(gam, dl), gadd2(gmul2(g2, dr), gadd2(gmul2(g3, dlo), gadd2(gmul2(g4, dz), gmul2(g5, dzw))))))
# CUT THE ROPE 3: was O(m^2) (wnth/nth2 x6 per row). to_vec the 6 codewords once (O(m)), vget O(1).
fn build_b_b3_go(lv: List<List<Int>>, rv: List<List<Int>>, ov: List<List<Int>>, qv: List<G2>, zv: List<G2>, cv: List<List<Int>>, lz: G2, rz: G2, oz: G2, qz: G2, zz: G2, zwz: G2, z: G2, wz: G2, gam: G2, i: Int, m: Int) : List<G2> =
if i >= m then [] else [deep_batch_b3(vget(lv, i), vget(rv, i), vget(ov, i), vget(qv, i), vget(zv, i), vget(cv, i), lz, rz, oz, qz, zz, zwz, z, wz, gam)] ++ build_b_b3_go(lv, rv, ov, qv, zv, cv, lz, rz, oz, qz, zz, zwz, z, wz, gam, i + 1, m)
fn build_b_b3(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<G2>, zcw: List<G2>, coset: List<List<Int>>, lz: G2, rz: G2, oz: G2, qz: G2, zz: G2, zwz: G2, z: G2, wz: G2, gam: G2, i: Int, m: Int) : List<G2> =
build_b_b3_go(to_vec(lcw), to_vec(rcw), to_vec(ocw), to_vec(qcw), to_vec(zcw), to_vec(coset), lz, rz, oz, qz, zz, zwz, z, wz, gam, 0, m)
fn extract_tl1(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<G2>, zcw: List<G2>, p: Int, m: Int) : TLeaf =
let half : Int = m / 2 in let j : Int = p % half in
TLeaf(wnth(lcw, j), wnth(rcw, j), wnth(ocw, j), nth2(qcw, j), nth2(zcw, j),
wnth(lcw, j + half), wnth(rcw, j + half), wnth(ocw, j + half), nth2(qcw, j + half), nth2(zcw, j + half))
fn extract_tls(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<G2>, zcw: List<G2>, queries: List<Int>, m: Int) : List<TLeaf> =
match queries { [] => []; [p, ...rest] => [extract_tl1(lcw, rcw, ocw, qcw, zcw, p, m)] ++ extract_tls(lcw, rcw, ocw, qcw, zcw, rest, m) }
# build the batched trace opening: per-query leaf values + one shared co-path per trace tree (lev1=LRO/root1,
# lev3=quotient/root3, lev2=Z/root2), over the canonical opened-leaf set {j, j+half}.
fn extract_tbatch(lcw: List<List<Int>>, rcw: List<List<Int>>, ocw: List<List<Int>>, qcw: List<G2>, zcw: List<G2>, lev1: List<List<List<Int>>>, lev2: List<List<List<Int>>>, lev3: List<List<List<Int>>>, queries: List<Int>, m: Int) : TBatch =
let oidx : List<Int> = oidx_of(queries, m / 2) in
TBatch(extract_tls(lcw, rcw, ocw, qcw, zcw, queries, m), bcopath(lev1, 0, oidx, []), bcopath(lev3, 0, oidx, []), bcopath(lev2, 0, oidx, []))
fn prove_b3(gates: List<Gate>, w: List<List<Int>>) : ProofB3 =
let n : Int = ng(gates) in let m : Int = fri_dsize(n) in let coset : List<List<Int>> = fri_coset(n) in let rot : Int = m / n in let omega : List<Int> = root_pow2(ilog2(n)) in
let stmt : List<Int> = stmt_seed_of(gates) in let pc : List<List<List<Int>>> = public_cols(gates, n) in let pm : List<List<List<Int>>> = perm_cols(gates, n) in
let cells : List<Pair<Int, Int>> = all_cells(gates, 0, n) in
let lvals : List<List<Int>> = lpad(c_l(gates, w), n) in let rvals : List<List<Int>> = lpad(c_r(gates, w), n) in let ovals : List<List<Int>> = lpad(c_o(gates, w), n) in
let ll : List<List<Int>> = icol(c_l(gates, w), n) in let rr : List<List<Int>> = icol(c_r(gates, w), n) in let oo : List<List<Int>> = icol(c_o(gates, w), n) in
let lcw : List<List<Int>> = lde(ll, n) in let rcw : List<List<Int>> = lde(rr, n) in let ocw : List<List<Int>> = lde(oo, n) in
let qgatecw : List<List<Int>> = q_cw(gates, w) in