-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHonkVerifier.sol
More file actions
1883 lines (1633 loc) · 76.3 KB
/
Copy pathHonkVerifier.sol
File metadata and controls
1883 lines (1633 loc) · 76.3 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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Aztec
pragma solidity >=0.8.21;
uint256 constant N = 4096;
uint256 constant LOG_N = 12;
uint256 constant NUMBER_OF_PUBLIC_INPUTS = 3;
library HonkVerificationKey {
function loadVerificationKey() internal pure returns (Honk.VerificationKey memory) {
Honk.VerificationKey memory vk = Honk.VerificationKey({
circuitSize: uint256(4096),
logCircuitSize: uint256(12),
publicInputsSize: uint256(3),
ql: Honk.G1Point({
x: uint256(0x0d6a525a067624ae041c97eecc4050b2fb4e3eabec573ced86846a1de06de9e6),
y: uint256(0x061076a7ed19b69d9654ebdcb6af2a46c0d1bd464eadbf6efe9630d0d320628e)
}),
qr: Honk.G1Point({
x: uint256(0x24311d52f4ebf88c4549a150c34f4c62802a650f33fd88ea0b3473a8865a79e4),
y: uint256(0x21d21c0b362d5a6e36e13d21f21edafede7fc9ce581a13a0c611d8aeb9108fef)
}),
qo: Honk.G1Point({
x: uint256(0x2d2b0911b96bab4b6bda8eb2ddcd9a555c20e7c8834c9bfa7a17232e8f71b305),
y: uint256(0x12a1944483b502cc69a60666cd35fa7e3898854feeaf9d7d83060733a2092bcf)
}),
q4: Honk.G1Point({
x: uint256(0x0be01bd257aef0f9ee4308b76863491600c9f1ff623bd8ea7709736805417412),
y: uint256(0x1791b746a11c3e8676ba34f1ebaf90bca5b46b7c12e25d802f75af42f9fbc2fa)
}),
qm: Honk.G1Point({
x: uint256(0x17015d2eb96fbd242ba65e57e4740f472b859261e2312a5673b3762752ae1f06),
y: uint256(0x27f97b95ce7fd08065141bc0e61f647c5a35e933fa2dc37e1f49c1c09d5cfa17)
}),
qc: Honk.G1Point({
x: uint256(0x17d31ab35290a4e81bc2c27bd138f010f3fd55ac852fc71877b47cca17a6d4f7),
y: uint256(0x29c96315a9cb8549c65d10cb232d1a30a029822774003d384ca2bd9bb3cf77a4)
}),
qArith: Honk.G1Point({
x: uint256(0x0400c951aee6caa7844cf012c3e8f8ca97fced8f823eddb541674eb2cae52c99),
y: uint256(0x2714e5139cb040e44fc8a8d81e6cb53b9b077bfb453f992dba8cecd0141d4bae)
}),
qDeltaRange: Honk.G1Point({
x: uint256(0x1d7660585bc866aa9d21cb1daaf236ef42bcea6b12b1d74d6df2c65b06be1956),
y: uint256(0x2d625a5a16141e689d5a82f4f2f2c99fa11613eead8641f92c88c362b9ba80a9)
}),
qElliptic: Honk.G1Point({
x: uint256(0x2c5cb8ef2861c6e7ece2e365bfd2d13a2cd258642b594f7d5e10832a6d678431),
y: uint256(0x0b896fdac415facde3825c10de4f1299d7850686c4cd6083c4922c8ff9e29cf6)
}),
qAux: Honk.G1Point({
x: uint256(0x2925c442cddbd1e16b591f9f42d19d7a0e26f3fdd007538b91024b2ddbb06c5e),
y: uint256(0x21215a6939f4469ea5a64384ebbb4a8ce90fc6ae35461f3b0e54578583217a3f)
}),
qLookup: Honk.G1Point({
x: uint256(0x2594d00a131b347f472a021eac09e25eacba35749a9ba8f8c7f4a726ff63a910),
y: uint256(0x2499be5abe1cf5463534a1d0613f82449f1989f1186f2d0b67295bda7f8a0f55)
}),
qPoseidon2External: Honk.G1Point({
x: uint256(0x1db2c2f3b102e45b899adc6aab894780382deeb3513d51c07a81531249083e32),
y: uint256(0x2fe23813f2afd98e925a08c842fa1bbdb996dbac706189ddd9358f839d662bd9)
}),
qPoseidon2Internal: Honk.G1Point({
x: uint256(0x23637e71230fbd4e2f18f8be21df082684954271cfbf65dda8e183609fa02f79),
y: uint256(0x183c9be87491a8e2356e521ee559fc3be096038fd6a0348cc6d1d54b4b74b35d)
}),
s1: Honk.G1Point({
x: uint256(0x046daa84eae0181f9dd19b5a8c81dcdd0966581ee74e877b6aaa353ed8684321),
y: uint256(0x1e032751cb31cf5019c76a59727dfa363f77e234a40aec00114ce7a82b69b39d)
}),
s2: Honk.G1Point({
x: uint256(0x0ef32977675d6ff5fbfecf57246a23f7b2f7f65de2c7ff7ca01b3e7af0b40aca),
y: uint256(0x111d1e30a28db42181d200768ee5498c5d20fa06cddfb7037186bdf2290e4a21)
}),
s3: Honk.G1Point({
x: uint256(0x2fa4c2181a24994a05db8cde4ad6f848b05c3868f399e9e1ef07f987b237f17f),
y: uint256(0x2c96d7939fd728d479a8ae98c1114b9e46e45c456809fc807758ebfe2faa5795)
}),
s4: Honk.G1Point({
x: uint256(0x15cb6c190fd475062c7084d496090891970d13b63344924159061492ed0cdf3a),
y: uint256(0x209d987efead43c50ed37ab8de5890f4c93b00711e0fdb2cbe5140b586b142c2)
}),
t1: Honk.G1Point({
x: uint256(0x1fb7c5d789d32e42a08e41296286139f603c7607ce186d0cce6e472dfe021473),
y: uint256(0x09d80a7c25410f51739aadc54ad122874e4551efc35bd23807ecf23a79ef418a)
}),
t2: Honk.G1Point({
x: uint256(0x108788a192d4d3c38e445629bb550acf212f9b2049b6fb1cc76900bd482fb5b0),
y: uint256(0x195266ac0788c227762333892ad282a4679450ae72e8e8b4a1ead0e63d4f4e04)
}),
t3: Honk.G1Point({
x: uint256(0x192a87ba208ab02639f2453bde4d15e8c489e522f16c07fd8e312a10142484d8),
y: uint256(0x0978af3f655d807c77436eec1810420c3170702a20ae32933baa816253797b15)
}),
t4: Honk.G1Point({
x: uint256(0x1a552bd8d3265d1f23e7ff166cf20fffa5c0688c867cfd3a2ea65452d8ad60a4),
y: uint256(0x1cb1414f7b9f8edb7c7a0d61f66e24e632a4050d9b3be0d6c35109aa99693039)
}),
id1: Honk.G1Point({
x: uint256(0x0f2c9761e6d936aa76306afa427597331c4178c77a8f89d5dda1e8d29f77e47b),
y: uint256(0x0959db1afadec04d4f9ee52b753474982ad5719056e563486742d6cf6ff27e83)
}),
id2: Honk.G1Point({
x: uint256(0x11faa3842183cdaa9551e4f60eaad839a06ea433601a4fdbda40fa323632d049),
y: uint256(0x19f5f9f7997d9da6c7c8be77d03f1742c17a73a53b552759cc7c775d3d337e31)
}),
id3: Honk.G1Point({
x: uint256(0x2fabea9beff03a4a31da5d8de3ee11573979478a16b507907c7dc3e7d3103a45),
y: uint256(0x2e5cc5daeb3b0c69e04486fac1119cbbcac4a9c89deab14abe21bd3b72669751)
}),
id4: Honk.G1Point({
x: uint256(0x169c1ec0466cc95007cb8da5a92190128a055f437ad0adc70f1c93aae5270612),
y: uint256(0x102c475ec7ae251f4fdcb4051161e52ced2d4b62389e1229bd95a9475a8a51b5)
}),
lagrangeFirst: Honk.G1Point({
x: uint256(0x0000000000000000000000000000000000000000000000000000000000000001),
y: uint256(0x0000000000000000000000000000000000000000000000000000000000000002)
}),
lagrangeLast: Honk.G1Point({
x: uint256(0x2a029c8a3f09a0d0dac0719a6b4ba9d65f90c5882c9d16f2de067cc3183228bc),
y: uint256(0x2956166e0d0985348ca1476caf882c40e0a58ae98840aabb4ba6ad552089993a)
})
});
return vk;
}
}
pragma solidity ^0.8.27;
type Fr is uint256;
using { add as + } for Fr global;
using { sub as - } for Fr global;
using { mul as * } for Fr global;
using { exp as ^ } for Fr global;
using { notEqual as != } for Fr global;
using { equal as == } for Fr global;
uint256 constant MODULUS =
21888242871839275222246405745257275088548364400416034343698204186575808495617; // Prime field order
Fr constant MINUS_ONE = Fr.wrap(MODULUS - 1);
// Instantiation
library FrLib
{
function from(uint256 value) internal pure returns(Fr)
{
return Fr.wrap(value % MODULUS);
}
function fromBytes32(bytes32 value) internal pure returns(Fr)
{
return Fr.wrap(uint256(value) % MODULUS);
}
function toBytes32(Fr value) internal pure returns(bytes32)
{
return bytes32(Fr.unwrap(value));
}
function invert(Fr value) internal view returns(Fr)
{
uint256 v = Fr.unwrap(value);
uint256 result;
// Call the modexp precompile to invert in the field
assembly
{
let free := mload(0x40)
mstore(free, 0x20)
mstore(add(free, 0x20), 0x20)
mstore(add(free, 0x40), 0x20)
mstore(add(free, 0x60), v)
mstore(add(free, 0x80), sub(MODULUS, 2))
mstore(add(free, 0xa0), MODULUS)
let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
if iszero(success) {
revert(0, 0)
}
result := mload(0x00)
}
return Fr.wrap(result);
}
function pow(Fr base, uint256 v) internal view returns(Fr)
{
uint256 b = Fr.unwrap(base);
uint256 result;
// Call the modexp precompile to invert in the field
assembly
{
let free := mload(0x40)
mstore(free, 0x20)
mstore(add(free, 0x20), 0x20)
mstore(add(free, 0x40), 0x20)
mstore(add(free, 0x60), b)
mstore(add(free, 0x80), v)
mstore(add(free, 0xa0), MODULUS)
let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
if iszero(success) {
revert(0, 0)
}
result := mload(0x00)
}
return Fr.wrap(result);
}
function div(Fr numerator, Fr denominator) internal view returns(Fr)
{
return numerator * invert(denominator);
}
function sqr(Fr value) internal pure returns (Fr) {
return value * value;
}
function unwrap(Fr value) internal pure returns (uint256) {
return Fr.unwrap(value);
}
function neg(Fr value) internal pure returns (Fr) {
return Fr.wrap(MODULUS - Fr.unwrap(value));
}
}
// Free functions
function add(Fr a, Fr b) pure returns(Fr)
{
return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
}
function mul(Fr a, Fr b) pure returns(Fr)
{
return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
}
function sub(Fr a, Fr b) pure returns(Fr)
{
return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
}
function exp(Fr base, Fr exponent) pure returns(Fr)
{
if (Fr.unwrap(exponent) == 0) return Fr.wrap(1);
for (uint256 i = 1; i < Fr.unwrap(exponent); i += i) {
base = base * base;
}
return base;
}
function notEqual(Fr a, Fr b) pure returns(bool)
{
return Fr.unwrap(a) != Fr.unwrap(b);
}
function equal(Fr a, Fr b) pure returns(bool)
{
return Fr.unwrap(a) == Fr.unwrap(b);
}
uint256 constant CONST_PROOF_SIZE_LOG_N = 28;
uint256 constant NUMBER_OF_SUBRELATIONS = 26;
uint256 constant BATCHED_RELATION_PARTIAL_LENGTH = 8;
uint256 constant NUMBER_OF_ENTITIES = 40;
uint256 constant NUMBER_UNSHIFTED = 35;
uint256 constant NUMBER_TO_BE_SHIFTED = 5;
// Alphas are used as relation separators so there should be NUMBER_OF_SUBRELATIONS - 1
uint256 constant NUMBER_OF_ALPHAS = 25;
// Prime field order
uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
uint256 constant P = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // Prime field order, F_r
// ENUM FOR WIRES
enum WIRE {
Q_M,
Q_C,
Q_L,
Q_R,
Q_O,
Q_4,
Q_LOOKUP,
Q_ARITH,
Q_RANGE,
Q_ELLIPTIC,
Q_AUX,
Q_POSEIDON2_EXTERNAL,
Q_POSEIDON2_INTERNAL,
SIGMA_1,
SIGMA_2,
SIGMA_3,
SIGMA_4,
ID_1,
ID_2,
ID_3,
ID_4,
TABLE_1,
TABLE_2,
TABLE_3,
TABLE_4,
LAGRANGE_FIRST,
LAGRANGE_LAST,
W_L,
W_R,
W_O,
W_4,
Z_PERM,
LOOKUP_INVERSES,
LOOKUP_READ_COUNTS,
LOOKUP_READ_TAGS,
W_L_SHIFT,
W_R_SHIFT,
W_O_SHIFT,
W_4_SHIFT,
Z_PERM_SHIFT
}
library Honk {
struct G1Point {
uint256 x;
uint256 y;
}
struct G1ProofPoint {
uint256 x_0;
uint256 x_1;
uint256 y_0;
uint256 y_1;
}
struct VerificationKey {
// Misc Params
uint256 circuitSize;
uint256 logCircuitSize;
uint256 publicInputsSize;
// Selectors
G1Point qm;
G1Point qc;
G1Point ql;
G1Point qr;
G1Point qo;
G1Point q4;
G1Point qLookup; // Lookup
G1Point qArith; // Arithmetic widget
G1Point qDeltaRange; // Delta Range sort
G1Point qAux; // Auxillary
G1Point qElliptic; // Auxillary
G1Point qPoseidon2External;
G1Point qPoseidon2Internal;
// Copy cnstraints
G1Point s1;
G1Point s2;
G1Point s3;
G1Point s4;
// Copy identity
G1Point id1;
G1Point id2;
G1Point id3;
G1Point id4;
// Precomputed lookup table
G1Point t1;
G1Point t2;
G1Point t3;
G1Point t4;
// Fixed first and last
G1Point lagrangeFirst;
G1Point lagrangeLast;
}
struct RelationParameters {
// challenges
Fr eta;
Fr etaTwo;
Fr etaThree;
Fr beta;
Fr gamma;
// derived
Fr publicInputsDelta;
}
struct Proof {
// Free wires
Honk.G1ProofPoint w1;
Honk.G1ProofPoint w2;
Honk.G1ProofPoint w3;
Honk.G1ProofPoint w4;
// Lookup helpers - Permutations
Honk.G1ProofPoint zPerm;
// Lookup helpers - logup
Honk.G1ProofPoint lookupReadCounts;
Honk.G1ProofPoint lookupReadTags;
Honk.G1ProofPoint lookupInverses;
// Sumcheck
Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
// Shplemini
Honk.G1ProofPoint[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
Honk.G1ProofPoint shplonkQ;
Honk.G1ProofPoint kzgQuotient;
}
}
// Transcript library to generate fiat shamir challenges
struct Transcript {
// Oink
Honk.RelationParameters relationParameters;
Fr[NUMBER_OF_ALPHAS] alphas;
Fr[CONST_PROOF_SIZE_LOG_N] gateChallenges;
// Sumcheck
Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
// Gemini
Fr rho;
Fr geminiR;
// Shplonk
Fr shplonkNu;
Fr shplonkZ;
}
library TranscriptLib {
function generateTranscript(Honk.Proof memory proof, bytes32[] calldata publicInputs, uint256 circuitSize, uint256 publicInputsSize, uint256 pubInputsOffset)
internal
pure
returns (Transcript memory t)
{
Fr previousChallenge;
(t.relationParameters, previousChallenge) =
generateRelationParametersChallenges(proof, publicInputs, circuitSize, publicInputsSize, pubInputsOffset, previousChallenge);
(t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
(t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge);
(t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge);
(t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
(t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge);
(t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge);
(t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
return t;
}
function splitChallenge(Fr challenge) internal pure returns (Fr first, Fr second) {
uint256 challengeU256 = uint256(Fr.unwrap(challenge));
uint256 lo = challengeU256 & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
uint256 hi = challengeU256 >> 128;
first = FrLib.fromBytes32(bytes32(lo));
second = FrLib.fromBytes32(bytes32(hi));
}
function generateRelationParametersChallenges(
Honk.Proof memory proof,
bytes32[] calldata publicInputs,
uint256 circuitSize,
uint256 publicInputsSize,
uint256 pubInputsOffset,
Fr previousChallenge
) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
(rp.eta, rp.etaTwo, rp.etaThree, previousChallenge) =
generateEtaChallenge(proof, publicInputs, circuitSize, publicInputsSize, pubInputsOffset);
(rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaAndGammaChallenges(previousChallenge, proof);
}
function generateEtaChallenge(Honk.Proof memory proof, bytes32[] calldata publicInputs, uint256 circuitSize, uint256 publicInputsSize, uint256 pubInputsOffset)
internal
pure
returns (Fr eta, Fr etaTwo, Fr etaThree, Fr previousChallenge)
{
bytes32[] memory round0 = new bytes32[](3 + publicInputsSize + 12);
round0[0] = bytes32(circuitSize);
round0[1] = bytes32(publicInputsSize);
round0[2] = bytes32(pubInputsOffset);
for (uint256 i = 0; i < publicInputsSize; i++) {
round0[3 + i] = bytes32(publicInputs[i]);
}
// Create the first challenge
// Note: w4 is added to the challenge later on
round0[3 + publicInputsSize] = bytes32(proof.w1.x_0);
round0[3 + publicInputsSize + 1] = bytes32(proof.w1.x_1);
round0[3 + publicInputsSize + 2] = bytes32(proof.w1.y_0);
round0[3 + publicInputsSize + 3] = bytes32(proof.w1.y_1);
round0[3 + publicInputsSize + 4] = bytes32(proof.w2.x_0);
round0[3 + publicInputsSize + 5] = bytes32(proof.w2.x_1);
round0[3 + publicInputsSize + 6] = bytes32(proof.w2.y_0);
round0[3 + publicInputsSize + 7] = bytes32(proof.w2.y_1);
round0[3 + publicInputsSize + 8] = bytes32(proof.w3.x_0);
round0[3 + publicInputsSize + 9] = bytes32(proof.w3.x_1);
round0[3 + publicInputsSize + 10] = bytes32(proof.w3.y_0);
round0[3 + publicInputsSize + 11] = bytes32(proof.w3.y_1);
previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round0)));
(eta, etaTwo) = splitChallenge(previousChallenge);
previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge))));
Fr unused;
(etaThree, unused) = splitChallenge(previousChallenge);
}
function generateBetaAndGammaChallenges(Fr previousChallenge, Honk.Proof memory proof)
internal
pure
returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
{
bytes32[13] memory round1;
round1[0] = FrLib.toBytes32(previousChallenge);
round1[1] = bytes32(proof.lookupReadCounts.x_0);
round1[2] = bytes32(proof.lookupReadCounts.x_1);
round1[3] = bytes32(proof.lookupReadCounts.y_0);
round1[4] = bytes32(proof.lookupReadCounts.y_1);
round1[5] = bytes32(proof.lookupReadTags.x_0);
round1[6] = bytes32(proof.lookupReadTags.x_1);
round1[7] = bytes32(proof.lookupReadTags.y_0);
round1[8] = bytes32(proof.lookupReadTags.y_1);
round1[9] = bytes32(proof.w4.x_0);
round1[10] = bytes32(proof.w4.x_1);
round1[11] = bytes32(proof.w4.y_0);
round1[12] = bytes32(proof.w4.y_1);
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round1)));
(beta, gamma) = splitChallenge(nextPreviousChallenge);
}
// Alpha challenges non-linearise the gate contributions
function generateAlphaChallenges(Fr previousChallenge, Honk.Proof memory proof)
internal
pure
returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
{
// Generate the original sumcheck alpha 0 by hashing zPerm and zLookup
uint256[9] memory alpha0;
alpha0[0] = Fr.unwrap(previousChallenge);
alpha0[1] = proof.lookupInverses.x_0;
alpha0[2] = proof.lookupInverses.x_1;
alpha0[3] = proof.lookupInverses.y_0;
alpha0[4] = proof.lookupInverses.y_1;
alpha0[5] = proof.zPerm.x_0;
alpha0[6] = proof.zPerm.x_1;
alpha0[7] = proof.zPerm.y_0;
alpha0[8] = proof.zPerm.y_1;
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(alpha0)));
(alphas[0], alphas[1]) = splitChallenge(nextPreviousChallenge);
for (uint256 i = 1; i < NUMBER_OF_ALPHAS / 2; i++) {
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(nextPreviousChallenge))));
(alphas[2 * i], alphas[2 * i + 1]) = splitChallenge(nextPreviousChallenge);
}
if (((NUMBER_OF_ALPHAS & 1) == 1) && (NUMBER_OF_ALPHAS > 2)) {
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(nextPreviousChallenge))));
Fr unused;
(alphas[NUMBER_OF_ALPHAS - 1], unused) = splitChallenge(nextPreviousChallenge);
}
}
function generateGateChallenges(Fr previousChallenge)
internal
pure
returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
{
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge))));
Fr unused;
(gateChallenges[i], unused) = splitChallenge(previousChallenge);
}
nextPreviousChallenge = previousChallenge;
}
function generateSumcheckChallenges(Honk.Proof memory proof, Fr prevChallenge)
internal
pure
returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
{
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
Fr[BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
univariateChal[0] = prevChallenge;
for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
}
prevChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(univariateChal)));
Fr unused;
(sumcheckChallenges[i], unused) = splitChallenge(prevChallenge);
}
nextPreviousChallenge = prevChallenge;
}
function generateRhoChallenge(Honk.Proof memory proof, Fr prevChallenge)
internal
pure
returns (Fr rho, Fr nextPreviousChallenge)
{
Fr[NUMBER_OF_ENTITIES + 1] memory rhoChallengeElements;
rhoChallengeElements[0] = prevChallenge;
for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
rhoChallengeElements[i + 1] = proof.sumcheckEvaluations[i];
}
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(rhoChallengeElements)));
Fr unused;
(rho, unused) = splitChallenge(nextPreviousChallenge);
}
function generateGeminiRChallenge(Honk.Proof memory proof, Fr prevChallenge)
internal
pure
returns (Fr geminiR, Fr nextPreviousChallenge)
{
uint256[(CONST_PROOF_SIZE_LOG_N - 1) * 4 + 1] memory gR;
gR[0] = Fr.unwrap(prevChallenge);
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N - 1; i++) {
gR[1 + i * 4] = proof.geminiFoldComms[i].x_0;
gR[2 + i * 4] = proof.geminiFoldComms[i].x_1;
gR[3 + i * 4] = proof.geminiFoldComms[i].y_0;
gR[4 + i * 4] = proof.geminiFoldComms[i].y_1;
}
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(gR)));
Fr unused;
(geminiR, unused) = splitChallenge(nextPreviousChallenge);
}
function generateShplonkNuChallenge(Honk.Proof memory proof, Fr prevChallenge)
internal
pure
returns (Fr shplonkNu, Fr nextPreviousChallenge)
{
uint256[(CONST_PROOF_SIZE_LOG_N) + 1] memory shplonkNuChallengeElements;
shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
shplonkNuChallengeElements[i + 1] = Fr.unwrap(proof.geminiAEvaluations[i]);
}
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkNuChallengeElements)));
Fr unused;
(shplonkNu, unused) = splitChallenge(nextPreviousChallenge);
}
function generateShplonkZChallenge(Honk.Proof memory proof, Fr prevChallenge)
internal
pure
returns (Fr shplonkZ, Fr nextPreviousChallenge)
{
uint256[5] memory shplonkZChallengeElements;
shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
shplonkZChallengeElements[1] = proof.shplonkQ.x_0;
shplonkZChallengeElements[2] = proof.shplonkQ.x_1;
shplonkZChallengeElements[3] = proof.shplonkQ.y_0;
shplonkZChallengeElements[4] = proof.shplonkQ.y_1;
nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkZChallengeElements)));
Fr unused;
(shplonkZ, unused) = splitChallenge(nextPreviousChallenge);
}
function loadProof(bytes calldata proof) internal pure returns (Honk.Proof memory p) {
// Commitments
p.w1 = bytesToG1ProofPoint(proof[0x0:0x80]);
p.w2 = bytesToG1ProofPoint(proof[0x80:0x100]);
p.w3 = bytesToG1ProofPoint(proof[0x100:0x180]);
// Lookup / Permutation Helper Commitments
p.lookupReadCounts = bytesToG1ProofPoint(proof[0x180:0x200]);
p.lookupReadTags = bytesToG1ProofPoint(proof[0x200:0x280]);
p.w4 = bytesToG1ProofPoint(proof[0x280:0x300]);
p.lookupInverses = bytesToG1ProofPoint(proof[0x300:0x380]);
p.zPerm = bytesToG1ProofPoint(proof[0x380:0x400]);
uint256 boundary = 0x400;
// Sumcheck univariates
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
p.sumcheckUnivariates[i][j] = bytesToFr(proof[boundary:boundary + 0x20]);
boundary += 0x20;
}
}
// Sumcheck evaluations
for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
p.sumcheckEvaluations[i] = bytesToFr(proof[boundary:boundary + 0x20]);
boundary += 0x20;
}
// Gemini
// Read gemini fold univariates
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N - 1; i++) {
p.geminiFoldComms[i] = bytesToG1ProofPoint(proof[boundary:boundary + 0x80]);
boundary += 0x80;
}
// Read gemini a evaluations
for (uint256 i = 0; i < CONST_PROOF_SIZE_LOG_N; i++) {
p.geminiAEvaluations[i] = bytesToFr(proof[boundary:boundary + 0x20]);
boundary += 0x20;
}
// Shplonk
p.shplonkQ = bytesToG1ProofPoint(proof[boundary:boundary + 0x80]);
boundary = boundary + 0x80;
// KZG
p.kzgQuotient = bytesToG1ProofPoint(proof[boundary:boundary + 0x80]);
}
}
// Fr utility
function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
require(proofSection.length == 0x20, "invalid bytes scalar");
scalar = FrLib.fromBytes32(bytes32(proofSection));
}
// EC Point utilities
function convertProofPoint(Honk.G1ProofPoint memory input) pure returns (Honk.G1Point memory) {
return Honk.G1Point({x: input.x_0 | (input.x_1 << 136), y: input.y_0 | (input.y_1 << 136)});
}
function bytesToG1ProofPoint(bytes calldata proofSection) pure returns (Honk.G1ProofPoint memory point) {
require(proofSection.length == 0x80, "invalid bytes point");
point = Honk.G1ProofPoint({
x_0: uint256(bytes32(proofSection[0x00:0x20])),
x_1: uint256(bytes32(proofSection[0x20:0x40])),
y_0: uint256(bytes32(proofSection[0x40:0x60])),
y_1: uint256(bytes32(proofSection[0x60:0x80]))
});
}
function negateInplace(Honk.G1Point memory point) pure returns (Honk.G1Point memory) {
point.y = (Q - point.y) % Q;
return point;
}
function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool) {
bytes memory input = abi.encodePacked(
rhs.x,
rhs.y,
// Fixed G1 point
uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
lhs.x,
lhs.y,
// G1 point from VK
uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
);
(bool success, bytes memory result) = address(0x08).staticcall(input);
bool decodedResult = abi.decode(result, (bool));
return success && decodedResult;
}
library RelationsLib {
Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
function accumulateRelationEvaluations(
Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
Honk.RelationParameters memory rp,
Fr[NUMBER_OF_ALPHAS] memory alphas,
Fr powPartialEval
) internal pure returns (Fr accumulator) {
Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
// Accumulate all relations in Ultra Honk - each with varying number of subrelations
accumulateArithmeticRelation(purportedEvaluations, evaluations, powPartialEval);
accumulatePermutationRelation(purportedEvaluations, rp, evaluations, powPartialEval);
accumulateLogDerivativeLookupRelation(purportedEvaluations, rp, evaluations, powPartialEval);
accumulateDeltaRangeRelation(purportedEvaluations, evaluations, powPartialEval);
accumulateEllipticRelation(purportedEvaluations, evaluations, powPartialEval);
accumulateAuxillaryRelation(purportedEvaluations, rp, evaluations, powPartialEval);
accumulatePoseidonExternalRelation(purportedEvaluations, evaluations, powPartialEval);
accumulatePoseidonInternalRelation(purportedEvaluations, evaluations, powPartialEval);
// batch the subrelations with the alpha challenges to obtain the full honk relation
accumulator = scaleAndBatchSubrelations(evaluations, alphas);
}
/**
* Aesthetic helper function that is used to index by enum into proof.sumcheckEvaluations, it avoids
* the relation checking code being cluttered with uint256 type casting, which is often a different colour in code
* editors, and thus is noisy.
*/
function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
return p[uint256(_wire)];
}
uint256 internal constant NEG_HALF_MODULO_P = 0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000;
/**
* Ultra Arithmetic Relation
*
*/
function accumulateArithmeticRelation(
Fr[NUMBER_OF_ENTITIES] memory p,
Fr[NUMBER_OF_SUBRELATIONS] memory evals,
Fr domainSep
) internal pure {
// Relation 0
Fr q_arith = wire(p, WIRE.Q_ARITH);
{
Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
Fr accum = (q_arith - Fr.wrap(3)) * (wire(p, WIRE.Q_M) * wire(p, WIRE.W_R) * wire(p, WIRE.W_L)) * neg_half;
accum = accum + (wire(p, WIRE.Q_L) * wire(p, WIRE.W_L)) + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_R))
+ (wire(p, WIRE.Q_O) * wire(p, WIRE.W_O)) + (wire(p, WIRE.Q_4) * wire(p, WIRE.W_4)) + wire(p, WIRE.Q_C);
accum = accum + (q_arith - Fr.wrap(1)) * wire(p, WIRE.W_4_SHIFT);
accum = accum * q_arith;
accum = accum * domainSep;
evals[0] = accum;
}
// Relation 1
{
Fr accum = wire(p, WIRE.W_L) + wire(p, WIRE.W_4) - wire(p, WIRE.W_L_SHIFT) + wire(p, WIRE.Q_M);
accum = accum * (q_arith - Fr.wrap(2));
accum = accum * (q_arith - Fr.wrap(1));
accum = accum * q_arith;
accum = accum * domainSep;
evals[1] = accum;
}
}
function accumulatePermutationRelation(
Fr[NUMBER_OF_ENTITIES] memory p,
Honk.RelationParameters memory rp,
Fr[NUMBER_OF_SUBRELATIONS] memory evals,
Fr domainSep
) internal pure {
Fr grand_product_numerator;
Fr grand_product_denominator;
{
Fr num = wire(p, WIRE.W_L) + wire(p, WIRE.ID_1) * rp.beta + rp.gamma;
num = num * (wire(p, WIRE.W_R) + wire(p, WIRE.ID_2) * rp.beta + rp.gamma);
num = num * (wire(p, WIRE.W_O) + wire(p, WIRE.ID_3) * rp.beta + rp.gamma);
num = num * (wire(p, WIRE.W_4) + wire(p, WIRE.ID_4) * rp.beta + rp.gamma);
grand_product_numerator = num;
}
{
Fr den = wire(p, WIRE.W_L) + wire(p, WIRE.SIGMA_1) * rp.beta + rp.gamma;
den = den * (wire(p, WIRE.W_R) + wire(p, WIRE.SIGMA_2) * rp.beta + rp.gamma);
den = den * (wire(p, WIRE.W_O) + wire(p, WIRE.SIGMA_3) * rp.beta + rp.gamma);
den = den * (wire(p, WIRE.W_4) + wire(p, WIRE.SIGMA_4) * rp.beta + rp.gamma);
grand_product_denominator = den;
}
// Contribution 2
{
Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
acc = acc
- (
(wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
* grand_product_denominator
);
acc = acc * domainSep;
evals[2] = acc;
}
// Contribution 3
{
Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
evals[3] = acc;
}
}
function accumulateLogDerivativeLookupRelation(
Fr[NUMBER_OF_ENTITIES] memory p,
Honk.RelationParameters memory rp,
Fr[NUMBER_OF_SUBRELATIONS] memory evals,
Fr domainSep
) internal pure {
Fr write_term;
Fr read_term;
// Calculate the write term (the table accumulation)
{
write_term = wire(p, WIRE.TABLE_1) + rp.gamma + (wire(p, WIRE.TABLE_2) * rp.eta)
+ (wire(p, WIRE.TABLE_3) * rp.etaTwo) + (wire(p, WIRE.TABLE_4) * rp.etaThree);
}
// Calculate the write term
{
Fr derived_entry_1 = wire(p, WIRE.W_L) + rp.gamma + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_L_SHIFT));
Fr derived_entry_2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_M) * wire(p, WIRE.W_R_SHIFT);
Fr derived_entry_3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_C) * wire(p, WIRE.W_O_SHIFT);
read_term = derived_entry_1 + (derived_entry_2 * rp.eta) + (derived_entry_3 * rp.etaTwo)
+ (wire(p, WIRE.Q_O) * rp.etaThree);
}
Fr read_inverse = wire(p, WIRE.LOOKUP_INVERSES) * write_term;
Fr write_inverse = wire(p, WIRE.LOOKUP_INVERSES) * read_term;
Fr inverse_exists_xor = wire(p, WIRE.LOOKUP_READ_TAGS) + wire(p, WIRE.Q_LOOKUP)
- (wire(p, WIRE.LOOKUP_READ_TAGS) * wire(p, WIRE.Q_LOOKUP));
// Inverse calculated correctly relation
Fr accumulatorNone = read_term * write_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
accumulatorNone = accumulatorNone * domainSep;
// Inverse
Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * read_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * write_inverse;
evals[4] = accumulatorNone;
evals[5] = accumulatorOne;
}
function accumulateDeltaRangeRelation(
Fr[NUMBER_OF_ENTITIES] memory p,
Fr[NUMBER_OF_SUBRELATIONS] memory evals,
Fr domainSep
) internal pure {
Fr minus_one = Fr.wrap(0) - Fr.wrap(1);
Fr minus_two = Fr.wrap(0) - Fr.wrap(2);
Fr minus_three = Fr.wrap(0) - Fr.wrap(3);
// Compute wire differences
Fr delta_1 = wire(p, WIRE.W_R) - wire(p, WIRE.W_L);
Fr delta_2 = wire(p, WIRE.W_O) - wire(p, WIRE.W_R);
Fr delta_3 = wire(p, WIRE.W_4) - wire(p, WIRE.W_O);
Fr delta_4 = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_4);
// Contribution 6
{
Fr acc = delta_1;
acc = acc * (delta_1 + minus_one);
acc = acc * (delta_1 + minus_two);
acc = acc * (delta_1 + minus_three);
acc = acc * wire(p, WIRE.Q_RANGE);
acc = acc * domainSep;
evals[6] = acc;
}
// Contribution 7
{
Fr acc = delta_2;
acc = acc * (delta_2 + minus_one);
acc = acc * (delta_2 + minus_two);
acc = acc * (delta_2 + minus_three);
acc = acc * wire(p, WIRE.Q_RANGE);
acc = acc * domainSep;
evals[7] = acc;
}
// Contribution 8
{
Fr acc = delta_3;
acc = acc * (delta_3 + minus_one);
acc = acc * (delta_3 + minus_two);
acc = acc * (delta_3 + minus_three);
acc = acc * wire(p, WIRE.Q_RANGE);
acc = acc * domainSep;
evals[8] = acc;
}
// Contribution 9
{
Fr acc = delta_4;
acc = acc * (delta_4 + minus_one);
acc = acc * (delta_4 + minus_two);
acc = acc * (delta_4 + minus_three);
acc = acc * wire(p, WIRE.Q_RANGE);
acc = acc * domainSep;
evals[9] = acc;
}
}
struct EllipticParams {
// Points
Fr x_1;
Fr y_1;
Fr x_2;
Fr y_2;
Fr y_3;
Fr x_3;
// push accumulators into memory
Fr x_double_identity;
}