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