Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
honk_contract.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
9#include <iostream>
10
11// Source code for the Ultrahonk Solidity verifier.
12// It's expected that the AcirComposer will inject a library which will load the verification key into memory.
13// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays)
14static const char HONK_CONTRACT_SOURCE[] = R"(
15pragma solidity ^0.8.27;
16
17interface IVerifier {
18 function verify(bytes calldata _proof, bytes32[] calldata _publicInputs) external view returns (bool);
19}
20
21type Fr is uint256;
22
23using {add as +} for Fr global;
24using {sub as -} for Fr global;
25using {mul as *} for Fr global;
26
27using {exp as ^} for Fr global;
28using {notEqual as !=} for Fr global;
29using {equal as ==} for Fr global;
30
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);
39// Instantiation
40
41library FrLib {
42 function from(uint256 value) internal pure returns (Fr) {
43 unchecked {
44 return Fr.wrap(value % MODULUS);
45 }
46 }
47
48 function fromBytes32(bytes32 value) internal pure returns (Fr) {
49 unchecked {
50 return Fr.wrap(uint256(value) % MODULUS);
51 }
52 }
53
54 function toBytes32(Fr value) internal pure returns (bytes32) {
55 unchecked {
56 return bytes32(Fr.unwrap(value));
57 }
58 }
59
60 function invert(Fr value) internal view returns (Fr) {
61 uint256 v = Fr.unwrap(value);
62 uint256 result;
63
64 // Call the modexp precompile to invert in the field
65 assembly {
66 let free := mload(0x40)
67 mstore(free, 0x20)
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)
74 if iszero(success) {
75 revert(0, 0)
76 }
77 result := mload(0x00)
78 mstore(0x40, add(free, 0x80))
79 }
80
81 return Fr.wrap(result);
82 }
83
84 function pow(Fr base, uint256 v) internal view returns (Fr) {
85 uint256 b = Fr.unwrap(base);
86 uint256 result;
87
88 // Call the modexp precompile to invert in the field
89 assembly {
90 let free := mload(0x40)
91 mstore(free, 0x20)
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)
98 if iszero(success) {
99 revert(0, 0)
100 }
101 result := mload(0x00)
102 mstore(0x40, add(free, 0x80))
103 }
104
105 return Fr.wrap(result);
106 }
107
108 function div(Fr numerator, Fr denominator) internal view returns (Fr) {
109 unchecked {
110 return numerator * invert(denominator);
111 }
112 }
113
114 function sqr(Fr value) internal pure returns (Fr) {
115 unchecked {
116 return value * value;
117 }
118 }
119
120 function unwrap(Fr value) internal pure returns (uint256) {
121 unchecked {
122 return Fr.unwrap(value);
123 }
124 }
125
126 function neg(Fr value) internal pure returns (Fr) {
127 unchecked {
128 return Fr.wrap(MODULUS - Fr.unwrap(value));
129 }
130 }
131}
132
133// Free functions
134function add(Fr a, Fr b) pure returns (Fr) {
135 unchecked {
136 return Fr.wrap(addmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
137 }
138}
139
140function mul(Fr a, Fr b) pure returns (Fr) {
141 unchecked {
142 return Fr.wrap(mulmod(Fr.unwrap(a), Fr.unwrap(b), MODULUS));
143 }
144}
145
146function sub(Fr a, Fr b) pure returns (Fr) {
147 unchecked {
148 return Fr.wrap(addmod(Fr.unwrap(a), MODULUS - Fr.unwrap(b), MODULUS));
149 }
150}
151
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) {
156 base = base * base;
157 }
158 return base;
159}
160
161function notEqual(Fr a, Fr b) pure returns (bool) {
162 unchecked {
163 return Fr.unwrap(a) != Fr.unwrap(b);
164 }
165}
166
167function equal(Fr a, Fr b) pure returns (bool) {
168 unchecked {
169 return Fr.unwrap(a) == Fr.unwrap(b);
170 }
171}
172
173uint256 constant CONST_PROOF_SIZE_LOG_N = 28;
174
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;
182
183uint256 constant FIELD_ELEMENT_SIZE = 0x20;
184uint256 constant GROUP_ELEMENT_SIZE = 0x40;
185
186// Alphas are used as relation separators so there should be NUMBER_OF_SUBRELATIONS - 1
187uint256 constant NUMBER_OF_ALPHAS = NUMBER_OF_SUBRELATIONS - 1;
188
189// ENUM FOR WIRES
190enum WIRE {
191 Q_M,
192 Q_C,
193 Q_L,
194 Q_R,
195 Q_O,
196 Q_4,
197 Q_LOOKUP,
198 Q_ARITH,
199 Q_RANGE,
200 Q_ELLIPTIC,
201 Q_MEMORY,
202 Q_NNF,
203 Q_POSEIDON2_EXTERNAL,
204 Q_POSEIDON2_INTERNAL,
205 SIGMA_1,
206 SIGMA_2,
207 SIGMA_3,
208 SIGMA_4,
209 ID_1,
210 ID_2,
211 ID_3,
212 ID_4,
213 TABLE_1,
214 TABLE_2,
215 TABLE_3,
216 TABLE_4,
217 LAGRANGE_FIRST,
218 LAGRANGE_LAST,
219 W_L,
220 W_R,
221 W_O,
222 W_4,
223 Z_PERM,
224 LOOKUP_INVERSES,
225 LOOKUP_READ_COUNTS,
226 LOOKUP_READ_TAGS,
227 W_L_SHIFT,
228 W_R_SHIFT,
229 W_O_SHIFT,
230 W_4_SHIFT,
231 Z_PERM_SHIFT
232}
233
234library Honk {
235 struct G1Point {
236 uint256 x;
237 uint256 y;
238 }
239
240 struct VerificationKey {
241 // Misc Params
242 uint256 circuitSize;
243 uint256 logCircuitSize;
244 uint256 publicInputsSize;
245 // Selectors
246 G1Point qm;
247 G1Point qc;
248 G1Point ql;
249 G1Point qr;
250 G1Point qo;
251 G1Point q4;
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;
260 // Copy cnstraints
261 G1Point s1;
262 G1Point s2;
263 G1Point s3;
264 G1Point s4;
265 // Copy identity
266 G1Point id1;
267 G1Point id2;
268 G1Point id3;
269 G1Point id4;
270 // Precomputed lookup table
271 G1Point t1;
272 G1Point t2;
273 G1Point t3;
274 G1Point t4;
275 // Fixed first and last
276 G1Point lagrangeFirst;
277 G1Point lagrangeLast;
278 }
279
280 struct RelationParameters {
281 // challenges
282 Fr eta;
283 Fr etaTwo;
284 Fr etaThree;
285 Fr beta;
286 Fr gamma;
287 // derived
288 Fr publicInputsDelta;
289 }
290
291 struct Proof {
292 // Pairing point object
293 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
294 // Free wires
295 G1Point w1;
296 G1Point w2;
297 G1Point w3;
298 G1Point w4;
299 // Lookup helpers - Permutations
300 G1Point zPerm;
301 // Lookup helpers - logup
302 G1Point lookupReadCounts;
303 G1Point lookupReadTags;
304 G1Point lookupInverses;
305 // Sumcheck
306 Fr[BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
307 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
308 // Shplemini
309 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
310 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
311 G1Point shplonkQ;
312 G1Point kzgQuotient;
313 }
314
315 struct ZKProof {
316 // Pairing point object
317 Fr[PAIRING_POINTS_SIZE] pairingPointObject;
318 // Commitments to wire polynomials
319 G1Point w1;
320 G1Point w2;
321 G1Point w3;
322 G1Point w4;
323 // Commitments to logup witness polynomials
324 G1Point lookupReadCounts;
325 G1Point lookupReadTags;
326 G1Point lookupInverses;
327 // Commitment to grand permutation polynomial
328 G1Point zPerm;
329 G1Point[3] libraCommitments;
330 // Sumcheck
331 Fr libraSum;
332 Fr[ZK_BATCHED_RELATION_PARTIAL_LENGTH][CONST_PROOF_SIZE_LOG_N] sumcheckUnivariates;
333 Fr[NUMBER_OF_ENTITIES] sumcheckEvaluations;
334 Fr libraEvaluation;
335 // ZK
336 G1Point geminiMaskingPoly;
337 Fr geminiMaskingEval;
338 // Shplemini
339 G1Point[CONST_PROOF_SIZE_LOG_N - 1] geminiFoldComms;
340 Fr[CONST_PROOF_SIZE_LOG_N] geminiAEvaluations;
341 Fr[4] libraPolyEvals;
342 G1Point shplonkQ;
343 G1Point kzgQuotient;
344 }
345}
346
347// Transcript library to generate fiat shamir challenges
348struct Transcript {
349 // Oink
350 Honk.RelationParameters relationParameters;
351 Fr[NUMBER_OF_ALPHAS] alphas;
352 Fr[CONST_PROOF_SIZE_LOG_N] gateChallenges;
353 // Sumcheck
354 Fr[CONST_PROOF_SIZE_LOG_N] sumCheckUChallenges;
355 // Gemini
356 Fr rho;
357 Fr geminiR;
358 // Shplonk
359 Fr shplonkNu;
360 Fr shplonkZ;
361}
362
363library TranscriptLib {
364 function generateTranscript(
365 Honk.Proof memory proof,
366 bytes32[] calldata publicInputs,
367 uint256 vkHash,
368 uint256 publicInputsSize,
369 uint256 logN
370 ) internal view returns (Transcript memory t) {
371 Fr previousChallenge;
372 (t.relationParameters, previousChallenge) =
373 generateRelationParametersChallenges(proof, publicInputs, vkHash, publicInputsSize, previousChallenge);
374
375 (t.alphas, previousChallenge) = generateAlphaChallenges(previousChallenge, proof);
376
377 (t.gateChallenges, previousChallenge) = generateGateChallenges(previousChallenge, logN);
378
379 (t.sumCheckUChallenges, previousChallenge) = generateSumcheckChallenges(proof, previousChallenge, logN);
380
381 (t.rho, previousChallenge) = generateRhoChallenge(proof, previousChallenge);
382
383 (t.geminiR, previousChallenge) = generateGeminiRChallenge(proof, previousChallenge, logN);
384
385 (t.shplonkNu, previousChallenge) = generateShplonkNuChallenge(proof, previousChallenge, logN);
386
387 (t.shplonkZ, previousChallenge) = generateShplonkZChallenge(proof, previousChallenge);
388
389 return t;
390 }
391
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));
398 }
399
400 function generateRelationParametersChallenges(
401 Honk.Proof memory proof,
402 bytes32[] calldata publicInputs,
403 uint256 vkHash,
404 uint256 publicInputsSize,
405 Fr previousChallenge
406 ) internal pure returns (Honk.RelationParameters memory rp, Fr nextPreviousChallenge) {
407 (rp.eta, rp.etaTwo, rp.etaThree, previousChallenge) =
408 generateEtaChallenge(proof, publicInputs, vkHash, publicInputsSize);
409
410 (rp.beta, rp.gamma, nextPreviousChallenge) = generateBetaAndGammaChallenges(previousChallenge, proof);
411 }
412
413 function generateEtaChallenge(
414 Honk.Proof memory proof,
415 bytes32[] calldata publicInputs,
416 uint256 vkHash,
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);
421
422 for (uint256 i = 0; i < publicInputsSize - PAIRING_POINTS_SIZE; i++) {
423 round0[1 + i] = bytes32(publicInputs[i]);
424 }
425 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
426 round0[1 + publicInputsSize - PAIRING_POINTS_SIZE + i] = FrLib.toBytes32(proof.pairingPointObject[i]);
427 }
428
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);
437
438 previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round0)));
439 (eta, etaTwo) = splitChallenge(previousChallenge);
440
441 previousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(previousChallenge))));
442 Fr unused;
443 (etaThree, unused) = splitChallenge(previousChallenge);
444 }
445
446 function generateBetaAndGammaChallenges(Fr previousChallenge, Honk.Proof memory proof)
447 internal
448 pure
449 returns (Fr beta, Fr gamma, Fr nextPreviousChallenge)
450 {
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);
459
460 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(round1)));
461 (beta, gamma) = splitChallenge(nextPreviousChallenge);
462 }
463
464 // Alpha challenges non-linearise the gate contributions
465 function generateAlphaChallenges(Fr previousChallenge, Honk.Proof memory proof)
466 internal
467 pure
468 returns (Fr[NUMBER_OF_ALPHAS] memory alphas, Fr nextPreviousChallenge)
469 {
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;
477
478 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(alpha0)));
479 (alphas[0], alphas[1]) = splitChallenge(nextPreviousChallenge);
480
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);
484 }
485 if (((NUMBER_OF_ALPHAS & 1) == 1) && (NUMBER_OF_ALPHAS > 2)) {
486 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(Fr.unwrap(nextPreviousChallenge))));
487 Fr unused;
488 (alphas[NUMBER_OF_ALPHAS - 1], unused) = splitChallenge(nextPreviousChallenge);
489 }
490 }
491
492 function generateGateChallenges(Fr previousChallenge, uint256 logN)
493 internal
494 pure
495 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory gateChallenges, Fr nextPreviousChallenge)
496 {
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];
501 }
502 nextPreviousChallenge = previousChallenge;
503 }
504
505 function generateSumcheckChallenges(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
506 internal
507 pure
508 returns (Fr[CONST_PROOF_SIZE_LOG_N] memory sumcheckChallenges, Fr nextPreviousChallenge)
509 {
510 for (uint256 i = 0; i < logN; i++) {
511 Fr[BATCHED_RELATION_PARTIAL_LENGTH + 1] memory univariateChal;
512 univariateChal[0] = prevChallenge;
513
514 for (uint256 j = 0; j < BATCHED_RELATION_PARTIAL_LENGTH; j++) {
515 univariateChal[j + 1] = proof.sumcheckUnivariates[i][j];
516 }
517 prevChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(univariateChal)));
518 Fr unused;
519 (sumcheckChallenges[i], unused) = splitChallenge(prevChallenge);
520 }
521 nextPreviousChallenge = prevChallenge;
522 }
523
524 function generateRhoChallenge(Honk.Proof memory proof, Fr prevChallenge)
525 internal
526 pure
527 returns (Fr rho, Fr nextPreviousChallenge)
528 {
529 Fr[NUMBER_OF_ENTITIES + 1] memory rhoChallengeElements;
530 rhoChallengeElements[0] = prevChallenge;
531
532 for (uint256 i = 0; i < NUMBER_OF_ENTITIES; i++) {
533 rhoChallengeElements[i + 1] = proof.sumcheckEvaluations[i];
534 }
535
536 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(rhoChallengeElements)));
537 Fr unused;
538 (rho, unused) = splitChallenge(nextPreviousChallenge);
539 }
540
541 function generateGeminiRChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
542 internal
543 pure
544 returns (Fr geminiR, Fr nextPreviousChallenge)
545 {
546 uint256[] memory gR = new uint256[]((logN - 1) * 2 + 1);
547 gR[0] = Fr.unwrap(prevChallenge);
548
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;
552 }
553
554 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(gR)));
555 Fr unused;
556 (geminiR, unused) = splitChallenge(nextPreviousChallenge);
557 }
558
559 function generateShplonkNuChallenge(Honk.Proof memory proof, Fr prevChallenge, uint256 logN)
560 internal
561 pure
562 returns (Fr shplonkNu, Fr nextPreviousChallenge)
563 {
564 uint256[] memory shplonkNuChallengeElements = new uint256[](logN + 1);
565 shplonkNuChallengeElements[0] = Fr.unwrap(prevChallenge);
566
567 for (uint256 i = 0; i < logN; i++) {
568 shplonkNuChallengeElements[i + 1] = Fr.unwrap(proof.geminiAEvaluations[i]);
569 }
570
571 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkNuChallengeElements)));
572 Fr unused;
573 (shplonkNu, unused) = splitChallenge(nextPreviousChallenge);
574 }
575
576 function generateShplonkZChallenge(Honk.Proof memory proof, Fr prevChallenge)
577 internal
578 view
579 returns (Fr shplonkZ, Fr nextPreviousChallenge)
580 {
581 uint256[3] memory shplonkZChallengeElements;
582 shplonkZChallengeElements[0] = Fr.unwrap(prevChallenge);
583
584 shplonkZChallengeElements[1] = proof.shplonkQ.x;
585 shplonkZChallengeElements[2] = proof.shplonkQ.y;
586
587 nextPreviousChallenge = FrLib.fromBytes32(keccak256(abi.encodePacked(shplonkZChallengeElements)));
588 Fr unused;
589 (shplonkZ, unused) = splitChallenge(nextPreviousChallenge);
590 }
591
592 function loadProof(bytes calldata proof, uint256 logN) internal pure returns (Honk.Proof memory p) {
593 uint256 boundary = 0x00;
594
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;
599 }
600 // Commitments
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;
607
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;
619
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;
625 }
626 }
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;
631 }
632
633 // Gemini
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;
638 }
639
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;
644 }
645
646 // Shplonk
647 p.shplonkQ = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
648 boundary += GROUP_ELEMENT_SIZE;
649 // KZG
650 p.kzgQuotient = bytesToG1Point(proof[boundary:boundary + GROUP_ELEMENT_SIZE]);
651 }
652}
653
654// Field arithmetic libraries
655
656library RelationsLib {
657 Fr internal constant GRUMPKIN_CURVE_B_PARAMETER_NEGATED = Fr.wrap(17); // -(-17)
658
659 function accumulateRelationEvaluations(
660 Fr[NUMBER_OF_ENTITIES] memory purportedEvaluations,
661 Honk.RelationParameters memory rp,
662 Fr[NUMBER_OF_ALPHAS] memory alphas,
663 Fr powPartialEval
664 ) internal pure returns (Fr accumulator) {
665 Fr[NUMBER_OF_SUBRELATIONS] memory evaluations;
666
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);
679 }
680
686 function wire(Fr[NUMBER_OF_ENTITIES] memory p, WIRE _wire) internal pure returns (Fr) {
687 return p[uint256(_wire)];
688 }
689
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,
699 Fr domainSep
700 ) internal pure {
701 // Relation 0
702 Fr q_arith = wire(p, WIRE.Q_ARITH);
703 {
704 Fr neg_half = Fr.wrap(NEG_HALF_MODULO_P);
705
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;
712 evals[0] = accum;
713 }
714
715 // Relation 1
716 {
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;
722 evals[1] = accum;
723 }
724 }
725
726 function accumulatePermutationRelation(
727 Fr[NUMBER_OF_ENTITIES] memory p,
728 Honk.RelationParameters memory rp,
729 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
730 Fr domainSep
731 ) internal pure {
732 Fr grand_product_numerator;
733 Fr grand_product_denominator;
734
735 {
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);
740
741 grand_product_numerator = num;
742 }
743 {
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);
748
749 grand_product_denominator = den;
750 }
751
752 // Contribution 2
753 {
754 Fr acc = (wire(p, WIRE.Z_PERM) + wire(p, WIRE.LAGRANGE_FIRST)) * grand_product_numerator;
755
756 acc = acc
757 - (
758 (wire(p, WIRE.Z_PERM_SHIFT) + (wire(p, WIRE.LAGRANGE_LAST) * rp.publicInputsDelta))
759 * grand_product_denominator
760 );
761 acc = acc * domainSep;
762 evals[2] = acc;
763 }
764
765 // Contribution 3
766 {
767 Fr acc = (wire(p, WIRE.LAGRANGE_LAST) * wire(p, WIRE.Z_PERM_SHIFT)) * domainSep;
768 evals[3] = acc;
769 }
770 }
771
772 function accumulateLogDerivativeLookupRelation(
773 Fr[NUMBER_OF_ENTITIES] memory p,
774 Honk.RelationParameters memory rp,
775 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
776 Fr domainSep
777 ) internal pure {
778 Fr write_term;
779 Fr read_term;
780
781 // Calculate the write term (the table accumulation)
782 {
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);
785 }
786
787 // Calculate the write term
788 {
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);
792
793 read_term = derived_entry_1 + (derived_entry_2 * rp.eta) + (derived_entry_3 * rp.etaTwo)
794 + (wire(p, WIRE.Q_O) * rp.etaThree);
795 }
796
797 Fr read_inverse = wire(p, WIRE.LOOKUP_INVERSES) * write_term;
798 Fr write_inverse = wire(p, WIRE.LOOKUP_INVERSES) * read_term;
799
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));
802
803 // Inverse calculated correctly relation
804 Fr accumulatorNone = read_term * write_term * wire(p, WIRE.LOOKUP_INVERSES) - inverse_exists_xor;
805 accumulatorNone = accumulatorNone * domainSep;
806
807 // Inverse
808 Fr accumulatorOne = wire(p, WIRE.Q_LOOKUP) * read_inverse - wire(p, WIRE.LOOKUP_READ_COUNTS) * write_inverse;
809
810 Fr read_tag = wire(p, WIRE.LOOKUP_READ_TAGS);
811
812 Fr read_tag_boolean_relation = read_tag * read_tag - read_tag;
813
814 evals[4] = accumulatorNone;
815 evals[5] = accumulatorOne;
816 evals[6] = read_tag_boolean_relation * domainSep;
817 }
818
819 function accumulateDeltaRangeRelation(
820 Fr[NUMBER_OF_ENTITIES] memory p,
821 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
822 Fr domainSep
823 ) internal pure {
824 Fr minus_one = ZERO - ONE;
825 Fr minus_two = ZERO - Fr.wrap(2);
826 Fr minus_three = ZERO - Fr.wrap(3);
827
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);
833
834 // Contribution 6
835 {
836 Fr acc = delta_1;
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;
842 evals[7] = acc;
843 }
844
845 // Contribution 7
846 {
847 Fr acc = delta_2;
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;
853 evals[8] = acc;
854 }
855
856 // Contribution 8
857 {
858 Fr acc = delta_3;
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;
864 evals[9] = acc;
865 }
866
867 // Contribution 9
868 {
869 Fr acc = delta_4;
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;
875 evals[10] = acc;
876 }
877 }
878
879 struct EllipticParams {
880 // Points
881 Fr x_1;
882 Fr y_1;
883 Fr x_2;
884 Fr y_2;
885 Fr y_3;
886 Fr x_3;
887 // push accumulators into memory
888 Fr x_double_identity;
889 }
890
891 function accumulateEllipticRelation(
892 Fr[NUMBER_OF_ENTITIES] memory p,
893 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
894 Fr domainSep
895 ) internal pure {
896 EllipticParams memory ep;
897 ep.x_1 = wire(p, WIRE.W_R);
898 ep.y_1 = wire(p, WIRE.W_O);
899
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);
904
905 Fr q_sign = wire(p, WIRE.Q_L);
906 Fr q_is_double = wire(p, WIRE.Q_M);
907
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);
912 {
913 // Move to top
914 Fr partialEval = domainSep;
915
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;
921
922 evals[11] = x_add_identity * partialEval * wire(p, WIRE.Q_ELLIPTIC) * (ONE - q_is_double);
923 }
924
925 // Contribution 11 point addition, x-coordinate check
926 // q_elliptic * (q_sign * y1 + y3)(x2 - x1) + (x3 - x1)(y2 - q_sign * y1) = 0
927 {
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);
932 }
933
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
937 {
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);
942
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;
945
946 Fr acc = ep.x_double_identity * domainSep * wire(p, WIRE.Q_ELLIPTIC) * q_is_double;
947 evals[11] = evals[11] + acc;
948 }
949
950 // Contribution 11 point doubling, y-coordinate check
951 // (y1 + y1) (2y1) - (3 * x1 * x1)(x1 - x3) = 0
952 {
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;
956 }
957 }
958
959 // Parameters used within the Memory Relation
960 // A struct is used to work around stack too deep. This relation has alot of variables
961 struct MemParams {
962 Fr memory_record_check;
963 Fr partial_record_check;
964 Fr next_gate_access_type;
965 Fr record_delta;
966 Fr index_delta;
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;
969 Fr access_check;
970 Fr next_gate_access_type_is_boolean;
971 Fr ROM_consistency_check_identity;
972 Fr RAM_consistency_check_identity;
973 Fr timestamp_delta;
974 Fr RAM_timestamp_check_identity;
975 Fr memory_identity;
976 Fr index_is_monotonically_increasing;
977 }
978
979 function accumulateMemoryRelation(
980 Fr[NUMBER_OF_ENTITIES] memory p,
981 Honk.RelationParameters memory rp,
982 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
983 Fr domainSep
984 ) internal pure {
985 MemParams memory ap;
986
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);
1034
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);
1053
1054 ap.index_is_monotonically_increasing = ap.index_delta * ap.index_delta - ap.index_delta; // deg 2
1055
1056 ap.adjacent_values_match_if_adjacent_indices_match = (ap.index_delta * MINUS_ONE + ONE) * ap.record_delta; // deg 2
1057
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
1062
1063 ap.ROM_consistency_check_identity = ap.memory_record_check * (wire(p, WIRE.Q_L) * wire(p, WIRE.Q_R)); // deg 3 or 7
1064
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
1086
1087 // reverse order we could re-use `ap.partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta
1088 // deg 1 or 4
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;
1093
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
1097
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
1102 // deg 2 or 4
1103 ap.next_gate_access_type_is_boolean =
1104 ap.next_gate_access_type * ap.next_gate_access_type - ap.next_gate_access_type;
1105
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
1111
1112 ap.RAM_consistency_check_identity = ap.access_check * (wire(p, WIRE.Q_O)); // deg 3 or 9
1113
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
1127
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
1138
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;
1142 }
1143
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);
1147
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
1150 struct NnfParams {
1151 Fr limb_subproduct;
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;
1157 Fr nnf_identity;
1158 }
1159
1160 function accumulateNnfRelation(
1161 Fr[NUMBER_OF_ENTITIES] memory p,
1162 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1163 Fr domainSep
1164 ) internal pure {
1165 NnfParams memory ap;
1166
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);
1186
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);
1192
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);
1197
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);
1201
1202 // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm
1203 // deg 2
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);
1214
1215 // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm
1216 // deg 2
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);
1227
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
1230
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;
1234 }
1235
1236 struct PoseidonExternalParams {
1237 Fr s1;
1238 Fr s2;
1239 Fr s3;
1240 Fr s4;
1241 Fr u1;
1242 Fr u2;
1243 Fr u3;
1244 Fr u4;
1245 Fr t0;
1246 Fr t1;
1247 Fr t2;
1248 Fr t3;
1249 Fr v1;
1250 Fr v2;
1251 Fr v3;
1252 Fr v4;
1253 Fr q_pos_by_scaling;
1254 }
1255
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?
1260 ) internal pure {
1261 PoseidonExternalParams memory ep;
1262
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);
1267
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
1287
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));
1290
1291 evals[21] = evals[21] + ep.q_pos_by_scaling * (ep.v2 - wire(p, WIRE.W_R_SHIFT));
1292
1293 evals[22] = evals[22] + ep.q_pos_by_scaling * (ep.v3 - wire(p, WIRE.W_O_SHIFT));
1294
1295 evals[23] = evals[23] + ep.q_pos_by_scaling * (ep.v4 - wire(p, WIRE.W_4_SHIFT));
1296 }
1297
1298 struct PoseidonInternalParams {
1299 Fr u1;
1300 Fr u2;
1301 Fr u3;
1302 Fr u4;
1303 Fr u_sum;
1304 Fr v1;
1305 Fr v2;
1306 Fr v3;
1307 Fr v4;
1308 Fr s1;
1309 Fr q_pos_by_scaling;
1310 }
1311
1312 function accumulatePoseidonInternalRelation(
1313 Fr[NUMBER_OF_ENTITIES] memory p,
1314 Fr[NUMBER_OF_SUBRELATIONS] memory evals,
1315 Fr domainSep
1316 ) internal pure {
1317 PoseidonInternalParams memory ip;
1318
1319 Fr[4] memory INTERNAL_MATRIX_DIAGONAL = [
1320 FrLib.from(0x10dc6e9c006ea38b04b1e03b4bd9490c0d03f98929ca1d7fb56821fd19d3b6e7),
1321 FrLib.from(0x0c28145b6a44df3e0149b3d0a30b3bb599df9756d4dd9b84a86b38cfb45a740b),
1322 FrLib.from(0x00544b8338791518b2c7645a50392798b21f75bb60e3596170067d00141cac15),
1323 FrLib.from(0x222c01175718386f2e2e82eb122789e352e105a3b8fa852613bc534433ee428b)
1324 ];
1325
1326 // add round constants
1327 ip.s1 = wire(p, WIRE.W_L) + wire(p, WIRE.Q_L);
1328
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);
1334
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;
1337
1338 ip.q_pos_by_scaling = wire(p, WIRE.Q_POSEIDON2_INTERNAL) * domainSep;
1339
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));
1342
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));
1345
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));
1348
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));
1351 }
1352
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];
1358
1359 for (uint256 i = 1; i < NUMBER_OF_SUBRELATIONS; ++i) {
1360 accumulator = accumulator + evaluations[i] * subrelationChallenges[i - 1];
1361 }
1362 }
1363}
1364
1365// Field arithmetic libraries - prevent littering the code with modmul / addmul
1366
1367library CommitmentSchemeLib {
1368 using FrLib for Fr;
1369
1370 // Avoid stack too deep
1371 struct ShpleminiIntermediates {
1372 Fr unshiftedScalar;
1373 Fr shiftedScalar;
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;
1382 Fr[4] denominators;
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;
1394 }
1395
1396 function computeSquares(Fr r, uint256 logN) internal pure returns (Fr[] memory) {
1397 Fr[] memory squares = new Fr[](logN);
1398 squares[0] = r;
1399 for (uint256 i = 1; i < logN; ++i) {
1400 squares[i] = squares[i - 1].sqr();
1401 }
1402 return squares;
1403 }
1404 // Compute the evaluations Aₗ(r^{2ˡ}) for l = 0, ..., m-1
1405
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,
1411 uint256 logSize
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];
1417
1418 Fr batchedEvalRoundAcc = (
1419 (challengePower * batchedEvalAccumulator * Fr.wrap(2))
1420 - geminiEvaluations[i - 1] * (challengePower * (ONE - u) - u)
1421 );
1422 // Divide by the denominator
1423 batchedEvalRoundAcc = batchedEvalRoundAcc * (challengePower * (ONE - u) + u).invert();
1424 if (i <= logSize) {
1425 batchedEvalAccumulator = batchedEvalRoundAcc;
1426 foldPosEvaluations[i - 1] = batchedEvalRoundAcc;
1427 }
1428 }
1429 return foldPosEvaluations;
1430 }
1431}
1432
1433uint256 constant Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; // EC group order. F_q
1434
1435function bytes32ToString(bytes32 value) pure returns (string memory result) {
1436 bytes memory alphabet = "0123456789abcdef";
1437
1438 bytes memory str = new bytes(66);
1439 str[0] = "0";
1440 str[1] = "x";
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)];
1444 }
1445 result = string(str);
1446}
1447
1448// Fr utility
1449
1450function bytesToFr(bytes calldata proofSection) pure returns (Fr scalar) {
1451 scalar = FrLib.fromBytes32(bytes32(proofSection));
1452}
1453
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
1459 });
1460}
1461
1462function negateInplace(Honk.G1Point memory point) pure returns (Honk.G1Point memory) {
1463 point.y = (Q - point.y) % Q;
1464 return point;
1465}
1466
1479function convertPairingPointsToG1(Fr[PAIRING_POINTS_SIZE] memory pairingPoints)
1480 pure
1481 returns (Honk.G1Point memory lhs, Honk.G1Point memory rhs)
1482{
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;
1487 lhs.x = lhsX;
1488
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;
1493 lhs.y = lhsY;
1494
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;
1499 rhs.x = rhsX;
1500
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;
1505 rhs.y = rhsY;
1506}
1507
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
1523 // hash the accum X
1524 // hash the accum Y
1525
1526 (Honk.G1Point memory proofLhs, Honk.G1Point memory proofRhs) = convertPairingPointsToG1(proofPairingPoints);
1527
1528 uint256[8] memory recursionSeparatorElements;
1529
1530 // Proof points
1531 recursionSeparatorElements[0] = proofLhs.x;
1532 recursionSeparatorElements[1] = proofLhs.y;
1533 recursionSeparatorElements[2] = proofRhs.x;
1534 recursionSeparatorElements[3] = proofRhs.y;
1535
1536 // Accumulator points
1537 recursionSeparatorElements[4] = accLhs.x;
1538 recursionSeparatorElements[5] = accLhs.y;
1539 recursionSeparatorElements[6] = accRhs.x;
1540 recursionSeparatorElements[7] = accRhs.y;
1541
1542 recursionSeparator = FrLib.fromBytes32(keccak256(abi.encodePacked(recursionSeparatorElements)));
1543}
1544
1554function mulWithSeperator(Honk.G1Point memory basePoint, Honk.G1Point memory other, Fr recursionSeperator)
1555 view
1556 returns (Honk.G1Point memory)
1557{
1558 Honk.G1Point memory result;
1559
1560 result = ecMul(recursionSeperator, basePoint);
1561 result = ecAdd(result, other);
1562
1563 return result;
1564}
1565
1574function ecMul(Fr value, Honk.G1Point memory point) view returns (Honk.G1Point memory) {
1575 Honk.G1Point memory result;
1576
1577 assembly {
1578 let free := mload(0x40)
1579 // Write the point into memory (two 32 byte words)
1580 // Memory layout:
1581 // Address | value
1582 // free | point.x
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)
1587 // Memory layout:
1588 // Address | value
1589 // free + 0x40| value
1590 mstore(add(free, 0x40), value)
1591
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) {
1596 revert(0, 0)
1597 }
1598 // Copy the result of the multiplication back into the result memory location.
1599 // Memory layout:
1600 // Address | value
1601 // result | result.x
1602 // result + 0x20| result.y
1603 mstore(result, mload(free))
1604 mstore(add(result, 0x20), mload(add(free, 0x20)))
1605
1606 mstore(0x40, add(free, 0x60))
1607 }
1608
1609 return result;
1610}
1611
1620function ecAdd(Honk.G1Point memory lhs, Honk.G1Point memory rhs) view returns (Honk.G1Point memory) {
1621 Honk.G1Point memory result;
1622
1623 assembly {
1624 let free := mload(0x40)
1625 // Write lhs into memory (two 32 byte words)
1626 // Memory layout:
1627 // Address | value
1628 // free | lhs.x
1629 // free + 0x20| lhs.y
1630 mstore(free, mload(lhs))
1631 mstore(add(free, 0x20), mload(add(lhs, 0x20)))
1632
1633 // Write rhs into memory (two 32 byte words)
1634 // Memory layout:
1635 // Address | value
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)))
1640
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) }
1645
1646 // Copy the result of the addition back into the result memory location.
1647 // Memory layout:
1648 // Address | value
1649 // result | result.x
1650 // result + 0x20| result.y
1651 mstore(result, mload(free))
1652 mstore(add(result, 0x20), mload(add(free, 0x20)))
1653
1654 mstore(0x40, add(free, 0x80))
1655 }
1656
1657 return result;
1658}
1659
1660function validateOnCurve(Honk.G1Point memory point) pure {
1661 uint256 x = point.x;
1662 uint256 y = point.y;
1663
1664 bool success = false;
1665 assembly {
1666 let xx := mulmod(x, x, Q)
1667 success := eq(mulmod(y, y, Q), addmod(mulmod(x, xx, Q), 3, Q))
1668 }
1669
1670 require(success, "point is not on the curve");
1671}
1672
1673function pairing(Honk.G1Point memory rhs, Honk.G1Point memory lhs) view returns (bool decodedResult) {
1674 bytes memory input = abi.encodePacked(
1675 rhs.x,
1676 rhs.y,
1677 // Fixed G2 point
1678 uint256(0x198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2),
1679 uint256(0x1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed),
1680 uint256(0x090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b),
1681 uint256(0x12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa),
1682 lhs.x,
1683 lhs.y,
1684 // G2 point from VK
1685 uint256(0x260e01b251f6f1c7e7ff4e580791dee8ea51d87a358e038b4efe30fac09383c1),
1686 uint256(0x0118c4d5b837bcc2bc89b5b398b5974e9f5944073b32078b7e231fec938883b0),
1687 uint256(0x04fc6369f7110fe3d25156c1bb9a72859cf2a04641f99ba4ee413c80da6a5fe4),
1688 uint256(0x22febda3c0c0632a56475b4214e5615e11e6dd3f96e6cea2854a87d4dacc5e55)
1689 );
1690
1691 (bool success, bytes memory result) = address(0x08).staticcall(input);
1692 decodedResult = success && abi.decode(result, (bool));
1693}
1694
1695// Field arithmetic libraries - prevent littering the code with modmul / addmul
1696
1697
1698
1699
1700abstract contract BaseHonkVerifier is IVerifier {
1701 using FrLib for Fr;
1702
1703 uint256 immutable $N;
1704 uint256 immutable $LOG_N;
1705 uint256 immutable $VK_HASH;
1706 uint256 immutable $NUM_PUBLIC_INPUTS;
1707
1708 constructor(uint256 _N, uint256 _logN, uint256 _vkHash, uint256 _numPublicInputs) {
1709 $N = _N;
1710 $LOG_N = _logN;
1711 $VK_HASH = _vkHash;
1712 $NUM_PUBLIC_INPUTS = _numPublicInputs;
1713 }
1714
1715 // Errors
1716 error ProofLengthWrong();
1717 error ProofLengthWrongWithLogN(uint256 logN, uint256 actualLength, uint256 expectedLength);
1718 error PublicInputsLengthWrong();
1719 error SumcheckFailed();
1720 error ShpleminiFailed();
1721
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
1726
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
1731
1732 // Sumcheck
1733 proofLength += logN * BATCHED_RELATION_PARTIAL_LENGTH * NUM_ELEMENTS_FR; // sumcheck univariates
1734 proofLength += NUMBER_OF_ENTITIES * NUM_ELEMENTS_FR; // sumcheck evaluations
1735
1736 // Gemini
1737 proofLength += (logN - 1) * NUM_ELEMENTS_COMM; // Gemini Fold commitments
1738 proofLength += logN * NUM_ELEMENTS_FR; // Gemini evaluations
1739
1740 // Shplonk and KZG commitments
1741 proofLength += NUM_ELEMENTS_COMM * 2; // Shplonk Q and KZG W commitments
1742
1743 // Pairing points
1744 proofLength += PAIRING_POINTS_SIZE; // pairing inputs carried on public inputs
1745
1746 return proofLength;
1747 }
1748
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;
1752
1753 function loadVerificationKey() internal pure virtual returns (Honk.VerificationKey memory);
1754
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);
1758
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);
1762 }
1763
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();
1768 }
1769
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);
1772
1773 // Derive public input delta
1774 t.relationParameters.publicInputsDelta = computePublicInputDelta(
1775 publicInputs,
1776 p.pairingPointObject,
1777 t.relationParameters.beta,
1778 t.relationParameters.gamma, /*pubInputsOffset=*/
1779 1
1780 );
1781
1782 // Sumcheck
1783 bool sumcheckVerified = verifySumcheck(p, t);
1784 if (!sumcheckVerified) revert SumcheckFailed();
1785
1786 bool shpleminiVerified = verifyShplemini(p, vk, t);
1787 if (!shpleminiVerified) revert ShpleminiFailed();
1788
1789 return sumcheckVerified && shpleminiVerified; // Boolean condition not required - nice for vanity :)
1790 }
1791
1792 uint256 constant PERMUTATION_ARGUMENT_VALUE_SEPARATOR = 1 << 28;
1793
1794 function computePublicInputDelta(
1795 bytes32[] memory publicInputs,
1796 Fr[PAIRING_POINTS_SIZE] memory pairingPointObject,
1797 Fr beta,
1798 Fr gamma,
1799 uint256 offset
1800 ) internal view returns (Fr publicInputDelta) {
1801 Fr numerator = ONE;
1802 Fr denominator = ONE;
1803
1804 Fr numeratorAcc = gamma + (beta * FrLib.from(PERMUTATION_ARGUMENT_VALUE_SEPARATOR + offset));
1805 Fr denominatorAcc = gamma - (beta * FrLib.from(offset + 1));
1806
1807 {
1808 for (uint256 i = 0; i < $NUM_PUBLIC_INPUTS - PAIRING_POINTS_SIZE; i++) {
1809 Fr pubInput = FrLib.fromBytes32(publicInputs[i]);
1810
1811 numerator = numerator * (numeratorAcc + pubInput);
1812 denominator = denominator * (denominatorAcc + pubInput);
1813
1814 numeratorAcc = numeratorAcc + beta;
1815 denominatorAcc = denominatorAcc - beta;
1816 }
1817
1818 for (uint256 i = 0; i < PAIRING_POINTS_SIZE; i++) {
1819 Fr pubInput = pairingPointObject[i];
1820
1821 numerator = numerator * (numeratorAcc + pubInput);
1822 denominator = denominator * (denominatorAcc + pubInput);
1823
1824 numeratorAcc = numeratorAcc + beta;
1825 denominatorAcc = denominatorAcc - beta;
1826 }
1827 }
1828
1829 publicInputDelta = FrLib.div(numerator, denominator);
1830 }
1831
1832 function verifySumcheck(Honk.Proof memory proof, Transcript memory tp) internal view returns (bool verified) {
1833 Fr roundTarget;
1834 Fr powPartialEvaluation = ONE;
1835
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();
1841
1842 Fr roundChallenge = tp.sumCheckUChallenges[round];
1843
1844 // Update the round target for the next rounf
1845 roundTarget = computeNextTargetSum(roundUnivariate, roundChallenge);
1846 powPartialEvaluation = partiallyEvaluatePOW(tp.gateChallenges[round], powPartialEvaluation, roundChallenge);
1847 }
1848
1849 // Last round
1850 Fr grandHonkRelationSum = RelationsLib.accumulateRelationEvaluations(
1851 proof.sumcheckEvaluations, tp.relationParameters, tp.alphas, powPartialEvaluation
1852 );
1853 verified = (grandHonkRelationSum == roundTarget);
1854 }
1855
1856 function checkSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariate, Fr roundTarget)
1857 internal
1858 pure
1859 returns (bool checked)
1860 {
1861 Fr totalSum = roundUnivariate[0] + roundUnivariate[1];
1862 checked = totalSum == roundTarget;
1863 }
1864
1865 // Return the new target sum for the next sumcheck round
1866 function computeNextTargetSum(Fr[BATCHED_RELATION_PARTIAL_LENGTH] memory roundUnivariates, Fr roundChallenge)
1867 internal
1868 view
1869 returns (Fr targetSum)
1870 {
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)
1880 ];
1881 // To compute the next target sum, we evaluate the given univariate at a point u (challenge).
1882
1883 // Performing Barycentric evaluations
1884 // Compute B(x)
1885 Fr numeratorValue = ONE;
1886 for (uint256 i = 0; i < BATCHED_RELATION_PARTIAL_LENGTH; ++i) {
1887 numeratorValue = numeratorValue * (roundChallenge - Fr.wrap(i));
1888 }
1889
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;
1896 }
1897
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;
1902 }
1903
1904 // Scale the sum by the value of B(x)
1905 targetSum = targetSum * numeratorValue;
1906 }
1907
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)
1910 internal
1911 pure
1912 returns (Fr newEvaluation)
1913 {
1914 Fr univariateEval = ONE + (roundChallenge * (gateChallenge - ONE));
1915 newEvaluation = currentEvaluation * univariateEval;
1916 }
1917
1918 function verifyShplemini(Honk.Proof memory proof, Honk.VerificationKey memory vk, Transcript memory tp)
1919 internal
1920 view
1921 returns (bool verified)
1922 {
1923 CommitmentSchemeLib.ShpleminiIntermediates memory mem; // stack
1924
1925 // - Compute vector (r, r², ... , r²⁽ⁿ⁻¹⁾), where n = log_circuit_size
1926 Fr[] memory powers_of_evaluation_challenge = CommitmentSchemeLib.computeSquares(tp.geminiR, $LOG_N);
1927
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);
1931
1932 mem.posInvertedDenominator = (tp.shplonkZ - powers_of_evaluation_challenge[0]).invert();
1933 mem.negInvertedDenominator = (tp.shplonkZ + powers_of_evaluation_challenge[0]).invert();
1934
1935 mem.unshiftedScalar = mem.posInvertedDenominator + (tp.shplonkNu * mem.negInvertedDenominator);
1936 mem.shiftedScalar =
1937 tp.geminiR.invert() * (mem.posInvertedDenominator - (tp.shplonkNu * mem.negInvertedDenominator));
1938
1939 scalars[0] = ONE;
1940 commitments[0] = proof.shplonkQ;
1941
1942 /* Batch multivariate opening claims, shifted and unshifted
1943 * The vector of scalars is populated as follows:
1944 * \f[
1945 * \left(
1946 * - \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right),
1947 * \ldots,
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),
1950 * \ldots,
1951 * - \rho^{k+m-1} \times \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right)
1952 * \right)
1953 * \f]
1954 *
1955 * The following vector is concatenated to the vector of commitments:
1956 * \f[
1957 * f_0, \ldots, f_{m-1}, f_{\text{shift}, 0}, \ldots, f_{\text{shift}, k-1}
1958 * \f]
1959 *
1960 * Simultaneously, the evaluation of the multilinear polynomial
1961 * \f[
1962 * \sum \rho^i \cdot f_i + \sum \rho^{i+k} \cdot f_{\text{shift}, i}
1963 * \f]
1964 * at the challenge point \f$ (u_0,\ldots, u_{n-1}) \f$ is computed.
1965 *
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$.
1968 */
1969 mem.batchingChallenge = ONE;
1970 mem.batchedEvaluation = ZERO;
1971
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;
1978 }
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;
1988
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;
1993 }
1994
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;
2023
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;
2033
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
2037 *
2038 * 1. Moves the vector
2039 * \f[
2040 * \left( \text{com}(A_1), \text{com}(A_2), \ldots, \text{com}(A_{n-1}) \right)
2041 * \f]
2042 * to the 'commitments' vector.
2043 *
2044 * 2. Computes the scalars:
2045 * \f[
2046 * \frac{\nu^{2}}{z + r^2}, \frac{\nu^3}{z + r^4}, \ldots, \frac{\nu^{n-1}}{z + r^{2^{n-1}}}
2047 * \f]
2048 * and places them into the 'scalars' vector.
2049 *
2050 * 3. Accumulates the summands of the constant term:
2051 * \f[
2052 * \sum_{i=2}^{n-1} \frac{\nu^{i} \cdot A_i(-r^{2^i})}{z + r^{2^i}}
2053 * \f]
2054 * and adds them to the 'constant_term_accumulator'.
2055 */
2056
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,
2063 $LOG_N
2064 );
2065
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);
2070
2071 mem.batchingChallenge = tp.shplonkNu.sqr();
2072
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);
2077
2078 if (!dummy_round) {
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();
2082
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();
2088
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;
2096 }
2097
2098 commitments[NUMBER_UNSHIFTED + 1 + i] = proof.geminiFoldComms[i];
2099 }
2100
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;
2104
2105 Honk.G1Point memory quotient_commitment = proof.kzgQuotient;
2106
2107 commitments[NUMBER_UNSHIFTED + $LOG_N + 1] = quotient_commitment;
2108 scalars[NUMBER_UNSHIFTED + $LOG_N + 1] = tp.shplonkZ; // evaluation challenge
2109
2110 Honk.G1Point memory P_0_agg = batchMul(commitments, scalars);
2111 Honk.G1Point memory P_1_agg = negateInplace(quotient_commitment);
2112
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);
2117
2118 // Validate the points from the proof are on the curve
2119 validateOnCurve(P_0_other);
2120 validateOnCurve(P_1_other);
2121
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);
2125
2126 return pairing(P_0_agg, P_1_agg);
2127 }
2128
2129 function batchMul(Honk.G1Point[] memory base, Fr[] memory scalars)
2130 internal
2131 view
2132 returns (Honk.G1Point memory result)
2133 {
2134 uint256 limit = NUMBER_UNSHIFTED + $LOG_N + 2;
2135
2136 // Validate all points are on the curve
2137 for (uint256 i = 0; i < limit; ++i) {
2138 validateOnCurve(base[i]);
2139 }
2140
2141 bool success = true;
2142 assembly {
2143 let free := mload(0x40)
2144
2145 let count := 0x01
2146 for {} lt(count, add(limit, 1)) { count := add(count, 1) } {
2147 // Get loop offsets
2148 let base_base := add(base, mul(count, 0x20))
2149 let scalar_base := add(scalars, mul(count, 0x20))
2150
2151 mstore(add(free, 0x40), mload(mload(base_base)))
2152 mstore(add(free, 0x60), mload(add(0x20, mload(base_base))))
2153 // Add scalar
2154 mstore(add(free, 0x80), mload(scalar_base))
2155
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))
2159 }
2160
2161 // Return the result
2162 mstore(result, mload(free))
2163 mstore(add(result, 0x20), mload(add(free, 0x20)))
2164 }
2165
2166 require(success, ShpleminiFailed());
2167 }
2168}
2169
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();
2173 }
2174}
2175)";
2176
2177inline std::string get_honk_solidity_verifier(auto const& verification_key)
2178{
2179 std::ostringstream stream;
2180 output_vk_sol_ultra_honk(stream, verification_key, "HonkVerificationKey");
2181 return stream.str() + HONK_CONTRACT_SOURCE;
2182}
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)