Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
honk_contract.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
9#include <iostream>
10
11// Source code for the Ultrahonk Solidity verifier.
12// It's expected that the AcirComposer will inject a library which will load the verification key into memory.
13// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
14static const char HONK_CONTRACT_SOURCE[] = R"(
15pragma solidity ^0.8.27;
16
17interface IVerifier {
18 function verify(bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool);
19}
20
25library Errors {
26 error ValueGeLimbMax();
27 error ValueGeGroupOrder();
28 error ValueGeFieldOrder();
29
30 error InvertOfZero();
31 error NotPowerOfTwo();
32 error ModExpFailed();
33
34 error ProofLengthWrong();
35 error ProofLengthWrongWithLogN(uint256 logN, uint256 actualLength, uint256 expectedLength);
36 error PublicInputsLengthWrong();
37 error SumcheckFailed();
38 error ShpleminiFailed();
39
40 error PointAtInfinity();
41
42 error ConsistencyCheckFailed();
43 error GeminiChallengeInSubgroup();
44}
45
46type Fr is uint256;
47
48using {add as +} for Fr global;
49using {sub as -} for Fr global;
50using {mul as *} for Fr global;
51
52using {notEqual as !=} for Fr global;
53using {equal as ==} for Fr global;
54
55uint256 constant SUBGROUP_SIZE = 256;
56uint256 constant MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // Prime field order
57uint256 constant P = MODULUS;
58Fr constant SUBGROUP_GENERATOR = Fr.wrap(0x07b0c561a6148404f086204a9f36ffb0617942546750f230c893619174a57a76);
59Fr constant SUBGROUP_GENERATOR_INVERSE = Fr.wrap(0x204bd3277422fad364751ad938e2b5e6a54cf8c68712848a692c553d0329f5d6);
60Fr constant MINUS_ONE = Fr.wrap(MODULUS - 1);
61Fr constant ONE = Fr.wrap(1);
62Fr constant ZERO = Fr.wrap(0);
63// Instantiation
64
65library FrLib {
66 bytes4 internal constant FRLIB_MODEXP_FAILED_SELECTOR = 0xf8d61709;
67
68 function invert(Fr value) internal view returns (Fr) {
69 uint256 v = Fr.unwrap(value);
70 require(v != 0, Errors.InvertOfZero());
71
72 uint256 result;
73
74 // Call the modexp precompile to invert in the field
75 assembly {
76 let free := mload(0x40)
77 mstore(free, 0x20)
78 mstore(add(free, 0x20), 0x20)
79 mstore(add(free, 0x40), 0x20)
80 mstore(add(free, 0x60), v)
81 mstore(add(free, 0x80), sub(MODULUS, 2))
82 mstore(add(free, 0xa0), MODULUS)
83 let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
84 if iszero(success) {
85 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
86 revert(0, 0x04)
87 }
88 result := mload(0x00)
89 mstore(0x40, add(free, 0xc0))
90 }
91
92 return Fr.wrap(result);
93 }
94
95 function pow(Fr base, uint256 v) internal view returns (Fr) {
96 uint256 b = Fr.unwrap(base);
97 // Only works for power of 2
98 require(v > 0 && (v & (v - 1)) == 0, Errors.NotPowerOfTwo());
99 uint256 result;
100
101 // Call the modexp precompile to invert in the field
102 assembly {
103 let free := mload(0x40)
104 mstore(free, 0x20)
105 mstore(add(free, 0x20), 0x20)
106 mstore(add(free, 0x40), 0x20)
107 mstore(add(free, 0x60), b)
108 mstore(add(free, 0x80), v)
109 mstore(add(free, 0xa0), MODULUS)
110 let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
111 if iszero(success) {
112 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
113 revert(0, 0x04)
114 }
115 result := mload(0x00)
116 mstore(0x40, add(free, 0xc0))
117 }
118
119 return Fr.wrap(result);
120 }
121
122 function div(Fr numerator, Fr denominator) internal view returns (Fr) {
123 unchecked {
124 return numerator * invert(denominator);
125 }
126 }
127
128 function sqr(Fr value) internal pure returns (Fr) {
129 unchecked {
130 return value * value;
131 }
132 }
133
134 function unwrap(Fr value) internal pure returns (uint256) {
135 unchecked {
136 return Fr.unwrap(value);
137 }
138 }
139
140 function neg(Fr value) internal pure returns (Fr) {
141 unchecked {
142 return Fr.wrap(MODULUS - Fr.unwrap(value));
143 }
144 }
145
146 function from(uint256 value) internal pure returns (Fr) {
147 unchecked {
148 require(value < MODULUS, Errors.ValueGeFieldOrder());
149 return Fr.wrap(value);
150 }
151 }
152
153 function fromBytes32(bytes32 value) internal pure returns (Fr) {
154 unchecked {
155 uint256 v = uint256(value);
156 require(v < MODULUS, Errors.ValueGeFieldOrder());
157 return Fr.wrap(v);
158 }
159 }
160
161 function toBytes32(Fr value) internal pure returns (bytes32) {
162 unchecked {
163 return bytes32(Fr.unwrap(value));
164 }
165 }
166}
167
168// Free functions
169function add(Fr a, Fr b) pure returns (Fr) {
170 unchecked {
171 return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
172 }
173}
174
175function mul(Fr a, Fr b) pure returns (Fr) {
176 unchecked {
177 return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
178 }
179}
180
181function sub(Fr a, Fr b) pure returns (Fr) {
182 unchecked {
183 return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
184 }
185}
186
187function notEqual(Fr a, Fr b) pure returns (bool) {
188 unchecked {
189 return Fr.unwrap(a) != Fr.unwrap(b);
190 }
191}
192
193function equal(Fr a, Fr b) pure returns (bool) {
194 unchecked {
195 return Fr.unwrap(a) == Fr.unwrap(b);
196 }
197}
198
199uint256 constant CONST_PROOF_SIZE_LOG_N = 28;
200
201uint256 constant NUMBER_OF_SUBRELATIONS = 28;
202uint256 constant BATCHED_RELATION_PARTIAL_LENGTH = 8;
203uint256 constant ZK_BATCHED_RELATION_PARTIAL_LENGTH = 9;
204uint256 constant NUMBER_OF_ENTITIES = 41;
205// The number of entities added for ZK (gemini_masking_poly)
206uint256 constant NUM_MASKING_POLYNOMIALS = 1;
207uint256 constant NUMBER_OF_ENTITIES_ZK = NUMBER_OF_ENTITIES + NUM_MASKING_POLYNOMIALS;
208uint256 constant NUMBER_UNSHIFTED = 36;
209uint256 constant NUMBER_UNSHIFTED_ZK = NUMBER_UNSHIFTED + NUM_MASKING_POLYNOMIALS;
210uint256 constant NUMBER_TO_BE_SHIFTED = 5;
211uint256 constant PAIRING_POINTS_SIZE = 8;
212
213uint256 constant FIELD_ELEMENT_SIZE = 0x20;
214uint256 constant GROUP_ELEMENT_SIZE = 0x40;
215
216// Powers of alpha used to batch subrelations (alpha, alpha^2, ..., alpha^(NUM_SUBRELATIONS-1))
217uint256 constant NUMBER_OF_ALPHAS = NUMBER_OF_SUBRELATIONS - 1;
218
219// ENUM FOR WIRES
220enum WIRE {
221 Q_M,
222 Q_C,
223 Q_L,
224 Q_R,
225 Q_O,
226 Q_4,
227 Q_LOOKUP,
228 Q_ARITH,
229 Q_RANGE,
230 Q_ELLIPTIC,
231 Q_MEMORY,
232 Q_NNF,
233 Q_POSEIDON2_EXTERNAL,
234 Q_POSEIDON2_INTERNAL,
235 SIGMA_1,
236 SIGMA_2,
237 SIGMA_3,
238 SIGMA_4,
239 ID_1,
240 ID_2,
241 ID_3,
242 ID_4,
243 TABLE_1,
244 TABLE_2,
245 TABLE_3,
246 TABLE_4,
247 LAGRANGE_FIRST,
248 LAGRANGE_LAST,
249 W_L,
250 W_R,
251 W_O,
252 W_4,
253 Z_PERM,
254 LOOKUP_INVERSES,
255 LOOKUP_READ_COUNTS,
256 LOOKUP_READ_TAGS,
257 W_L_SHIFT,
258 W_R_SHIFT,
259 W_O_SHIFT,
260 W_4_SHIFT,
261 Z_PERM_SHIFT
262}
263
264library Honk {
265 struct G1Point {
266 uint256 x;
267 uint256 y;
268 }
269
270 struct VerificationKey {
271 // Misc Params
272 uint256 circuitSize;
273 uint256 logCircuitSize;
274 uint256 publicInputsSize;
275 // Selectors
276 G1Point qm;
277 G1Point qc;
278 G1Point ql;
279 G1Point qr;
280 G1Point qo;
281 G1Point q4;
282 G1Point qLookup; // Lookup
283 G1Point qArith; // Arithmetic widget
284 G1Point qDeltaRange; // Delta Range sort
285 G1Point qMemory; // Memory
286 G1Point qNnf; // Non-native Field
287 G1Point qElliptic; // Auxillary
288 G1Point qPoseidon2External;
289 G1Point qPoseidon2Internal;
290 // Copy constraints
291 G1Point s1;
292 G1Point s2;
293 G1Point s3;
294 G1Point s4;
295 // Copy identity
296 G1Point id1;
297 G1Point id2;
298 G1Point id3;
299 G1Point id4;
300 // Precomputed lookup table
301 G1Point t1;
302 G1Point t2;
303 G1Point t3;
304 G1Point t4;
305 // Fixed first and last
306 G1Point lagrangeFirst;
307 G1Point lagrangeLast;
308 }
309
310 struct RelationParameters {
311 // challenges
312 Fr eta;
313 Fr beta;
314 Fr gamma;
315 // derived
316 Fr publicInputsDelta;
317 }
318
319 struct Proof {
320 // Pairing point object
321 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
322 // Free wires
323 G1Point w1;
324 G1Point w2;
325 G1Point w3;
326 G1Point w4;
327 // Lookup helpers - Permutations
328 G1Point zPerm;
329 // Lookup helpers - logup
330 G1Point lookupReadCounts;
331 G1Point lookupReadTags;
332 G1Point lookupInverses;
333 // Sumcheck
334 Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
335 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
336 // Shplemini
337 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
338 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
339 G1Point shplonkQ;
340 G1Point kzgQuotient;
341 }
342
344 struct ZKProof {
345 // Pairing point object
346 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
347 // ZK: Gemini masking polynomial commitment (sent first, right after public inputs)
348 G1Point geminiMaskingPoly;
349 // Commitments to wire polynomials
350 G1Point w1;
351 G1Point w2;
352 G1Point w3;
353 G1Point w4;
354 // Commitments to logup witness polynomials
355 G1Point lookupReadCounts;
356 G1Point lookupReadTags;
357 G1Point lookupInverses;
358 // Commitment to grand permutation polynomial
359 G1Point zPerm;
360 G1Point[3] libraCommitments;
361 // Sumcheck
362 Fr libraSum;
363 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
364 Fr libraEvaluation;
365 Fr[NUMBER_OF_ENTITIES_ZK] sumcheckEvaluations; // Includes gemini_masking_poly eval at index 0 (first position)
366 // Shplemini
367 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
368 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
369 Fr[4] libraPolyEvals;
370 G1Point shplonkQ;
371 G1Point kzgQuotient;
372 }
373}
374
375// Transcript library to generate fiat shamir challenges
376struct Transcript {
377 // Oink
378 Honk.RelationParameters relationParameters;
379 Fr[NUMBER_OF_ALPHAS] alphas; // Powers of alpha: [alpha, alpha^2, ..., alpha^(NUM_SUBRELATIONS-1)]
380 Fr[CONST_PROOF_SIZE_LOG_N] gateChallenges;
381 // Sumcheck
382 Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
383 // Gemini
384 Fr rho;
385 Fr geminiR;
386 // Shplonk
387 Fr shplonkNu;
388 Fr shplonkZ;
389}
390
391library TranscriptLib {
392 function generateTranscript(
393 Honk.Proof memory proof,
394 bytes32[] calldata publicInputs,
395 uint256 vkHash,
396 uint256 publicInputsSize,
397 uint256 logN
398 ) internal pure returns (Transcript memory t) {
399 Fr previousChallenge;
400 (t.relationParameters, previousChallenge) =
401 generateRelationParametersChallenges(proof, publicInputs, vkHash, publicInputsSize, previousChallenge);
402
403 (t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
404
405 (t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge, logN);
406
407 (t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge, logN);
408
409 (t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
410
411 (t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge, logN);
412
413 (t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge, logN);
414
415 (t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
416
417 return t;
418 }
419
420 function splitChallenge(Fr challenge) internal pure returns (Fr first, Fr second) {
421 uint256 challengeU256 = uint256(Fr.unwrap(challenge));
422 // Split into two equal 127-bit chunks (254/2)
423 uint256 lo = challengeU256 & 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; // 127 bits
424 uint256 hi = challengeU256 >> 127;
425 first = FrLib.from(lo);
426 second = FrLib.from(hi);
427 }
428
429 function generateRelationParametersChallenges(
430 Honk.Proof memory proof,
431 bytes32[] calldata publicInputs,
432 uint256 vkHash,
433 uint256 publicInputsSize,
434 Fr previousChallenge
435 ) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
436 (rp.eta, previousChallenge) = generateEtaChallenge(proof, publicInputs, vkHash, publicInputsSize);
437
438 (rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaGammaChallenges(previousChallenge, proof);
439 }
440
441 function generateEtaChallenge(
442 Honk.Proof memory proof,
443 bytes32[] calldata publicInputs,
444 uint256 vkHash,
445 uint256 publicInputsSize
446 ) internal pure returns (Fr eta, Fr previousChallenge) {
447 bytes32[] memory round0 = new bytes32[](1 + publicInputsSize + 6);
448 round0[0] = bytes32(vkHash);
449
450 for (uint256 i = 0; i < publicInputsSize - PAIRING_POINTS_SIZE; i++) {
451 require(uint256(publicInputs[i]) < P, Errors.ValueGeFieldOrder());
452 round0[1 + i] = publicInputs[i];
453 }
454 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
455 round0[1 + publicInputsSize - PAIRING_POINTS_SIZE + i] = FrLib.toBytes32(proof.pairingPointObject[i]);
456 }
457
458 // Create the first challenge
459 // Note: w4 is added to the challenge later on
460 round0[1 + publicInputsSize] = bytes32(proof.w1.x);
461 round0[1 + publicInputsSize + 1] = bytes32(proof.w1.y);
462 round0[1 + publicInputsSize + 2] = bytes32(proof.w2.x);
463 round0[1 + publicInputsSize + 3] = bytes32(proof.w2.y);
464 round0[1 + publicInputsSize + 4] = bytes32(proof.w3.x);
465 round0[1 + publicInputsSize + 5] = bytes32(proof.w3.y);
466
467 previousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(round0))) % P);
468 (eta,) = splitChallenge(previousChallenge);
469 }
470
471 function generateBetaGammaChallenges(Fr previousChallenge, Honk.Proof memory proof)
472 internal
473 pure
474 returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
475 {
476 bytes32[7] memory round1;
477 round1[0] = FrLib.toBytes32(previousChallenge);
478 round1[1] = bytes32(proof.lookupReadCounts.x);
479 round1[2] = bytes32(proof.lookupReadCounts.y);
480 round1[3] = bytes32(proof.lookupReadTags.x);
481 round1[4] = bytes32(proof.lookupReadTags.y);
482 round1[5] = bytes32(proof.w4.x);
483 round1[6] = bytes32(proof.w4.y);
484
485 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(round1))) % P);
486 (beta, gamma) = splitChallenge(nextPreviousChallenge);
487 }
488
489 // Alpha challenges non-linearise the gate contributions
490 function generateAlphaChallenges(Fr previousChallenge, Honk.Proof memory proof)
491 internal
492 pure
493 returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
494 {
495 // Generate the original sumcheck alpha 0 by hashing zPerm and zLookup
496 uint256[5] memory alpha0;
497 alpha0[0] = Fr.unwrap(previousChallenge);
498 alpha0[1] = proof.lookupInverses.x;
499 alpha0[2] = proof.lookupInverses.y;
500 alpha0[3] = proof.zPerm.x;
501 alpha0[4] = proof.zPerm.y;
502
503 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(alpha0))) % P);
504 Fr alpha;
505 (alpha,) = splitChallenge(nextPreviousChallenge);
506
507 // Compute powers of alpha for batching subrelations
508 alphas[0] = alpha;
509 for (uint256 i = 1; i < NUMBER_OF_ALPHAS; i++) {
510 alphas[i] = alphas[i - 1] * alpha;
511 }
512 }
513
514 function generateGateChallenges(Fr previousChallenge, uint256 logN)
515 internal
516 pure
517 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
518 {
519 previousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge)))) % P);
520 (gateChallenges[0],) = splitChallenge(previousChallenge);
521 for (uint256 i = 1; i < logN; i++) {
522 gateChallenges[i] = gateChallenges[i - 1] * gateChallenges[i - 1];
523 }
524 nextPreviousChallenge = previousChallenge;
525 }
526
527 function generateSumcheckChallenges(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
528 internal
529 pure
530 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
531 {
532 for (uint256 i = 0; i < logN; i++) {
533 Fr[BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
534 univariateChal[0] = prevChallenge;
535
536 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
537 univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
538 }
539 prevChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(univariateChal))) % P);
540 Fr unused;
541 (sumcheckChallenges[i], unused) = splitChallenge(prevChallenge);
542 }
543 nextPreviousChallenge = prevChallenge;
544 }
545
546 function generateRhoChallenge(Honk.Proof memory proof, Fr prevChallenge)
547 internal
548 pure
549 returns (Fr rho, Fr nextPreviousChallenge)
550 {
551 Fr[NUMBER_OF_ENTITIES + 1] memory rhoChallengeElements;
552 rhoChallengeElements[0] = prevChallenge;
553
554 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
555 rhoChallengeElements[i + 1] = proof.sumcheckEvaluations[i];
556 }
557
558 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(rhoChallengeElements))) % P);
559 Fr unused;
560 (rho, unused) = splitChallenge(nextPreviousChallenge);
561 }
562
563 function generateGeminiRChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
564 internal
565 pure
566 returns (Fr geminiR, Fr nextPreviousChallenge)
567 {
568 uint256[] memory gR = new uint256[]((logN - 1) * 2 + 1);
569 gR[0] = Fr.unwrap(prevChallenge);
570
571 for (uint256 i = 0; i < logN - 1; i++) {
572 gR[1 + i * 2] = proof.geminiFoldComms[i].x;
573 gR[2 + i * 2] = proof.geminiFoldComms[i].y;
574 }
575
576 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(gR))) % P);
577 Fr unused;
578 (geminiR, unused) = splitChallenge(nextPreviousChallenge);
579 }
580
581 function generateShplonkNuChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
582 internal
583 pure
584 returns (Fr shplonkNu, Fr nextPreviousChallenge)
585 {
586 uint256[] memory shplonkNuChallengeElements = new uint256[](logN + 1);
587 shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
588
589 for (uint256 i = 0; i < logN; i++) {
590 shplonkNuChallengeElements[i + 1] = Fr.unwrap(proof.geminiAEvaluations[i]);
591 }
592
593 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkNuChallengeElements))) % P);
594 Fr unused;
595 (shplonkNu, unused) = splitChallenge(nextPreviousChallenge);
596 }
597
598 function generateShplonkZChallenge(Honk.Proof memory proof, Fr prevChallenge)
599 internal
600 pure
601 returns (Fr shplonkZ, Fr nextPreviousChallenge)
602 {
603 uint256[3] memory shplonkZChallengeElements;
604 shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
605
606 shplonkZChallengeElements[1] = proof.shplonkQ.x;
607 shplonkZChallengeElements[2] = proof.shplonkQ.y;
608
609 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkZChallengeElements))) % P);
610 Fr unused;
611 (shplonkZ, unused) = splitChallenge(nextPreviousChallenge);
612 }
613
614 function loadProof(bytes calldata proof, uint256 logN) internal pure returns (Honk.Proof memory p) {
615 uint256 boundary = 0x00;
616
617 // Pairing point object
618 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
619 uint256 limb = uint256(bytes32(proof[boundary:boundary + FIELD_ELEMENT_SIZE]));
620 // lo limbs (even index) < 2^136, hi limbs (odd index) < 2^120
621 require(limb < 2 ** (i % 2 == 0 ? 136 : 120), Errors.ValueGeLimbMax());
622 p.pairingPointObject[i] = FrLib.from(limb);
623 boundary += FIELD_ELEMENT_SIZE;
624 }
625 // Commitments
626 p.w1 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
627 boundary += GROUP_ELEMENT_SIZE;
628 p.w2 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
629 boundary += GROUP_ELEMENT_SIZE;
630 p.w3 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
631 boundary += GROUP_ELEMENT_SIZE;
632
633 // Lookup / Permutation Helper Commitments
634 p.lookupReadCounts = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
635 boundary += GROUP_ELEMENT_SIZE;
636 p.lookupReadTags = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
637 boundary += GROUP_ELEMENT_SIZE;
638 p.w4 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
639 boundary += GROUP_ELEMENT_SIZE;
640 p.lookupInverses = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
641 boundary += GROUP_ELEMENT_SIZE;
642 p.zPerm = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
643 boundary += GROUP_ELEMENT_SIZE;
644
645 // Sumcheck univariates
646 for (uint256 i = 0; i < logN; i++) {
647 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
648 p.sumcheckUnivariates[i][j] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
649 boundary += FIELD_ELEMENT_SIZE;
650 }
651 }
652 // Sumcheck evaluations
653 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
654 p.sumcheckEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
655 boundary += FIELD_ELEMENT_SIZE;
656 }
657
658 // Gemini
659 // Read gemini fold univariates
660 for (uint256 i = 0; i < logN - 1; i++) {
661 p.geminiFoldComms[i] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
662 boundary += GROUP_ELEMENT_SIZE;
663 }
664
665 // Read gemini a evaluations
666 for (uint256 i = 0; i < logN; i++) {
667 p.geminiAEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
668 boundary += FIELD_ELEMENT_SIZE;
669 }
670
671 // Shplonk
672 p.shplonkQ = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
673 boundary += GROUP_ELEMENT_SIZE;
674 // KZG
675 p.kzgQuotient = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
676 }
677}
678
679library RelationsLib {
680 struct EllipticParams {
681 // Points
682 Fr x_1;
683 Fr y_1;
684 Fr x_2;
685 Fr y_2;
686 Fr y_3;
687 Fr x_3;
688 // push accumulators into memory
689 Fr x_double_identity;
690 }
691
692 // Parameters used within the Memory Relation
693 // A struct is used to work around stack too deep. This relation has alot of variables
694 struct MemParams {
695 Fr memory_record_check;
696 Fr partial_record_check;
697 Fr next_gate_access_type;
698 Fr record_delta;
699 Fr index_delta;
700 Fr adjacent_values_match_if_adjacent_indices_match;
701 Fr adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation;
702 Fr access_check;
703 Fr next_gate_access_type_is_boolean;
704 Fr ROM_consistency_check_identity;
705 Fr RAM_consistency_check_identity;
706 Fr timestamp_delta;
707 Fr RAM_timestamp_check_identity;
708 Fr memory_identity;
709 Fr index_is_monotonically_increasing;
710 }
711
712 // Parameters used within the Non-Native Field Relation
713 // A struct is used to work around stack too deep. This relation has alot of variables
714 struct NnfParams {
715 Fr limb_subproduct;
716 Fr non_native_field_gate_1;
717 Fr non_native_field_gate_2;
718 Fr non_native_field_gate_3;
719 Fr limb_accumulator_1;
720 Fr limb_accumulator_2;
721 Fr nnf_identity;
722 }
723
724 struct PoseidonExternalParams {
725 Fr s1;
726 Fr s2;
727 Fr s3;
728 Fr s4;
729 Fr u1;
730 Fr u2;
731 Fr u3;
732 Fr u4;
733 Fr t0;
734 Fr t1;
735 Fr t2;
736 Fr t3;
737 Fr v1;
738 Fr v2;
739 Fr v3;
740 Fr v4;
741 Fr q_pos_by_scaling;
742 }
743
744 struct PoseidonInternalParams {
745 Fr u1;
746 Fr u2;
747 Fr u3;
748 Fr u4;
749 Fr u_sum;
750 Fr v1;
751 Fr v2;
752 Fr v3;
753 Fr v4;
754 Fr s1;
755 Fr q_pos_by_scaling;
756 }
757
758 Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
759 uint256 internal constant NEG_HALF_MODULO_P = 0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000;
760
761 // Constants for the Non-native Field relation
762 Fr internal constant LIMB_SIZE = Fr.wrap(uint256(1) << 68);
763 Fr internal constant SUBLIMB_SHIFT = Fr.wrap(uint256(1) << 14);
764
765 function accumulateRelationEvaluations(
766 Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
767 Honk.RelationParameters memory rp,
768 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges,
769 Fr powPartialEval
770 ) internal pure returns (Fr accumulator) {
771 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
772
773 // Accumulate all relations in Ultra Honk - each with varying number of subrelations
774 accumulateArithmeticRelation(purportedEvaluations, evaluations, powPartialEval);
775 accumulatePermutationRelation(purportedEvaluations, rp, evaluations, powPartialEval);
776 accumulateLogDerivativeLookupRelation(purportedEvaluations, rp, evaluations, powPartialEval);
777 accumulateDeltaRangeRelation(purportedEvaluations, evaluations, powPartialEval);
778 accumulateEllipticRelation(purportedEvaluations, evaluations, powPartialEval);
779 accumulateMemoryRelation(purportedEvaluations, rp, evaluations, powPartialEval);
780 accumulateNnfRelation(purportedEvaluations, evaluations, powPartialEval);
781 accumulatePoseidonExternalRelation(purportedEvaluations, evaluations, powPartialEval);
782 accumulatePoseidonInternalRelation(purportedEvaluations, evaluations, powPartialEval);
783
784 // batch the subrelations with the precomputed alpha powers to obtain the full honk relation
785 accumulator = scaleAndBatchSubrelations(evaluations, subrelationChallenges);
786 }
787
793 function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
794 return p[uint256(_wire)];
795 }
796
801 function accumulateArithmeticRelation(
802 Fr[NUMBER_OF_ENTITIES] memory p,
803 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
804 Fr domainSep
805 ) internal pure {
806 // Relation 0
807 Fr q_arith = wire(p, WIRE.Q_ARITH);
808 {
809 Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
810
811 Fr accum = (q_arith - Fr.wrap(3)) * (wire(p, WIRE.Q_M) * wire(p, WIRE.W_R) * wire(p, WIRE.W_L)) * neg_half;
812 accum = accum + (wire(p, WIRE.Q_L) * wire(p, WIRE.W_L)) + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_R))
813 + (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);
814 accum = accum + (q_arith - ONE) * wire(p, WIRE.W_4_SHIFT);
815 accum = accum * q_arith;
816 accum = accum * domainSep;
817 evals[0] = accum;
818 }
819
820 // Relation 1
821 {
822 Fr accum = wire(p, WIRE.W_L) + wire(p, WIRE.W_4) - wire(p, WIRE.W_L_SHIFT) + wire(p, WIRE.Q_M);
823 accum = accum * (q_arith - Fr.wrap(2));
824 accum = accum * (q_arith - ONE);
825 accum = accum * q_arith;
826 accum = accum * domainSep;
827 evals[1] = accum;
828 }
829 }
830
831 function accumulatePermutationRelation(
832 Fr[NUMBER_OF_ENTITIES] memory p,
833 Honk.RelationParameters memory rp,
834 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
835 Fr domainSep
836 ) internal pure {
837 Fr grand_product_numerator;
838 Fr grand_product_denominator;
839
840 {
841 Fr num = wire(p, WIRE.W_L) + wire(p, WIRE.ID_1) * rp.beta + rp.gamma;
842 num = num * (wire(p, WIRE.W_R) + wire(p, WIRE.ID_2) * rp.beta + rp.gamma);
843 num = num * (wire(p, WIRE.W_O) + wire(p, WIRE.ID_3) * rp.beta + rp.gamma);
844 num = num * (wire(p, WIRE.W_4) + wire(p, WIRE.ID_4) * rp.beta + rp.gamma);
845
846 grand_product_numerator = num;
847 }
848 {
849 Fr den = wire(p, WIRE.W_L) + wire(p, WIRE.SIGMA_1) * rp.beta + rp.gamma;
850 den = den * (wire(p, WIRE.W_R) + wire(p, WIRE.SIGMA_2) * rp.beta + rp.gamma);
851 den = den * (wire(p, WIRE.W_O) + wire(p, WIRE.SIGMA_3) * rp.beta + rp.gamma);
852 den = den * (wire(p, WIRE.W_4) + wire(p, WIRE.SIGMA_4) * rp.beta + rp.gamma);
853
854 grand_product_denominator = den;
855 }
856
857 // Contribution 2
858 {
859 Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
860
861 acc = acc
862 - ((wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
863 * grand_product_denominator);
864 acc = acc * domainSep;
865 evals[2] = acc;
866 }
867
868 // Contribution 3
869 {
870 Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
871 evals[3] = acc;
872 }
873 }
874
875 function accumulateLogDerivativeLookupRelation(
876 Fr[NUMBER_OF_ENTITIES] memory p,
877 Honk.RelationParameters memory rp,
878 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
879 Fr domainSep
880 ) internal pure {
881 Fr table_term;
882 Fr lookup_term;
883
884 // Calculate the write term (the table accumulation)
885 // table_term = table_1 + γ + table_2 * β + table_3 * β² + table_4 * β³
886 {
887 Fr beta_sqr = rp.beta * rp.beta;
888 table_term = wire(p, WIRE.TABLE_1) + rp.gamma + (wire(p, WIRE.TABLE_2) * rp.beta)
889 + (wire(p, WIRE.TABLE_3) * beta_sqr) + (wire(p, WIRE.TABLE_4) * beta_sqr * rp.beta);
890 }
891
892 // Calculate the read term
893 // lookup_term = derived_entry_1 + γ + derived_entry_2 * β + derived_entry_3 * β² + q_index * β³
894 {
895 Fr beta_sqr = rp.beta * rp.beta;
896 Fr derived_entry_1 = wire(p, WIRE.W_L) + rp.gamma + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_L_SHIFT));
897 Fr derived_entry_2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_M) * wire(p, WIRE.W_R_SHIFT);
898 Fr derived_entry_3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_C) * wire(p, WIRE.W_O_SHIFT);
899
900 lookup_term = derived_entry_1 + (derived_entry_2 * rp.beta) + (derived_entry_3 * beta_sqr)
901 + (wire(p, WIRE.Q_O) * beta_sqr * rp.beta);
902 }
903
904 Fr lookup_inverse = wire(p, WIRE.LOOKUP_INVERSES) * table_term;
905 Fr table_inverse = wire(p, WIRE.LOOKUP_INVERSES) * lookup_term;
906
907 Fr inverse_exists_xor =
908 wire(p, WIRE.LOOKUP_READ_TAGS) + wire(p, WIRE.Q_LOOKUP)
909 - (wire(p, WIRE.LOOKUP_READ_TAGS) * wire(p, WIRE.Q_LOOKUP));
910
911 // Inverse calculated correctly relation
912 Fr accumulatorNone = lookup_term * table_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
913 accumulatorNone = accumulatorNone * domainSep;
914
915 // Inverse
916 Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * lookup_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * table_inverse;
917
918 Fr read_tag = wire(p, WIRE.LOOKUP_READ_TAGS);
919
920 Fr read_tag_boolean_relation = read_tag * read_tag - read_tag;
921
922 evals[4] = accumulatorNone;
923 evals[5] = accumulatorOne;
924 evals[6] = read_tag_boolean_relation * domainSep;
925 }
926
927 function accumulateDeltaRangeRelation(
928 Fr[NUMBER_OF_ENTITIES] memory p,
929 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
930 Fr domainSep
931 ) internal pure {
932 Fr minus_one = ZERO - ONE;
933 Fr minus_two = ZERO - Fr.wrap(2);
934 Fr minus_three = ZERO - Fr.wrap(3);
935
936 // Compute wire differences
937 Fr delta_1 = wire(p, WIRE.W_R) - wire(p, WIRE.W_L);
938 Fr delta_2 = wire(p, WIRE.W_O) - wire(p, WIRE.W_R);
939 Fr delta_3 = wire(p, WIRE.W_4) - wire(p, WIRE.W_O);
940 Fr delta_4 = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_4);
941
942 // Contribution 6
943 {
944 Fr acc = delta_1;
945 acc = acc * (delta_1 + minus_one);
946 acc = acc * (delta_1 + minus_two);
947 acc = acc * (delta_1 + minus_three);
948 acc = acc * wire(p, WIRE.Q_RANGE);
949 acc = acc * domainSep;
950 evals[7] = acc;
951 }
952
953 // Contribution 7
954 {
955 Fr acc = delta_2;
956 acc = acc * (delta_2 + minus_one);
957 acc = acc * (delta_2 + minus_two);
958 acc = acc * (delta_2 + minus_three);
959 acc = acc * wire(p, WIRE.Q_RANGE);
960 acc = acc * domainSep;
961 evals[8] = acc;
962 }
963
964 // Contribution 8
965 {
966 Fr acc = delta_3;
967 acc = acc * (delta_3 + minus_one);
968 acc = acc * (delta_3 + minus_two);
969 acc = acc * (delta_3 + minus_three);
970 acc = acc * wire(p, WIRE.Q_RANGE);
971 acc = acc * domainSep;
972 evals[9] = acc;
973 }
974
975 // Contribution 9
976 {
977 Fr acc = delta_4;
978 acc = acc * (delta_4 + minus_one);
979 acc = acc * (delta_4 + minus_two);
980 acc = acc * (delta_4 + minus_three);
981 acc = acc * wire(p, WIRE.Q_RANGE);
982 acc = acc * domainSep;
983 evals[10] = acc;
984 }
985 }
986
987 function accumulateEllipticRelation(
988 Fr[NUMBER_OF_ENTITIES] memory p,
989 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
990 Fr domainSep
991 ) internal pure {
992 EllipticParams memory ep;
993 ep.x_1 = wire(p, WIRE.W_R);
994 ep.y_1 = wire(p, WIRE.W_O);
995
996 ep.x_2 = wire(p, WIRE.W_L_SHIFT);
997 ep.y_2 = wire(p, WIRE.W_4_SHIFT);
998 ep.y_3 = wire(p, WIRE.W_O_SHIFT);
999 ep.x_3 = wire(p, WIRE.W_R_SHIFT);
1000
1001 Fr q_sign = wire(p, WIRE.Q_L);
1002 Fr q_is_double = wire(p, WIRE.Q_M);
1003
1004 // Contribution 10 point addition, x-coordinate check
1005 // q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
1006 Fr x_diff = (ep.x_2 - ep.x_1);
1007 Fr y1_sqr = (ep.y_1 * ep.y_1);
1008 {
1009 // Move to top
1010 Fr partialEval = domainSep;
1011
1012 Fr y2_sqr = (ep.y_2 * ep.y_2);
1013 Fr y1y2 = ep.y_1 * ep.y_2 * q_sign;
1014 Fr x_add_identity = (ep.x_3 + ep.x_2 + ep.x_1);
1015 x_add_identity = x_add_identity * x_diff * x_diff;
1016 x_add_identity = x_add_identity - y2_sqr - y1_sqr + y1y2 + y1y2;
1017
1018 evals[11] = x_add_identity * partialEval * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
1019 }
1020
1021 // Contribution 11 point addition, x-coordinate check
1022 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
1023 {
1024 Fr y1_plus_y3 = ep.y_1 + ep.y_3;
1025 Fr y_diff = ep.y_2 * q_sign - ep.y_1;
1026 Fr y_add_identity = y1_plus_y3 * x_diff + (ep.x_3 - ep.x_1) * y_diff;
1027 evals[12] = y_add_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
1028 }
1029
1030 // Contribution 10 point doubling, x-coordinate check
1031 // (x3 + x1 + x1) (4y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
1032 // N.B. we're using the equivalence x1*x1*x1 === y1*y1 - curve_b to reduce degree by 1
1033 {
1034 Fr x_pow_4 = (y1_sqr + GRUMPKIN_CURVE_B_PARAMETER_NEGATED) * ep.x_1;
1035 Fr y1_sqr_mul_4 = y1_sqr + y1_sqr;
1036 y1_sqr_mul_4 = y1_sqr_mul_4 + y1_sqr_mul_4;
1037 Fr x1_pow_4_mul_9 = x_pow_4 * Fr.wrap(9);
1038
1039 // NOTE: pushed into memory (stack >:'( )
1040 ep.x_double_identity = (ep.x_3 + ep.x_1 + ep.x_1) * y1_sqr_mul_4 - x1_pow_4_mul_9;
1041
1042 Fr acc = ep.x_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
1043 evals[11] = evals[11] + acc;
1044 }
1045
1046 // Contribution 11 point doubling, y-coordinate check
1047 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
1048 {
1049 Fr x1_sqr_mul_3 = (ep.x_1 + ep.x_1 + ep.x_1) * ep.x_1;
1050 Fr y_double_identity = x1_sqr_mul_3 * (ep.x_1 - ep.x_3) - (ep.y_1 + ep.y_1) * (ep.y_1 + ep.y_3);
1051 evals[12] = evals[12] + y_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
1052 }
1053 }
1054
1055 function accumulateMemoryRelation(
1056 Fr[NUMBER_OF_ENTITIES] memory p,
1057 Honk.RelationParameters memory rp,
1058 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1059 Fr domainSep
1060 ) internal pure {
1061 MemParams memory ap;
1062
1063 // Compute eta powers locally
1064 Fr eta_two = rp.eta * rp.eta;
1065 Fr eta_three = eta_two * rp.eta;
1066
1108 ap.memory_record_check = wire(p, WIRE.W_O) * eta_three;
1109 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_R) * eta_two);
1110 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_L) * rp.eta);
1111 ap.memory_record_check = ap.memory_record_check + wire(p, WIRE.Q_C);
1112 ap.partial_record_check = ap.memory_record_check; // used in RAM consistency check; deg 1 or 4
1113 ap.memory_record_check = ap.memory_record_check - wire(p, WIRE.W_4);
1114
1131 ap.index_delta = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_L);
1132 ap.record_delta = wire(p, WIRE.W_4_SHIFT) - wire(p, WIRE.W_4);
1133
1134 ap.index_is_monotonically_increasing = ap.index_delta * (ap.index_delta - Fr.wrap(1)); // deg 2
1135
1136 ap.adjacent_values_match_if_adjacent_indices_match = (ap.index_delta * MINUS_ONE + ONE) * ap.record_delta; // deg 2
1137
1138 evals[14] = ap.adjacent_values_match_if_adjacent_indices_match * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1139 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1140 evals[15] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1141 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1142
1143 ap.ROM_consistency_check_identity = ap.memory_record_check * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R)); // deg 3 or 7
1144
1164 Fr access_type = (wire(p, WIRE.W_4) - ap.partial_record_check); // will be 0 or 1 for honest Prover; deg 1 or 4
1165 ap.access_check = access_type * (access_type - Fr.wrap(1)); // check value is 0 or 1; deg 2 or 8
1166
1167 // reverse order we could re-use `ap.partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta
1168 // deg 1 or 4
1169 ap.next_gate_access_type = wire(p, WIRE.W_O_SHIFT) * eta_three;
1170 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_R_SHIFT) * eta_two);
1171 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_L_SHIFT) * rp.eta);
1172 ap.next_gate_access_type = wire(p, WIRE.W_4_SHIFT) - ap.next_gate_access_type;
1173
1174 Fr value_delta = wire(p, WIRE.W_O_SHIFT) - wire(p, WIRE.W_O);
1175 ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation =
1176 (ap.index_delta * MINUS_ONE + ONE) * value_delta * (ap.next_gate_access_type * MINUS_ONE + ONE); // deg 3 or 6
1177
1178 // We can't apply the RAM consistency check identity on the final entry in the sorted list (the wires in the
1179 // next gate would make the identity fail). We need to validate that its 'access type' bool is correct. Can't
1180 // do with an arithmetic gate because of the `eta` factors. We need to check that the *next* gate's access
1181 // type is correct, to cover this edge case
1182 // deg 2 or 4
1183 ap.next_gate_access_type_is_boolean =
1184 ap.next_gate_access_type * ap.next_gate_access_type - ap.next_gate_access_type;
1185
1186 // Putting it all together...
1187 evals[16] = ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation
1188 * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5 or 8
1189 evals[17] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4
1190 evals[18] = ap.next_gate_access_type_is_boolean * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 6
1191
1192 ap.RAM_consistency_check_identity = ap.access_check * (wire(p, WIRE.Q_O)); // deg 3 or 9
1193
1205 ap.timestamp_delta = wire(p, WIRE.W_R_SHIFT) - wire(p, WIRE.W_R);
1206 ap.RAM_timestamp_check_identity = (ap.index_delta * MINUS_ONE + ONE) * ap.timestamp_delta - wire(p, WIRE.W_O); // deg 3
1207
1213 ap.memory_identity = ap.ROM_consistency_check_identity; // deg 3 or 6
1214 ap.memory_identity =
1215 ap.memory_identity + ap.RAM_timestamp_check_identity * (wire(p, WIRE.Q_4) * wire(p, WIRE.Q_L)); // deg 4
1216 ap.memory_identity = ap.memory_identity + ap.memory_record_check * (wire(p, WIRE.Q_M) * wire(p, WIRE.Q_L)); // deg 3 or 6
1217 ap.memory_identity = ap.memory_identity + ap.RAM_consistency_check_identity; // deg 3 or 9
1218
1219 // (deg 3 or 9) + (deg 4) + (deg 3)
1220 ap.memory_identity = ap.memory_identity * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 10
1221 evals[13] = ap.memory_identity;
1222 }
1223
1224 function accumulateNnfRelation(
1225 Fr[NUMBER_OF_ENTITIES] memory p,
1226 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1227 Fr domainSep
1228 ) internal pure {
1229 NnfParams memory ap;
1230
1243 ap.limb_subproduct = wire(p, WIRE.W_L) * wire(p, WIRE.W_R_SHIFT) + wire(p, WIRE.W_L_SHIFT) * wire(p, WIRE.W_R);
1244 ap.non_native_field_gate_2 =
1245 (wire(p, WIRE.W_L) * wire(p, WIRE.W_4) + wire(p, WIRE.W_R) * wire(p, WIRE.W_O) - wire(p, WIRE.W_O_SHIFT));
1246 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * LIMB_SIZE;
1247 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 - wire(p, WIRE.W_4_SHIFT);
1248 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 + ap.limb_subproduct;
1249 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * wire(p, WIRE.Q_4);
1250
1251 ap.limb_subproduct = ap.limb_subproduct * LIMB_SIZE;
1252 ap.limb_subproduct = ap.limb_subproduct + (wire(p, WIRE.W_L_SHIFT) * wire(p, WIRE.W_R_SHIFT));
1253 ap.non_native_field_gate_1 = ap.limb_subproduct;
1254 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 - (wire(p, WIRE.W_O) + wire(p, WIRE.W_4));
1255 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 * wire(p, WIRE.Q_O);
1256
1257 ap.non_native_field_gate_3 = ap.limb_subproduct;
1258 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 + wire(p, WIRE.W_4);
1259 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 - (wire(p, WIRE.W_O_SHIFT) + wire(p, WIRE.W_4_SHIFT));
1260 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 * wire(p, WIRE.Q_M);
1261
1262 Fr non_native_field_identity =
1263 ap.non_native_field_gate_1 + ap.non_native_field_gate_2 + ap.non_native_field_gate_3;
1264 non_native_field_identity = non_native_field_identity * wire(p, WIRE.Q_R);
1265
1266 // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm
1267 // deg 2
1268 ap.limb_accumulator_1 = wire(p, WIRE.W_R_SHIFT) * SUBLIMB_SHIFT;
1269 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L_SHIFT);
1270 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1271 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_O);
1272 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1273 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_R);
1274 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1275 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L);
1276 ap.limb_accumulator_1 = ap.limb_accumulator_1 - wire(p, WIRE.W_4);
1277 ap.limb_accumulator_1 = ap.limb_accumulator_1 * wire(p, WIRE.Q_4);
1278
1279 // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm
1280 // deg 2
1281 ap.limb_accumulator_2 = wire(p, WIRE.W_O_SHIFT) * SUBLIMB_SHIFT;
1282 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_R_SHIFT);
1283 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1284 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_L_SHIFT);
1285 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1286 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_4);
1287 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1288 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_O);
1289 ap.limb_accumulator_2 = ap.limb_accumulator_2 - wire(p, WIRE.W_4_SHIFT);
1290 ap.limb_accumulator_2 = ap.limb_accumulator_2 * wire(p, WIRE.Q_M);
1291
1292 Fr limb_accumulator_identity = ap.limb_accumulator_1 + ap.limb_accumulator_2;
1293 limb_accumulator_identity = limb_accumulator_identity * wire(p, WIRE.Q_O); // deg 3
1294
1295 ap.nnf_identity = non_native_field_identity + limb_accumulator_identity;
1296 ap.nnf_identity = ap.nnf_identity * (wire(p, WIRE.Q_NNF) * domainSep);
1297 evals[19] = ap.nnf_identity;
1298 }
1299
1300 function accumulatePoseidonExternalRelation(
1301 Fr[NUMBER_OF_ENTITIES] memory p,
1302 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1303 Fr domainSep
1304 ) internal pure {
1305 PoseidonExternalParams memory ep;
1306
1307 ep.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1308 ep.s2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_R);
1309 ep.s3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_O);
1310 ep.s4 = wire(p, WIRE.W_4) + wire(p, WIRE.Q_4);
1311
1312 ep.u1 = ep.s1 * ep.s1 * ep.s1 * ep.s1 * ep.s1;
1313 ep.u2 = ep.s2 * ep.s2 * ep.s2 * ep.s2 * ep.s2;
1314 ep.u3 = ep.s3 * ep.s3 * ep.s3 * ep.s3 * ep.s3;
1315 ep.u4 = ep.s4 * ep.s4 * ep.s4 * ep.s4 * ep.s4;
1316 // matrix mul v = M_E * u with 14 additions
1317 ep.t0 = ep.u1 + ep.u2; // u_1 + u_2
1318 ep.t1 = ep.u3 + ep.u4; // u_3 + u_4
1319 ep.t2 = ep.u2 + ep.u2 + ep.t1; // 2u_2
1320 // ep.t2 += ep.t1; // 2u_2 + u_3 + u_4
1321 ep.t3 = ep.u4 + ep.u4 + ep.t0; // 2u_4
1322 // ep.t3 += ep.t0; // u_1 + u_2 + 2u_4
1323 ep.v4 = ep.t1 + ep.t1;
1324 ep.v4 = ep.v4 + ep.v4 + ep.t3;
1325 // ep.v4 += ep.t3; // u_1 + u_2 + 4u_3 + 6u_4
1326 ep.v2 = ep.t0 + ep.t0;
1327 ep.v2 = ep.v2 + ep.v2 + ep.t2;
1328 // ep.v2 += ep.t2; // 4u_1 + 6u_2 + u_3 + u_4
1329 ep.v1 = ep.t3 + ep.v2; // 5u_1 + 7u_2 + u_3 + 3u_4
1330 ep.v3 = ep.t2 + ep.v4; // u_1 + 3u_2 + 5u_3 + 7u_4
1331
1332 ep.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_EXTERNAL) * domainSep;
1333 evals[20] = evals[20] + ep.q_pos_by_scaling * (ep.v1 - wire(p, WIRE.W_L_SHIFT));
1334
1335 evals[21] = evals[21] + ep.q_pos_by_scaling * (ep.v2 - wire(p, WIRE.W_R_SHIFT));
1336
1337 evals[22] = evals[22] + ep.q_pos_by_scaling * (ep.v3 - wire(p, WIRE.W_O_SHIFT));
1338
1339 evals[23] = evals[23] + ep.q_pos_by_scaling * (ep.v4 - wire(p, WIRE.W_4_SHIFT));
1340 }
1341
1342 function accumulatePoseidonInternalRelation(
1343 Fr[NUMBER_OF_ENTITIES] memory p,
1344 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1345 Fr domainSep
1346 ) internal pure {
1347 PoseidonInternalParams memory ip;
1348
1349 Fr[4] memory INTERNAL_MATRIX_DIAGONAL = [
1350 FrLib.from(0x10dc6e9c006ea38b04b1e03b4bd9490c0d03f98929ca1d7fb56821fd19d3b6e7),
1351 FrLib.from(0x0c28145b6a44df3e0149b3d0a30b3bb599df9756d4dd9b84a86b38cfb45a740b),
1352 FrLib.from(0x00544b8338791518b2c7645a50392798b21f75bb60e3596170067d00141cac15),
1353 FrLib.from(0x222c01175718386f2e2e82eb122789e352e105a3b8fa852613bc534433ee428b)
1354 ];
1355
1356 // add round constants
1357 ip.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1358
1359 // apply s-box round
1360 ip.u1 = ip.s1 * ip.s1 * ip.s1 * ip.s1 * ip.s1;
1361 ip.u2 = wire(p, WIRE.W_R);
1362 ip.u3 = wire(p, WIRE.W_O);
1363 ip.u4 = wire(p, WIRE.W_4);
1364
1365 // matrix mul with v = M_I * u 4 muls and 7 additions
1366 ip.u_sum = ip.u1 + ip.u2 + ip.u3 + ip.u4;
1367
1368 ip.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_INTERNAL) * domainSep;
1369
1370 ip.v1 = ip.u1 * INTERNAL_MATRIX_DIAGONAL[0] + ip.u_sum;
1371 evals[24] = evals[24] + ip.q_pos_by_scaling * (ip.v1 - wire(p, WIRE.W_L_SHIFT));
1372
1373 ip.v2 = ip.u2 * INTERNAL_MATRIX_DIAGONAL[1] + ip.u_sum;
1374 evals[25] = evals[25] + ip.q_pos_by_scaling * (ip.v2 - wire(p, WIRE.W_R_SHIFT));
1375
1376 ip.v3 = ip.u3 * INTERNAL_MATRIX_DIAGONAL[2] + ip.u_sum;
1377 evals[26] = evals[26] + ip.q_pos_by_scaling * (ip.v3 - wire(p, WIRE.W_O_SHIFT));
1378
1379 ip.v4 = ip.u4 * INTERNAL_MATRIX_DIAGONAL[3] + ip.u_sum;
1380 evals[27] = evals[27] + ip.q_pos_by_scaling * (ip.v4 - wire(p, WIRE.W_4_SHIFT));
1381 }
1382
1383 // Batch subrelation evaluations using precomputed powers of alpha
1384 // First subrelation is implicitly scaled by 1, subsequent ones use powers from the subrelationChallenges array
1385 function scaleAndBatchSubrelations(
1386 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations,
1387 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges
1388 ) internal pure returns (Fr accumulator) {
1389 accumulator = evaluations[0];
1390
1391 for (uint256 i = 1; i < NUMBER_OF_SUBRELATIONS; ++i) {
1392 accumulator = accumulator + evaluations[i] * subrelationChallenges[i - 1];
1393 }
1394 }
1395}
1396
1397library CommitmentSchemeLib {
1398 using FrLib for Fr;
1399
1400 // Avoid stack too deep
1401 struct ShpleminiIntermediates {
1402 Fr unshiftedScalar;
1403 Fr shiftedScalar;
1404 Fr unshiftedScalarNeg;
1405 Fr shiftedScalarNeg;
1406 // Scalar to be multiplied by [1]₁
1407 Fr constantTermAccumulator;
1408 // Accumulator for powers of rho
1409 Fr batchingChallenge;
1410 // Linear combination of multilinear (sumcheck) evaluations and powers of rho
1411 Fr batchedEvaluation;
1412 Fr[4] denominators;
1413 Fr[4] batchingScalars;
1414 // 1/(z - r^{2^i}) for i = 0, ..., logSize, dynamically updated
1415 Fr posInvertedDenominator;
1416 // 1/(z + r^{2^i}) for i = 0, ..., logSize, dynamically updated
1417 Fr negInvertedDenominator;
1418 // ν^{2i} * 1/(z - r^{2^i})
1419 Fr scalingFactorPos;
1420 // ν^{2i+1} * 1/(z + r^{2^i})
1421 Fr scalingFactorNeg;
1422 // Fold_i(r^{2^i}) reconstructed by Verifier
1423 Fr[] foldPosEvaluations;
1424 }
1425
1426 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., m-1
1427 function computeFoldPosEvaluations(
1428 Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckUChallenges,
1429 Fr batchedEvalAccumulator,
1430 Fr[CONST_PROOF_SIZE_LOG_N] memory geminiEvaluations,
1431 Fr[] memory geminiEvalChallengePowers,
1432 uint256 logSize
1433 ) internal view returns (Fr[] memory) {
1434 Fr[] memory foldPosEvaluations = new Fr[](logSize);
1435 for (uint256 i = logSize; i > 0; --i) {
1436 Fr challengePower = geminiEvalChallengePowers[i - 1];
1437 Fr u = sumcheckUChallenges[i - 1];
1438
1439 Fr batchedEvalRoundAcc = ((challengePower * batchedEvalAccumulator * Fr.wrap(2)) - geminiEvaluations[i - 1]
1440 * (challengePower * (ONE - u) - u));
1441 // Divide by the denominator
1442 batchedEvalRoundAcc = batchedEvalRoundAcc * (challengePower * (ONE - u) + u).invert();
1443
1444 batchedEvalAccumulator = batchedEvalRoundAcc;
1445 foldPosEvaluations[i - 1] = batchedEvalRoundAcc;
1446 }
1447 return foldPosEvaluations;
1448 }
1449
1450 function computeSquares(Fr r, uint256 logN) internal pure returns (Fr[] memory) {
1451 Fr[] memory squares = new Fr[](logN);
1452 squares[0] = r;
1453 for (uint256 i = 1; i < logN; ++i) {
1454 squares[i] = squares[i - 1].sqr();
1455 }
1456 return squares;
1457 }
1458}
1459
1460uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
1461
1462// Fr utility
1463
1464function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
1465 scalar = FrLib.fromBytes32(bytes32(proofSection));
1466}
1467
1468// EC Point utilities
1469function bytesToG1Point(bytes calldata proofSection) pure returns (Honk.G1Point memory point) {
1470 uint256 x = uint256(bytes32(proofSection[0x00:0x20]));
1471 uint256 y = uint256(bytes32(proofSection[0x20:0x40]));
1472 require(x < Q && y < Q, Errors.ValueGeGroupOrder());
1473
1474 // Reject the point at infinity (0,0). EVM precompiles silently treat (0,0)
1475 // as the identity element, which could zero out commitments.
1476 // On-curve validation (y² = x³ + 3) is handled by the ecAdd/ecMul precompiles
1477 // per EIP-196, so we only need to catch this special case here.
1478 require((x | y) != 0, Errors.PointAtInfinity());
1479
1480 point = Honk.G1Point({x: x, y: y});
1481}
1482
1483function negateInplace(Honk.G1Point memory point) pure returns (Honk.G1Point memory) {
1484 // When y == 0 (order-2 point), negation is the same point. Q - 0 = Q which is >= Q.
1485 if (point.y != 0) {
1486 point.y = Q - point.y;
1487 }
1488 return point;
1489}
1490
1504function convertPairingPointsToG1(Fr[PAIRING_POINTS_SIZE] memory pairingPoints)
1505 pure
1506 returns (Honk.G1Point memory lhs, Honk.G1Point memory rhs)
1507{
1508 // P0 (lhs): x = lo | (hi << 136)
1509 uint256 lhsX = Fr.unwrap(pairingPoints[0]);
1510 lhsX |= Fr.unwrap(pairingPoints[1]) << 136;
1511
1512 uint256 lhsY = Fr.unwrap(pairingPoints[2]);
1513 lhsY |= Fr.unwrap(pairingPoints[3]) << 136;
1514
1515 // P1 (rhs): x = lo | (hi << 136)
1516 uint256 rhsX = Fr.unwrap(pairingPoints[4]);
1517 rhsX |= Fr.unwrap(pairingPoints[5]) << 136;
1518
1519 uint256 rhsY = Fr.unwrap(pairingPoints[6]);
1520 rhsY |= Fr.unwrap(pairingPoints[7]) << 136;
1521
1522 // Reconstructed coordinates must be < Q to prevent malleability.
1523 // Without this, two different limb encodings could map to the same curve point
1524 // (via mulmod reduction in on-curve checks) but produce different transcript hashes.
1525 require(lhsX < Q && lhsY < Q && rhsX < Q && rhsY < Q, Errors.ValueGeGroupOrder());
1526
1527 lhs.x = lhsX;
1528 lhs.y = lhsY;
1529 rhs.x = rhsX;
1530 rhs.y = rhsY;
1531}
1532
1541function generateRecursionSeparator(
1542 Fr[PAIRING_POINTS_SIZE] memory proofPairingPoints,
1543 Honk.G1Point memory accLhs,
1544 Honk.G1Point memory accRhs
1545) pure returns (Fr recursionSeparator) {
1546 // hash the proof aggregated X
1547 // hash the proof aggregated Y
1548 // hash the accum X
1549 // hash the accum Y
1550
1551 (Honk.G1Point memory proofLhs, Honk.G1Point memory proofRhs) = convertPairingPointsToG1(proofPairingPoints);
1552
1553 uint256[8] memory recursionSeparatorElements;
1554
1555 // Proof points
1556 recursionSeparatorElements[0] = proofLhs.x;
1557 recursionSeparatorElements[1] = proofLhs.y;
1558 recursionSeparatorElements[2] = proofRhs.x;
1559 recursionSeparatorElements[3] = proofRhs.y;
1560
1561 // Accumulator points
1562 recursionSeparatorElements[4] = accLhs.x;
1563 recursionSeparatorElements[5] = accLhs.y;
1564 recursionSeparatorElements[6] = accRhs.x;
1565 recursionSeparatorElements[7] = accRhs.y;
1566
1567 recursionSeparator = FrLib.from(uint256(keccak256(abi.encodePacked(recursionSeparatorElements))) % P);
1568}
1569
1579function mulWithSeperator(Honk.G1Point memory basePoint, Honk.G1Point memory other, Fr recursionSeperator)
1580 view
1581 returns (Honk.G1Point memory)
1582{
1583 Honk.G1Point memory result;
1584
1585 result = ecMul(recursionSeperator, basePoint);
1586 result = ecAdd(result, other);
1587
1588 return result;
1589}
1590
1599function ecMul(Fr value, Honk.G1Point memory point) view returns (Honk.G1Point memory) {
1600 Honk.G1Point memory result;
1601
1602 assembly {
1603 let free := mload(0x40)
1604 // Write the point into memory (two 32 byte words)
1605 // Memory layout:
1606 // Address | value
1607 // free | point.x
1608 // free + 0x20| point.y
1609 mstore(free, mload(point))
1610 mstore(add(free, 0x20), mload(add(point, 0x20)))
1611 // Write the scalar into memory (one 32 byte word)
1612 // Memory layout:
1613 // Address | value
1614 // free + 0x40| value
1615 mstore(add(free, 0x40), value)
1616
1617 // Call the ecMul precompile, it takes in the following
1618 // [point.x, point.y, scalar], and returns the result back into the free memory location.
1619 let success := staticcall(gas(), 0x07, free, 0x60, free, 0x40)
1620 if iszero(success) {
1621 revert(0, 0)
1622 }
1623 // Copy the result of the multiplication back into the result memory location.
1624 // Memory layout:
1625 // Address | value
1626 // result | result.x
1627 // result + 0x20| result.y
1628 mstore(result, mload(free))
1629 mstore(add(result, 0x20), mload(add(free, 0x20)))
1630
1631 mstore(0x40, add(free, 0x60))
1632 }
1633
1634 return result;
1635}
1636
1645function ecAdd(Honk.G1Point memory lhs, Honk.G1Point memory rhs) view returns (Honk.G1Point memory) {
1646 Honk.G1Point memory result;
1647
1648 assembly {
1649 let free := mload(0x40)
1650 // Write lhs into memory (two 32 byte words)
1651 // Memory layout:
1652 // Address | value
1653 // free | lhs.x
1654 // free + 0x20| lhs.y
1655 mstore(free, mload(lhs))
1656 mstore(add(free, 0x20), mload(add(lhs, 0x20)))
1657
1658 // Write rhs into memory (two 32 byte words)
1659 // Memory layout:
1660 // Address | value
1661 // free + 0x40| rhs.x
1662 // free + 0x60| rhs.y
1663 mstore(add(free, 0x40), mload(rhs))
1664 mstore(add(free, 0x60), mload(add(rhs, 0x20)))
1665
1666 // Call the ecAdd precompile, it takes in the following
1667 // [lhs.x, lhs.y, rhs.x, rhs.y], and returns their addition back into the free memory location.
1668 let success := staticcall(gas(), 0x06, free, 0x80, free, 0x40)
1669 if iszero(success) { revert(0, 0) }
1670
1671 // Copy the result of the addition back into the result memory location.
1672 // Memory layout:
1673 // Address | value
1674 // result | result.x
1675 // result + 0x20| result.y
1676 mstore(result, mload(free))
1677 mstore(add(result, 0x20), mload(add(free, 0x20)))
1678
1679 mstore(0x40, add(free, 0x80))
1680 }
1681
1682 return result;
1683}
1684
1685function rejectPointAtInfinity(Honk.G1Point memory point) pure {
1686 require((point.x | point.y) != 0, Errors.PointAtInfinity());
1687}
1688
1693function arePairingPointsDefault(Fr[PAIRING_POINTS_SIZE] memory pairingPoints) pure returns (bool) {
1694 uint256 acc = 0;
1695 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1696 acc |= Fr.unwrap(pairingPoints[i]);
1697 }
1698 return acc == 0;
1699}
1700
1701function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool decodedResult) {
1702 bytes memory input = abi.encodePacked(
1703 rhs.x,
1704 rhs.y,
1705 // Fixed G2 point
1706 uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
1707 uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
1708 uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
1709 uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
1710 lhs.x,
1711 lhs.y,
1712 // G2 point from VK
1713 uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
1714 uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
1715 uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
1716 uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
1717 );
1718
1719 (bool success, bytes memory result) = address(0x08).staticcall(input);
1720 decodedResult = success && abi.decode(result, (bool));
1721}
1722
1723abstract contract BaseHonkVerifier is IVerifier {
1724 using FrLib for Fr;
1725
1726 // Constants for proof length calculation (matching UltraKeccakFlavor)
1727 uint256 internal constant NUM_WITNESS_ENTITIES = 8;
1728 uint256 internal constant NUM_ELEMENTS_COMM = 2; // uint256 elements for curve points
1729 uint256 internal constant NUM_ELEMENTS_FR = 1; // uint256 elements for field elements
1730
1731 // Number of field elements in a ultra keccak honk proof for log_n = 25, including pairing point object.
1732 uint256 internal constant PROOF_SIZE = 350; // Legacy constant - will be replaced by calculateProofSize($LOG_N)
1733 uint256 internal constant SHIFTED_COMMITMENTS_START = 29;
1734
1735 uint256 internal constant PERMUTATION_ARGUMENT_VALUE_SEPARATOR = 1 << 28;
1736
1737 uint256 internal immutable $N;
1738 uint256 internal immutable $LOG_N;
1739 uint256 internal immutable $VK_HASH;
1740 uint256 internal immutable $NUM_PUBLIC_INPUTS;
1741
1742 constructor(uint256 _N, uint256 _logN, uint256 _vkHash, uint256 _numPublicInputs) {
1743 $N = _N;
1744 $LOG_N = _logN;
1745 $VK_HASH = _vkHash;
1746 $NUM_PUBLIC_INPUTS = _numPublicInputs;
1747 }
1748
1749 function verify(bytes calldata proof, bytes32[] calldata publicInputs) public view override returns (bool) {
1750 // Calculate expected proof size based on $LOG_N
1751 uint256 expectedProofSize = calculateProofSize($LOG_N);
1752
1753 // Check the received proof is the expected size where each field element is 32 bytes
1754 if (proof.length != expectedProofSize * 32) {
1755 revert Errors.ProofLengthWrongWithLogN($LOG_N, proof.length, expectedProofSize * 32);
1756 }
1757
1758 Honk.VerificationKey memory vk = loadVerificationKey();
1759 Honk.Proof memory p = TranscriptLib.loadProof(proof, $LOG_N);
1760 if (publicInputs.length != vk.publicInputsSize - PAIRING_POINTS_SIZE) {
1761 revert Errors.PublicInputsLengthWrong();
1762 }
1763
1764 // Generate the fiat shamir challenges for the whole protocol
1765 Transcript memory t = TranscriptLib.generateTranscript(p, publicInputs, $VK_HASH, $NUM_PUBLIC_INPUTS, $LOG_N);
1766
1767 // Derive public input delta
1768 t.relationParameters.publicInputsDelta = computePublicInputDelta(
1769 publicInputs,
1770 p.pairingPointObject,
1771 t.relationParameters.beta,
1772 t.relationParameters.gamma, /*pubInputsOffset=*/
1773 1
1774 );
1775
1776 // Sumcheck
1777 bool sumcheckVerified = verifySumcheck(p, t);
1778 if (!sumcheckVerified) revert Errors.SumcheckFailed();
1779
1780 bool shpleminiVerified = verifyShplemini(p, vk, t);
1781 if (!shpleminiVerified) revert Errors.ShpleminiFailed();
1782
1783 return sumcheckVerified && shpleminiVerified; // Boolean condition not required - nice for vanity :)
1784 }
1785
1786 function computePublicInputDelta(
1787 bytes32[] memory publicInputs,
1788 Fr[PAIRING_POINTS_SIZE] memory pairingPointObject,
1789 Fr beta,
1790 Fr gamma,
1791 uint256 offset
1792 ) internal view returns (Fr publicInputDelta) {
1793 Fr numerator = ONE;
1794 Fr denominator = ONE;
1795
1796 Fr numeratorAcc = gamma + (beta * FrLib.from(PERMUTATION_ARGUMENT_VALUE_SEPARATOR + offset));
1797 Fr denominatorAcc = gamma - (beta * FrLib.from(offset + 1));
1798
1799 {
1800 for (uint256 i = 0; i < $NUM_PUBLIC_INPUTS - PAIRING_POINTS_SIZE; i++) {
1801 Fr pubInput = FrLib.fromBytes32(publicInputs[i]);
1802
1803 numerator = numerator * (numeratorAcc + pubInput);
1804 denominator = denominator * (denominatorAcc + pubInput);
1805
1806 numeratorAcc = numeratorAcc + beta;
1807 denominatorAcc = denominatorAcc - beta;
1808 }
1809
1810 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1811 Fr pubInput = pairingPointObject[i];
1812
1813 numerator = numerator * (numeratorAcc + pubInput);
1814 denominator = denominator * (denominatorAcc + pubInput);
1815
1816 numeratorAcc = numeratorAcc + beta;
1817 denominatorAcc = denominatorAcc - beta;
1818 }
1819 }
1820
1821 publicInputDelta = FrLib.div(numerator, denominator);
1822 }
1823
1824 function verifySumcheck(Honk.Proof memory proof, Transcript memory tp) internal view returns (bool verified) {
1825 Fr roundTarget;
1826 Fr powPartialEvaluation = ONE;
1827
1828 // We perform sumcheck reductions over log n rounds ( the multivariate degree )
1829 for (uint256 round = 0; round < $LOG_N; ++round) {
1830 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate = proof.sumcheckUnivariates[round];
1831 bool valid = checkSum(roundUnivariate, roundTarget);
1832 if (!valid) revert Errors.SumcheckFailed();
1833
1834 Fr roundChallenge = tp.sumCheckUChallenges[round];
1835
1836 // Update the round target for the next rounf
1837 roundTarget = computeNextTargetSum(roundUnivariate, roundChallenge);
1838 powPartialEvaluation = partiallyEvaluatePOW(tp.gateChallenges[round], powPartialEvaluation, roundChallenge);
1839 }
1840
1841 // Last round
1842 Fr grandHonkRelationSum = RelationsLib.accumulateRelationEvaluations(
1843 proof.sumcheckEvaluations, tp.relationParameters, tp.alphas, powPartialEvaluation
1844 );
1845 verified = (grandHonkRelationSum == roundTarget);
1846 }
1847
1848 // Return the new target sum for the next sumcheck round
1849 function computeNextTargetSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariates, Fr roundChallenge)
1850 internal
1851 view
1852 returns (Fr targetSum)
1853 {
1854 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory BARYCENTRIC_LAGRANGE_DENOMINATORS = [
1855 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51),
1856 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000002d0),
1857 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff11),
1858 Fr.wrap(0x0000000000000000000000000000000000000000000000000000000000000090),
1859 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff71),
1860 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000000f0),
1861 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffd31),
1862 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000013b0)
1863 ];
1864 // To compute the next target sum, we evaluate the given univariate at a point u (challenge).
1865
1866 // Performing Barycentric evaluations
1867 // Compute B(x)
1868 Fr numeratorValue = ONE;
1869 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1870 numeratorValue = numeratorValue * (roundChallenge - Fr.wrap(i));
1871 }
1872
1873 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory denominatorInverses;
1874 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1875 Fr inv = BARYCENTRIC_LAGRANGE_DENOMINATORS[i];
1876 inv = inv * (roundChallenge - Fr.wrap(i));
1877 inv = FrLib.invert(inv);
1878 denominatorInverses[i] = inv;
1879 }
1880
1881 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1882 Fr term = roundUnivariates[i];
1883 term = term * denominatorInverses[i];
1884 targetSum = targetSum + term;
1885 }
1886
1887 // Scale the sum by the value of B(x)
1888 targetSum = targetSum * numeratorValue;
1889 }
1890
1891 function verifyShplemini(Honk.Proof memory proof, Honk.VerificationKey memory vk, Transcript memory tp)
1892 internal
1893 view
1894 returns (bool verified)
1895 {
1896 CommitmentSchemeLib.ShpleminiIntermediates memory mem; // stack
1897
1898 // - Compute vector (r, r², ... , r²⁽ⁿ⁻¹⁾), where n = log_circuit_size
1899 Fr[] memory powers_of_evaluation_challenge = CommitmentSchemeLib.computeSquares(tp.geminiR, $LOG_N);
1900
1901 // Arrays hold values that will be linearly combined for the gemini and shplonk batch openings
1902 Fr[] memory scalars = new Fr[](NUMBER_UNSHIFTED + $LOG_N + 2);
1903 Honk.G1Point[] memory commitments = new Honk.G1Point[](NUMBER_UNSHIFTED + $LOG_N + 2);
1904
1905 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[0]).invert();
1906 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[0]).invert();
1907
1908 mem.unshiftedScalar = mem.posInvertedDenominator + (tp.shplonkNu * mem.negInvertedDenominator);
1909 mem.shiftedScalar =
1910 tp.geminiR.invert() * (mem.posInvertedDenominator - (tp.shplonkNu * mem.negInvertedDenominator));
1911
1912 scalars[0] = ONE;
1913 commitments[0] = proof.shplonkQ;
1914
1915 /* Batch multivariate opening claims, shifted and unshifted
1916 * The vector of scalars is populated as follows:
1917 * \f[
1918 * \left(
1919 * - \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
1920 * \ldots,
1921 * - \rho^{i+k-1} \times \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
1922 * - \rho^{i+k} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right),
1923 * \ldots,
1924 * - \rho^{k+m-1} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right)
1925 * \right)
1926 * \f]
1927 *
1928 * The following vector is concatenated to the vector of commitments:
1929 * \f[
1930 * f_0, \ldots, f_{m-1}, f_{\text{shift}, 0}, \ldots, f_{\text{shift}, k-1}
1931 * \f]
1932 *
1933 * Simultaneously, the evaluation of the multilinear polynomial
1934 * \f[
1935 * \sum \rho^i \cdot f_i + \sum \rho^{i+k} \cdot f_{\text{shift}, i}
1936 * \f]
1937 * at the challenge point \f$ (u_0,\ldots, u_{n-1}) \f$ is computed.
1938 *
1939 * This approach minimizes the number of iterations over the commitments to multilinear polynomials
1940 * and eliminates the need to store the powers of \f$ \rho \f$.
1941 */
1942 mem.batchingChallenge = ONE;
1943 mem.batchedEvaluation = ZERO;
1944
1945 mem.unshiftedScalarNeg = mem.unshiftedScalar.neg();
1946 mem.shiftedScalarNeg = mem.shiftedScalar.neg();
1947 for (uint256 i = 1; i <= NUMBER_UNSHIFTED; ++i) {
1948 scalars[i] = mem.unshiftedScalarNeg * mem.batchingChallenge;
1949 mem.batchedEvaluation = mem.batchedEvaluation + (proof.sumcheckEvaluations[i - 1] * mem.batchingChallenge);
1950 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
1951 }
1952 // g commitments are accumulated at r
1953 // For each of the to be shifted commitments perform the shift in place by
1954 // adding to the unshifted value.
1955 // We do so, as the values are to be used in batchMul later, and as
1956 // `a * c + b * c = (a + b) * c` this will allow us to reduce memory and compute.
1957 // Applied to w1, w2, w3, w4 and zPerm
1958 for (uint256 i = 0; i < NUMBER_TO_BE_SHIFTED; ++i) {
1959 uint256 scalarOff = i + SHIFTED_COMMITMENTS_START;
1960 uint256 evaluationOff = i + NUMBER_UNSHIFTED;
1961
1962 scalars[scalarOff] = scalars[scalarOff] + (mem.shiftedScalarNeg * mem.batchingChallenge);
1963 mem.batchedEvaluation =
1964 mem.batchedEvaluation + (proof.sumcheckEvaluations[evaluationOff] * mem.batchingChallenge);
1965 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
1966 }
1967
1968 commitments[1] = vk.qm;
1969 commitments[2] = vk.qc;
1970 commitments[3] = vk.ql;
1971 commitments[4] = vk.qr;
1972 commitments[5] = vk.qo;
1973 commitments[6] = vk.q4;
1974 commitments[7] = vk.qLookup;
1975 commitments[8] = vk.qArith;
1976 commitments[9] = vk.qDeltaRange;
1977 commitments[10] = vk.qElliptic;
1978 commitments[11] = vk.qMemory;
1979 commitments[12] = vk.qNnf;
1980 commitments[13] = vk.qPoseidon2External;
1981 commitments[14] = vk.qPoseidon2Internal;
1982 commitments[15] = vk.s1;
1983 commitments[16] = vk.s2;
1984 commitments[17] = vk.s3;
1985 commitments[18] = vk.s4;
1986 commitments[19] = vk.id1;
1987 commitments[20] = vk.id2;
1988 commitments[21] = vk.id3;
1989 commitments[22] = vk.id4;
1990 commitments[23] = vk.t1;
1991 commitments[24] = vk.t2;
1992 commitments[25] = vk.t3;
1993 commitments[26] = vk.t4;
1994 commitments[27] = vk.lagrangeFirst;
1995 commitments[28] = vk.lagrangeLast;
1996
1997 // Accumulate proof points
1998 commitments[29] = proof.w1;
1999 commitments[30] = proof.w2;
2000 commitments[31] = proof.w3;
2001 commitments[32] = proof.w4;
2002 commitments[33] = proof.zPerm;
2003 commitments[34] = proof.lookupInverses;
2004 commitments[35] = proof.lookupReadCounts;
2005 commitments[36] = proof.lookupReadTags;
2006
2007 /* Batch gemini claims from the prover
2008 * place the commitments to gemini aᵢ to the vector of commitments, compute the contributions from
2009 * aᵢ(−r²ⁱ) for i=1, … , n−1 to the constant term accumulator, add corresponding scalars
2010 *
2011 * 1. Moves the vector
2012 * \f[
2013 * \left( \text{com}(A_1), \text{com}(A_2), \ldots, \text{com}(A_{n-1}) \right)
2014 * \f]
2015 * to the 'commitments' vector.
2016 *
2017 * 2. Computes the scalars:
2018 * \f[
2019 * \frac{\nu^{2}}{z + r^2}, \frac{\nu^3}{z + r^4}, \ldots, \frac{\nu^{n-1}}{z + r^{2^{n-1}}}
2020 * \f]
2021 * and places them into the 'scalars' vector.
2022 *
2023 * 3. Accumulates the summands of the constant term:
2024 * \f[
2025 * \sum_{i=2}^{n-1} \frac{\nu^{i} \cdot A_i(-r^{2^i})}{z + r^{2^i}}
2026 * \f]
2027 * and adds them to the 'constant_term_accumulator'.
2028 */
2029
2030 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., $LOG_N - 1
2031 Fr[] memory foldPosEvaluations = CommitmentSchemeLib.computeFoldPosEvaluations(
2032 tp.sumCheckUChallenges,
2033 mem.batchedEvaluation,
2034 proof.geminiAEvaluations,
2035 powers_of_evaluation_challenge,
2036 $LOG_N
2037 );
2038
2039 // Compute the Shplonk constant term contributions from A₀(±r)
2040 mem.constantTermAccumulator = foldPosEvaluations[0] * mem.posInvertedDenominator;
2041 mem.constantTermAccumulator =
2042 mem.constantTermAccumulator + (proof.geminiAEvaluations[0] * tp.shplonkNu * mem.negInvertedDenominator);
2043
2044 mem.batchingChallenge = tp.shplonkNu.sqr();
2045
2046 // Compute Shplonk constant term contributions from Aₗ(± r^{2ˡ}) for l = 1, ..., m-1;
2047 // Compute scalar multipliers for each fold commitment
2048 for (uint256 i = 0; i < $LOG_N - 1; ++i) {
2049 // Update inverted denominators
2050 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[i + 1]).invert();
2051 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[i + 1]).invert();
2052
2053 // Compute the scalar multipliers for Aₗ(± r^{2ˡ}) and [Aₗ]
2054 mem.scalingFactorPos = mem.batchingChallenge * mem.posInvertedDenominator;
2055 mem.scalingFactorNeg = mem.batchingChallenge * tp.shplonkNu * mem.negInvertedDenominator;
2056 // [Aₗ] is multiplied by -v^{2l}/(z-r^{2^l}) - v^{2l+1} /(z+ r^{2^l})
2057 scalars[NUMBER_UNSHIFTED + 1 + i] = mem.scalingFactorNeg.neg() + mem.scalingFactorPos.neg();
2058
2059 // Accumulate the const term contribution given by
2060 // v^{2l} * Aₗ(r^{2ˡ}) /(z-r^{2^l}) + v^{2l+1} * Aₗ(-r^{2ˡ}) /(z+ r^{2^l})
2061 Fr accumContribution = mem.scalingFactorNeg * proof.geminiAEvaluations[i + 1];
2062
2063 accumContribution = accumContribution + mem.scalingFactorPos * foldPosEvaluations[i + 1];
2064 mem.constantTermAccumulator = mem.constantTermAccumulator + accumContribution;
2065 // Update the running power of v
2066 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu * tp.shplonkNu;
2067
2068 commitments[NUMBER_UNSHIFTED + 1 + i] = proof.geminiFoldComms[i];
2069 }
2070
2071 // Finalize the batch opening claim
2072 commitments[NUMBER_UNSHIFTED + $LOG_N] = Honk.G1Point({x: 1, y: 2});
2073 scalars[NUMBER_UNSHIFTED + $LOG_N] = mem.constantTermAccumulator;
2074
2075 Honk.G1Point memory quotient_commitment = proof.kzgQuotient;
2076
2077 commitments[NUMBER_UNSHIFTED + $LOG_N + 1] = quotient_commitment;
2078 scalars[NUMBER_UNSHIFTED + $LOG_N + 1] = tp.shplonkZ; // evaluation challenge
2079
2080 Honk.G1Point memory P_0_agg = batchMul(commitments, scalars);
2081 Honk.G1Point memory P_1_agg = negateInplace(quotient_commitment);
2082
2083 // Aggregate pairing points (skip if default/infinity — no recursive verification occurred)
2084 if (!arePairingPointsDefault(proof.pairingPointObject)) {
2085 Fr recursionSeparator = generateRecursionSeparator(proof.pairingPointObject, P_0_agg, P_1_agg);
2086 (Honk.G1Point memory P_0_other, Honk.G1Point memory P_1_other) =
2087 convertPairingPointsToG1(proof.pairingPointObject);
2088
2089 // Validate the points from the proof are on the curve
2090 rejectPointAtInfinity(P_0_other);
2091 rejectPointAtInfinity(P_1_other);
2092
2093 // accumulate with aggregate points in proof
2094 P_0_agg = mulWithSeperator(P_0_agg, P_0_other, recursionSeparator);
2095 P_1_agg = mulWithSeperator(P_1_agg, P_1_other, recursionSeparator);
2096 }
2097
2098 return pairing(P_0_agg, P_1_agg);
2099 }
2100
2101 function batchMul(Honk.G1Point[] memory base, Fr[] memory scalars)
2102 internal
2103 view
2104 returns (Honk.G1Point memory result)
2105 {
2106 uint256 limit = NUMBER_UNSHIFTED + $LOG_N + 2;
2107
2108 // Validate all points are on the curve
2109 for (uint256 i = 0; i < limit; ++i) {
2110 rejectPointAtInfinity(base[i]);
2111 }
2112
2113 bool success = true;
2114 assembly {
2115 let free := mload(0x40)
2116
2117 let count := 0x01
2118 for {} lt(count, add(limit, 1)) { count := add(count, 1) } {
2119 // Get loop offsets
2120 let base_base := add(base, mul(count, 0x20))
2121 let scalar_base := add(scalars, mul(count, 0x20))
2122
2123 mstore(add(free, 0x40), mload(mload(base_base)))
2124 mstore(add(free, 0x60), mload(add(0x20, mload(base_base))))
2125 // Add scalar
2126 mstore(add(free, 0x80), mload(scalar_base))
2127
2128 success := and(success, staticcall(gas(), 7, add(free, 0x40), 0x60, add(free, 0x40), 0x40))
2129 // accumulator = accumulator + accumulator_2
2130 success := and(success, staticcall(gas(), 6, free, 0x80, free, 0x40))
2131 }
2132
2133 // Return the result
2134 mstore(result, mload(free))
2135 mstore(add(result, 0x20), mload(add(free, 0x20)))
2136 }
2137
2138 require(success, Errors.ShpleminiFailed());
2139 }
2140
2141 // Calculate proof size based on log_n (matching UltraKeccakFlavor formula)
2142 function calculateProofSize(uint256 logN) internal pure returns (uint256) {
2143 // Witness commitments
2144 uint256 proofLength = NUM_WITNESS_ENTITIES * NUM_ELEMENTS_COMM; // witness commitments
2145
2146 // Sumcheck
2147 proofLength += logN * BATCHED_RELATION_PARTIAL_LENGTH * NUM_ELEMENTS_FR; // sumcheck univariates
2148 proofLength += NUMBER_OF_ENTITIES * NUM_ELEMENTS_FR; // sumcheck evaluations
2149
2150 // Gemini
2151 proofLength += (logN - 1) * NUM_ELEMENTS_COMM; // Gemini Fold commitments
2152 proofLength += logN * NUM_ELEMENTS_FR; // Gemini evaluations
2153
2154 // Shplonk and KZG commitments
2155 proofLength += NUM_ELEMENTS_COMM * 2; // Shplonk Q and KZG W commitments
2156
2157 // Pairing points
2158 proofLength += PAIRING_POINTS_SIZE; // pairing inputs carried on public inputs
2159
2160 return proofLength;
2161 }
2162
2163 function checkSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate, Fr roundTarget)
2164 internal
2165 pure
2166 returns (bool checked)
2167 {
2168 Fr totalSum = roundUnivariate[0] + roundUnivariate[1];
2169 checked = totalSum == roundTarget;
2170 }
2171
2172 // Univariate evaluation of the monomial ((1-X_l) + X_l.B_l) at the challenge point X_l=u_l
2173 function partiallyEvaluatePOW(Fr gateChallenge, Fr currentEvaluation, Fr roundChallenge)
2174 internal
2175 pure
2176 returns (Fr newEvaluation)
2177 {
2178 Fr univariateEval = ONE + (roundChallenge * (gateChallenge - ONE));
2179 newEvaluation = currentEvaluation * univariateEval;
2180 }
2181
2182 function loadVerificationKey() internal pure virtual returns (Honk.VerificationKey memory);
2183}
2184
2185contract HonkVerifier is BaseHonkVerifier(N, LOG_N, VK_HASH, NUMBER_OF_PUBLIC_INPUTS) {
2186 function loadVerificationKey() internal pure override returns (Honk.VerificationKey memory) {
2187 return HonkVerificationKey.loadVerificationKey();
2188 }
2189}
2190)";
2191
2192inline std::string get_honk_solidity_verifier(auto const& verification_key)
2193{
2194 std::ostringstream stream;
2195 output_vk_sol_ultra_honk(stream, verification_key, "HonkVerificationKey");
2196 return stream.str() + HONK_CONTRACT_SOURCE;
2197}
std::string get_honk_solidity_verifier(auto const &verification_key)
void output_vk_sol_ultra_honk(std::ostream &os, auto const &key, std::string const &class_name, bool include_types_import=false)