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);
23using {add as +} for Fr global;
24using {sub as -} for Fr global;
25using {mul as *} for Fr global;
27using {exp as ^} for Fr global;
28using {notEqual as !=} for Fr global;
29using {equal as ==} for Fr global;
31uint256 constant SUBGROUP_SIZE = 256;
32uint256 constant MODULUS = 21888242871839275222246405745257275088548364400416034343698204186575808495617; // Prime field order
33uint256 constant P = MODULUS;
34Fr constant SUBGROUP_GENERATOR = Fr.wrap(0x07b0c561a6148404f086204a9f36ffb0617942546750f230c893619174a57a76);
35Fr constant SUBGROUP_GENERATOR_INVERSE = Fr.wrap(0x204bd3277422fad364751ad938e2b5e6a54cf8c68712848a692c553d0329f5d6);
36Fr constant MINUS_ONE = Fr.wrap(MODULUS - 1);
37Fr constant ONE = Fr.wrap(1);
38Fr constant ZERO = Fr.wrap(0);
42 function from(uint256 value) internal pure returns (Fr) {
44 return Fr.wrap(value % MODULUS);
48 function fromBytes32(bytes32 value) internal pure returns (Fr) {
50 return Fr.wrap(uint256(value) % MODULUS);
54 function toBytes32(Fr value) internal pure returns (bytes32) {
56 return bytes32(Fr.unwrap(value));
60 function invert(Fr value) internal view returns (Fr) {
61 uint256 v = Fr.unwrap(value);
64 // Call the modexp precompile to invert in the field
66 let free := mload(0x40)
68 mstore(add(free, 0x20), 0x20)
69 mstore(add(free, 0x40), 0x20)
70 mstore(add(free, 0x60), v)
71 mstore(add(free, 0x80), sub(MODULUS, 2))
72 mstore(add(free, 0xa0), MODULUS)
73 let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
78 mstore(0x40, add(free, 0x80))
81 return Fr.wrap(result);
84 function pow(Fr base, uint256 v) internal view returns (Fr) {
85 uint256 b = Fr.unwrap(base);
88 // Call the modexp precompile to invert in the field
90 let free := mload(0x40)
92 mstore(add(free, 0x20), 0x20)
93 mstore(add(free, 0x40), 0x20)
94 mstore(add(free, 0x60), b)
95 mstore(add(free, 0x80), v)
96 mstore(add(free, 0xa0), MODULUS)
97 let success := staticcall(gas(), 0x05, free, 0xc0, 0x00, 0x20)
101 result := mload(0x00)
102 mstore(0x40, add(free, 0x80))
105 return Fr.wrap(result);
108 function div(Fr numerator, Fr denominator) internal view returns (Fr) {
110 return numerator * invert(denominator);
114 function sqr(Fr value) internal pure returns (Fr) {
116 return value * value;
120 function unwrap(Fr value) internal pure returns (uint256) {
122 return Fr.unwrap(value);
126 function neg(Fr value) internal pure returns (Fr) {
128 return Fr.wrap(MODULUS - Fr.unwrap(value));
134function add(Fr a, Fr b) pure returns (Fr) {
136 return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
140function mul(Fr a, Fr b) pure returns (Fr) {
142 return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
146function sub(Fr a, Fr b) pure returns (Fr) {
148 return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
152function exp(Fr base, Fr exponent) pure returns (Fr) {
153 if (Fr.unwrap(exponent) == 0) return Fr.wrap(1);
154 // Implement exponent with a loop as we will overflow otherwise
155 for (uint256 i = 1; i < Fr.unwrap(exponent); i += i) {
161function notEqual(Fr a, Fr b) pure returns (bool) {
163 return Fr.unwrap(a) != Fr.unwrap(b);
167function equal(Fr a, Fr b) pure returns (bool) {
169 return Fr.unwrap(a) == Fr.unwrap(b);
173uint256 constant CONST_PROOF_SIZE_LOG_N = 28;
175uint256 constant NUMBER_OF_SUBRELATIONS = 28;
176uint256 constant BATCHED_RELATION_PARTIAL_LENGTH = 8;
177uint256 constant ZK_BATCHED_RELATION_PARTIAL_LENGTH = 9;
178uint256 constant NUMBER_OF_ENTITIES = 41;
179uint256 constant NUMBER_UNSHIFTED = 36;
180uint256 constant NUMBER_TO_BE_SHIFTED = 5;
181uint256 constant PAIRING_POINTS_SIZE = 16;
183uint256 constant FIELD_ELEMENT_SIZE = 0x20;
184uint256 constant GROUP_ELEMENT_SIZE = 0x40;
186// Alphas are used as relation separators so there should be NUMBER_OF_SUBRELATIONS - 1
187uint256 constant NUMBER_OF_ALPHAS = NUMBER_OF_SUBRELATIONS - 1;
203 Q_POSEIDON2_EXTERNAL,
204 Q_POSEIDON2_INTERNAL,
240 struct VerificationKey {
243 uint256 logCircuitSize;
244 uint256 publicInputsSize;
252 G1Point qLookup; // Lookup
253 G1Point qArith; // Arithmetic widget
254 G1Point qDeltaRange; // Delta Range sort
255 G1Point qMemory; // Memory
256 G1Point qNnf; // Non-native Field
257 G1Point qElliptic; // Auxillary
258 G1Point qPoseidon2External;
259 G1Point qPoseidon2Internal;
270 // Precomputed lookup table
275 // Fixed first and last
276 G1Point lagrangeFirst;
277 G1Point lagrangeLast;
280 struct RelationParameters {
288 Fr publicInputsDelta;
292 // Pairing point object
293 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
299 // Lookup helpers - Permutations
301 // Lookup helpers - logup
302 G1Point lookupReadCounts;
303 G1Point lookupReadTags;
304 G1Point lookupInverses;
306 Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
307 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
309 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
310 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
316 // Pairing point object
317 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
318 // Commitments to wire polynomials
323 // Commitments to logup witness polynomials
324 G1Point lookupReadCounts;
325 G1Point lookupReadTags;
326 G1Point lookupInverses;
327 // Commitment to grand permutation polynomial
329 G1Point[3] libraCommitments;
332 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
333 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
336 G1Point geminiMaskingPoly;
337 Fr geminiMaskingEval;
339 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
340 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
341 Fr[4] libraPolyEvals;
347// Transcript library to generate fiat shamir challenges
350 Honk.RelationParameters relationParameters;
351 Fr[NUMBER_OF_ALPHAS] alphas;
352 Fr[CONST_PROOF_SIZE_LOG_N] gateChallenges;
354 Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
363library TranscriptLib {
364 function generateTranscript(
365 Honk.Proof memory proof,
366 bytes32[] calldata publicInputs,
368 uint256 publicInputsSize,
370 ) internal view returns (Transcript memory t) {
371 Fr previousChallenge;
372 (t.relationParameters, previousChallenge) =
373 generateRelationParametersChallenges(proof, publicInputs, vkHash, publicInputsSize, previousChallenge);
375 (t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
377 (t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge, logN);
379 (t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge, logN);
381 (t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
383 (t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge, logN);
385 (t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge, logN);
387 (t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
392 function splitChallenge(Fr challenge) internal pure returns (Fr first, Fr second) {
393 uint256 challengeU256 = uint256(Fr.unwrap(challenge));
394 uint256 lo = challengeU256 & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
395 uint256 hi = challengeU256 >> 128;
396 first = FrLib.fromBytes32(bytes32(lo));
397 second = FrLib.fromBytes32(bytes32(hi));
400 function generateRelationParametersChallenges(
401 Honk.Proof memory proof,
402 bytes32[] calldata publicInputs,
404 uint256 publicInputsSize,
406 ) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
407 (rp.eta, rp.etaTwo, rp.etaThree, previousChallenge) =
408 generateEtaChallenge(proof, publicInputs, vkHash, publicInputsSize);
410 (rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaAndGammaChallenges(previousChallenge, proof);
413 function generateEtaChallenge(
414 Honk.Proof memory proof,
415 bytes32[] calldata publicInputs,
417 uint256 publicInputsSize
418 ) internal pure returns (Fr eta, Fr etaTwo, Fr etaThree, Fr previousChallenge) {
419 bytes32[] memory round0 = new bytes32[](1 + publicInputsSize + 6);
420 round0[0] = bytes32(vkHash);
422 for (uint256 i = 0; i < publicInputsSize - PAIRING_POINTS_SIZE; i++) {
423 round0[1 + i] = bytes32(publicInputs[i]);
425 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
426 round0[1 + publicInputsSize - PAIRING_POINTS_SIZE + i] = FrLib.toBytes32(proof.pairingPointObject[i]);
429 // Create the first challenge
430 // Note: w4 is added to the challenge later on
431 round0[1 + publicInputsSize] = bytes32(proof.w1.x);
432 round0[1 + publicInputsSize + 1] = bytes32(proof.w1.y);
433 round0[1 + publicInputsSize + 2] = bytes32(proof.w2.x);
434 round0[1 + publicInputsSize + 3] = bytes32(proof.w2.y);
435 round0[1 + publicInputsSize + 4] = bytes32(proof.w3.x);
436 round0[1 + publicInputsSize + 5] = bytes32(proof.w3.y);
438 previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round0)));
439 (eta, etaTwo) = splitChallenge(previousChallenge);
441 previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge))));
443 (etaThree, unused) = splitChallenge(previousChallenge);
446 function generateBetaAndGammaChallenges(Fr previousChallenge, Honk.Proof memory proof)
449 returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
451 bytes32[7] memory round1;
452 round1[0] = FrLib.toBytes32(previousChallenge);
453 round1[1] = bytes32(proof.lookupReadCounts.x);
454 round1[2] = bytes32(proof.lookupReadCounts.y);
455 round1[3] = bytes32(proof.lookupReadTags.x);
456 round1[4] = bytes32(proof.lookupReadTags.y);
457 round1[5] = bytes32(proof.w4.x);
458 round1[6] = bytes32(proof.w4.y);
460 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round1)));
461 (beta, gamma) = splitChallenge(nextPreviousChallenge);
464 // Alpha challenges non-linearise the gate contributions
465 function generateAlphaChallenges(Fr previousChallenge, Honk.Proof memory proof)
468 returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
470 // Generate the original sumcheck alpha 0 by hashing zPerm and zLookup
471 uint256[5] memory alpha0;
472 alpha0[0] = Fr.unwrap(previousChallenge);
473 alpha0[1] = proof.lookupInverses.x;
474 alpha0[2] = proof.lookupInverses.y;
475 alpha0[3] = proof.zPerm.x;
476 alpha0[4] = proof.zPerm.y;
478 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(alpha0)));
479 (alphas[0], alphas[1]) = splitChallenge(nextPreviousChallenge);
481 for (uint256 i = 1; i < NUMBER_OF_ALPHAS / 2; i++) {
482 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(nextPreviousChallenge))));
483 (alphas[2 * i], alphas[2 * i + 1]) = splitChallenge(nextPreviousChallenge);
485 if (((NUMBER_OF_ALPHAS & 1) == 1) && (NUMBER_OF_ALPHAS > 2)) {
486 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(nextPreviousChallenge))));
488 (alphas[NUMBER_OF_ALPHAS - 1], unused) = splitChallenge(nextPreviousChallenge);
492 function generateGateChallenges(Fr previousChallenge, uint256 logN)
495 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
497 previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge))));
498 (gateChallenges[0],) = splitChallenge(previousChallenge);
499 for (uint256 i = 1; i < logN; i++) {
500 gateChallenges[i] = gateChallenges[i - 1] * gateChallenges[i - 1];
502 nextPreviousChallenge = previousChallenge;
505 function generateSumcheckChallenges(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
508 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
510 for (uint256 i = 0; i < logN; i++) {
511 Fr[BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
512 univariateChal[0] = prevChallenge;
514 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
515 univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
517 prevChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(univariateChal)));
519 (sumcheckChallenges[i], unused) = splitChallenge(prevChallenge);
521 nextPreviousChallenge = prevChallenge;
524 function generateRhoChallenge(Honk.Proof memory proof, Fr prevChallenge)
527 returns (Fr rho, Fr nextPreviousChallenge)
529 Fr[NUMBER_OF_ENTITIES + 1] memory rhoChallengeElements;
530 rhoChallengeElements[0] = prevChallenge;
532 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
533 rhoChallengeElements[i + 1] = proof.sumcheckEvaluations[i];
536 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(rhoChallengeElements)));
538 (rho, unused) = splitChallenge(nextPreviousChallenge);
541 function generateGeminiRChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
544 returns (Fr geminiR, Fr nextPreviousChallenge)
546 uint256[] memory gR = new uint256[]((logN - 1) * 2 + 1);
547 gR[0] = Fr.unwrap(prevChallenge);
549 for (uint256 i = 0; i < logN - 1; i++) {
550 gR[1 + i * 2] = proof.geminiFoldComms[i].x;
551 gR[2 + i * 2] = proof.geminiFoldComms[i].y;
554 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(gR)));
556 (geminiR, unused) = splitChallenge(nextPreviousChallenge);
559 function generateShplonkNuChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
562 returns (Fr shplonkNu, Fr nextPreviousChallenge)
564 uint256[] memory shplonkNuChallengeElements = new uint256[](logN + 1);
565 shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
567 for (uint256 i = 0; i < logN; i++) {
568 shplonkNuChallengeElements[i + 1] = Fr.unwrap(proof.geminiAEvaluations[i]);
571 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkNuChallengeElements)));
573 (shplonkNu, unused) = splitChallenge(nextPreviousChallenge);
576 function generateShplonkZChallenge(Honk.Proof memory proof, Fr prevChallenge)
579 returns (Fr shplonkZ, Fr nextPreviousChallenge)
581 uint256[3] memory shplonkZChallengeElements;
582 shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
584 shplonkZChallengeElements[1] = proof.shplonkQ.x;
585 shplonkZChallengeElements[2] = proof.shplonkQ.y;
587 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkZChallengeElements)));
589 (shplonkZ, unused) = splitChallenge(nextPreviousChallenge);
592 function loadProof(bytes calldata proof, uint256 logN) internal pure returns (Honk.Proof memory p) {
593 uint256 boundary = 0x00;
595 // Pairing point object
596 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
597 p.pairingPointObject[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
598 boundary += FIELD_ELEMENT_SIZE;
601 p.w1 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
602 boundary += GROUP_ELEMENT_SIZE;
603 p.w2 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
604 boundary += GROUP_ELEMENT_SIZE;
605 p.w3 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
606 boundary += GROUP_ELEMENT_SIZE;
608 // Lookup / Permutation Helper Commitments
609 p.lookupReadCounts = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
610 boundary += GROUP_ELEMENT_SIZE;
611 p.lookupReadTags = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
612 boundary += GROUP_ELEMENT_SIZE;
613 p.w4 = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
614 boundary += GROUP_ELEMENT_SIZE;
615 p.lookupInverses = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
616 boundary += GROUP_ELEMENT_SIZE;
617 p.zPerm = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
618 boundary += GROUP_ELEMENT_SIZE;
620 // Sumcheck univariates
621 for (uint256 i = 0; i < logN; i++) {
622 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
623 p.sumcheckUnivariates[i][j] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
624 boundary += FIELD_ELEMENT_SIZE;
627 // Sumcheck evaluations
628 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
629 p.sumcheckEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
630 boundary += FIELD_ELEMENT_SIZE;
634 // Read gemini fold univariates
635 for (uint256 i = 0; i < logN - 1; i++) {
636 p.geminiFoldComms[i] = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
637 boundary += GROUP_ELEMENT_SIZE;
640 // Read gemini a evaluations
641 for (uint256 i = 0; i < logN; i++) {
642 p.geminiAEvaluations[i] = bytesToFr(proof[boundary:boundary + FIELD_ELEMENT_SIZE]);
643 boundary += FIELD_ELEMENT_SIZE;
647 p.shplonkQ = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
648 boundary += GROUP_ELEMENT_SIZE;
650 p.kzgQuotient = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
654// Field arithmetic libraries
656library RelationsLib {
657 Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
659 function accumulateRelationEvaluations(
660 Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
661 Honk.RelationParameters memory rp,
662 Fr[NUMBER_OF_ALPHAS] memory alphas,
664 ) internal pure returns (Fr accumulator) {
665 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
667 // Accumulate all relations in Ultra Honk - each with varying number of subrelations
668 accumulateArithmeticRelation(purportedEvaluations, evaluations, powPartialEval);
669 accumulatePermutationRelation(purportedEvaluations, rp, evaluations, powPartialEval);
670 accumulateLogDerivativeLookupRelation(purportedEvaluations, rp, evaluations, powPartialEval);
671 accumulateDeltaRangeRelation(purportedEvaluations, evaluations, powPartialEval);
672 accumulateEllipticRelation(purportedEvaluations, evaluations, powPartialEval);
673 accumulateMemoryRelation(purportedEvaluations, rp, evaluations, powPartialEval);
674 accumulateNnfRelation(purportedEvaluations, evaluations, powPartialEval);
675 accumulatePoseidonExternalRelation(purportedEvaluations, evaluations, powPartialEval);
676 accumulatePoseidonInternalRelation(purportedEvaluations, evaluations, powPartialEval);
677 // batch the subrelations with the alpha challenges to obtain the full honk relation
678 accumulator = scaleAndBatchSubrelations(evaluations, alphas);
686 function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
687 return p[uint256(_wire)];
690 uint256 internal constant NEG_HALF_MODULO_P = 0x183227397098d014dc2822db40c0ac2e9419f4243cdcb848a1f0fac9f8000000;
696 function accumulateArithmeticRelation(
697 Fr[NUMBER_OF_ENTITIES] memory p,
698 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
702 Fr q_arith = wire(p, WIRE.Q_ARITH);
704 Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
706 Fr accum = (q_arith - Fr.wrap(3)) * (wire(p, WIRE.Q_M) * wire(p, WIRE.W_R) * wire(p, WIRE.W_L)) * neg_half;
707 accum = accum + (wire(p, WIRE.Q_L) * wire(p, WIRE.W_L)) + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_R))
708 + (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);
709 accum = accum + (q_arith - ONE) * wire(p, WIRE.W_4_SHIFT);
710 accum = accum * q_arith;
711 accum = accum * domainSep;
717 Fr accum = wire(p, WIRE.W_L) + wire(p, WIRE.W_4) - wire(p, WIRE.W_L_SHIFT) + wire(p, WIRE.Q_M);
718 accum = accum * (q_arith - Fr.wrap(2));
719 accum = accum * (q_arith - ONE);
720 accum = accum * q_arith;
721 accum = accum * domainSep;
726 function accumulatePermutationRelation(
727 Fr[NUMBER_OF_ENTITIES] memory p,
728 Honk.RelationParameters memory rp,
729 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
732 Fr grand_product_numerator;
733 Fr grand_product_denominator;
736 Fr num = wire(p, WIRE.W_L) + wire(p, WIRE.ID_1) * rp.beta + rp.gamma;
737 num = num * (wire(p, WIRE.W_R) + wire(p, WIRE.ID_2) * rp.beta + rp.gamma);
738 num = num * (wire(p, WIRE.W_O) + wire(p, WIRE.ID_3) * rp.beta + rp.gamma);
739 num = num * (wire(p, WIRE.W_4) + wire(p, WIRE.ID_4) * rp.beta + rp.gamma);
741 grand_product_numerator = num;
744 Fr den = wire(p, WIRE.W_L) + wire(p, WIRE.SIGMA_1) * rp.beta + rp.gamma;
745 den = den * (wire(p, WIRE.W_R) + wire(p, WIRE.SIGMA_2) * rp.beta + rp.gamma);
746 den = den * (wire(p, WIRE.W_O) + wire(p, WIRE.SIGMA_3) * rp.beta + rp.gamma);
747 den = den * (wire(p, WIRE.W_4) + wire(p, WIRE.SIGMA_4) * rp.beta + rp.gamma);
749 grand_product_denominator = den;
754 Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
758 (wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
759 * grand_product_denominator
761 acc = acc * domainSep;
767 Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
772 function accumulateLogDerivativeLookupRelation(
773 Fr[NUMBER_OF_ENTITIES] memory p,
774 Honk.RelationParameters memory rp,
775 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
781 // Calculate the write term (the table accumulation)
783 write_term = wire(p, WIRE.TABLE_1) + rp.gamma + (wire(p, WIRE.TABLE_2) * rp.eta)
784 + (wire(p, WIRE.TABLE_3) * rp.etaTwo) + (wire(p, WIRE.TABLE_4) * rp.etaThree);
787 // Calculate the write term
789 Fr derived_entry_1 = wire(p, WIRE.W_L) + rp.gamma + (wire(p, WIRE.Q_R) * wire(p, WIRE.W_L_SHIFT));
790 Fr derived_entry_2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_M) * wire(p, WIRE.W_R_SHIFT);
791 Fr derived_entry_3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_C) * wire(p, WIRE.W_O_SHIFT);
793 read_term = derived_entry_1 + (derived_entry_2 * rp.eta) + (derived_entry_3 * rp.etaTwo)
794 + (wire(p, WIRE.Q_O) * rp.etaThree);
797 Fr read_inverse = wire(p, WIRE.LOOKUP_INVERSES) * write_term;
798 Fr write_inverse = wire(p, WIRE.LOOKUP_INVERSES) * read_term;
800 Fr inverse_exists_xor = wire(p, WIRE.LOOKUP_READ_TAGS) + wire(p, WIRE.Q_LOOKUP)
801 - (wire(p, WIRE.LOOKUP_READ_TAGS) * wire(p, WIRE.Q_LOOKUP));
803 // Inverse calculated correctly relation
804 Fr accumulatorNone = read_term * write_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
805 accumulatorNone = accumulatorNone * domainSep;
808 Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * read_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * write_inverse;
810 Fr read_tag = wire(p, WIRE.LOOKUP_READ_TAGS);
812 Fr read_tag_boolean_relation = read_tag * read_tag - read_tag;
814 evals[4] = accumulatorNone;
815 evals[5] = accumulatorOne;
816 evals[6] = read_tag_boolean_relation * domainSep;
819 function accumulateDeltaRangeRelation(
820 Fr[NUMBER_OF_ENTITIES] memory p,
821 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
824 Fr minus_one = ZERO - ONE;
825 Fr minus_two = ZERO - Fr.wrap(2);
826 Fr minus_three = ZERO - Fr.wrap(3);
828 // Compute wire differences
829 Fr delta_1 = wire(p, WIRE.W_R) - wire(p, WIRE.W_L);
830 Fr delta_2 = wire(p, WIRE.W_O) - wire(p, WIRE.W_R);
831 Fr delta_3 = wire(p, WIRE.W_4) - wire(p, WIRE.W_O);
832 Fr delta_4 = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_4);
837 acc = acc * (delta_1 + minus_one);
838 acc = acc * (delta_1 + minus_two);
839 acc = acc * (delta_1 + minus_three);
840 acc = acc * wire(p, WIRE.Q_RANGE);
841 acc = acc * domainSep;
848 acc = acc * (delta_2 + minus_one);
849 acc = acc * (delta_2 + minus_two);
850 acc = acc * (delta_2 + minus_three);
851 acc = acc * wire(p, WIRE.Q_RANGE);
852 acc = acc * domainSep;
859 acc = acc * (delta_3 + minus_one);
860 acc = acc * (delta_3 + minus_two);
861 acc = acc * (delta_3 + minus_three);
862 acc = acc * wire(p, WIRE.Q_RANGE);
863 acc = acc * domainSep;
870 acc = acc * (delta_4 + minus_one);
871 acc = acc * (delta_4 + minus_two);
872 acc = acc * (delta_4 + minus_three);
873 acc = acc * wire(p, WIRE.Q_RANGE);
874 acc = acc * domainSep;
879 struct EllipticParams {
887 // push accumulators into memory
888 Fr x_double_identity;
891 function accumulateEllipticRelation(
892 Fr[NUMBER_OF_ENTITIES] memory p,
893 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
896 EllipticParams memory ep;
897 ep.x_1 = wire(p, WIRE.W_R);
898 ep.y_1 = wire(p, WIRE.W_O);
900 ep.x_2 = wire(p, WIRE.W_L_SHIFT);
901 ep.y_2 = wire(p, WIRE.W_4_SHIFT);
902 ep.y_3 = wire(p, WIRE.W_O_SHIFT);
903 ep.x_3 = wire(p, WIRE.W_R_SHIFT);
905 Fr q_sign = wire(p, WIRE.Q_L);
906 Fr q_is_double = wire(p, WIRE.Q_M);
908 // Contribution 10 point addition, x-coordinate check
909 // q_elliptic * (x3 + x2 + x1)(x2 - x1)(x2 - x1) - y2^2 - y1^2 + 2(y2y1)*q_sign = 0
910 Fr x_diff = (ep.x_2 - ep.x_1);
911 Fr y1_sqr = (ep.y_1 * ep.y_1);
914 Fr partialEval = domainSep;
916 Fr y2_sqr = (ep.y_2 * ep.y_2);
917 Fr y1y2 = ep.y_1 * ep.y_2 * q_sign;
918 Fr x_add_identity = (ep.x_3 + ep.x_2 + ep.x_1);
919 x_add_identity = x_add_identity * x_diff * x_diff;
920 x_add_identity = x_add_identity - y2_sqr - y1_sqr + y1y2 + y1y2;
922 evals[11] = x_add_identity * partialEval * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
925 // Contribution 11 point addition, x-coordinate check
926 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
928 Fr y1_plus_y3 = ep.y_1 + ep.y_3;
929 Fr y_diff = ep.y_2 * q_sign - ep.y_1;
930 Fr y_add_identity = y1_plus_y3 * x_diff + (ep.x_3 - ep.x_1) * y_diff;
931 evals[12] = y_add_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
934 // Contribution 10 point doubling, x-coordinate check
935 // (x3 + x1 + x1) (4y1*y1) - 9 * x1 * x1 * x1 * x1 = 0
936 // N.B. we're using the equivalence x1*x1*x1 === y1*y1 - curve_b to reduce degree by 1
938 Fr x_pow_4 = (y1_sqr + GRUMPKIN_CURVE_B_PARAMETER_NEGATED) * ep.x_1;
939 Fr y1_sqr_mul_4 = y1_sqr + y1_sqr;
940 y1_sqr_mul_4 = y1_sqr_mul_4 + y1_sqr_mul_4;
941 Fr x1_pow_4_mul_9 = x_pow_4 * Fr.wrap(9);
943 // NOTE: pushed into memory (stack >:'( )
944 ep.x_double_identity = (ep.x_3 + ep.x_1 + ep.x_1) * y1_sqr_mul_4 - x1_pow_4_mul_9;
946 Fr acc = ep.x_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
947 evals[11] = evals[11] + acc;
950 // Contribution 11 point doubling, y-coordinate check
951 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
953 Fr x1_sqr_mul_3 = (ep.x_1 + ep.x_1 + ep.x_1) * ep.x_1;
954 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);
955 evals[12] = evals[12] + y_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
959 // Parameters used within the Memory Relation
960 // A struct is used to work around stack too deep. This relation has alot of variables
962 Fr memory_record_check;
963 Fr partial_record_check;
964 Fr next_gate_access_type;
967 Fr adjacent_values_match_if_adjacent_indices_match;
968 Fr adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation;
970 Fr next_gate_access_type_is_boolean;
971 Fr ROM_consistency_check_identity;
972 Fr RAM_consistency_check_identity;
974 Fr RAM_timestamp_check_identity;
976 Fr index_is_monotonically_increasing;
979 function accumulateMemoryRelation(
980 Fr[NUMBER_OF_ENTITIES] memory p,
981 Honk.RelationParameters memory rp,
982 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1028 ap.memory_record_check = wire(p, WIRE.W_O) * rp.etaThree;
1029 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_R) * rp.etaTwo);
1030 ap.memory_record_check = ap.memory_record_check + (wire(p, WIRE.W_L) * rp.eta);
1031 ap.memory_record_check = ap.memory_record_check + wire(p, WIRE.Q_C);
1032 ap.partial_record_check = ap.memory_record_check; // used in RAM consistency check; deg 1 or 4
1033 ap.memory_record_check = ap.memory_record_check - wire(p, WIRE.W_4);
1051 ap.index_delta = wire(p, WIRE.W_L_SHIFT) - wire(p, WIRE.W_L);
1052 ap.record_delta = wire(p, WIRE.W_4_SHIFT) - wire(p, WIRE.W_4);
1054 ap.index_is_monotonically_increasing = ap.index_delta * ap.index_delta - ap.index_delta; // deg 2
1056 ap.adjacent_values_match_if_adjacent_indices_match = (ap.index_delta * MINUS_ONE + ONE) * ap.record_delta; // deg 2
1058 evals[14] = ap.adjacent_values_match_if_adjacent_indices_match * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1059 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1060 evals[15] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R))
1061 * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5
1063 ap.ROM_consistency_check_identity = ap.memory_record_check * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R)); // deg 3 or 7
1084 Fr access_type = (wire(p, WIRE.W_4) - ap.partial_record_check); // will be 0 or 1 for honest Prover; deg 1 or 4
1085 ap.access_check = access_type * access_type - access_type; // check value is 0 or 1; deg 2 or 8
1087 // reverse order we could re-use `ap.partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta
1089 ap.next_gate_access_type = wire(p, WIRE.W_O_SHIFT) * rp.etaThree;
1090 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_R_SHIFT) * rp.etaTwo);
1091 ap.next_gate_access_type = ap.next_gate_access_type + (wire(p, WIRE.W_L_SHIFT) * rp.eta);
1092 ap.next_gate_access_type = wire(p, WIRE.W_4_SHIFT) - ap.next_gate_access_type;
1094 Fr value_delta = wire(p, WIRE.W_O_SHIFT) - wire(p, WIRE.W_O);
1095 ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation =
1096 (ap.index_delta * MINUS_ONE + ONE) * value_delta * (ap.next_gate_access_type * MINUS_ONE + ONE); // deg 3 or 6
1098 // We can't apply the RAM consistency check identity on the final entry in the sorted list (the wires in the
1099 // next gate would make the identity fail). We need to validate that its 'access type' bool is correct. Can't
1100 // do with an arithmetic gate because of the `eta` factors. We need to check that the *next* gate's access
1101 // type is correct, to cover this edge case
1103 ap.next_gate_access_type_is_boolean =
1104 ap.next_gate_access_type * ap.next_gate_access_type - ap.next_gate_access_type;
1106 // Putting it all together...
1107 evals[16] = ap.adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation
1108 * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 5 or 8
1109 evals[17] = ap.index_is_monotonically_increasing * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4
1110 evals[18] = ap.next_gate_access_type_is_boolean * (wire(p, WIRE.Q_O)) * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 6
1112 ap.RAM_consistency_check_identity = ap.access_check * (wire(p, WIRE.Q_O)); // deg 3 or 9
1125 ap.timestamp_delta = wire(p, WIRE.W_R_SHIFT) - wire(p, WIRE.W_R);
1126 ap.RAM_timestamp_check_identity = (ap.index_delta * MINUS_ONE + ONE) * ap.timestamp_delta - wire(p, WIRE.W_O); // deg 3
1133 ap.memory_identity = ap.ROM_consistency_check_identity; // deg 3 or 6
1134 ap.memory_identity =
1135 ap.memory_identity + ap.RAM_timestamp_check_identity * (wire(p, WIRE.Q_4) * wire(p, WIRE.Q_L)); // deg 4
1136 ap.memory_identity = ap.memory_identity + ap.memory_record_check * (wire(p, WIRE.Q_M) * wire(p, WIRE.Q_L)); // deg 3 or 6
1137 ap.memory_identity = ap.memory_identity + ap.RAM_consistency_check_identity; // deg 3 or 9
1139 // (deg 3 or 9) + (deg 4) + (deg 3)
1140 ap.memory_identity = ap.memory_identity * (wire(p, WIRE.Q_MEMORY) * domainSep); // deg 4 or 10
1141 evals[13] = ap.memory_identity;
1144 // Constants for the Non-native Field relation
1145 Fr constant LIMB_SIZE = Fr.wrap(uint256(1) << 68);
1146 Fr constant SUBLIMB_SHIFT = Fr.wrap(uint256(1) << 14);
1148 // Parameters used within the Non-Native Field Relation
1149 // A struct is used to work around stack too deep. This relation has alot of variables
1152 Fr non_native_field_gate_1;
1153 Fr non_native_field_gate_2;
1154 Fr non_native_field_gate_3;
1155 Fr limb_accumulator_1;
1156 Fr limb_accumulator_2;
1160 function accumulateNnfRelation(
1161 Fr[NUMBER_OF_ENTITIES] memory p,
1162 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1165 NnfParams memory ap;
1179 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);
1180 ap.non_native_field_gate_2 =
1181 (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));
1182 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * LIMB_SIZE;
1183 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 - wire(p, WIRE.W_4_SHIFT);
1184 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 + ap.limb_subproduct;
1185 ap.non_native_field_gate_2 = ap.non_native_field_gate_2 * wire(p, WIRE.Q_4);
1187 ap.limb_subproduct = ap.limb_subproduct * LIMB_SIZE;
1188 ap.limb_subproduct = ap.limb_subproduct + (wire(p, WIRE.W_L_SHIFT) * wire(p, WIRE.W_R_SHIFT));
1189 ap.non_native_field_gate_1 = ap.limb_subproduct;
1190 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 - (wire(p, WIRE.W_O) + wire(p, WIRE.W_4));
1191 ap.non_native_field_gate_1 = ap.non_native_field_gate_1 * wire(p, WIRE.Q_O);
1193 ap.non_native_field_gate_3 = ap.limb_subproduct;
1194 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 + wire(p, WIRE.W_4);
1195 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 - (wire(p, WIRE.W_O_SHIFT) + wire(p, WIRE.W_4_SHIFT));
1196 ap.non_native_field_gate_3 = ap.non_native_field_gate_3 * wire(p, WIRE.Q_M);
1198 Fr non_native_field_identity =
1199 ap.non_native_field_gate_1 + ap.non_native_field_gate_2 + ap.non_native_field_gate_3;
1200 non_native_field_identity = non_native_field_identity * wire(p, WIRE.Q_R);
1202 // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm
1204 ap.limb_accumulator_1 = wire(p, WIRE.W_R_SHIFT) * SUBLIMB_SHIFT;
1205 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L_SHIFT);
1206 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1207 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_O);
1208 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1209 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_R);
1210 ap.limb_accumulator_1 = ap.limb_accumulator_1 * SUBLIMB_SHIFT;
1211 ap.limb_accumulator_1 = ap.limb_accumulator_1 + wire(p, WIRE.W_L);
1212 ap.limb_accumulator_1 = ap.limb_accumulator_1 - wire(p, WIRE.W_4);
1213 ap.limb_accumulator_1 = ap.limb_accumulator_1 * wire(p, WIRE.Q_4);
1215 // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm
1217 ap.limb_accumulator_2 = wire(p, WIRE.W_O_SHIFT) * SUBLIMB_SHIFT;
1218 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_R_SHIFT);
1219 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1220 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_L_SHIFT);
1221 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1222 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_4);
1223 ap.limb_accumulator_2 = ap.limb_accumulator_2 * SUBLIMB_SHIFT;
1224 ap.limb_accumulator_2 = ap.limb_accumulator_2 + wire(p, WIRE.W_O);
1225 ap.limb_accumulator_2 = ap.limb_accumulator_2 - wire(p, WIRE.W_4_SHIFT);
1226 ap.limb_accumulator_2 = ap.limb_accumulator_2 * wire(p, WIRE.Q_M);
1228 Fr limb_accumulator_identity = ap.limb_accumulator_1 + ap.limb_accumulator_2;
1229 limb_accumulator_identity = limb_accumulator_identity * wire(p, WIRE.Q_O); // deg 3
1231 ap.nnf_identity = non_native_field_identity + limb_accumulator_identity;
1232 ap.nnf_identity = ap.nnf_identity * (wire(p, WIRE.Q_NNF) * domainSep);
1233 evals[19] = ap.nnf_identity;
1236 struct PoseidonExternalParams {
1253 Fr q_pos_by_scaling;
1256 function accumulatePoseidonExternalRelation(
1257 Fr[NUMBER_OF_ENTITIES] memory p,
1258 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1259 Fr domainSep // i guess this is the scaling factor?
1261 PoseidonExternalParams memory ep;
1263 ep.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1264 ep.s2 = wire(p, WIRE.W_R) + wire(p, WIRE.Q_R);
1265 ep.s3 = wire(p, WIRE.W_O) + wire(p, WIRE.Q_O);
1266 ep.s4 = wire(p, WIRE.W_4) + wire(p, WIRE.Q_4);
1268 ep.u1 = ep.s1 * ep.s1 * ep.s1 * ep.s1 * ep.s1;
1269 ep.u2 = ep.s2 * ep.s2 * ep.s2 * ep.s2 * ep.s2;
1270 ep.u3 = ep.s3 * ep.s3 * ep.s3 * ep.s3 * ep.s3;
1271 ep.u4 = ep.s4 * ep.s4 * ep.s4 * ep.s4 * ep.s4;
1272 // matrix mul v = M_E * u with 14 additions
1273 ep.t0 = ep.u1 + ep.u2; // u_1 + u_2
1274 ep.t1 = ep.u3 + ep.u4; // u_3 + u_4
1275 ep.t2 = ep.u2 + ep.u2 + ep.t1; // 2u_2
1276 // ep.t2 += ep.t1; // 2u_2 + u_3 + u_4
1277 ep.t3 = ep.u4 + ep.u4 + ep.t0; // 2u_4
1278 // ep.t3 += ep.t0; // u_1 + u_2 + 2u_4
1279 ep.v4 = ep.t1 + ep.t1;
1280 ep.v4 = ep.v4 + ep.v4 + ep.t3;
1281 // ep.v4 += ep.t3; // u_1 + u_2 + 4u_3 + 6u_4
1282 ep.v2 = ep.t0 + ep.t0;
1283 ep.v2 = ep.v2 + ep.v2 + ep.t2;
1284 // ep.v2 += ep.t2; // 4u_1 + 6u_2 + u_3 + u_4
1285 ep.v1 = ep.t3 + ep.v2; // 5u_1 + 7u_2 + u_3 + 3u_4
1286 ep.v3 = ep.t2 + ep.v4; // u_1 + 3u_2 + 5u_3 + 7u_4
1288 ep.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_EXTERNAL) * domainSep;
1289 evals[20] = evals[20] + ep.q_pos_by_scaling * (ep.v1 - wire(p, WIRE.W_L_SHIFT));
1291 evals[21] = evals[21] + ep.q_pos_by_scaling * (ep.v2 - wire(p, WIRE.W_R_SHIFT));
1293 evals[22] = evals[22] + ep.q_pos_by_scaling * (ep.v3 - wire(p, WIRE.W_O_SHIFT));
1295 evals[23] = evals[23] + ep.q_pos_by_scaling * (ep.v4 - wire(p, WIRE.W_4_SHIFT));
1298 struct PoseidonInternalParams {
1309 Fr q_pos_by_scaling;
1312 function accumulatePoseidonInternalRelation(
1313 Fr[NUMBER_OF_ENTITIES] memory p,
1314 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1317 PoseidonInternalParams memory ip;
1319 Fr[4] memory INTERNAL_MATRIX_DIAGONAL = [
1320 FrLib.from(0x10dc6e9c006ea38b04b1e03b4bd9490c0d03f98929ca1d7fb56821fd19d3b6e7),
1321 FrLib.from(0x0c28145b6a44df3e0149b3d0a30b3bb599df9756d4dd9b84a86b38cfb45a740b),
1322 FrLib.from(0x00544b8338791518b2c7645a50392798b21f75bb60e3596170067d00141cac15),
1323 FrLib.from(0x222c01175718386f2e2e82eb122789e352e105a3b8fa852613bc534433ee428b)
1326 // add round constants
1327 ip.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1329 // apply s-box round
1330 ip.u1 = ip.s1 * ip.s1 * ip.s1 * ip.s1 * ip.s1;
1331 ip.u2 = wire(p, WIRE.W_R);
1332 ip.u3 = wire(p, WIRE.W_O);
1333 ip.u4 = wire(p, WIRE.W_4);
1335 // matrix mul with v = M_I * u 4 muls and 7 additions
1336 ip.u_sum = ip.u1 + ip.u2 + ip.u3 + ip.u4;
1338 ip.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_INTERNAL) * domainSep;
1340 ip.v1 = ip.u1 * INTERNAL_MATRIX_DIAGONAL[0] + ip.u_sum;
1341 evals[24] = evals[24] + ip.q_pos_by_scaling * (ip.v1 - wire(p, WIRE.W_L_SHIFT));
1343 ip.v2 = ip.u2 * INTERNAL_MATRIX_DIAGONAL[1] + ip.u_sum;
1344 evals[25] = evals[25] + ip.q_pos_by_scaling * (ip.v2 - wire(p, WIRE.W_R_SHIFT));
1346 ip.v3 = ip.u3 * INTERNAL_MATRIX_DIAGONAL[2] + ip.u_sum;
1347 evals[26] = evals[26] + ip.q_pos_by_scaling * (ip.v3 - wire(p, WIRE.W_O_SHIFT));
1349 ip.v4 = ip.u4 * INTERNAL_MATRIX_DIAGONAL[3] + ip.u_sum;
1350 evals[27] = evals[27] + ip.q_pos_by_scaling * (ip.v4 - wire(p, WIRE.W_4_SHIFT));
1353 function scaleAndBatchSubrelations(
1354 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations,
1355 Fr[NUMBER_OF_ALPHAS] memory subrelationChallenges
1356 ) internal pure returns (Fr accumulator) {
1357 accumulator = accumulator + evaluations[0];
1359 for (uint256 i = 1; i < NUMBER_OF_SUBRELATIONS; ++i) {
1360 accumulator = accumulator + evaluations[i] * subrelationChallenges[i - 1];
1365// Field arithmetic libraries - prevent littering the code with modmul / addmul
1367library CommitmentSchemeLib {
1370 // Avoid stack too deep
1371 struct ShpleminiIntermediates {
1374 Fr unshiftedScalarNeg;
1375 Fr shiftedScalarNeg;
1376 // Scalar to be multiplied by [1]₁
1377 Fr constantTermAccumulator;
1378 // Accumulator for powers of rho
1379 Fr batchingChallenge;
1380 // Linear combination of multilinear (sumcheck) evaluations and powers of rho
1381 Fr batchedEvaluation;
1383 Fr[4] batchingScalars;
1384 // 1/(z - r^{2^i}) for i = 0, ..., logSize, dynamically updated
1385 Fr posInvertedDenominator;
1386 // 1/(z + r^{2^i}) for i = 0, ..., logSize, dynamically updated
1387 Fr negInvertedDenominator;
1388 // ν^{2i} * 1/(z - r^{2^i})
1389 Fr scalingFactorPos;
1390 // ν^{2i+1} * 1/(z + r^{2^i})
1391 Fr scalingFactorNeg;
1392 // Fold_i(r^{2^i}) reconstructed by Verifier
1393 Fr[] foldPosEvaluations;
1396 function computeSquares(Fr r, uint256 logN) internal pure returns (Fr[] memory) {
1397 Fr[] memory squares = new Fr[](logN);
1399 for (uint256 i = 1; i < logN; ++i) {
1400 squares[i] = squares[i - 1].sqr();
1404 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., m-1
1406 function computeFoldPosEvaluations(
1407 Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckUChallenges,
1408 Fr batchedEvalAccumulator,
1409 Fr[CONST_PROOF_SIZE_LOG_N] memory geminiEvaluations,
1410 Fr[] memory geminiEvalChallengePowers,
1412 ) internal view returns (Fr[] memory) {
1413 Fr[] memory foldPosEvaluations = new Fr[](logSize);
1414 for (uint256 i = logSize; i > 0; --i) {
1415 Fr challengePower = geminiEvalChallengePowers[i - 1];
1416 Fr u = sumcheckUChallenges[i - 1];
1418 Fr batchedEvalRoundAcc = (
1419 (challengePower * batchedEvalAccumulator * Fr.wrap(2))
1420 - geminiEvaluations[i - 1] * (challengePower * (ONE - u) - u)
1422 // Divide by the denominator
1423 batchedEvalRoundAcc = batchedEvalRoundAcc * (challengePower * (ONE - u) + u).invert();
1425 batchedEvalAccumulator = batchedEvalRoundAcc;
1426 foldPosEvaluations[i - 1] = batchedEvalRoundAcc;
1429 return foldPosEvaluations;
1433uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
1435function bytes32ToString(bytes32 value) pure returns (string memory result) {
1436 bytes memory alphabet = "0123456789abcdef";
1438 bytes memory str = new bytes(66);
1441 for (uint256 i = 0; i < 32; i++) {
1442 str[2 + i * 2] = alphabet[uint8(value[i] >> 4)];
1443 str[3 + i * 2] = alphabet[uint8(value[i] & 0x0f)];
1445 result = string(str);
1450function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
1451 scalar = FrLib.fromBytes32(bytes32(proofSection));
1454// EC Point utilities
1455function bytesToG1Point(bytes calldata proofSection) pure returns (Honk.G1Point memory point) {
1456 point = Honk.G1Point({
1457 x: uint256(bytes32(proofSection[0x00:0x20])) % Q,
1458 y: uint256(bytes32(proofSection[0x20:0x40])) % Q
1462function negateInplace(Honk.G1Point memory point) pure returns (Honk.G1Point memory) {
1463 point.y = (Q - point.y) % Q;
1479function convertPairingPointsToG1(Fr[PAIRING_POINTS_SIZE] memory pairingPoints)
1481 returns (Honk.G1Point memory lhs, Honk.G1Point memory rhs)
1483 uint256 lhsX = Fr.unwrap(pairingPoints[0]);
1484 lhsX |= Fr.unwrap(pairingPoints[1]) << 68;
1485 lhsX |= Fr.unwrap(pairingPoints[2]) << 136;
1486 lhsX |= Fr.unwrap(pairingPoints[3]) << 204;
1489 uint256 lhsY = Fr.unwrap(pairingPoints[4]);
1490 lhsY |= Fr.unwrap(pairingPoints[5]) << 68;
1491 lhsY |= Fr.unwrap(pairingPoints[6]) << 136;
1492 lhsY |= Fr.unwrap(pairingPoints[7]) << 204;
1495 uint256 rhsX = Fr.unwrap(pairingPoints[8]);
1496 rhsX |= Fr.unwrap(pairingPoints[9]) << 68;
1497 rhsX |= Fr.unwrap(pairingPoints[10]) << 136;
1498 rhsX |= Fr.unwrap(pairingPoints[11]) << 204;
1501 uint256 rhsY = Fr.unwrap(pairingPoints[12]);
1502 rhsY |= Fr.unwrap(pairingPoints[13]) << 68;
1503 rhsY |= Fr.unwrap(pairingPoints[14]) << 136;
1504 rhsY |= Fr.unwrap(pairingPoints[15]) << 204;
1516function generateRecursionSeparator(
1517 Fr[PAIRING_POINTS_SIZE] memory proofPairingPoints,
1518 Honk.G1Point memory accLhs,
1519 Honk.G1Point memory accRhs
1520) pure returns (Fr recursionSeparator) {
1521 // hash the proof aggregated X
1522 // hash the proof aggregated Y
1526 (Honk.G1Point memory proofLhs, Honk.G1Point memory proofRhs) = convertPairingPointsToG1(proofPairingPoints);
1528 uint256[8] memory recursionSeparatorElements;
1531 recursionSeparatorElements[0] = proofLhs.x;
1532 recursionSeparatorElements[1] = proofLhs.y;
1533 recursionSeparatorElements[2] = proofRhs.x;
1534 recursionSeparatorElements[3] = proofRhs.y;
1536 // Accumulator points
1537 recursionSeparatorElements[4] = accLhs.x;
1538 recursionSeparatorElements[5] = accLhs.y;
1539 recursionSeparatorElements[6] = accRhs.x;
1540 recursionSeparatorElements[7] = accRhs.y;
1542 recursionSeparator = FrLib.fromBytes32(keccak256(abi.encodePacked(recursionSeparatorElements)));
1554function mulWithSeperator(Honk.G1Point memory basePoint, Honk.G1Point memory other, Fr recursionSeperator)
1556 returns (Honk.G1Point memory)
1558 Honk.G1Point memory result;
1560 result = ecMul(recursionSeperator, basePoint);
1561 result = ecAdd(result, other);
1574function ecMul(Fr value, Honk.G1Point memory point) view returns (Honk.G1Point memory) {
1575 Honk.G1Point memory result;
1578 let free := mload(0x40)
1579 // Write the point into memory (two 32 byte words)
1583 // free + 0x20| point.y
1584 mstore(free, mload(point))
1585 mstore(add(free, 0x20), mload(add(point, 0x20)))
1586 // Write the scalar into memory (one 32 byte word)
1589 // free + 0x40| value
1590 mstore(add(free, 0x40), value)
1592 // Call the ecMul precompile, it takes in the following
1593 // [point.x, point.y, scalar], and returns the result back into the free memory location.
1594 let success := staticcall(gas(), 0x07, free, 0x60, free, 0x40)
1595 if iszero(success) {
1598 // Copy the result of the multiplication back into the result memory location.
1601 // result | result.x
1602 // result + 0x20| result.y
1603 mstore(result, mload(free))
1604 mstore(add(result, 0x20), mload(add(free, 0x20)))
1606 mstore(0x40, add(free, 0x60))
1620function ecAdd(Honk.G1Point memory lhs, Honk.G1Point memory rhs) view returns (Honk.G1Point memory) {
1621 Honk.G1Point memory result;
1624 let free := mload(0x40)
1625 // Write lhs into memory (two 32 byte words)
1629 // free + 0x20| lhs.y
1630 mstore(free, mload(lhs))
1631 mstore(add(free, 0x20), mload(add(lhs, 0x20)))
1633 // Write rhs into memory (two 32 byte words)
1636 // free + 0x40| rhs.x
1637 // free + 0x60| rhs.y
1638 mstore(add(free, 0x40), mload(rhs))
1639 mstore(add(free, 0x60), mload(add(rhs, 0x20)))
1641 // Call the ecAdd precompile, it takes in the following
1642 // [lhs.x, lhs.y, rhs.x, rhs.y], and returns their addition back into the free memory location.
1643 let success := staticcall(gas(), 0x06, free, 0x80, free, 0x40)
1644 if iszero(success) { revert(0, 0) }
1646 // Copy the result of the addition back into the result memory location.
1649 // result | result.x
1650 // result + 0x20| result.y
1651 mstore(result, mload(free))
1652 mstore(add(result, 0x20), mload(add(free, 0x20)))
1654 mstore(0x40, add(free, 0x80))
1660function validateOnCurve(Honk.G1Point memory point) pure {
1661 uint256 x = point.x;
1662 uint256 y = point.y;
1664 bool success = false;
1666 let xx := mulmod(x, x, Q)
1667 success := eq(mulmod(y, y, Q), addmod(mulmod(x, xx, Q), 3, Q))
1670 require(success, "point is not on the curve");
1673function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool decodedResult) {
1674 bytes memory input = abi.encodePacked(
1678 uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
1679 uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
1680 uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
1681 uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
1685 uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
1686 uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
1687 uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
1688 uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
1691 (bool success, bytes memory result) = address(0x08).staticcall(input);
1692 decodedResult = success && abi.decode(result, (bool));
1695// Field arithmetic libraries - prevent littering the code with modmul / addmul
1700abstract contract BaseHonkVerifier is IVerifier {
1703 uint256 immutable $N;
1704 uint256 immutable $LOG_N;
1705 uint256 immutable $VK_HASH;
1706 uint256 immutable $NUM_PUBLIC_INPUTS;
1708 constructor(uint256 _N, uint256 _logN, uint256 _vkHash, uint256 _numPublicInputs) {
1712 $NUM_PUBLIC_INPUTS = _numPublicInputs;
1716 error ProofLengthWrong();
1717 error ProofLengthWrongWithLogN(uint256 logN, uint256 actualLength, uint256 expectedLength);
1718 error PublicInputsLengthWrong();
1719 error SumcheckFailed();
1720 error ShpleminiFailed();
1722 // Constants for proof length calculation (matching UltraKeccakFlavor)
1723 uint256 constant NUM_WITNESS_ENTITIES = 8;
1724 uint256 constant NUM_ELEMENTS_COMM = 2; // uint256 elements for curve points
1725 uint256 constant NUM_ELEMENTS_FR = 1; // uint256 elements for field elements
1727 // Calculate proof size based on log_n (matching UltraKeccakFlavor formula)
1728 function calculateProofSize(uint256 logN) internal pure returns (uint256) {
1729 // Witness commitments
1730 uint256 proofLength = NUM_WITNESS_ENTITIES * NUM_ELEMENTS_COMM; // witness commitments
1733 proofLength += logN * BATCHED_RELATION_PARTIAL_LENGTH * NUM_ELEMENTS_FR; // sumcheck univariates
1734 proofLength += NUMBER_OF_ENTITIES * NUM_ELEMENTS_FR; // sumcheck evaluations
1737 proofLength += (logN - 1) * NUM_ELEMENTS_COMM; // Gemini Fold commitments
1738 proofLength += logN * NUM_ELEMENTS_FR; // Gemini evaluations
1740 // Shplonk and KZG commitments
1741 proofLength += NUM_ELEMENTS_COMM * 2; // Shplonk Q and KZG W commitments
1744 proofLength += PAIRING_POINTS_SIZE; // pairing inputs carried on public inputs
1749 // Number of field elements in a ultra keccak honk proof for log_n = 25, including pairing point object.
1750 uint256 constant PROOF_SIZE = 350; // Legacy constant - will be replaced by calculateProofSize($LOG_N)
1751 uint256 constant SHIFTED_COMMITMENTS_START = 29;
1753 function loadVerificationKey() internal pure virtual returns (Honk.VerificationKey memory);
1755 function verify(bytes calldata proof, bytes32[] calldata publicInputs) public view override returns (bool) {
1756 // Calculate expected proof size based on $LOG_N
1757 uint256 expectedProofSize = calculateProofSize($LOG_N);
1759 // Check the received proof is the expected size where each field element is 32 bytes
1760 if (proof.length != expectedProofSize * 32) {
1761 revert ProofLengthWrongWithLogN($LOG_N, proof.length, expectedProofSize * 32);
1764 Honk.VerificationKey memory vk = loadVerificationKey();
1765 Honk.Proof memory p = TranscriptLib.loadProof(proof, $LOG_N);
1766 if (publicInputs.length != vk.publicInputsSize - PAIRING_POINTS_SIZE) {
1767 revert PublicInputsLengthWrong();
1770 // Generate the fiat shamir challenges for the whole protocol
1771 Transcript memory t = TranscriptLib.generateTranscript(p, publicInputs, $VK_HASH, $NUM_PUBLIC_INPUTS, $LOG_N);
1773 // Derive public input delta
1774 t.relationParameters.publicInputsDelta = computePublicInputDelta(
1776 p.pairingPointObject,
1777 t.relationParameters.beta,
1778 t.relationParameters.gamma, /*pubInputsOffset=*/
1783 bool sumcheckVerified = verifySumcheck(p, t);
1784 if (!sumcheckVerified) revert SumcheckFailed();
1786 bool shpleminiVerified = verifyShplemini(p, vk, t);
1787 if (!shpleminiVerified) revert ShpleminiFailed();
1789 return sumcheckVerified && shpleminiVerified; // Boolean condition not required - nice for vanity :)
1792 uint256 constant PERMUTATION_ARGUMENT_VALUE_SEPARATOR = 1 << 28;
1794 function computePublicInputDelta(
1795 bytes32[] memory publicInputs,
1796 Fr[PAIRING_POINTS_SIZE] memory pairingPointObject,
1800 ) internal view returns (Fr publicInputDelta) {
1802 Fr denominator = ONE;
1804 Fr numeratorAcc = gamma + (beta * FrLib.from(PERMUTATION_ARGUMENT_VALUE_SEPARATOR + offset));
1805 Fr denominatorAcc = gamma - (beta * FrLib.from(offset + 1));
1808 for (uint256 i = 0; i < $NUM_PUBLIC_INPUTS - PAIRING_POINTS_SIZE; i++) {
1809 Fr pubInput = FrLib.fromBytes32(publicInputs[i]);
1811 numerator = numerator * (numeratorAcc + pubInput);
1812 denominator = denominator * (denominatorAcc + pubInput);
1814 numeratorAcc = numeratorAcc + beta;
1815 denominatorAcc = denominatorAcc - beta;
1818 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1819 Fr pubInput = pairingPointObject[i];
1821 numerator = numerator * (numeratorAcc + pubInput);
1822 denominator = denominator * (denominatorAcc + pubInput);
1824 numeratorAcc = numeratorAcc + beta;
1825 denominatorAcc = denominatorAcc - beta;
1829 publicInputDelta = FrLib.div(numerator, denominator);
1832 function verifySumcheck(Honk.Proof memory proof, Transcript memory tp) internal view returns (bool verified) {
1834 Fr powPartialEvaluation = ONE;
1836 // We perform sumcheck reductions over log n rounds ( the multivariate degree )
1837 for (uint256 round = 0; round < $LOG_N; ++round) {
1838 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate = proof.sumcheckUnivariates[round];
1839 bool valid = checkSum(roundUnivariate, roundTarget);
1840 if (!valid) revert SumcheckFailed();
1842 Fr roundChallenge = tp.sumCheckUChallenges[round];
1844 // Update the round target for the next rounf
1845 roundTarget = computeNextTargetSum(roundUnivariate, roundChallenge);
1846 powPartialEvaluation = partiallyEvaluatePOW(tp.gateChallenges[round], powPartialEvaluation, roundChallenge);
1850 Fr grandHonkRelationSum = RelationsLib.accumulateRelationEvaluations(
1851 proof.sumcheckEvaluations, tp.relationParameters, tp.alphas, powPartialEvaluation
1853 verified = (grandHonkRelationSum == roundTarget);
1856 function checkSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate, Fr roundTarget)
1859 returns (bool checked)
1861 Fr totalSum = roundUnivariate[0] + roundUnivariate[1];
1862 checked = totalSum == roundTarget;
1865 // Return the new target sum for the next sumcheck round
1866 function computeNextTargetSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariates, Fr roundChallenge)
1869 returns (Fr targetSum)
1871 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory BARYCENTRIC_LAGRANGE_DENOMINATORS = [
1872 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffec51),
1873 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000002d0),
1874 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff11),
1875 Fr.wrap(0x0000000000000000000000000000000000000000000000000000000000000090),
1876 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593efffff71),
1877 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000000f0),
1878 Fr.wrap(0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593effffd31),
1879 Fr.wrap(0x00000000000000000000000000000000000000000000000000000000000013b0)
1881 // To compute the next target sum, we evaluate the given univariate at a point u (challenge).
1883 // Performing Barycentric evaluations
1885 Fr numeratorValue = ONE;
1886 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1887 numeratorValue = numeratorValue * (roundChallenge - Fr.wrap(i));
1890 Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory denominatorInverses;
1891 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1892 Fr inv = BARYCENTRIC_LAGRANGE_DENOMINATORS[i];
1893 inv = inv * (roundChallenge - Fr.wrap(i));
1894 inv = FrLib.invert(inv);
1895 denominatorInverses[i] = inv;
1898 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1899 Fr term = roundUnivariates[i];
1900 term = term * denominatorInverses[i];
1901 targetSum = targetSum + term;
1904 // Scale the sum by the value of B(x)
1905 targetSum = targetSum * numeratorValue;
1908 // Univariate evaluation of the monomial ((1-X_l) + X_l.B_l) at the challenge point X_l=u_l
1909 function partiallyEvaluatePOW(Fr gateChallenge, Fr currentEvaluation, Fr roundChallenge)
1912 returns (Fr newEvaluation)
1914 Fr univariateEval = ONE + (roundChallenge * (gateChallenge - ONE));
1915 newEvaluation = currentEvaluation * univariateEval;
1918 function verifyShplemini(Honk.Proof memory proof, Honk.VerificationKey memory vk, Transcript memory tp)
1921 returns (bool verified)
1923 CommitmentSchemeLib.ShpleminiIntermediates memory mem; // stack
1925 // - Compute vector (r, r², ... , r²⁽ⁿ⁻¹⁾), where n = log_circuit_size
1926 Fr[] memory powers_of_evaluation_challenge = CommitmentSchemeLib.computeSquares(tp.geminiR, $LOG_N);
1928 // Arrays hold values that will be linearly combined for the gemini and shplonk batch openings
1929 Fr[] memory scalars = new Fr[](NUMBER_UNSHIFTED + $LOG_N + 2);
1930 Honk.G1Point[] memory commitments = new Honk.G1Point[](NUMBER_UNSHIFTED + $LOG_N + 2);
1932 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[0]).invert();
1933 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[0]).invert();
1935 mem.unshiftedScalar = mem.posInvertedDenominator + (tp.shplonkNu * mem.negInvertedDenominator);
1937 tp.geminiR.invert() * (mem.posInvertedDenominator - (tp.shplonkNu * mem.negInvertedDenominator));
1940 commitments[0] = proof.shplonkQ;
1942 /* Batch multivariate opening claims, shifted and unshifted
1943 * The vector of scalars is populated as follows:
1946 * - \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
1948 * - \rho^{i+k-1} \times \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
1949 * - \rho^{i+k} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right),
1951 * - \rho^{k+m-1} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right)
1955 * The following vector is concatenated to the vector of commitments:
1957 * f_0, \ldots, f_{m-1}, f_{\text{shift}, 0}, \ldots, f_{\text{shift}, k-1}
1960 * Simultaneously, the evaluation of the multilinear polynomial
1962 * \sum \rho^i \cdot f_i + \sum \rho^{i+k} \cdot f_{\text{shift}, i}
1964 * at the challenge point \f$ (u_0,\ldots, u_{n-1}) \f$ is computed.
1966 * This approach minimizes the number of iterations over the commitments to multilinear polynomials
1967 * and eliminates the need to store the powers of \f$ \rho \f$.
1969 mem.batchingChallenge = ONE;
1970 mem.batchedEvaluation = ZERO;
1972 mem.unshiftedScalarNeg = mem.unshiftedScalar.neg();
1973 mem.shiftedScalarNeg = mem.shiftedScalar.neg();
1974 for (uint256 i = 1; i <= NUMBER_UNSHIFTED; ++i) {
1975 scalars[i] = mem.unshiftedScalarNeg * mem.batchingChallenge;
1976 mem.batchedEvaluation = mem.batchedEvaluation + (proof.sumcheckEvaluations[i - 1] * mem.batchingChallenge);
1977 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
1979 // g commitments are accumulated at r
1980 // For each of the to be shifted commitments perform the shift in place by
1981 // adding to the unshifted value.
1982 // We do so, as the values are to be used in batchMul later, and as
1983 // `a * c + b * c = (a + b) * c` this will allow us to reduce memory and compute.
1984 // Applied to w1, w2, w3, w4 and zPerm
1985 for (uint256 i = 0; i < NUMBER_TO_BE_SHIFTED; ++i) {
1986 uint256 scalarOff = i + SHIFTED_COMMITMENTS_START;
1987 uint256 evaluationOff = i + NUMBER_UNSHIFTED;
1989 scalars[scalarOff] = scalars[scalarOff] + (mem.shiftedScalarNeg * mem.batchingChallenge);
1990 mem.batchedEvaluation =
1991 mem.batchedEvaluation + (proof.sumcheckEvaluations[evaluationOff] * mem.batchingChallenge);
1992 mem.batchingChallenge = mem.batchingChallenge * tp.rho;
1995 commitments[1] = vk.qm;
1996 commitments[2] = vk.qc;
1997 commitments[3] = vk.ql;
1998 commitments[4] = vk.qr;
1999 commitments[5] = vk.qo;
2000 commitments[6] = vk.q4;
2001 commitments[7] = vk.qLookup;
2002 commitments[8] = vk.qArith;
2003 commitments[9] = vk.qDeltaRange;
2004 commitments[10] = vk.qElliptic;
2005 commitments[11] = vk.qMemory;
2006 commitments[12] = vk.qNnf;
2007 commitments[13] = vk.qPoseidon2External;
2008 commitments[14] = vk.qPoseidon2Internal;
2009 commitments[15] = vk.s1;
2010 commitments[16] = vk.s2;
2011 commitments[17] = vk.s3;
2012 commitments[18] = vk.s4;
2013 commitments[19] = vk.id1;
2014 commitments[20] = vk.id2;
2015 commitments[21] = vk.id3;
2016 commitments[22] = vk.id4;
2017 commitments[23] = vk.t1;
2018 commitments[24] = vk.t2;
2019 commitments[25] = vk.t3;
2020 commitments[26] = vk.t4;
2021 commitments[27] = vk.lagrangeFirst;
2022 commitments[28] = vk.lagrangeLast;
2024 // Accumulate proof points
2025 commitments[29] = proof.w1;
2026 commitments[30] = proof.w2;
2027 commitments[31] = proof.w3;
2028 commitments[32] = proof.w4;
2029 commitments[33] = proof.zPerm;
2030 commitments[34] = proof.lookupInverses;
2031 commitments[35] = proof.lookupReadCounts;
2032 commitments[36] = proof.lookupReadTags;
2034 /* Batch gemini claims from the prover
2035 * place the commitments to gemini aᵢ to the vector of commitments, compute the contributions from
2036 * aᵢ(−r²ⁱ) for i=1, … , n−1 to the constant term accumulator, add corresponding scalars
2038 * 1. Moves the vector
2040 * \left( \text{com}(A_1), \text{com}(A_2), \ldots, \text{com}(A_{n-1}) \right)
2042 * to the 'commitments' vector.
2044 * 2. Computes the scalars:
2046 * \frac{\nu^{2}}{z + r^2}, \frac{\nu^3}{z + r^4}, \ldots, \frac{\nu^{n-1}}{z + r^{2^{n-1}}}
2048 * and places them into the 'scalars' vector.
2050 * 3. Accumulates the summands of the constant term:
2052 * \sum_{i=2}^{n-1} \frac{\nu^{i} \cdot A_i(-r^{2^i})}{z + r^{2^i}}
2054 * and adds them to the 'constant_term_accumulator'.
2057 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., $LOG_N - 1
2058 Fr[] memory foldPosEvaluations = CommitmentSchemeLib.computeFoldPosEvaluations(
2059 tp.sumCheckUChallenges,
2060 mem.batchedEvaluation,
2061 proof.geminiAEvaluations,
2062 powers_of_evaluation_challenge,
2066 // Compute the Shplonk constant term contributions from A₀(±r)
2067 mem.constantTermAccumulator = foldPosEvaluations[0] * mem.posInvertedDenominator;
2068 mem.constantTermAccumulator =
2069 mem.constantTermAccumulator + (proof.geminiAEvaluations[0] * tp.shplonkNu * mem.negInvertedDenominator);
2071 mem.batchingChallenge = tp.shplonkNu.sqr();
2073 // Compute Shplonk constant term contributions from Aₗ(± r^{2ˡ}) for l = 1, ..., m-1;
2074 // Compute scalar multipliers for each fold commitment
2075 for (uint256 i = 0; i < $LOG_N - 1; ++i) {
2076 bool dummy_round = i >= ($LOG_N - 1);
2079 // Update inverted denominators
2080 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[i + 1]).invert();
2081 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[i + 1]).invert();
2083 // Compute the scalar multipliers for Aₗ(± r^{2ˡ}) and [Aₗ]
2084 mem.scalingFactorPos = mem.batchingChallenge * mem.posInvertedDenominator;
2085 mem.scalingFactorNeg = mem.batchingChallenge * tp.shplonkNu * mem.negInvertedDenominator;
2086 // [Aₗ] is multiplied by -v^{2l}/(z-r^{2^l}) - v^{2l+1} /(z+ r^{2^l})
2087 scalars[NUMBER_UNSHIFTED + 1 + i] = mem.scalingFactorNeg.neg() + mem.scalingFactorPos.neg();
2089 // Accumulate the const term contribution given by
2090 // v^{2l} * Aₗ(r^{2ˡ}) /(z-r^{2^l}) + v^{2l+1} * Aₗ(-r^{2ˡ}) /(z+ r^{2^l})
2091 Fr accumContribution = mem.scalingFactorNeg * proof.geminiAEvaluations[i + 1];
2092 accumContribution = accumContribution + mem.scalingFactorPos * foldPosEvaluations[i + 1];
2093 mem.constantTermAccumulator = mem.constantTermAccumulator + accumContribution;
2094 // Update the running power of v
2095 mem.batchingChallenge = mem.batchingChallenge * tp.shplonkNu * tp.shplonkNu;
2098 commitments[NUMBER_UNSHIFTED + 1 + i] = proof.geminiFoldComms[i];
2101 // Finalize the batch opening claim
2102 commitments[NUMBER_UNSHIFTED + $LOG_N] = Honk.G1Point({x: 1, y: 2});
2103 scalars[NUMBER_UNSHIFTED + $LOG_N] = mem.constantTermAccumulator;
2105 Honk.G1Point memory quotient_commitment = proof.kzgQuotient;
2107 commitments[NUMBER_UNSHIFTED + $LOG_N + 1] = quotient_commitment;
2108 scalars[NUMBER_UNSHIFTED + $LOG_N + 1] = tp.shplonkZ; // evaluation challenge
2110 Honk.G1Point memory P_0_agg = batchMul(commitments, scalars);
2111 Honk.G1Point memory P_1_agg = negateInplace(quotient_commitment);
2113 // Aggregate pairing points
2114 Fr recursionSeparator = generateRecursionSeparator(proof.pairingPointObject, P_0_agg, P_1_agg);
2115 (Honk.G1Point memory P_0_other, Honk.G1Point memory P_1_other) =
2116 convertPairingPointsToG1(proof.pairingPointObject);
2118 // Validate the points from the proof are on the curve
2119 validateOnCurve(P_0_other);
2120 validateOnCurve(P_1_other);
2122 // accumulate with aggregate points in proof
2123 P_0_agg = mulWithSeperator(P_0_agg, P_0_other, recursionSeparator);
2124 P_1_agg = mulWithSeperator(P_1_agg, P_1_other, recursionSeparator);
2126 return pairing(P_0_agg, P_1_agg);
2129 function batchMul(Honk.G1Point[] memory base, Fr[] memory scalars)
2132 returns (Honk.G1Point memory result)
2134 uint256 limit = NUMBER_UNSHIFTED + $LOG_N + 2;
2136 // Validate all points are on the curve
2137 for (uint256 i = 0; i < limit; ++i) {
2138 validateOnCurve(base[i]);
2141 bool success = true;
2143 let free := mload(0x40)
2146 for {} lt(count, add(limit, 1)) { count := add(count, 1) } {
2148 let base_base := add(base, mul(count, 0x20))
2149 let scalar_base := add(scalars, mul(count, 0x20))
2151 mstore(add(free, 0x40), mload(mload(base_base)))
2152 mstore(add(free, 0x60), mload(add(0x20, mload(base_base))))
2154 mstore(add(free, 0x80), mload(scalar_base))
2156 success := and(success, staticcall(gas(), 7, add(free, 0x40), 0x60, add(free, 0x40), 0x40))
2157 // accumulator = accumulator + accumulator_2
2158 success := and(success, staticcall(gas(), 6, free, 0x80, free, 0x40))
2161 // Return the result
2162 mstore(result, mload(free))
2163 mstore(add(result, 0x20), mload(add(free, 0x20)))
2166 require(success, ShpleminiFailed());
2170contract HonkVerifier is BaseHonkVerifier(N, LOG_N, VK_HASH, NUMBER_OF_PUBLIC_INPUTS) {
2171 function loadVerificationKey() internal pure override returns (Honk.VerificationKey memory) {
2172 return HonkVerificationKey.loadVerificationKey();
2179 std::ostringstream stream;
2181 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)