14static const char HONK_CONTRACT_SOURCE[] = R
"(
15pragma solidity ^0.8.27;
18 function verify(bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool);
26 error ValueGeLimbMax();
27 error ValueGeGroupOrder();
28 error ValueGeFieldOrder();
31 error NotPowerOfTwo();
34 error ProofLengthWrong();
35 error ProofLengthWrongWithLogN(uint256 logN, uint256 actualLength, uint256 expectedLength);
36 error PublicInputsLengthWrong();
37 error SumcheckFailed();
38 error ShpleminiFailed();
40 error PointAtInfinity();
42 error ConsistencyCheckFailed();
43 error GeminiChallengeInSubgroup();
48using {add as +} for Fr global;
49using {sub as -} for Fr global;
50using {mul as *} for Fr global;
52using {notEqual as !=} for Fr global;
53using {equal as ==} for Fr global;
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);
66 bytes4 internal constant FRLIB_MODEXP_FAILED_SELECTOR = 0xf8d61709;
68 function invert(Fr value) internal view returns (Fr) {
69 uint256 v = Fr.unwrap(value);
70 require(v != 0, Errors.InvertOfZero());
74 // Call the modexp precompile to invert in the field
76 let free := mload(0x40)
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)
85 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
89 mstore(0x40, add(free, 0xc0))
92 return Fr.wrap(result);
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());
101 // Call the modexp precompile to invert in the field
103 let free := mload(0x40)
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)
112 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
115 result := mload(0x00)
116 mstore(0x40, add(free, 0xc0))
119 return Fr.wrap(result);
122 function div(Fr numerator, Fr denominator) internal view returns (Fr) {
124 return numerator * invert(denominator);
128 function sqr(Fr value) internal pure returns (Fr) {
130 return value * value;
134 function unwrap(Fr value) internal pure returns (uint256) {
136 return Fr.unwrap(value);
140 function neg(Fr value) internal pure returns (Fr) {
142 return Fr.wrap(MODULUS - Fr.unwrap(value));
146 function from(uint256 value) internal pure returns (Fr) {
148 require(value < MODULUS, Errors.ValueGeFieldOrder());
149 return Fr.wrap(value);
153 function fromBytes32(bytes32 value) internal pure returns (Fr) {
155 uint256 v = uint256(value);
156 require(v < MODULUS, Errors.ValueGeFieldOrder());
161 function toBytes32(Fr value) internal pure returns (bytes32) {
163 return bytes32(Fr.unwrap(value));
169function add(Fr a, Fr b) pure returns (Fr) {
171 return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
175function mul(Fr a, Fr b) pure returns (Fr) {
177 return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
181function sub(Fr a, Fr b) pure returns (Fr) {
183 return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
187function notEqual(Fr a, Fr b) pure returns (bool) {
189 return Fr.unwrap(a) != Fr.unwrap(b);
193function equal(Fr a, Fr b) pure returns (bool) {
195 return Fr.unwrap(a) == Fr.unwrap(b);
199uint256 constant CONST_PROOF_SIZE_LOG_N = 28;
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;
213uint256 constant FIELD_ELEMENT_SIZE = 0x20;
214uint256 constant GROUP_ELEMENT_SIZE = 0x40;
216// Powers of alpha used to batch subrelations (alpha, alpha^2, ..., alpha^(NUM_SUBRELATIONS-1))
217uint256 constant NUMBER_OF_ALPHAS = NUMBER_OF_SUBRELATIONS - 1;
233 Q_POSEIDON2_EXTERNAL,
234 Q_POSEIDON2_INTERNAL,
270 struct VerificationKey {
273 uint256 logCircuitSize;
274 uint256 publicInputsSize;
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;
300 // Precomputed lookup table
305 // Fixed first and last
306 G1Point lagrangeFirst;
307 G1Point lagrangeLast;
310 struct RelationParameters {
316 Fr publicInputsDelta;
320 // Pairing point object
321 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
327 // Lookup helpers - Permutations
329 // Lookup helpers - logup
330 G1Point lookupReadCounts;
331 G1Point lookupReadTags;
332 G1Point lookupInverses;
334 Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
335 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
337 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
338 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
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
354 // Commitments to logup witness polynomials
355 G1Point lookupReadCounts;
356 G1Point lookupReadTags;
357 G1Point lookupInverses;
358 // Commitment to grand permutation polynomial
360 G1Point[3] libraCommitments;
363 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
365 Fr[NUMBER_OF_ENTITIES_ZK] sumcheckEvaluations; // Includes gemini_masking_poly eval at index 0 (first position)
367 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
368 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
369 Fr[4] libraPolyEvals;
375// Transcript library to generate fiat shamir challenges
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;
382 Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
391library TranscriptLib {
392 function generateTranscript(
393 Honk.Proof memory proof,
394 bytes32[] calldata publicInputs,
396 uint256 publicInputsSize,
398 ) internal pure returns (Transcript memory t) {
399 Fr previousChallenge;
400 (t.relationParameters, previousChallenge) =
401 generateRelationParametersChallenges(proof, publicInputs, vkHash, publicInputsSize, previousChallenge);
403 (t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
405 (t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge, logN);
407 (t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge, logN);
409 (t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
411 (t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge, logN);
413 (t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge, logN);
415 (t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
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);
429 function generateRelationParametersChallenges(
430 Honk.Proof memory proof,
431 bytes32[] calldata publicInputs,
433 uint256 publicInputsSize,
435 ) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
436 (rp.eta, previousChallenge) = generateEtaChallenge(proof, publicInputs, vkHash, publicInputsSize);
438 (rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaGammaChallenges(previousChallenge, proof);
441 function generateEtaChallenge(
442 Honk.Proof memory proof,
443 bytes32[] calldata publicInputs,
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);
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];
454 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
455 round0[1 + publicInputsSize - PAIRING_POINTS_SIZE + i] = FrLib.toBytes32(proof.pairingPointObject[i]);
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);
467 previousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(round0))) % P);
468 (eta,) = splitChallenge(previousChallenge);
471 function generateBetaGammaChallenges(Fr previousChallenge, Honk.Proof memory proof)
474 returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
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);
485 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(round1))) % P);
486 (beta, gamma) = splitChallenge(nextPreviousChallenge);
489 // Alpha challenges non-linearise the gate contributions
490 function generateAlphaChallenges(Fr previousChallenge, Honk.Proof memory proof)
493 returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
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;
503 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(alpha0))) % P);
505 (alpha,) = splitChallenge(nextPreviousChallenge);
507 // Compute powers of alpha for batching subrelations
509 for (uint256 i = 1; i < NUMBER_OF_ALPHAS; i++) {
510 alphas[i] = alphas[i - 1] * alpha;
514 function generateGateChallenges(Fr previousChallenge, uint256 logN)
517 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
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];
524 nextPreviousChallenge = previousChallenge;
527 function generateSumcheckChallenges(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
530 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
532 for (uint256 i = 0; i < logN; i++) {
533 Fr[BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
534 univariateChal[0] = prevChallenge;
536 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
537 univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
539 prevChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(univariateChal))) % P);
541 (sumcheckChallenges[i], unused) = splitChallenge(prevChallenge);
543 nextPreviousChallenge = prevChallenge;
546 function generateRhoChallenge(Honk.Proof memory proof, Fr prevChallenge)
549 returns (Fr rho, Fr nextPreviousChallenge)
551 Fr[NUMBER_OF_ENTITIES + 1] memory rhoChallengeElements;
552 rhoChallengeElements[0] = prevChallenge;
554 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
555 rhoChallengeElements[i + 1] = proof.sumcheckEvaluations[i];
558 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(rhoChallengeElements))) % P);
560 (rho, unused) = splitChallenge(nextPreviousChallenge);
563 function generateGeminiRChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
566 returns (Fr geminiR, Fr nextPreviousChallenge)
568 uint256[] memory gR = new uint256[]((logN - 1) * 2 + 1);
569 gR[0] = Fr.unwrap(prevChallenge);
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;
576 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(gR))) % P);
578 (geminiR, unused) = splitChallenge(nextPreviousChallenge);
581 function generateShplonkNuChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
584 returns (Fr shplonkNu, Fr nextPreviousChallenge)
586 uint256[] memory shplonkNuChallengeElements = new uint256[](logN + 1);
587 shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
589 for (uint256 i = 0; i < logN; i++) {
590 shplonkNuChallengeElements[i + 1] = Fr.unwrap(proof.geminiAEvaluations[i]);
593 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkNuChallengeElements))) % P);
595 (shplonkNu, unused) = splitChallenge(nextPreviousChallenge);
598 function generateShplonkZChallenge(Honk.Proof memory proof, Fr prevChallenge)
601 returns (Fr shplonkZ, Fr nextPreviousChallenge)
603 uint256[3] memory shplonkZChallengeElements;
604 shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
606 shplonkZChallengeElements[1] = proof.shplonkQ.x;
607 shplonkZChallengeElements[2] = proof.shplonkQ.y;
609 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkZChallengeElements))) % P);
611 (shplonkZ, unused) = splitChallenge(nextPreviousChallenge);
614 function loadProof(bytes calldata proof, uint256 logN) internal pure returns (Honk.Proof memory p) {
615 uint256 boundary = 0x00;
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;
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;
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;
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;
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;
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;
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;
672 p.shplonkQ = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
673 boundary += GROUP_ELEMENT_SIZE;
675 p.kzgQuotient = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
679library RelationsLib {
680 struct EllipticParams {
688 // push accumulators into memory
689 Fr x_double_identity;
692 // Parameters used within the Memory Relation
693 // A struct is used to work around stack too deep. This relation has alot of variables
695 Fr memory_record_check;
696 Fr partial_record_check;
697 Fr next_gate_access_type;
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;
703 Fr next_gate_access_type_is_boolean;
704 Fr ROM_consistency_check_identity;
705 Fr RAM_consistency_check_identity;
707 Fr RAM_timestamp_check_identity;
709 Fr index_is_monotonically_increasing;
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
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;
724 struct PoseidonExternalParams {
744 struct PoseidonInternalParams {
758 Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
759 uint256 internal constant NEG_HALF_MODULO_P = 0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000;
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);
765 function accumulateRelationEvaluations(
766 Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
767 Honk.RelationParameters memory rp,
768 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges,
770 ) internal pure returns (Fr accumulator) {
771 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
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);
784 // batch the subrelations with the precomputed alpha powers to obtain the full honk relation
785 accumulator = scaleAndBatchSubrelations(evaluations, subrelationChallenges);
793 function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
794 return p[uint256(_wire)];
801 function accumulateArithmeticRelation(
802 Fr[NUMBER_OF_ENTITIES] memory p,
803 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
807 Fr q_arith = wire(p, WIRE.Q_ARITH);
809 Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
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;
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;
831 function accumulatePermutationRelation(
832 Fr[NUMBER_OF_ENTITIES] memory p,
833 Honk.RelationParameters memory rp,
834 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
837 Fr grand_product_numerator;
838 Fr grand_product_denominator;
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);
846 grand_product_numerator = num;
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);
854 grand_product_denominator = den;
859 Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
862 - ((wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
863 * grand_product_denominator);
864 acc = acc * domainSep;
870 Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
875 function accumulateLogDerivativeLookupRelation(
876 Fr[NUMBER_OF_ENTITIES] memory p,
877 Honk.RelationParameters memory rp,
878 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
884 // Calculate the write term (the table accumulation)
885 // table_term = table_1 + γ + table_2 * β + table_3 * β² + table_4 * β³
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);
892 // Calculate the read term
893 // lookup_term = derived_entry_1 + γ + derived_entry_2 * β + derived_entry_3 * β² + q_index * β³
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);
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);
904 Fr lookup_inverse = wire(p, WIRE.LOOKUP_INVERSES) * table_term;
905 Fr table_inverse = wire(p, WIRE.LOOKUP_INVERSES) * lookup_term;
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));
911 // Inverse calculated correctly relation
912 Fr accumulatorNone = lookup_term * table_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
913 accumulatorNone = accumulatorNone * domainSep;
916 Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * lookup_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * table_inverse;
918 Fr read_tag = wire(p, WIRE.LOOKUP_READ_TAGS);
920 Fr read_tag_boolean_relation = read_tag * read_tag - read_tag;
922 evals[4] = accumulatorNone;
923 evals[5] = accumulatorOne;
924 evals[6] = read_tag_boolean_relation * domainSep;
927 function accumulateDeltaRangeRelation(
928 Fr[NUMBER_OF_ENTITIES] memory p,
929 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
932 Fr minus_one = ZERO - ONE;
933 Fr minus_two = ZERO - Fr.wrap(2);
934 Fr minus_three = ZERO - Fr.wrap(3);
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);
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;
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;
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;
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;
987 function accumulateEllipticRelation(
988 Fr[NUMBER_OF_ENTITIES] memory p,
989 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
992 EllipticParams memory ep;
993 ep.x_1 = wire(p, WIRE.W_R);
994 ep.y_1 = wire(p, WIRE.W_O);
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);
1001 Fr q_sign = wire(p, WIRE.Q_L);
1002 Fr q_is_double = wire(p, WIRE.Q_M);
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);
1010 Fr partialEval = domainSep;
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;
1018 evals[11] = x_add_identity * partialEval * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
1021 // Contribution 11 point addition, x-coordinate check
1022 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
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);
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
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);
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;
1042 Fr acc = ep.x_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
1043 evals[11] = evals[11] + acc;
1046 // Contribution 11 point doubling, y-coordinate check
1047 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
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;
1055 function accumulateMemoryRelation(
1056 Fr[NUMBER_OF_ENTITIES] memory p,
1057 Honk.RelationParameters memory rp,
1058 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1061 MemParams memory ap;
1063 // Compute eta powers locally
1064 Fr eta_two = rp.eta * rp.eta;
1065 Fr eta_three = eta_two * rp.eta;
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);
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);
1134 ap.index_is_monotonically_increasing = ap.index_delta * (ap.index_delta - Fr.wrap(1)); // deg 2
1136 ap.adjacent_values_match_if_adjacent_indices_match = (ap.index_delta * MINUS_ONE + ONE) * ap.record_delta; // deg 2
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
1143 ap.ROM_consistency_check_identity = ap.memory_record_check * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R)); // deg 3 or 7
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
1167 // reverse order we could re-use `ap.partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta
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;
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
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
1183 ap.next_gate_access_type_is_boolean =
1184 ap.next_gate_access_type * ap.next_gate_access_type - ap.next_gate_access_type;
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
1192 ap.RAM_consistency_check_identity = ap.access_check * (wire(p, WIRE.Q_O)); // deg 3 or 9
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
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
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;
1224 function accumulateNnfRelation(
1225 Fr[NUMBER_OF_ENTITIES] memory p,
1226 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1229 NnfParams memory ap;
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);
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);
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);
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);
1266 // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm
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);
1279 // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm
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);
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
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;
1300 function accumulatePoseidonExternalRelation(
1301 Fr[NUMBER_OF_ENTITIES] memory p,
1302 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1305 PoseidonExternalParams memory ep;
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);
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
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));
1335 evals[21] = evals[21] + ep.q_pos_by_scaling * (ep.v2 - wire(p, WIRE.W_R_SHIFT));
1337 evals[22] = evals[22] + ep.q_pos_by_scaling * (ep.v3 - wire(p, WIRE.W_O_SHIFT));
1339 evals[23] = evals[23] + ep.q_pos_by_scaling * (ep.v4 - wire(p, WIRE.W_4_SHIFT));
1342 function accumulatePoseidonInternalRelation(
1343 Fr[NUMBER_OF_ENTITIES] memory p,
1344 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1347 PoseidonInternalParams memory ip;
1349 Fr[4] memory INTERNAL_MATRIX_DIAGONAL = [
1350 FrLib.from(0x10dc6e9c006ea38b04b1e03b4bd9490c0d03f98929ca1d7fb56821fd19d3b6e7),
1351 FrLib.from(0x0c28145b6a44df3e0149b3d0a30b3bb599df9756d4dd9b84a86b38cfb45a740b),
1352 FrLib.from(0x00544b8338791518b2c7645a50392798b21f75bb60e3596170067d00141cac15),
1353 FrLib.from(0x222c01175718386f2e2e82eb122789e352e105a3b8fa852613bc534433ee428b)
1356 // add round constants
1357 ip.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
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);
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;
1368 ip.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_INTERNAL) * domainSep;
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));
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));
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));
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));
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];
1391 for (uint256 i = 1; i < NUMBER_OF_SUBRELATIONS; ++i) {
1392 accumulator = accumulator + evaluations[i] * subrelationChallenges[i - 1];
1397library CommitmentSchemeLib {
1400 // Avoid stack too deep
1401 struct ShpleminiIntermediates {
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;
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;
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,
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];
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();
1444 batchedEvalAccumulator = batchedEvalRoundAcc;
1445 foldPosEvaluations[i - 1] = batchedEvalRoundAcc;
1447 return foldPosEvaluations;
1450 function computeSquares(Fr r, uint256 logN) internal pure returns (Fr[] memory) {
1451 Fr[] memory squares = new Fr[](logN);
1453 for (uint256 i = 1; i < logN; ++i) {
1454 squares[i] = squares[i - 1].sqr();
1460uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
1464function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
1465 scalar = FrLib.fromBytes32(bytes32(proofSection));
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());
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());
1480 point = Honk.G1Point({x: x, y: y});
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.
1486 point.y = Q - point.y;
1504function convertPairingPointsToG1(Fr[PAIRING_POINTS_SIZE] memory pairingPoints)
1506 returns (Honk.G1Point memory lhs, Honk.G1Point memory rhs)
1508 // P0 (lhs): x = lo | (hi << 136)
1509 uint256 lhsX = Fr.unwrap(pairingPoints[0]);
1510 lhsX |= Fr.unwrap(pairingPoints[1]) << 136;
1512 uint256 lhsY = Fr.unwrap(pairingPoints[2]);
1513 lhsY |= Fr.unwrap(pairingPoints[3]) << 136;
1515 // P1 (rhs): x = lo | (hi << 136)
1516 uint256 rhsX = Fr.unwrap(pairingPoints[4]);
1517 rhsX |= Fr.unwrap(pairingPoints[5]) << 136;
1519 uint256 rhsY = Fr.unwrap(pairingPoints[6]);
1520 rhsY |= Fr.unwrap(pairingPoints[7]) << 136;
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());
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
1551 (Honk.G1Point memory proofLhs, Honk.G1Point memory proofRhs) = convertPairingPointsToG1(proofPairingPoints);
1553 uint256[8] memory recursionSeparatorElements;
1556 recursionSeparatorElements[0] = proofLhs.x;
1557 recursionSeparatorElements[1] = proofLhs.y;
1558 recursionSeparatorElements[2] = proofRhs.x;
1559 recursionSeparatorElements[3] = proofRhs.y;
1561 // Accumulator points
1562 recursionSeparatorElements[4] = accLhs.x;
1563 recursionSeparatorElements[5] = accLhs.y;
1564 recursionSeparatorElements[6] = accRhs.x;
1565 recursionSeparatorElements[7] = accRhs.y;
1567 recursionSeparator = FrLib.from(uint256(keccak256(abi.encodePacked(recursionSeparatorElements))) % P);
1579function mulWithSeperator(Honk.G1Point memory basePoint, Honk.G1Point memory other, Fr recursionSeperator)
1581 returns (Honk.G1Point memory)
1583 Honk.G1Point memory result;
1585 result = ecMul(recursionSeperator, basePoint);
1586 result = ecAdd(result, other);
1599function ecMul(Fr value, Honk.G1Point memory point) view returns (Honk.G1Point memory) {
1600 Honk.G1Point memory result;
1603 let free := mload(0x40)
1604 // Write the point into memory (two 32 byte words)
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)
1614 // free + 0x40| value
1615 mstore(add(free, 0x40), value)
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) {
1623 // Copy the result of the multiplication back into the result memory location.
1626 // result | result.x
1627 // result + 0x20| result.y
1628 mstore(result, mload(free))
1629 mstore(add(result, 0x20), mload(add(free, 0x20)))
1631 mstore(0x40, add(free, 0x60))
1645function ecAdd(Honk.G1Point memory lhs, Honk.G1Point memory rhs) view returns (Honk.G1Point memory) {
1646 Honk.G1Point memory result;
1649 let free := mload(0x40)
1650 // Write lhs into memory (two 32 byte words)
1654 // free + 0x20| lhs.y
1655 mstore(free, mload(lhs))
1656 mstore(add(free, 0x20), mload(add(lhs, 0x20)))
1658 // Write rhs into memory (two 32 byte words)
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)))
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) }
1671 // Copy the result of the addition back into the result memory location.
1674 // result | result.x
1675 // result + 0x20| result.y
1676 mstore(result, mload(free))
1677 mstore(add(result, 0x20), mload(add(free, 0x20)))
1679 mstore(0x40, add(free, 0x80))
1685function rejectPointAtInfinity(Honk.G1Point memory point) pure {
1686 require((point.x | point.y) != 0, Errors.PointAtInfinity());
1693function arePairingPointsDefault(Fr[PAIRING_POINTS_SIZE] memory pairingPoints) pure returns (bool) {
1695 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1696 acc |= Fr.unwrap(pairingPoints[i]);
1701function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool decodedResult) {
1702 bytes memory input = abi.encodePacked(
1706 uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
1707 uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
1708 uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
1709 uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
1713 uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
1714 uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
1715 uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
1716 uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
1719 (bool success, bytes memory result) = address(0x08).staticcall(input);
1720 decodedResult = success && abi.decode(result, (bool));
1723abstract contract BaseHonkVerifier is IVerifier {
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
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;
1735 uint256 internal constant PERMUTATION_ARGUMENT_VALUE_SEPARATOR = 1 << 28;
1737 uint256 internal immutable $N;
1738 uint256 internal immutable $LOG_N;
1739 uint256 internal immutable $VK_HASH;
1740 uint256 internal immutable $NUM_PUBLIC_INPUTS;
1742 constructor(uint256 _N, uint256 _logN, uint256 _vkHash, uint256 _numPublicInputs) {
1746 $NUM_PUBLIC_INPUTS = _numPublicInputs;
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);
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);
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();
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);
1767 // Derive public input delta
1768 t.relationParameters.publicInputsDelta = computePublicInputDelta(
1770 p.pairingPointObject,
1771 t.relationParameters.beta,
1772 t.relationParameters.gamma, /*pubInputsOffset=*/
1777 bool sumcheckVerified = verifySumcheck(p, t);
1778 if (!sumcheckVerified) revert Errors.SumcheckFailed();
1780 bool shpleminiVerified = verifyShplemini(p, vk, t);
1781 if (!shpleminiVerified) revert Errors.ShpleminiFailed();
1783 return sumcheckVerified && shpleminiVerified; // Boolean condition not required - nice for vanity :)
1786 function computePublicInputDelta(
1787 bytes32[] memory publicInputs,
1788 Fr[PAIRING_POINTS_SIZE] memory pairingPointObject,
1792 ) internal view returns (Fr publicInputDelta) {
1794 Fr denominator = ONE;
1796 Fr numeratorAcc = gamma + (beta * FrLib.from(PERMUTATION_ARGUMENT_VALUE_SEPARATOR + offset));
1797 Fr denominatorAcc = gamma - (beta * FrLib.from(offset + 1));
1800 for (uint256 i = 0; i < $NUM_PUBLIC_INPUTS - PAIRING_POINTS_SIZE; i++) {
1801 Fr pubInput = FrLib.fromBytes32(publicInputs[i]);
1803 numerator = numerator * (numeratorAcc + pubInput);
1804 denominator = denominator * (denominatorAcc + pubInput);
1806 numeratorAcc = numeratorAcc + beta;
1807 denominatorAcc = denominatorAcc - beta;
1810 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1811 Fr pubInput = pairingPointObject[i];
1813 numerator = numerator * (numeratorAcc + pubInput);
1814 denominator = denominator * (denominatorAcc + pubInput);
1816 numeratorAcc = numeratorAcc + beta;
1817 denominatorAcc = denominatorAcc - beta;
1821 publicInputDelta = FrLib.div(numerator, denominator);
1824 function verifySumcheck(Honk.Proof memory proof, Transcript memory tp) internal view returns (bool verified) {
1826 Fr powPartialEvaluation = ONE;
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();
1834 Fr roundChallenge = tp.sumCheckUChallenges[round];
1836 // Update the round target for the next rounf
1837 roundTarget = computeNextTargetSum(roundUnivariate, roundChallenge);
1838 powPartialEvaluation = partiallyEvaluatePOW(tp.gateChallenges[round], powPartialEvaluation, roundChallenge);
1842 Fr grandHonkRelationSum = RelationsLib.accumulateRelationEvaluations(
1843 proof.sumcheckEvaluations, tp.relationParameters, tp.alphas, powPartialEvaluation
1845 verified = (grandHonkRelationSum == roundTarget);
1848 // Return the new target sum for the next sumcheck round
1849 function computeNextTargetSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariates, Fr roundChallenge)
1852 returns (Fr targetSum)
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)
1864 // To compute the next target sum, we evaluate the given univariate at a point u (challenge).
1866 // Performing Barycentric evaluations
1868 Fr numeratorValue = ONE;
1869 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1870 numeratorValue = numeratorValue * (roundChallenge - Fr.wrap(i));
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;
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;
1887 // Scale the sum by the value of B(x)
1888 targetSum = targetSum * numeratorValue;
1891 function verifyShplemini(Honk.Proof memory proof, Honk.VerificationKey memory vk, Transcript memory tp)
1894 returns (bool verified)
1896 CommitmentSchemeLib.ShpleminiIntermediates memory mem; // stack
1898 // - Compute vector (r, r², ... , r²⁽ⁿ⁻¹⁾), where n = log_circuit_size
1899 Fr[] memory powers_of_evaluation_challenge = CommitmentSchemeLib.computeSquares(tp.geminiR, $LOG_N);
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);
1905 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[0]).invert();
1906 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[0]).invert();
1908 mem.unshiftedScalar = mem.posInvertedDenominator + (tp.shplonkNu * mem.negInvertedDenominator);
1910 tp.geminiR.invert() * (mem.posInvertedDenominator - (tp.shplonkNu * mem.negInvertedDenominator));
1913 commitments[0] = proof.shplonkQ;
1915 /* Batch multivariate opening claims, shifted and unshifted
1916 * The vector of scalars is populated as follows:
1919 * - \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
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),
1924 * - \rho^{k+m-1} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right)
1928 * The following vector is concatenated to the vector of commitments:
1930 * f_0, \ldots, f_{m-1}, f_{\text{shift}, 0}, \ldots, f_{\text{shift}, k-1}
1933 * Simultaneously, the evaluation of the multilinear polynomial
1935 * \sum \rho^i \cdot f_i + \sum \rho^{i+k} \cdot f_{\text{shift}, i}
1937 * at the challenge point \f$ (u_0,\ldots, u_{n-1}) \f$ is computed.
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$.
1942 mem.batchingChallenge = ONE;
1943 mem.batchedEvaluation = ZERO;
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;
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;
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;
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;
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;
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
2011 * 1. Moves the vector
2013 * \left( \text{com}(A_1), \text{com}(A_2), \ldots, \text{com}(A_{n-1}) \right)
2015 * to the 'commitments' vector.
2017 * 2. Computes the scalars:
2019 * \frac{\nu^{2}}{z + r^2}, \frac{\nu^3}{z + r^4}, \ldots, \frac{\nu^{n-1}}{z + r^{2^{n-1}}}
2021 * and places them into the 'scalars' vector.
2023 * 3. Accumulates the summands of the constant term:
2025 * \sum_{i=2}^{n-1} \frac{\nu^{i} \cdot A_i(-r^{2^i})}{z + r^{2^i}}
2027 * and adds them to the 'constant_term_accumulator'.
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,
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);
2044 mem.batchingChallenge = tp.shplonkNu.sqr();
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();
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();
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];
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;
2068 commitments[NUMBER_UNSHIFTED + 1 + i] = proof.geminiFoldComms[i];
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;
2075 Honk.G1Point memory quotient_commitment = proof.kzgQuotient;
2077 commitments[NUMBER_UNSHIFTED + $LOG_N + 1] = quotient_commitment;
2078 scalars[NUMBER_UNSHIFTED + $LOG_N + 1] = tp.shplonkZ; // evaluation challenge
2080 Honk.G1Point memory P_0_agg = batchMul(commitments, scalars);
2081 Honk.G1Point memory P_1_agg = negateInplace(quotient_commitment);
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);
2089 // Validate the points from the proof are on the curve
2090 rejectPointAtInfinity(P_0_other);
2091 rejectPointAtInfinity(P_1_other);
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);
2098 return pairing(P_0_agg, P_1_agg);
2101 function batchMul(Honk.G1Point[] memory base, Fr[] memory scalars)
2104 returns (Honk.G1Point memory result)
2106 uint256 limit = NUMBER_UNSHIFTED + $LOG_N + 2;
2108 // Validate all points are on the curve
2109 for (uint256 i = 0; i < limit; ++i) {
2110 rejectPointAtInfinity(base[i]);
2113 bool success = true;
2115 let free := mload(0x40)
2118 for {} lt(count, add(limit, 1)) { count := add(count, 1) } {
2120 let base_base := add(base, mul(count, 0x20))
2121 let scalar_base := add(scalars, mul(count, 0x20))
2123 mstore(add(free, 0x40), mload(mload(base_base)))
2124 mstore(add(free, 0x60), mload(add(0x20, mload(base_base))))
2126 mstore(add(free, 0x80), mload(scalar_base))
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))
2133 // Return the result
2134 mstore(result, mload(free))
2135 mstore(add(result, 0x20), mload(add(free, 0x20)))
2138 require(success, Errors.ShpleminiFailed());
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
2147 proofLength += logN * BATCHED_RELATION_PARTIAL_LENGTH * NUM_ELEMENTS_FR; // sumcheck univariates
2148 proofLength += NUMBER_OF_ENTITIES * NUM_ELEMENTS_FR; // sumcheck evaluations
2151 proofLength += (logN - 1) * NUM_ELEMENTS_COMM; // Gemini Fold commitments
2152 proofLength += logN * NUM_ELEMENTS_FR; // Gemini evaluations
2154 // Shplonk and KZG commitments
2155 proofLength += NUM_ELEMENTS_COMM * 2; // Shplonk Q and KZG W commitments
2158 proofLength += PAIRING_POINTS_SIZE; // pairing inputs carried on public inputs
2163 function checkSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate, Fr roundTarget)
2166 returns (bool checked)
2168 Fr totalSum = roundUnivariate[0] + roundUnivariate[1];
2169 checked = totalSum == roundTarget;
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)
2176 returns (Fr newEvaluation)
2178 Fr univariateEval = ONE + (roundChallenge * (gateChallenge - ONE));
2179 newEvaluation = currentEvaluation * univariateEval;
2182 function loadVerificationKey() internal pure virtual returns (Honk.VerificationKey memory);
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();
2194 std::ostringstream stream;
2196 return stream.str() + HONK_CONTRACT_SOURCE;
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)