14static const char HONK_ZK_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);
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;
72 bytes4 internal constant FRLIB_MODEXP_FAILED_SELECTOR = 0xf8d61709;
74 function invert(Fr value) internal view returns (Fr) {
75 uint256 v = Fr.unwrap(value);
76 require(v != 0, Errors.InvertOfZero());
80 // Call the modexp precompile to invert in the field
82 let free := mload(0x40)
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)
91 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
95 mstore(0x40, add(free, 0xc0))
98 return Fr.wrap(result);
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());
107 // Call the modexp precompile to invert in the field
109 let free := mload(0x40)
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)
118 mstore(0x00, FRLIB_MODEXP_FAILED_SELECTOR)
121 result := mload(0x00)
122 mstore(0x40, add(free, 0xc0))
125 return Fr.wrap(result);
128 function div(Fr numerator, Fr denominator) internal view returns (Fr) {
130 return numerator * invert(denominator);
134 function sqr(Fr value) internal pure returns (Fr) {
136 return value * value;
140 function unwrap(Fr value) internal pure returns (uint256) {
142 return Fr.unwrap(value);
146 function neg(Fr value) internal pure returns (Fr) {
148 return Fr.wrap(MODULUS - Fr.unwrap(value));
152 function from(uint256 value) internal pure returns (Fr) {
154 require(value < MODULUS, Errors.ValueGeFieldOrder());
155 return Fr.wrap(value);
159 function fromBytes32(bytes32 value) internal pure returns (Fr) {
161 uint256 v = uint256(value);
162 require(v < MODULUS, Errors.ValueGeFieldOrder());
167 function toBytes32(Fr value) internal pure returns (bytes32) {
169 return bytes32(Fr.unwrap(value));
175function add(Fr a, Fr b) pure returns (Fr) {
177 return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
181function mul(Fr a, Fr b) pure returns (Fr) {
183 return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
187function sub(Fr a, Fr b) pure returns (Fr) {
189 return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
193function notEqual(Fr a, Fr b) pure returns (bool) {
195 return Fr.unwrap(a) != Fr.unwrap(b);
199function equal(Fr a, Fr b) pure returns (bool) {
201 return Fr.unwrap(a) == Fr.unwrap(b);
205uint256 constant CONST_PROOF_SIZE_LOG_N = 25;
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;
219uint256 constant FIELD_ELEMENT_SIZE = 0x20;
220uint256 constant GROUP_ELEMENT_SIZE = 0x40;
222// Powers of alpha used to batch subrelations (alpha, alpha^2, ..., alpha^(NUM_SUBRELATIONS-1))
223uint256 constant NUMBER_OF_ALPHAS = NUMBER_OF_SUBRELATIONS - 1;
225// Must match UltraFlavor_Generated::EntityId order.
253 Q_POSEIDON2_EXTERNAL,
254 Q_POSEIDON2_INTERNAL,
276 struct VerificationKey {
279 uint256 logCircuitSize;
280 uint256 publicInputsSize;
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;
306 // Precomputed lookup table
311 // Fixed first and last
312 G1Point lagrangeFirst;
313 G1Point lagrangeLast;
316 struct RelationParameters {
319 Fr romLogupGamma; // ROM-LogUp additive offset
323 Fr publicInputsDelta;
327 // Pairing point object
328 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
334 // Lookup helpers - Permutations
336 // Lookup helpers - logup
337 G1Point lookupReadCounts;
338 G1Point lookupReadTags;
339 G1Point lookupInverses;
341 Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
342 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
344 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
345 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
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
361 // Commitments to logup witness polynomials
362 G1Point lookupReadCounts;
363 G1Point lookupReadTags;
364 G1Point lookupInverses;
365 // Commitment to grand permutation polynomial
367 G1Point[3] libraCommitments;
370 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
372 Fr[NUMBER_OF_ENTITIES_ZK] sumcheckEvaluations; // Includes gemini_masking_poly eval at index 0 (first position)
374 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
375 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
376 Fr[4] libraPolyEvals;
382// ZKTranscript library to generate fiat shamir challenges, the ZK transcript only differest
386 Honk.RelationParameters relationParameters;
387 Fr[NUMBER_OF_ALPHAS] alphas; // Powers of alpha: [alpha, alpha^2, ..., alpha^(NUM_SUBRELATIONS-1)]
388 Fr[CONST_PROOF_SIZE_LOG_N] gateChallenges;
391 Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
398 Fr publicInputsDelta;
401library ZKTranscriptLib {
402 function generateTranscript(
403 Honk.ZKProof memory proof,
404 bytes32[] calldata publicInputs,
406 uint256 publicInputsSize,
408 ) external pure returns (ZKTranscript memory t) {
409 Fr previousChallenge;
410 (t.relationParameters, previousChallenge) =
411 generateRelationParametersChallenges(proof, publicInputs, vkHash, publicInputsSize, previousChallenge);
413 (t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
415 (t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge, logN);
416 (t.libraChallenge, previousChallenge) = generateLibraChallenge(previousChallenge, proof);
417 (t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge, logN);
419 (t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
421 (t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge, logN);
423 (t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge, logN);
425 (t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
429 function generateRelationParametersChallenges(
430 Honk.ZKProof memory proof,
431 bytes32[] calldata publicInputs,
433 uint256 publicInputsSize,
435 ) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
436 (rp.eta, rp.romLogupGamma, previousChallenge) =
437 generateEtaChallenge(proof, publicInputs, vkHash, publicInputsSize);
439 (rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaGammaChallenges(previousChallenge, proof);
442 function generateEtaChallenge(
443 Honk.ZKProof memory proof,
444 bytes32[] calldata publicInputs,
446 uint256 publicInputsSize
447 ) internal pure returns (Fr eta, Fr romLogupGamma, Fr previousChallenge) {
448 // Size: 1 (vkHash) + publicInputsSize + 8 (geminiMask(2) + 3 wires(6))
449 bytes32[] memory round0 = new bytes32[](1 + publicInputsSize + 8);
450 round0[0] = bytes32(vkHash);
452 for (uint256 i = 0; i < publicInputsSize - PAIRING_POINTS_SIZE; i++) {
453 require(uint256(publicInputs[i]) < P, Errors.ValueGeFieldOrder());
454 round0[1 + i] = publicInputs[i];
456 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
457 round0[1 + publicInputsSize - PAIRING_POINTS_SIZE + i] = FrLib.toBytes32(proof.pairingPointObject[i]);
460 // For ZK flavors: hash the gemini masking poly commitment (sent right after public inputs)
461 round0[1 + publicInputsSize] = bytes32(proof.geminiMaskingPoly.x);
462 round0[1 + publicInputsSize + 1] = bytes32(proof.geminiMaskingPoly.y);
464 // Create the first challenge
465 // Note: w4 is added to the challenge later on
466 round0[1 + publicInputsSize + 2] = bytes32(proof.w1.x);
467 round0[1 + publicInputsSize + 3] = bytes32(proof.w1.y);
468 round0[1 + publicInputsSize + 4] = bytes32(proof.w2.x);
469 round0[1 + publicInputsSize + 5] = bytes32(proof.w2.y);
470 round0[1 + publicInputsSize + 6] = bytes32(proof.w3.x);
471 round0[1 + publicInputsSize + 7] = bytes32(proof.w3.y);
473 eta = FrLib.from(uint256(keccak256(abi.encodePacked(round0))) % P);
474 romLogupGamma = FrLib.from(uint256(keccak256(abi.encodePacked(Fr.unwrap(eta)))) % P);
475 previousChallenge = romLogupGamma;
478 function generateBetaGammaChallenges(Fr previousChallenge, Honk.ZKProof memory proof)
481 returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
483 bytes32[7] memory round1;
484 round1[0] = FrLib.toBytes32(previousChallenge);
485 round1[1] = bytes32(proof.lookupReadCounts.x);
486 round1[2] = bytes32(proof.lookupReadCounts.y);
487 round1[3] = bytes32(proof.lookupReadTags.x);
488 round1[4] = bytes32(proof.lookupReadTags.y);
489 round1[5] = bytes32(proof.w4.x);
490 round1[6] = bytes32(proof.w4.y);
492 beta = FrLib.from(uint256(keccak256(abi.encodePacked(round1))) % P);
493 gamma = FrLib.from(uint256(keccak256(abi.encodePacked(Fr.unwrap(beta)))) % P);
494 nextPreviousChallenge = gamma;
497 // Alpha challenges non-linearise the gate contributions
498 function generateAlphaChallenges(Fr previousChallenge, Honk.ZKProof memory proof)
501 returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
503 // Generate the original sumcheck alpha 0 by hashing zPerm and zLookup
504 uint256[5] memory alpha0;
505 alpha0[0] = Fr.unwrap(previousChallenge);
506 alpha0[1] = proof.lookupInverses.x;
507 alpha0[2] = proof.lookupInverses.y;
508 alpha0[3] = proof.zPerm.x;
509 alpha0[4] = proof.zPerm.y;
511 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(alpha0))) % P);
512 Fr alpha = nextPreviousChallenge;
514 // Compute powers of alpha for batching subrelations
516 for (uint256 i = 1; i < NUMBER_OF_ALPHAS; i++) {
517 alphas[i] = alphas[i - 1] * alpha;
521 function generateGateChallenges(Fr previousChallenge, uint256 logN)
524 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
526 previousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge)))) % P);
527 gateChallenges[0] = previousChallenge;
528 for (uint256 i = 1; i < logN; i++) {
529 gateChallenges[i] = gateChallenges[i - 1] * gateChallenges[i - 1];
531 nextPreviousChallenge = previousChallenge;
534 function generateLibraChallenge(Fr previousChallenge, Honk.ZKProof memory proof)
537 returns (Fr libraChallenge, Fr nextPreviousChallenge)
539 // 2 comm, 1 sum, 1 challenge
540 uint256[4] memory challengeData;
541 challengeData[0] = Fr.unwrap(previousChallenge);
542 challengeData[1] = proof.libraCommitments[0].x;
543 challengeData[2] = proof.libraCommitments[0].y;
544 challengeData[3] = Fr.unwrap(proof.libraSum);
545 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(challengeData))) % P);
546 libraChallenge = nextPreviousChallenge;
549 function generateSumcheckChallenges(Honk.ZKProof memory proof, Fr prevChallenge, uint256 logN)
552 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
554 for (uint256 i = 0; i < logN; i++) {
555 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
556 univariateChal[0] = prevChallenge;
558 for (uint256 j = 0; j < ZK_BATCHED_RELATION_PARTIAL_LENGTH; j++) {
559 univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
561 prevChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(univariateChal))) % P);
562 sumcheckChallenges[i] = prevChallenge;
564 nextPreviousChallenge = prevChallenge;
567 // We add Libra claimed eval + 2 libra commitments (grand_sum, quotient)
568 function generateRhoChallenge(Honk.ZKProof memory proof, Fr prevChallenge)
571 returns (Fr rho, Fr nextPreviousChallenge)
573 uint256[NUMBER_OF_ENTITIES_ZK + 6] memory rhoChallengeElements;
574 rhoChallengeElements[0] = Fr.unwrap(prevChallenge);
576 for (i = 1; i <= NUMBER_OF_ENTITIES_ZK; i++) {
577 rhoChallengeElements[i] = Fr.unwrap(proof.sumcheckEvaluations[i - 1]);
579 rhoChallengeElements[i] = Fr.unwrap(proof.libraEvaluation);
581 rhoChallengeElements[i] = proof.libraCommitments[1].x;
582 rhoChallengeElements[i + 1] = proof.libraCommitments[1].y;
584 rhoChallengeElements[i] = proof.libraCommitments[2].x;
585 rhoChallengeElements[i + 1] = proof.libraCommitments[2].y;
587 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(rhoChallengeElements))) % P);
588 rho = nextPreviousChallenge;
591 function generateGeminiRChallenge(Honk.ZKProof memory proof, Fr prevChallenge, uint256 logN)
594 returns (Fr geminiR, Fr nextPreviousChallenge)
596 uint256[] memory gR = new uint256[]((logN - 1) * 2 + 1);
597 gR[0] = Fr.unwrap(prevChallenge);
599 for (uint256 i = 0; i < logN - 1; i++) {
600 gR[1 + i * 2] = proof.geminiFoldComms[i].x;
601 gR[2 + i * 2] = proof.geminiFoldComms[i].y;
604 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(gR))) % P);
605 geminiR = nextPreviousChallenge;
608 function generateShplonkNuChallenge(Honk.ZKProof memory proof, Fr prevChallenge, uint256 logN)
611 returns (Fr shplonkNu, Fr nextPreviousChallenge)
613 uint256[] memory shplonkNuChallengeElements = new uint256[](logN + 1 + 4);
614 shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
616 for (uint256 i = 1; i <= logN; i++) {
617 shplonkNuChallengeElements[i] = Fr.unwrap(proof.geminiAEvaluations[i - 1]);
620 uint256 libraIdx = 0;
621 for (uint256 i = logN + 1; i <= logN + 4; i++) {
622 shplonkNuChallengeElements[i] = Fr.unwrap(proof.libraPolyEvals[libraIdx]);
626 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkNuChallengeElements))) % P);
627 shplonkNu = nextPreviousChallenge;
630 function generateShplonkZChallenge(Honk.ZKProof memory proof, Fr prevChallenge)
633 returns (Fr shplonkZ, Fr nextPreviousChallenge)
635 uint256[3] memory shplonkZChallengeElements;
636 shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
638 shplonkZChallengeElements[1] = proof.shplonkQ.x;
639 shplonkZChallengeElements[2] = proof.shplonkQ.y;
641 nextPreviousChallenge = FrLib.from(uint256(keccak256(abi.encodePacked(shplonkZChallengeElements))) % P);
642 shplonkZ = nextPreviousChallenge;
645 function loadProof(bytes calldata proof, uint256 logN) internal pure returns (Honk.ZKProof memory p) {
646 uint256 boundary = 0x0;
648 // Pairing point object
649 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
650 uint256 limb = uint256(bytes32(proof[boundary:boundary + FIELD_ELEMENT_SIZE]));
651 // lo limbs (even index) < 2^136, hi limbs (odd index) < 2^120
652 require(limb < 2 ** (i % 2 == 0 ? 136 : 120), Errors.ValueGeLimbMax());
653 p.pairingPointObject[i] = FrLib.from(limb);
654 boundary += FIELD_ELEMENT_SIZE;
657 // Gemini masking polynomial commitment (sent first in ZK flavors, right after pairing points)
658 p.geminiMaskingPoly = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
659 boundary += GROUP_ELEMENT_SIZE;
662 p.w1 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
663 boundary += GROUP_ELEMENT_SIZE;
664 p.w2 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
665 boundary += GROUP_ELEMENT_SIZE;
666 p.w3 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
667 boundary += GROUP_ELEMENT_SIZE;
669 // Lookup / Permutation Helper Commitments
670 p.lookupReadCounts = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
671 boundary += GROUP_ELEMENT_SIZE;
672 p.lookupReadTags = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
673 boundary += GROUP_ELEMENT_SIZE;
674 p.w4 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
675 boundary += GROUP_ELEMENT_SIZE;
676 p.lookupInverses = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
677 boundary += GROUP_ELEMENT_SIZE;
678 p.zPerm = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
679 boundary += GROUP_ELEMENT_SIZE;
680 p.libraCommitments[0] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
681 boundary += GROUP_ELEMENT_SIZE;
683 p.libraSum = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
684 boundary += FIELD_ELEMENT_SIZE;
685 // Sumcheck univariates
686 for (uint256 i = 0; i < logN; i++) {
687 for (uint256 j = 0; j < ZK_BATCHED_RELATION_PARTIAL_LENGTH; j++) {
688 p.sumcheckUnivariates[i][j] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
689 boundary += FIELD_ELEMENT_SIZE;
693 // Sumcheck evaluations (includes gemini_masking_poly eval at index 0 for ZK flavors)
694 for (uint256 i = 0; i < NUMBER_OF_ENTITIES_ZK; i++) {
695 p.sumcheckEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
696 boundary += FIELD_ELEMENT_SIZE;
699 p.libraEvaluation = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
700 boundary += FIELD_ELEMENT_SIZE;
702 p.libraCommitments[1] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
703 boundary += GROUP_ELEMENT_SIZE;
704 p.libraCommitments[2] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
705 boundary += GROUP_ELEMENT_SIZE;
708 // Read gemini fold univariates
709 for (uint256 i = 0; i < logN - 1; i++) {
710 p.geminiFoldComms[i] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
711 boundary += GROUP_ELEMENT_SIZE;
714 // Read gemini a evaluations
715 for (uint256 i = 0; i < logN; i++) {
716 p.geminiAEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
717 boundary += FIELD_ELEMENT_SIZE;
720 for (uint256 i = 0; i < 4; i++) {
721 p.libraPolyEvals[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
722 boundary += FIELD_ELEMENT_SIZE;
726 p.shplonkQ = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
727 boundary += GROUP_ELEMENT_SIZE;
729 p.kzgQuotient = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
733library RelationsLib {
734 struct EllipticParams {
742 // push accumulators into memory
743 Fr x_double_identity;
746 // Parameters used within the Memory Relation
747 // A struct is used to work around stack too deep. This relation has alot of variables
749 Fr memory_record_check;
750 Fr partial_record_check;
751 Fr next_gate_access_type;
754 Fr adjacent_values_match_if_adjacent_indices_match;
755 Fr adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation;
757 Fr next_gate_access_type_is_boolean;
758 Fr ROM_consistency_check_identity;
759 Fr RAM_consistency_check_identity;
761 Fr RAM_timestamp_check_identity;
763 Fr index_is_monotonically_increasing;
766 // Parameters used within the Non-Native Field Relation
767 // A struct is used to work around stack too deep. This relation has alot of variables
770 Fr non_native_field_gate_1;
771 Fr non_native_field_gate_2;
772 Fr non_native_field_gate_3;
773 Fr limb_accumulator_1;
774 Fr limb_accumulator_2;
778 struct PoseidonExternalParams {
798 struct PoseidonInternalParams {
812 Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
813 uint256 internal constant NEG_HALF_MODULO_P = 0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000;
815 // Constants for the Non-native Field relation
816 Fr internal constant LIMB_SIZE = Fr.wrap(uint256(1) << 68);
817 Fr internal constant SUBLIMB_SHIFT = Fr.wrap(uint256(1) << 14);
819 function accumulateRelationEvaluations(
820 Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
821 Honk.RelationParameters memory rp,
822 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges,
824 ) external pure returns (Fr accumulator) {
825 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
827 // Accumulate all relations in Ultra Honk - each with varying number of subrelations
828 accumulateArithmeticRelation(purportedEvaluations, evaluations, powPartialEval);
829 accumulatePermutationRelation(purportedEvaluations, rp, evaluations, powPartialEval);
830 accumulateLogDerivativeLookupRelation(purportedEvaluations, rp, evaluations, powPartialEval);
831 accumulateDeltaRangeRelation(purportedEvaluations, evaluations, powPartialEval);
832 accumulateEllipticRelation(purportedEvaluations, evaluations, powPartialEval);
833 accumulateMemoryRelation(purportedEvaluations, rp, evaluations, powPartialEval);
834 accumulateRomLogupRelation(purportedEvaluations, rp, evaluations, powPartialEval);
835 accumulateNnfRelation(purportedEvaluations, evaluations, powPartialEval);
836 accumulatePoseidonExternalRelation(purportedEvaluations, evaluations, powPartialEval);
837 accumulatePoseidonInternalRelation(purportedEvaluations, evaluations, powPartialEval);
839 // batch the subrelations with the precomputed alpha powers to obtain the full honk relation
840 accumulator = scaleAndBatchSubrelations(evaluations, subrelationChallenges);
848 function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
849 return p[uint256(_wire)];
856 function accumulateArithmeticRelation(
857 Fr[NUMBER_OF_ENTITIES] memory p,
858 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
862 Fr q_arith = wire(p, WIRE.Q_ARITH);
864 Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
866 Fr accum = (q_arith - Fr.wrap(3)) * (wire(p, WIRE.Q_M) * wire(p, WIRE.W_R) * wire(p, WIRE.W_L)) * neg_half;
867 accum = accum + (wire(p, WIRE.Q_L) * wire(p, WIRE.W_L)) + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_R))
868 + (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);
869 accum = accum + (q_arith - ONE) * wire(p, WIRE.W_4_SHIFT);
870 accum = accum * q_arith;
871 accum = accum * domainSep;
877 Fr accum = wire(p, WIRE.W_L) + wire(p, WIRE.W_4) - wire(p, WIRE.W_L_SHIFT) + wire(p, WIRE.Q_M);
878 accum = accum * (q_arith - Fr.wrap(2));
879 accum = accum * (q_arith - ONE);
880 accum = accum * q_arith;
881 accum = accum * domainSep;
886 function accumulatePermutationRelation(
887 Fr[NUMBER_OF_ENTITIES] memory p,
888 Honk.RelationParameters memory rp,
889 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
892 Fr grand_product_numerator;
893 Fr grand_product_denominator;
896 Fr num = wire(p, WIRE.W_L) + wire(p, WIRE.ID_1) * rp.beta + rp.gamma;
897 num = num * (wire(p, WIRE.W_R) + wire(p, WIRE.ID_2) * rp.beta + rp.gamma);
898 num = num * (wire(p, WIRE.W_O) + wire(p, WIRE.ID_3) * rp.beta + rp.gamma);
899 num = num * (wire(p, WIRE.W_4) + wire(p, WIRE.ID_4) * rp.beta + rp.gamma);
901 grand_product_numerator = num;
904 Fr den = wire(p, WIRE.W_L) + wire(p, WIRE.SIGMA_1) * rp.beta + rp.gamma;
905 den = den * (wire(p, WIRE.W_R) + wire(p, WIRE.SIGMA_2) * rp.beta + rp.gamma);
906 den = den * (wire(p, WIRE.W_O) + wire(p, WIRE.SIGMA_3) * rp.beta + rp.gamma);
907 den = den * (wire(p, WIRE.W_4) + wire(p, WIRE.SIGMA_4) * rp.beta + rp.gamma);
909 grand_product_denominator = den;
914 Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
917 - ((wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
918 * grand_product_denominator);
919 acc = acc * domainSep;
925 Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
929 // Contribution 4: z_perm initialization check (lagrange_first * z_perm = 0)
931 Fr acc = (wire(p, WIRE.LAGRANGE_FIRST) * wire(p, WIRE.Z_PERM)) * domainSep;
936 function accumulateLogDerivativeLookupRelation(
937 Fr[NUMBER_OF_ENTITIES] memory p,
938 Honk.RelationParameters memory rp,
939 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
945 // Calculate the write term (the table accumulation)
946 // table_term = table_1 + γ + table_2 * β + table_3 * β² + table_4 * β³
948 Fr beta_sqr = rp.beta * rp.beta;
949 table_term = wire(p, WIRE.TABLE_1) + rp.gamma + (wire(p, WIRE.TABLE_2) * rp.beta)
950 + (wire(p, WIRE.TABLE_3) * beta_sqr) + (wire(p, WIRE.TABLE_4) * beta_sqr * rp.beta);
953 // Calculate the read term
954 // lookup_term = derived_entry_1 + γ + derived_entry_2 * β + derived_entry_3 * β² + q_index * β³
956 Fr beta_sqr = rp.beta * rp.beta;
957 Fr derived_entry_1 = wire(p, WIRE.W_L) + rp.gamma + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_L_SHIFT));
958 Fr derived_entry_2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_M) * wire(p, WIRE.W_R_SHIFT);
959 Fr derived_entry_3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_C) * wire(p, WIRE.W_O_SHIFT);
961 lookup_term = derived_entry_1 + (derived_entry_2 * rp.beta) + (derived_entry_3 * beta_sqr)
962 + (wire(p, WIRE.Q_O) * beta_sqr * rp.beta);
965 Fr lookup_inverse = wire(p, WIRE.LOOKUP_INVERSES) * table_term;
966 Fr table_inverse = wire(p, WIRE.LOOKUP_INVERSES) * lookup_term;
968 Fr inverse_exists_xor =
969 wire(p, WIRE.LOOKUP_READ_TAGS) + wire(p, WIRE.Q_LOOKUP)
970 - (wire(p, WIRE.LOOKUP_READ_TAGS) * wire(p, WIRE.Q_LOOKUP));
972 // Inverse calculated correctly relation
973 Fr accumulatorNone = lookup_term * table_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
974 accumulatorNone = accumulatorNone * domainSep;
977 Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * lookup_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * table_inverse;
979 Fr read_tag = wire(p, WIRE.LOOKUP_READ_TAGS);
981 Fr read_tag_boolean_relation = read_tag * read_tag - read_tag;
983 evals[3] = accumulatorNone;
984 evals[4] = accumulatorOne;
985 evals[5] = read_tag_boolean_relation * domainSep;
988 function accumulateDeltaRangeRelation(
989 Fr[NUMBER_OF_ENTITIES] memory p,
990 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
993 Fr minus_one = ZERO - ONE;
994 Fr minus_two = ZERO - Fr.wrap(2);
995 Fr minus_three = ZERO - Fr.wrap(3);
997 // Compute wire differences
998 Fr delta_1 = wire(p, WIRE.W_R) - wire(p, WIRE.W_L);
999 Fr delta_2 = wire(p, WIRE.W_O) - wire(p, WIRE.W_R);
1000 Fr delta_3 = wire(p, WIRE.W_4) - wire(p, WIRE.W_O);
1001 Fr delta_4 = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_4);
1006 acc = acc * (delta_1 + minus_one);
1007 acc = acc * (delta_1 + minus_two);
1008 acc = acc * (delta_1 + minus_three);
1009 acc = acc * wire(p, WIRE.Q_RANGE);
1010 acc = acc * domainSep;
1017 acc = acc * (delta_2 + minus_one);
1018 acc = acc * (delta_2 + minus_two);
1019 acc = acc * (delta_2 + minus_three);
1020 acc = acc * wire(p, WIRE.Q_RANGE);
1021 acc = acc * domainSep;
1028 acc = acc * (delta_3 + minus_one);
1029 acc = acc * (delta_3 + minus_two);
1030 acc = acc * (delta_3 + minus_three);
1031 acc = acc * wire(p, WIRE.Q_RANGE);
1032 acc = acc * domainSep;
1039 acc = acc * (delta_4 + minus_one);
1040 acc = acc * (delta_4 + minus_two);
1041 acc = acc * (delta_4 + minus_three);
1042 acc = acc * wire(p, WIRE.Q_RANGE);
1043 acc = acc * domainSep;
1048 function accumulateEllipticRelation(
1049 Fr[NUMBER_OF_ENTITIES] memory p,
1050 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1053 EllipticParams memory ep;
1054 ep.x_1 = wire(p, WIRE.W_R);
1055 ep.y_1 = wire(p, WIRE.W_O);
1057 ep.x_2 = wire(p, WIRE.W_L_SHIFT);
1058 ep.y_2 = wire(p, WIRE.W_4_SHIFT);
1059 ep.y_3 = wire(p, WIRE.W_O_SHIFT);
1060 ep.x_3 = wire(p, WIRE.W_R_SHIFT);
1062 Fr q_sign = wire(p, WIRE.Q_L);
1063 Fr q_is_double = wire(p, WIRE.Q_M);
1065 // Contribution 10 point addition, x-coordinate check
1066 // q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
1067 Fr x_diff = (ep.x_2 - ep.x_1);
1068 Fr y1_sqr = (ep.y_1 * ep.y_1);
1071 Fr partialEval = domainSep;
1073 Fr y2_sqr = (ep.y_2 * ep.y_2);
1074 Fr y1y2 = ep.y_1 * ep.y_2 * q_sign;
1075 Fr x_add_identity = (ep.x_3 + ep.x_2 + ep.x_1);
1076 x_add_identity = x_add_identity * x_diff * x_diff;
1077 x_add_identity = x_add_identity - y2_sqr - y1_sqr + y1y2 + y1y2;
1079 evals[12] = x_add_identity * partialEval * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
1082 // Contribution 11 point addition, x-coordinate check
1083 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
1085 Fr y1_plus_y3 = ep.y_1 + ep.y_3;
1086 Fr y_diff = ep.y_2 * q_sign - ep.y_1;
1087 Fr y_add_identity = y1_plus_y3 * x_diff + (ep.x_3 - ep.x_1) * y_diff;
1088 evals[13] = y_add_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
1091 // Contribution 10 point doubling, x-coordinate check
1092 // (x3 + x1 + x1) (4y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
1093 // N.B. we're using the equivalence x1*x1*x1 === y1*y1 - curve_b to reduce degree by 1
1095 Fr x_pow_4 = (y1_sqr + GRUMPKIN_CURVE_B_PARAMETER_NEGATED) * ep.x_1;
1096 Fr y1_sqr_mul_4 = y1_sqr + y1_sqr;
1097 y1_sqr_mul_4 = y1_sqr_mul_4 + y1_sqr_mul_4;
1098 Fr x1_pow_4_mul_9 = x_pow_4 * Fr.wrap(9);
1100 // NOTE: pushed into memory (stack >:'( )
1101 ep.x_double_identity = (ep.x_3 + ep.x_1 + ep.x_1) * y1_sqr_mul_4 - x1_pow_4_mul_9;
1103 Fr acc = ep.x_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
1104 evals[12] = evals[12] + acc;
1107 // Contribution 11 point doubling, y-coordinate check
1108 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
1110 Fr x1_sqr_mul_3 = (ep.x_1 + ep.x_1 + ep.x_1) * ep.x_1;
1111 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);
1112 evals[13] = evals[13] + y_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
1116 function accumulateMemoryRelation(
1117 Fr[NUMBER_OF_ENTITIES] memory p,
1118 Honk.RelationParameters memory rp,
1119 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1122 MemParams memory ap;
1124 // Compute eta powers locally
1125 Fr eta_two = rp.eta * rp.eta;
1126 Fr eta_three = eta_two * rp.eta;
1169 ap.memory_record_check = wire(p, WIRE.W_O) * eta_three;
1170 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_R) * eta_two);
1171 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_L) * rp.eta);
1172 ap.memory_record_check = ap.memory_record_check + wire(p, WIRE.Q_C);
1173 ap.partial_record_check = ap.memory_record_check; // used in RAM consistency check; deg 1 or 4
1174 ap.memory_record_check = ap.memory_record_check - wire(p, WIRE.W_4);
1192 ap.index_delta = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_L);
1193 ap.record_delta = wire(p, WIRE.W_4_SHIFT) - wire(p, WIRE.W_4);
1195 ap.index_is_monotonically_increasing = ap.index_delta * (ap.index_delta - Fr.wrap(1)); // deg 2
1197 ap.adjacent_values_match_if_adjacent_indices_match = (ap.index_delta * MINUS_ONE + ONE) * ap.record_delta; // deg 2
1199 evals[15] = ap.adjacent_values_match_if_adjacent_indices_match * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1200 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1201 evals[16] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1202 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1204 ap.ROM_consistency_check_identity = ap.memory_record_check * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R)); // deg 3 or 7
1225 Fr access_type = (wire(p, WIRE.W_4) - ap.partial_record_check); // will be 0 or 1 for honest Prover; deg 1 or 4
1226 ap.access_check = access_type * (access_type - Fr.wrap(1)); // check value is 0 or 1; deg 2 or 8
1228 // reverse order we could re-use `ap.partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta
1230 ap.next_gate_access_type = wire(p, WIRE.W_O_SHIFT) * eta_three;
1231 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_R_SHIFT) * eta_two);
1232 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_L_SHIFT) * rp.eta);
1233 ap.next_gate_access_type = wire(p, WIRE.W_4_SHIFT) - ap.next_gate_access_type;
1235 Fr value_delta = wire(p, WIRE.W_O_SHIFT) - wire(p, WIRE.W_O);
1236 ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation =
1237 (ap.index_delta * MINUS_ONE + ONE) * value_delta * (ap.next_gate_access_type * MINUS_ONE + ONE); // deg 3 or 6
1239 // We can't apply the RAM consistency check identity on the final entry in the sorted list (the wires in the
1240 // next gate would make the identity fail). We need to validate that its 'access type' bool is correct. Can't
1241 // do with an arithmetic gate because of the `eta` factors. We need to check that the *next* gate's access
1242 // type is correct, to cover this edge case
1244 ap.next_gate_access_type_is_boolean =
1245 ap.next_gate_access_type * ap.next_gate_access_type - ap.next_gate_access_type;
1247 // Putting it all together...
1248 evals[17] = ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation
1249 * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5 or 8
1250 evals[18] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4
1251 evals[19] = ap.next_gate_access_type_is_boolean * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 6
1253 ap.RAM_consistency_check_identity = ap.access_check * (wire(p, WIRE.Q_O)); // deg 3 or 9
1266 ap.timestamp_delta = wire(p, WIRE.W_R_SHIFT) - wire(p, WIRE.W_R);
1267 ap.RAM_timestamp_check_identity = (ap.index_delta * MINUS_ONE + ONE) * ap.timestamp_delta - wire(p, WIRE.W_O); // deg 3
1274 ap.memory_identity = ap.ROM_consistency_check_identity; // deg 3 or 6
1275 ap.memory_identity =
1276 ap.memory_identity + ap.RAM_timestamp_check_identity * (wire(p, WIRE.Q_4) * wire(p, WIRE.Q_L)); // deg 4
1277 ap.memory_identity = ap.memory_identity + ap.memory_record_check * (wire(p, WIRE.Q_M) * wire(p, WIRE.Q_L)); // deg 3 or 6
1278 ap.memory_identity = ap.memory_identity + ap.RAM_consistency_check_identity; // deg 3 or 9
1280 // (deg 3 or 9) + (deg 4) + (deg 3)
1281 ap.memory_identity = ap.memory_identity * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 10
1282 evals[14] = ap.memory_identity;
1298 function accumulateRomLogupRelation(
1299 Fr[NUMBER_OF_ENTITIES] memory p,
1300 Honk.RelationParameters memory rp,
1301 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1304 Fr eta_two = rp.eta * rp.eta;
1306 Fr q_logup_table = wire(p, WIRE.Q_R) * (ONE - wire(p, WIRE.Q_L));
1307 Fr q_logup_read = wire(p, WIRE.Q_4) * (ONE - wire(p, WIRE.Q_L));
1308 Fr denom = rp.romLogupGamma + wire(p, WIRE.W_L) + (wire(p, WIRE.W_R) * rp.eta) + (wire(p, WIRE.Q_C) * eta_two);
1310 // Subrelation 6: per-row inverse correctness (linearly independent, scaled by domainSep). deg 5.
1312 (q_logup_table + q_logup_read) * (wire(p, WIRE.W_4) * denom - ONE) * (wire(p, WIRE.Q_MEMORY) * domainSep);
1314 // Subrelation 7: LogUp sum identity. Linearly dependent: summed across the trace, so NOT scaled by
1315 // domainSep (mirrors the log-derivative lookup subrelation). deg 5.
1316 evals[21] = (q_logup_read - q_logup_table * wire(p, WIRE.W_O)) * wire(p, WIRE.W_4) * wire(p, WIRE.Q_MEMORY);
1319 function accumulateNnfRelation(
1320 Fr[NUMBER_OF_ENTITIES] memory p,
1321 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1324 NnfParams memory ap;
1338 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);
1339 ap.non_native_field_gate_2 =
1340 (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));
1341 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * LIMB_SIZE;
1342 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 - wire(p, WIRE.W_4_SHIFT);
1343 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 + ap.limb_subproduct;
1344 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * wire(p, WIRE.Q_4);
1346 ap.limb_subproduct = ap.limb_subproduct * LIMB_SIZE;
1347 ap.limb_subproduct = ap.limb_subproduct + (wire(p, WIRE.W_L_SHIFT) * wire(p, WIRE.W_R_SHIFT));
1348 ap.non_native_field_gate_1 = ap.limb_subproduct;
1349 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 - (wire(p, WIRE.W_O) + wire(p, WIRE.W_4));
1350 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 * wire(p, WIRE.Q_O);
1352 ap.non_native_field_gate_3 = ap.limb_subproduct;
1353 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 + wire(p, WIRE.W_4);
1354 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 - (wire(p, WIRE.W_O_SHIFT) + wire(p, WIRE.W_4_SHIFT));
1355 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 * wire(p, WIRE.Q_M);
1357 Fr non_native_field_identity =
1358 ap.non_native_field_gate_1 + ap.non_native_field_gate_2 + ap.non_native_field_gate_3;
1359 non_native_field_identity = non_native_field_identity * wire(p, WIRE.Q_R);
1361 // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm
1363 ap.limb_accumulator_1 = wire(p, WIRE.W_R_SHIFT) * SUBLIMB_SHIFT;
1364 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L_SHIFT);
1365 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1366 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_O);
1367 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1368 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_R);
1369 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1370 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L);
1371 ap.limb_accumulator_1 = ap.limb_accumulator_1 - wire(p, WIRE.W_4);
1372 ap.limb_accumulator_1 = ap.limb_accumulator_1 * wire(p, WIRE.Q_4);
1374 // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm
1376 ap.limb_accumulator_2 = wire(p, WIRE.W_O_SHIFT) * SUBLIMB_SHIFT;
1377 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_R_SHIFT);
1378 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1379 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_L_SHIFT);
1380 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1381 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_4);
1382 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1383 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_O);
1384 ap.limb_accumulator_2 = ap.limb_accumulator_2 - wire(p, WIRE.W_4_SHIFT);
1385 ap.limb_accumulator_2 = ap.limb_accumulator_2 * wire(p, WIRE.Q_M);
1387 Fr limb_accumulator_identity = ap.limb_accumulator_1 + ap.limb_accumulator_2;
1388 limb_accumulator_identity = limb_accumulator_identity * wire(p, WIRE.Q_O); // deg 3
1390 ap.nnf_identity = non_native_field_identity + limb_accumulator_identity;
1391 ap.nnf_identity = ap.nnf_identity * (wire(p, WIRE.Q_NNF) * domainSep);
1392 evals[22] = ap.nnf_identity;
1395 function accumulatePoseidonExternalRelation(
1396 Fr[NUMBER_OF_ENTITIES] memory p,
1397 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1400 PoseidonExternalParams memory ep;
1402 ep.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1403 ep.s2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_R);
1404 ep.s3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_O);
1405 ep.s4 = wire(p, WIRE.W_4) + wire(p, WIRE.Q_4);
1407 ep.u1 = ep.s1 * ep.s1 * ep.s1 * ep.s1 * ep.s1;
1408 ep.u2 = ep.s2 * ep.s2 * ep.s2 * ep.s2 * ep.s2;
1409 ep.u3 = ep.s3 * ep.s3 * ep.s3 * ep.s3 * ep.s3;
1410 ep.u4 = ep.s4 * ep.s4 * ep.s4 * ep.s4 * ep.s4;
1411 // matrix mul v = M_E * u with 14 additions
1412 ep.t0 = ep.u1 + ep.u2; // u_1 + u_2
1413 ep.t1 = ep.u3 + ep.u4; // u_3 + u_4
1414 ep.t2 = ep.u2 + ep.u2 + ep.t1; // 2u_2
1415 // ep.t2 += ep.t1; // 2u_2 + u_3 + u_4
1416 ep.t3 = ep.u4 + ep.u4 + ep.t0; // 2u_4
1417 // ep.t3 += ep.t0; // u_1 + u_2 + 2u_4
1418 ep.v4 = ep.t1 + ep.t1;
1419 ep.v4 = ep.v4 + ep.v4 + ep.t3;
1420 // ep.v4 += ep.t3; // u_1 + u_2 + 4u_3 + 6u_4
1421 ep.v2 = ep.t0 + ep.t0;
1422 ep.v2 = ep.v2 + ep.v2 + ep.t2;
1423 // ep.v2 += ep.t2; // 4u_1 + 6u_2 + u_3 + u_4
1424 ep.v1 = ep.t3 + ep.v2; // 5u_1 + 7u_2 + u_3 + 3u_4
1425 ep.v3 = ep.t2 + ep.v4; // u_1 + 3u_2 + 5u_3 + 7u_4
1427 ep.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_EXTERNAL) * domainSep;
1428 evals[23] = evals[23] + ep.q_pos_by_scaling * (ep.v1 - wire(p, WIRE.W_L_SHIFT));
1430 evals[24] = evals[24] + ep.q_pos_by_scaling * (ep.v2 - wire(p, WIRE.W_R_SHIFT));
1432 evals[25] = evals[25] + ep.q_pos_by_scaling * (ep.v3 - wire(p, WIRE.W_O_SHIFT));
1434 evals[26] = evals[26] + ep.q_pos_by_scaling * (ep.v4 - wire(p, WIRE.W_4_SHIFT));
1437 function accumulatePoseidonInternalRelation(
1438 Fr[NUMBER_OF_ENTITIES] memory p,
1439 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1442 PoseidonInternalParams memory ip;
1444 Fr[4] memory INTERNAL_MATRIX_DIAGONAL = [
1445 FrLib.from(0x10dc6e9c006ea38b04b1e03b4bd9490c0d03f98929ca1d7fb56821fd19d3b6e7),
1446 FrLib.from(0x0c28145b6a44df3e0149b3d0a30b3bb599df9756d4dd9b84a86b38cfb45a740b),
1447 FrLib.from(0x00544b8338791518b2c7645a50392798b21f75bb60e3596170067d00141cac15),
1448 FrLib.from(0x222c01175718386f2e2e82eb122789e352e105a3b8fa852613bc534433ee428b)
1451 // add round constants
1452 ip.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1454 // apply s-box round
1455 ip.u1 = ip.s1 * ip.s1 * ip.s1 * ip.s1 * ip.s1;
1456 ip.u2 = wire(p, WIRE.W_R);
1457 ip.u3 = wire(p, WIRE.W_O);
1458 ip.u4 = wire(p, WIRE.W_4);
1460 // matrix mul with v = M_I * u 4 muls and 7 additions
1461 ip.u_sum = ip.u1 + ip.u2 + ip.u3 + ip.u4;
1463 ip.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_INTERNAL) * domainSep;
1465 ip.v1 = ip.u1 * INTERNAL_MATRIX_DIAGONAL[0] + ip.u_sum;
1466 evals[27] = evals[27] + ip.q_pos_by_scaling * (ip.v1 - wire(p, WIRE.W_L_SHIFT));
1468 ip.v2 = ip.u2 * INTERNAL_MATRIX_DIAGONAL[1] + ip.u_sum;
1469 evals[28] = evals[28] + ip.q_pos_by_scaling * (ip.v2 - wire(p, WIRE.W_R_SHIFT));
1471 ip.v3 = ip.u3 * INTERNAL_MATRIX_DIAGONAL[2] + ip.u_sum;
1472 evals[29] = evals[29] + ip.q_pos_by_scaling * (ip.v3 - wire(p, WIRE.W_O_SHIFT));
1474 ip.v4 = ip.u4 * INTERNAL_MATRIX_DIAGONAL[3] + ip.u_sum;
1475 evals[30] = evals[30] + ip.q_pos_by_scaling * (ip.v4 - wire(p, WIRE.W_4_SHIFT));
1478 // Batch subrelation evaluations using precomputed powers of alpha
1479 // First subrelation is implicitly scaled by 1, subsequent ones use powers from the subrelationChallenges array
1480 function scaleAndBatchSubrelations(
1481 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations,
1482 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges
1483 ) internal pure returns (Fr accumulator) {
1484 accumulator = evaluations[0];
1486 for (uint256 i = 1; i < NUMBER_OF_SUBRELATIONS; ++i) {
1487 accumulator = accumulator + evaluations[i] * subrelationChallenges[i - 1];
1492library CommitmentSchemeLib {
1495 // Avoid stack too deep
1496 struct ShpleminiIntermediates {
1499 Fr unshiftedScalarNeg;
1500 Fr shiftedScalarNeg;
1501 // Scalar to be multiplied by [1]₁
1502 Fr constantTermAccumulator;
1503 // Accumulator for powers of rho
1504 Fr batchingChallenge;
1505 // Linear combination of multilinear (sumcheck) evaluations and powers of rho
1506 Fr batchedEvaluation;
1507 Fr[NUM_SMALL_IPA_OPENING_CLAIMS] denominators;
1508 Fr[NUM_SMALL_IPA_OPENING_CLAIMS] batchingScalars;
1509 // 1/(z - r^{2^i}) for i = 0, ..., logSize, dynamically updated
1510 Fr posInvertedDenominator;
1511 // 1/(z + r^{2^i}) for i = 0, ..., logSize, dynamically updated
1512 Fr negInvertedDenominator;
1513 // ν^{2i} * 1/(z - r^{2^i})
1514 Fr scalingFactorPos;
1515 // ν^{2i+1} * 1/(z + r^{2^i})
1516 Fr scalingFactorNeg;
1517 // Fold_i(r^{2^i}) reconstructed by Verifier
1518 Fr[] foldPosEvaluations;
1521 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., m-1
1522 function computeFoldPosEvaluations(
1523 Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckUChallenges,
1524 Fr batchedEvalAccumulator,
1525 Fr[CONST_PROOF_SIZE_LOG_N] memory geminiEvaluations,
1526 Fr[] memory geminiEvalChallengePowers,
1528 ) internal view returns (Fr[] memory) {
1529 Fr[] memory foldPosEvaluations = new Fr[](logSize);
1530 for (uint256 i = logSize; i > 0; --i) {
1531 Fr challengePower = geminiEvalChallengePowers[i - 1];
1532 Fr u = sumcheckUChallenges[i - 1];
1534 Fr batchedEvalRoundAcc = ((challengePower * batchedEvalAccumulator * Fr.wrap(2)) - geminiEvaluations[i - 1]
1535 * (challengePower * (ONE - u) - u));
1536 // Divide by the denominator
1537 batchedEvalRoundAcc = batchedEvalRoundAcc * (challengePower * (ONE - u) + u).invert();
1539 batchedEvalAccumulator = batchedEvalRoundAcc;
1540 foldPosEvaluations[i - 1] = batchedEvalRoundAcc;
1542 return foldPosEvaluations;
1545 function computeSquares(Fr r, uint256 logN) internal pure returns (Fr[] memory) {
1546 Fr[] memory squares = new Fr[](logN);
1548 for (uint256 i = 1; i < logN; ++i) {
1549 squares[i] = squares[i - 1].sqr();
1555uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
1559function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
1560 scalar = FrLib.fromBytes32(bytes32(proofSection));
1563// EC Point utilities
1564function bytesToG1Point(bytes calldata proofSection) pure returns (Honk.G1Point memory point) {
1565 uint256 x = uint256(bytes32(proofSection[0x00:0x20]));
1566 uint256 y = uint256(bytes32(proofSection[0x20:0x40]));
1567 require(x < Q && y < Q, Errors.ValueGeGroupOrder());
1569 // (0,0) is the canonical EIP-196 encoding of the identity. It is accepted here
1570 // because polynomial commitments to identically-zero polynomials (e.g. unused
1571 // selector or table polys) are legitimately the identity. On-curve validation
1572 // (y² = x³ + 3) is handled by the ecAdd/ecMul precompiles per EIP-196.
1573 point = Honk.G1Point({x: x, y: y});
1576function negateInplace(Honk.G1Point memory point) pure returns (Honk.G1Point memory) {
1577 // When y == 0 (order-2 point), negation is the same point. Q - 0 = Q which is >= Q.
1579 point.y = Q - point.y;
1597function convertPairingPointsToG1(Fr[PAIRING_POINTS_SIZE] memory pairingPoints)
1599 returns (Honk.G1Point memory lhs, Honk.G1Point memory rhs)
1601 // P0 (lhs): x = lo | (hi << 136)
1602 uint256 lhsX = Fr.unwrap(pairingPoints[0]);
1603 lhsX |= Fr.unwrap(pairingPoints[1]) << 136;
1605 uint256 lhsY = Fr.unwrap(pairingPoints[2]);
1606 lhsY |= Fr.unwrap(pairingPoints[3]) << 136;
1608 // P1 (rhs): x = lo | (hi << 136)
1609 uint256 rhsX = Fr.unwrap(pairingPoints[4]);
1610 rhsX |= Fr.unwrap(pairingPoints[5]) << 136;
1612 uint256 rhsY = Fr.unwrap(pairingPoints[6]);
1613 rhsY |= Fr.unwrap(pairingPoints[7]) << 136;
1615 // Reconstructed coordinates must be < Q to prevent malleability.
1616 // Without this, two different limb encodings could map to the same curve point
1617 // (via mulmod reduction in on-curve checks) but produce different transcript hashes.
1618 require(lhsX < Q && lhsY < Q && rhsX < Q && rhsY < Q, Errors.ValueGeGroupOrder());
1634function generateRecursionSeparator(
1635 Fr[PAIRING_POINTS_SIZE] memory proofPairingPoints,
1636 Honk.G1Point memory accLhs,
1637 Honk.G1Point memory accRhs
1638) pure returns (Fr recursionSeparator) {
1639 // hash the proof aggregated X
1640 // hash the proof aggregated Y
1644 (Honk.G1Point memory proofLhs, Honk.G1Point memory proofRhs) = convertPairingPointsToG1(proofPairingPoints);
1646 uint256[8] memory recursionSeparatorElements;
1649 recursionSeparatorElements[0] = proofLhs.x;
1650 recursionSeparatorElements[1] = proofLhs.y;
1651 recursionSeparatorElements[2] = proofRhs.x;
1652 recursionSeparatorElements[3] = proofRhs.y;
1654 // Accumulator points
1655 recursionSeparatorElements[4] = accLhs.x;
1656 recursionSeparatorElements[5] = accLhs.y;
1657 recursionSeparatorElements[6] = accRhs.x;
1658 recursionSeparatorElements[7] = accRhs.y;
1660 recursionSeparator = FrLib.from(uint256(keccak256(abi.encodePacked(recursionSeparatorElements))) % P);
1672function mulWithSeperator(Honk.G1Point memory basePoint, Honk.G1Point memory other, Fr recursionSeperator)
1674 returns (Honk.G1Point memory)
1676 Honk.G1Point memory result;
1678 result = ecMul(recursionSeperator, basePoint);
1679 result = ecAdd(result, other);
1692function ecMul(Fr value, Honk.G1Point memory point) view returns (Honk.G1Point memory) {
1693 Honk.G1Point memory result;
1696 let free := mload(0x40)
1697 // Write the point into memory (two 32 byte words)
1701 // free + 0x20| point.y
1702 mstore(free, mload(point))
1703 mstore(add(free, 0x20), mload(add(point, 0x20)))
1704 // Write the scalar into memory (one 32 byte word)
1707 // free + 0x40| value
1708 mstore(add(free, 0x40), value)
1710 // Call the ecMul precompile, it takes in the following
1711 // [point.x, point.y, scalar], and returns the result back into the free memory location.
1712 let success := staticcall(gas(), 0x07, free, 0x60, free, 0x40)
1713 if iszero(success) {
1716 // Copy the result of the multiplication back into the result memory location.
1719 // result | result.x
1720 // result + 0x20| result.y
1721 mstore(result, mload(free))
1722 mstore(add(result, 0x20), mload(add(free, 0x20)))
1724 mstore(0x40, add(free, 0x60))
1738function ecAdd(Honk.G1Point memory lhs, Honk.G1Point memory rhs) view returns (Honk.G1Point memory) {
1739 Honk.G1Point memory result;
1742 let free := mload(0x40)
1743 // Write lhs into memory (two 32 byte words)
1747 // free + 0x20| lhs.y
1748 mstore(free, mload(lhs))
1749 mstore(add(free, 0x20), mload(add(lhs, 0x20)))
1751 // Write rhs into memory (two 32 byte words)
1754 // free + 0x40| rhs.x
1755 // free + 0x60| rhs.y
1756 mstore(add(free, 0x40), mload(rhs))
1757 mstore(add(free, 0x60), mload(add(rhs, 0x20)))
1759 // Call the ecAdd precompile, it takes in the following
1760 // [lhs.x, lhs.y, rhs.x, rhs.y], and returns their addition back into the free memory location.
1761 let success := staticcall(gas(), 0x06, free, 0x80, free, 0x40)
1762 if iszero(success) { revert(0, 0) }
1764 // Copy the result of the addition back into the result memory location.
1767 // result | result.x
1768 // result + 0x20| result.y
1769 mstore(result, mload(free))
1770 mstore(add(result, 0x20), mload(add(free, 0x20)))
1772 mstore(0x40, add(free, 0x80))
1778function rejectPointAtInfinity(Honk.G1Point memory point) pure {
1779 require((point.x | point.y) != 0, Errors.PointAtInfinity());
1786function arePairingPointsDefault(Fr[PAIRING_POINTS_SIZE] memory pairingPoints) pure returns (bool) {
1788 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1789 acc |= Fr.unwrap(pairingPoints[i]);
1794function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool decodedResult) {
1795 bytes memory input = abi.encodePacked(
1799 uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
1800 uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
1801 uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
1802 uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
1806 uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
1807 uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
1808 uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
1809 uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
1812 (bool success, bytes memory result) = address(0x08).staticcall(input);
1813 decodedResult = success && abi.decode(result, (bool));
1816abstract contract BaseZKHonkVerifier is IVerifier {
1819 struct PairingInputs {
1824 struct SmallSubgroupIpaIntermediates {
1825 Fr[SUBGROUP_SIZE] challengePolyLagrange;
1826 Fr challengePolyEval;
1830 Fr[SUBGROUP_SIZE] denominators; // this has to disappear
1834 // Constants for proof length calculation (matching UltraKeccakZKFlavor)
1835 uint256 internal constant NUM_WITNESS_ENTITIES = 8 + NUM_MASKING_POLYNOMIALS;
1836 uint256 internal constant NUM_ELEMENTS_COMM = 2; // uint256 elements for curve points
1837 uint256 internal constant NUM_ELEMENTS_FR = 1; // uint256 elements for field elements
1838 uint256 internal constant NUM_LIBRA_EVALUATIONS = 4; // libra evaluations
1840 uint256 internal constant LIBRA_COMMITMENTS = 3;
1841 uint256 internal constant LIBRA_EVALUATIONS = 4;
1842 uint256 internal constant LIBRA_UNIVARIATES_LENGTH = 9;
1844 uint256 internal constant SHIFTED_COMMITMENTS_START = 30;
1845 uint256 internal constant PERMUTATION_ARGUMENT_VALUE_SEPARATOR = 1 << 28;
1847 uint256 internal immutable $N;
1848 uint256 internal immutable $LOG_N;
1849 uint256 internal immutable $VK_HASH;
1850 uint256 internal immutable $NUM_PUBLIC_INPUTS;
1851 uint256 internal immutable $MSMSize;
1853 constructor(uint256 _N, uint256 _logN, uint256 _vkHash, uint256 _numPublicInputs) {
1857 $NUM_PUBLIC_INPUTS = _numPublicInputs;
1858 $MSMSize = NUMBER_UNSHIFTED_ZK + _logN + LIBRA_COMMITMENTS + 2;
1861 function verify(bytes calldata proof, bytes32[] calldata publicInputs)
1865 returns (bool verified)
1867 // Calculate expected proof size based on $LOG_N
1868 uint256 expectedProofSize = calculateProofSize($LOG_N);
1870 // Check the received proof is the expected size where each field element is 32 bytes
1872 proof.length == expectedProofSize, Errors.ProofLengthWrongWithLogN($LOG_N, proof.length, expectedProofSize)
1875 Honk.VerificationKey memory vk = loadVerificationKey();
1876 Honk.ZKProof memory p = ZKTranscriptLib.loadProof(proof, $LOG_N);
1878 require(publicInputs.length == vk.publicInputsSize - PAIRING_POINTS_SIZE, Errors.PublicInputsLengthWrong());
1880 // Generate the fiat shamir challenges for the whole protocol
1881 ZKTranscript memory t =
1882 ZKTranscriptLib.generateTranscript(p, publicInputs, $VK_HASH, $NUM_PUBLIC_INPUTS, $LOG_N);
1884 // Derive public input delta
1885 t.relationParameters.publicInputsDelta = computePublicInputDelta(
1887 p.pairingPointObject,
1888 t.relationParameters.beta,
1889 t.relationParameters.gamma,
1890 5 // pubInputsOffset = NUM_DISABLED_ROWS_IN_SUMCHECK + NUM_ZERO_ROWS = 4 + 1
1894 require(verifySumcheck(p, t), Errors.SumcheckFailed());
1895 require(verifyShplemini(p, vk, t), Errors.ShpleminiFailed());
1900 function computePublicInputDelta(
1901 bytes32[] memory publicInputs,
1902 Fr[PAIRING_POINTS_SIZE] memory pairingPointObject,
1906 ) internal view returns (Fr publicInputDelta) {
1907 Fr numerator = Fr.wrap(1);
1908 Fr denominator = Fr.wrap(1);
1910 Fr numeratorAcc = gamma + (beta * FrLib.from(PERMUTATION_ARGUMENT_VALUE_SEPARATOR + offset));
1911 Fr denominatorAcc = gamma - (beta * FrLib.from(offset + 1));
1914 for (uint256 i = 0; i < $NUM_PUBLIC_INPUTS - PAIRING_POINTS_SIZE; i++) {
1915 Fr pubInput = FrLib.fromBytes32(publicInputs[i]);
1917 numerator = numerator * (numeratorAcc + pubInput);
1918 denominator = denominator * (denominatorAcc + pubInput);
1920 numeratorAcc = numeratorAcc + beta;
1921 denominatorAcc = denominatorAcc - beta;
1924 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1925 Fr pubInput = pairingPointObject[i];
1927 numerator = numerator * (numeratorAcc + pubInput);
1928 denominator = denominator * (denominatorAcc + pubInput);
1930 numeratorAcc = numeratorAcc + beta;
1931 denominatorAcc = denominatorAcc - beta;
1935 // Fr delta = numerator / denominator; // TOOO: batch invert later?
1936 publicInputDelta = FrLib.div(numerator, denominator);
1939 function verifySumcheck(Honk.ZKProof memory proof, ZKTranscript memory tp) internal view returns (bool verified) {
1940 Fr roundTargetSum = tp.libraChallenge * proof.libraSum; // default 0
1941 Fr powPartialEvaluation = Fr.wrap(1);
1943 // We perform sumcheck reductions over log n rounds ( the multivariate degree )
1944 for (uint256 round; round < $LOG_N; ++round) {
1945 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate = proof.sumcheckUnivariates[round];
1946 Fr totalSum = roundUnivariate[0] + roundUnivariate[1];
1947 require(totalSum == roundTargetSum, Errors.SumcheckFailed());
1949 Fr roundChallenge = tp.sumCheckUChallenges[round];
1951 // Update the round target for the next rounf
1952 roundTargetSum = computeNextTargetSum(roundUnivariate, roundChallenge);
1953 powPartialEvaluation =
1954 powPartialEvaluation * (Fr.wrap(1) + roundChallenge * (tp.gateChallenges[round] - Fr.wrap(1)));
1958 // For ZK flavors: sumcheckEvaluations has 42 elements
1959 // Index 0 is gemini_masking_poly, indices 1-41 are the regular entities used in relations
1960 Fr[NUMBER_OF_ENTITIES] memory relationsEvaluations;
1961 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
1962 relationsEvaluations[i] = proof.sumcheckEvaluations[i + NUM_MASKING_POLYNOMIALS]; // Skip gemini_masking_poly at index 0
1964 Fr grandHonkRelationSum = RelationsLib.accumulateRelationEvaluations(
1965 relationsEvaluations, tp.relationParameters, tp.alphas, powPartialEvaluation
1968 // Row-disabling polynomial: 1 - ∏_{i≥2}(1 - u_i)
1969 Fr evaluation = Fr.wrap(1);
1970 for (uint256 i = 2; i < $LOG_N; i++) {
1971 evaluation = evaluation * (Fr.wrap(1) - tp.sumCheckUChallenges[i]);
1974 grandHonkRelationSum =
1975 grandHonkRelationSum * (Fr.wrap(1) - evaluation) + proof.libraEvaluation * tp.libraChallenge;
1976 verified = (grandHonkRelationSum == roundTargetSum);
1979 // Return the new target sum for the next sumcheck round
1980 function computeNextTargetSum(Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariates, Fr roundChallenge)
1983 returns (Fr targetSum)
1985 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH] memory BARYCENTRIC_LAGRANGE_DENOMINATORS = [
1986 Fr.wrap(0x0000000000000000000000000000000000000000000000000000000000009d80),
1987 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51),
1988 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000005a0),
1989 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffd31),
1990 Fr.wrap(0x0000000000000000000000000000000000000000000000000000000000000240),
1991 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffd31),
1992 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000005a0),
1993 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51),
1994 Fr.wrap(0x0000000000000000000000000000000000000000000000000000000000009d80)
1997 // To compute the next target sum, we evaluate the given univariate at a point u (challenge).
1999 // Performing Barycentric evaluations
2001 Fr numeratorValue = Fr.wrap(1);
2002 for (uint256 i = 0; i < ZK_BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
2003 numeratorValue = numeratorValue * (roundChallenge - Fr.wrap(i));
2006 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH] memory denominatorInverses;
2007 for (uint256 i = 0; i < ZK_BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
2008 denominatorInverses[i] = FrLib.invert(BARYCENTRIC_LAGRANGE_DENOMINATORS[i] * (roundChallenge - Fr.wrap(i)));
2011 for (uint256 i = 0; i < ZK_BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
2012 targetSum = targetSum + roundUnivariates[i] * denominatorInverses[i];
2015 // Scale the sum by the value of B(x)
2016 targetSum = targetSum * numeratorValue;
2019 function verifyShplemini(Honk.ZKProof memory proof, Honk.VerificationKey memory vk, ZKTranscript memory tp)
2022 returns (bool verified)
2024 CommitmentSchemeLib.ShpleminiIntermediates memory mem; // stack
2026 // - Compute vector (r, r², ... , r²⁽ⁿ⁻¹⁾), where n = log_circuit_size
2027 Fr[] memory powers_of_evaluation_challenge = CommitmentSchemeLib.computeSquares(tp.geminiR, $LOG_N);
2028 // Arrays hold values that will be linearly combined for the gemini and shplonk batch openings
2029 Fr[] memory scalars = new Fr[]($MSMSize);
2030 Honk.G1Point[] memory commitments = new Honk.G1Point[]($MSMSize);
2032 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[0]).invert();
2033 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[0]).invert();
2035 mem.unshiftedScalar = mem.posInvertedDenominator + (tp.shplonkNu * mem.negInvertedDenominator);
2037 tp.geminiR.invert() * (mem.posInvertedDenominator - (tp.shplonkNu * mem.negInvertedDenominator));
2039 scalars[0] = Fr.wrap(1);
2040 commitments[0] = proof.shplonkQ;
2042 /* Batch multivariate opening claims, shifted and unshifted
2043 * The vector of scalars is populated as follows:
2046 * - \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
2048 * - \rho^{i+k-1} \times \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
2049 * - \rho^{i+k} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right),
2051 * - \rho^{k+m-1} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right)
2055 * The following vector is concatenated to the vector of commitments:
2057 * f_0, \ldots, f_{m-1}, f_{\text{shift}, 0}, \ldots, f_{\text{shift}, k-1}
2060 * Simultaneously, the evaluation of the multilinear polynomial
2062 * \sum \rho^i \cdot f_i + \sum \rho^{i+k} \cdot f_{\text{shift}, i}
2064 * at the challenge point \f$ (u_0,\ldots, u_{n-1}) \f$ is computed.
2066 * This approach minimizes the number of iterations over the commitments to multilinear polynomials
2067 * and eliminates the need to store the powers of \f$ \rho \f$.
2069 // For ZK flavors: evaluations array is [gemini_masking_poly, qm, qc, ql, qr, ...]
2070 // Start batching challenge at 1, not rho, to match non-ZK pattern
2071 mem.batchingChallenge = Fr.wrap(1);
2072 mem.batchedEvaluation = Fr.wrap(0);
2074 mem.unshiftedScalarNeg = mem.unshiftedScalar.neg();
2075 mem.shiftedScalarNeg = mem.shiftedScalar.neg();
2077 // Process all NUMBER_UNSHIFTED_ZK evaluations (includes gemini_masking_poly at index 0)
2078 for (uint256 i = 1; i <= NUMBER_UNSHIFTED_ZK; ++i) {
2079 scalars[i] = mem.unshiftedScalarNeg * mem.batchingChallenge;
2080 mem.batchedEvaluation = mem.batchedEvaluation
2081 + (proof.sumcheckEvaluations[i - NUM_MASKING_POLYNOMIALS] * mem.batchingChallenge);
2082 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
2084 // g commitments are accumulated at r
2085 // For each of the to be shifted commitments perform the shift in place by
2086 // adding to the unshifted value.
2087 // We do so, as the values are to be used in batchMul later, and as
2088 // `a * c + b * c = (a + b) * c` this will allow us to reduce memory and compute.
2089 // Applied to w1, w2, w3, w4 and zPerm
2090 for (uint256 i = 0; i < NUMBER_TO_BE_SHIFTED; ++i) {
2091 uint256 scalarOff = i + SHIFTED_COMMITMENTS_START;
2092 uint256 evaluationOff = i + NUMBER_UNSHIFTED_ZK;
2094 scalars[scalarOff] = scalars[scalarOff] + (mem.shiftedScalarNeg * mem.batchingChallenge);
2095 mem.batchedEvaluation =
2096 mem.batchedEvaluation + (proof.sumcheckEvaluations[evaluationOff] * mem.batchingChallenge);
2097 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
2100 commitments[1] = proof.geminiMaskingPoly;
2102 commitments[2] = vk.s1;
2103 commitments[3] = vk.s2;
2104 commitments[4] = vk.s3;
2105 commitments[5] = vk.s4;
2106 commitments[6] = vk.id1;
2107 commitments[7] = vk.id2;
2108 commitments[8] = vk.id3;
2109 commitments[9] = vk.id4;
2110 commitments[10] = vk.lagrangeFirst;
2111 commitments[11] = vk.lagrangeLast;
2112 commitments[12] = vk.qLookup;
2113 commitments[13] = vk.t1;
2114 commitments[14] = vk.t2;
2115 commitments[15] = vk.t3;
2116 commitments[16] = vk.t4;
2117 commitments[17] = vk.qm;
2118 commitments[18] = vk.qr;
2119 commitments[19] = vk.qo;
2120 commitments[20] = vk.qc;
2121 commitments[21] = vk.ql;
2122 commitments[22] = vk.q4;
2123 commitments[23] = vk.qArith;
2124 commitments[24] = vk.qDeltaRange;
2125 commitments[25] = vk.qElliptic;
2126 commitments[26] = vk.qMemory;
2127 commitments[27] = vk.qNnf;
2128 commitments[28] = vk.qPoseidon2External;
2129 commitments[29] = vk.qPoseidon2Internal;
2131 // Accumulate proof points
2132 commitments[30] = proof.w1;
2133 commitments[31] = proof.w2;
2134 commitments[32] = proof.w3;
2135 commitments[33] = proof.w4;
2136 commitments[34] = proof.zPerm;
2137 commitments[35] = proof.lookupInverses;
2138 commitments[36] = proof.lookupReadCounts;
2139 commitments[37] = proof.lookupReadTags;
2141 /* Batch gemini claims from the prover
2142 * place the commitments to gemini aᵢ to the vector of commitments, compute the contributions from
2143 * aᵢ(−r²ⁱ) for i=1, … , n−1 to the constant term accumulator, add corresponding scalars
2145 * 1. Moves the vector
2147 * \left( \text{com}(A_1), \text{com}(A_2), \ldots, \text{com}(A_{n-1}) \right)
2149 * to the 'commitments' vector.
2151 * 2. Computes the scalars:
2153 * \frac{\nu^{2}}{z + r^2}, \frac{\nu^3}{z + r^4}, \ldots, \frac{\nu^{n-1}}{z + r^{2^{n-1}}}
2155 * and places them into the 'scalars' vector.
2157 * 3. Accumulates the summands of the constant term:
2159 * \sum_{i=2}^{n-1} \frac{\nu^{i} \cdot A_i(-r^{2^i})}{z + r^{2^i}}
2161 * and adds them to the 'constant_term_accumulator'.
2164 // Add contributions from A₀(r) and A₀(-r) to constant_term_accumulator:
2165 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., $LOG_N - 1
2166 Fr[] memory foldPosEvaluations = CommitmentSchemeLib.computeFoldPosEvaluations(
2167 tp.sumCheckUChallenges,
2168 mem.batchedEvaluation,
2169 proof.geminiAEvaluations,
2170 powers_of_evaluation_challenge,
2174 mem.constantTermAccumulator = foldPosEvaluations[0] * mem.posInvertedDenominator;
2175 mem.constantTermAccumulator =
2176 mem.constantTermAccumulator + (proof.geminiAEvaluations[0] * tp.shplonkNu * mem.negInvertedDenominator);
2178 mem.batchingChallenge = tp.shplonkNu.sqr();
2179 uint256 boundary = NUMBER_UNSHIFTED_ZK + 1;
2181 // Compute Shplonk constant term contributions from Aₗ(± r^{2ˡ}) for l = 1, ..., m-1;
2182 // Compute scalar multipliers for each fold commitment
2183 for (uint256 i = 0; i < $LOG_N - 1; ++i) {
2184 bool dummy_round = i >= ($LOG_N - 1);
2187 // Update inverted denominators
2188 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[i + 1]).invert();
2189 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[i + 1]).invert();
2191 // Compute the scalar multipliers for Aₗ(± r^{2ˡ}) and [Aₗ]
2192 mem.scalingFactorPos = mem.batchingChallenge * mem.posInvertedDenominator;
2193 mem.scalingFactorNeg = mem.batchingChallenge * tp.shplonkNu * mem.negInvertedDenominator;
2194 scalars[boundary + i] = mem.scalingFactorNeg.neg() + mem.scalingFactorPos.neg();
2196 // Accumulate the const term contribution given by
2197 // v^{2l} * Aₗ(r^{2ˡ}) /(z-r^{2^l}) + v^{2l+1} * Aₗ(-r^{2ˡ}) /(z+ r^{2^l})
2198 Fr accumContribution = mem.scalingFactorNeg * proof.geminiAEvaluations[i + 1];
2199 accumContribution = accumContribution + mem.scalingFactorPos * foldPosEvaluations[i + 1];
2200 mem.constantTermAccumulator = mem.constantTermAccumulator + accumContribution;
2202 // Update the running power of v
2203 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu * tp.shplonkNu;
2205 commitments[boundary + i] = proof.geminiFoldComms[i];
2208 boundary += $LOG_N - 1;
2210 // Denominators 1/(z - point_i) for the five opening points {r, g*r, r, 1, r}.
2211 mem.denominators[0] = ONE.div(tp.shplonkZ - tp.geminiR);
2212 mem.denominators[1] = ONE.div(tp.shplonkZ - SUBGROUP_GENERATOR * tp.geminiR);
2213 mem.denominators[2] = mem.denominators[0];
2214 mem.denominators[SMALL_IPA_BOUNDARY_OPENING_IDX] = ONE.div(tp.shplonkZ - ONE);
2215 mem.denominators[NUM_SMALL_IPA_OPENING_CLAIMS - 1] = mem.denominators[0];
2217 // Iterate the opening claims in three segments — the inner loops can't be merged without an extra induction
2218 // variable, which pushes us into stack-too-deep.
2219 for (uint256 i = 0; i < SMALL_IPA_BOUNDARY_OPENING_IDX; i++) {
2220 Fr scalingFactor = mem.denominators[i] * mem.batchingChallenge;
2221 mem.batchingScalars[i] = scalingFactor.neg();
2222 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu;
2223 mem.constantTermAccumulator = mem.constantTermAccumulator + scalingFactor * proof.libraPolyEvals[i];
2226 // Boundary slot: claimed value is hardcoded 0, so no constantTermAccumulator contribution.
2228 Fr scalingFactor = mem.denominators[SMALL_IPA_BOUNDARY_OPENING_IDX] * mem.batchingChallenge;
2229 mem.batchingScalars[SMALL_IPA_BOUNDARY_OPENING_IDX] = scalingFactor.neg();
2230 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu;
2233 for (uint256 i = SMALL_IPA_BOUNDARY_OPENING_IDX + 1; i < NUM_SMALL_IPA_OPENING_CLAIMS; i++) {
2234 Fr scalingFactor = mem.denominators[i] * mem.batchingChallenge;
2235 mem.batchingScalars[i] = scalingFactor.neg();
2236 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu;
2237 mem.constantTermAccumulator = mem.constantTermAccumulator + scalingFactor * proof.libraPolyEvals[i - 1];
2240 // Group per-claim batching scalars by commitment: [G], [A] (three openings), [Q].
2241 scalars[boundary] = mem.batchingScalars[0];
2242 scalars[boundary + 1] =
2243 mem.batchingScalars[1] + mem.batchingScalars[2] + mem.batchingScalars[SMALL_IPA_BOUNDARY_OPENING_IDX];
2244 scalars[boundary + 2] = mem.batchingScalars[NUM_SMALL_IPA_OPENING_CLAIMS - 1];
2246 for (uint256 i = 0; i < LIBRA_COMMITMENTS; i++) {
2247 commitments[boundary++] = proof.libraCommitments[i];
2250 commitments[boundary] = Honk.G1Point({x: 1, y: 2});
2251 scalars[boundary++] = mem.constantTermAccumulator;
2254 checkEvalsConsistency(proof.libraPolyEvals, tp.geminiR, tp.sumCheckUChallenges, proof.libraEvaluation),
2255 Errors.ConsistencyCheckFailed()
2258 Honk.G1Point memory quotient_commitment = proof.kzgQuotient;
2260 commitments[boundary] = quotient_commitment;
2261 scalars[boundary] = tp.shplonkZ; // evaluation challenge
2263 PairingInputs memory pair;
2264 pair.P_0 = batchMul(commitments, scalars);
2265 pair.P_1 = negateInplace(quotient_commitment);
2267 // Aggregate pairing points (skip if default/infinity — no recursive verification occurred)
2268 if (!arePairingPointsDefault(proof.pairingPointObject)) {
2269 Fr recursionSeparator = generateRecursionSeparator(proof.pairingPointObject, pair.P_0, pair.P_1);
2270 (Honk.G1Point memory P_0_other, Honk.G1Point memory P_1_other) =
2271 convertPairingPointsToG1(proof.pairingPointObject);
2273 // Validate the points from the proof are on the curve
2274 rejectPointAtInfinity(P_0_other);
2275 rejectPointAtInfinity(P_1_other);
2277 // accumulate with aggregate points in proof
2278 pair.P_0 = mulWithSeperator(pair.P_0, P_0_other, recursionSeparator);
2279 pair.P_1 = mulWithSeperator(pair.P_1, P_1_other, recursionSeparator);
2282 return pairing(pair.P_0, pair.P_1);
2285 function checkEvalsConsistency(
2286 Fr[LIBRA_EVALUATIONS] memory libraPolyEvals,
2288 Fr[CONST_PROOF_SIZE_LOG_N] memory uChallenges,
2290 ) internal view returns (bool check) {
2291 Fr one = Fr.wrap(1);
2292 Fr vanishingPolyEval = geminiR.pow(SUBGROUP_SIZE) - one;
2293 require(vanishingPolyEval != Fr.wrap(0), Errors.GeminiChallengeInSubgroup());
2295 SmallSubgroupIpaIntermediates memory mem;
2296 mem.challengePolyLagrange[0] = one;
2297 for (uint256 round = 0; round < $LOG_N; round++) {
2298 uint256 currIdx = 1 + LIBRA_UNIVARIATES_LENGTH * round;
2299 mem.challengePolyLagrange[currIdx] = one;
2300 for (uint256 idx = currIdx + 1; idx < currIdx + LIBRA_UNIVARIATES_LENGTH; idx++) {
2301 mem.challengePolyLagrange[idx] = mem.challengePolyLagrange[idx - 1] * uChallenges[round];
2305 mem.rootPower = one;
2306 mem.challengePolyEval = Fr.wrap(0);
2307 for (uint256 idx = 0; idx < SUBGROUP_SIZE; idx++) {
2308 mem.denominators[idx] = mem.rootPower * geminiR - one;
2309 mem.denominators[idx] = mem.denominators[idx].invert();
2310 mem.challengePolyEval = mem.challengePolyEval + mem.challengePolyLagrange[idx] * mem.denominators[idx];
2311 mem.rootPower = mem.rootPower * SUBGROUP_GENERATOR_INVERSE;
2314 Fr numerator = vanishingPolyEval * Fr.wrap(SUBGROUP_SIZE).invert();
2315 mem.challengePolyEval = mem.challengePolyEval * numerator;
2316 mem.lagrangeFirst = mem.denominators[0] * numerator;
2317 mem.lagrangeLast = mem.denominators[SUBGROUP_SIZE - 1] * numerator;
2319 mem.diff = mem.lagrangeFirst * libraPolyEvals[2];
2321 mem.diff = mem.diff + (geminiR - SUBGROUP_GENERATOR_INVERSE)
2322 * (libraPolyEvals[1] - libraPolyEvals[2] - libraPolyEvals[0] * mem.challengePolyEval);
2323 mem.diff = mem.diff + mem.lagrangeLast * (libraPolyEvals[2] - libraEval) - vanishingPolyEval * libraPolyEvals[3];
2325 check = mem.diff == Fr.wrap(0);
2328 // This implementation is the same as above with different constants
2329 function batchMul(Honk.G1Point[] memory base, Fr[] memory scalars)
2332 returns (Honk.G1Point memory result)
2334 uint256 limit = $MSMSize;
2336 // Identity bases are accepted: VK selector/table polys may be identically zero,
2337 // and the ecAdd/ecMul precompiles treat (0,0) as the additive identity per EIP-196.
2338 // Soundness against an attacker substituting (0,0) for a non-zero commitment is
2339 // upheld by sumcheck/Shplemini, which would fail on inconsistent evaluations.
2341 bool success = true;
2343 let free := mload(0x40)
2346 for {} lt(count, add(limit, 1)) { count := add(count, 1) } {
2348 let base_base := add(base, mul(count, 0x20))
2349 let scalar_base := add(scalars, mul(count, 0x20))
2351 mstore(add(free, 0x40), mload(mload(base_base)))
2352 mstore(add(free, 0x60), mload(add(0x20, mload(base_base))))
2354 mstore(add(free, 0x80), mload(scalar_base))
2356 success := and(success, staticcall(gas(), 7, add(free, 0x40), 0x60, add(free, 0x40), 0x40))
2357 // accumulator = accumulator + accumulator_2
2358 success := and(success, staticcall(gas(), 6, free, 0x80, free, 0x40))
2361 // Return the result
2362 mstore(result, mload(free))
2363 mstore(add(result, 0x20), mload(add(free, 0x20)))
2366 require(success, Errors.ShpleminiFailed());
2369 // Calculate proof size based on log_n (matching UltraKeccakZKFlavor formula)
2370 function calculateProofSize(uint256 logN) internal pure returns (uint256) {
2371 // Witness and Libra commitments
2372 uint256 proofLength = NUM_WITNESS_ENTITIES * NUM_ELEMENTS_COMM; // witness commitments
2373 proofLength += NUM_ELEMENTS_COMM * 3; // Libra concat, grand sum, quotient comms + Gemini masking
2376 proofLength += logN * ZK_BATCHED_RELATION_PARTIAL_LENGTH * NUM_ELEMENTS_FR; // sumcheck univariates
2377 proofLength += NUMBER_OF_ENTITIES_ZK * NUM_ELEMENTS_FR; // sumcheck evaluations
2380 proofLength += NUM_ELEMENTS_FR * 2; // Libra sum, claimed eval
2381 proofLength += logN * NUM_ELEMENTS_FR; // Gemini a evaluations
2382 proofLength += NUM_LIBRA_EVALUATIONS * NUM_ELEMENTS_FR; // libra evaluations
2385 proofLength += (logN - 1) * NUM_ELEMENTS_COMM; // Gemini Fold commitments
2386 proofLength += NUM_ELEMENTS_COMM * 2; // Shplonk Q and KZG W commitments
2389 proofLength += PAIRING_POINTS_SIZE; // pairing inputs carried on public inputs
2391 return proofLength * 32;
2394 function loadVerificationKey() internal pure virtual returns (Honk.VerificationKey memory);
2397contract HonkVerifier is BaseZKHonkVerifier(N, LOG_N, VK_HASH, NUMBER_OF_PUBLIC_INPUTS) {
2398 function loadVerificationKey() internal pure override returns (Honk.VerificationKey memory) {
2399 return HonkVerificationKey.loadVerificationKey();
2406 std::ostringstream stream;
2408 return stream.str() + HONK_ZK_CONTRACT_SOURCE;
void output_vk_sol_ultra_honk(std::ostream &os, auto const &key, std::string const &class_name, bool include_types_import=false)
std::string get_honk_zk_solidity_verifier(auto const &verification_key)