From 296447724ac9ee13a1c558a54a18244203f2177a Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 23 Jan 2024 10:48:24 +0100 Subject: [PATCH] Refactor verifier contract: extract constants --- contracts/verifiers/local/verifier_groth.sol | 6 +++--- verifier/template/verifier_groth.sol | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/contracts/verifiers/local/verifier_groth.sol b/contracts/verifiers/local/verifier_groth.sol index 9931b54..2e5e9e5 100644 --- a/contracts/verifiers/local/verifier_groth.sol +++ b/contracts/verifiers/local/verifier_groth.sol @@ -19,6 +19,8 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.23; library Pairing { + // The prime q in the base field F_q for G1 + uint constant private q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; struct G1Point { uint X; uint Y; @@ -30,8 +32,6 @@ library Pairing { } /// The negation of p, i.e. p.addition(p.negate()) should be zero. function negate(G1Point memory p) internal pure returns (G1Point memory) { - // The prime q in the base field F_q for G1 - uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; if (p.X == 0 && p.Y == 0) return G1Point(0, 0); return G1Point(p.X, q - (p.Y % q)); @@ -145,6 +145,7 @@ library Pairing { } contract Verifier { using Pairing for *; + uint256 constant private snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; struct VerifyingKey { Pairing.G1Point alfa1; Pairing.G2Point beta2; @@ -169,7 +170,6 @@ contract Verifier { vk.IC[3] = Pairing.G1Point(18155166643053044822201627105588517913195535693446564472247126736722594445000, 13816319482622393060406816684195314200198627617641073470088058848129378231754); } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { - uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.IC.length,"verifier-bad-input"); // Compute the linear combination vk_x diff --git a/verifier/template/verifier_groth.sol b/verifier/template/verifier_groth.sol index 6db6ab9..f2c2a53 100644 --- a/verifier/template/verifier_groth.sol +++ b/verifier/template/verifier_groth.sol @@ -19,6 +19,8 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.23; library Pairing { + // The prime q in the base field F_q for G1 + uint constant private q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; struct G1Point { uint X; uint Y; @@ -30,8 +32,6 @@ library Pairing { } /// The negation of p, i.e. p.addition(p.negate()) should be zero. function negate(G1Point memory p) internal pure returns (G1Point memory) { - // The prime q in the base field F_q for G1 - uint q = 21888242871839275222246405745257275088696311157297823662689037894645226208583; if (p.X == 0 && p.Y == 0) return G1Point(0, 0); return G1Point(p.X, q - (p.Y % q)); @@ -145,6 +145,7 @@ library Pairing { } contract Verifier { using Pairing for *; + uint256 constant private snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; struct VerifyingKey { Pairing.G1Point alfa1; Pairing.G2Point beta2; @@ -166,7 +167,6 @@ contract Verifier { <%vk_ic_pts%> } function verify(uint[] memory input, Proof memory proof) internal view returns (uint) { - uint256 snark_scalar_field = 21888242871839275222246405745257275088548364400416034343698204186575808495617; VerifyingKey memory vk = verifyingKey(); require(input.length + 1 == vk.IC.length,"verifier-bad-input"); // Compute the linear combination vk_x