From 70b22b241ff2455f1ecad854ec1744ac7936d766 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 23 Jan 2024 13:28:53 +0100 Subject: [PATCH] Refactor verifier contract: remove unnecessary conversions --- contracts/Groth16.sol | 7 +++++++ contracts/Groth16Verifier.sol | 14 +------------- contracts/Marketplace.sol | 3 +-- contracts/Proofs.sol | 15 +++------------ contracts/TestMarketplace.sol | 2 +- contracts/TestProofs.sol | 2 +- contracts/TestVerifier.sol | 10 ++++------ contracts/Verifier.sol | 11 ----------- 8 files changed, 18 insertions(+), 46 deletions(-) delete mode 100644 contracts/Verifier.sol diff --git a/contracts/Groth16.sol b/contracts/Groth16.sol index 649bc0d..58478b0 100644 --- a/contracts/Groth16.sol +++ b/contracts/Groth16.sol @@ -16,3 +16,10 @@ struct Groth16Proof { G2Point b; G1Point c; } + +interface IGroth16Verifier { + function verify( + Groth16Proof calldata proof, + uint[] calldata pubSignals + ) external view returns (bool); +} diff --git a/contracts/Groth16Verifier.sol b/contracts/Groth16Verifier.sol index a8200c8..bea7c47 100644 --- a/contracts/Groth16Verifier.sol +++ b/contracts/Groth16Verifier.sol @@ -129,7 +129,7 @@ contract Groth16Verifier { _verifyingKey.ic.push(key.ic[i]); } } - function _verify(uint[] memory input, Groth16Proof memory proof) internal view returns (bool) { + function verify(Groth16Proof calldata proof, uint[] memory input) public view returns (bool) { require(input.length + 1 == _verifyingKey.ic.length,"verifier-bad-input"); // Compute the linear combination vkX G1Point memory vkX = G1Point(0, 0); @@ -145,16 +145,4 @@ contract Groth16Verifier { proof.c, _verifyingKey.delta2 ); } - function verifyProof( - uint[2] memory a, - uint[2][2] memory b, - uint[2] memory c, - uint[] memory input - ) public view returns (bool r) { - Groth16Proof memory proof; - proof.a = G1Point(a[0], a[1]); - proof.b = G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); - proof.c = G1Point(c[0], c[1]); - return _verify(input, proof); - } } diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index 73e2fe5..378b8e0 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -9,7 +9,6 @@ import "./Requests.sol"; import "./Proofs.sol"; import "./StateRetrieval.sol"; import "./Endian.sol"; -import "./Verifier.sol"; import "./Groth16.sol"; contract Marketplace is Proofs, StateRetrieval, Endian { @@ -57,7 +56,7 @@ contract Marketplace is Proofs, StateRetrieval, Endian { constructor( MarketplaceConfig memory configuration, IERC20 token_, - IVerifier verifier + IGroth16Verifier verifier ) Proofs(configuration.proofs, verifier) { token = token_; diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index e536402..5dfb02f 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -4,16 +4,15 @@ pragma solidity 0.8.23; import "./Configuration.sol"; import "./Requests.sol"; import "./Periods.sol"; -import "./Verifier.sol"; import "./Groth16.sol"; abstract contract Proofs is Periods { ProofConfig private _config; - IVerifier private _verifier; + IGroth16Verifier private _verifier; constructor( ProofConfig memory config, - IVerifier verifier + IGroth16Verifier verifier ) Periods(config.period) { require(block.number > 256, "Insufficient block height"); _config = config; @@ -115,15 +114,7 @@ abstract contract Proofs is Periods { uint[] memory pubSignals ) internal { require(!_received[id][_blockPeriod()], "Proof already submitted"); - require( - _verifier.verifyProof( - [proof.a.x, proof.a.y], - [proof.b.x, proof.b.y], - [proof.c.x, proof.c.y], - pubSignals - ), - "Invalid proof" - ); + require(_verifier.verify(proof, pubSignals), "Invalid proof"); _received[id][_blockPeriod()] = true; emit ProofSubmitted(id); } diff --git a/contracts/TestMarketplace.sol b/contracts/TestMarketplace.sol index e5da1b6..f67fa07 100644 --- a/contracts/TestMarketplace.sol +++ b/contracts/TestMarketplace.sol @@ -8,7 +8,7 @@ contract TestMarketplace is Marketplace { constructor( MarketplaceConfig memory config, IERC20 token, - IVerifier verifier + IGroth16Verifier verifier ) Marketplace(config, token, verifier) // solhint-disable-next-line no-empty-blocks {} diff --git a/contracts/TestProofs.sol b/contracts/TestProofs.sol index 7547b68..a90e00f 100644 --- a/contracts/TestProofs.sol +++ b/contracts/TestProofs.sol @@ -9,7 +9,7 @@ contract TestProofs is Proofs { constructor( ProofConfig memory config, - IVerifier verifier + IGroth16Verifier verifier ) Proofs(config, verifier) {} // solhint-disable-line no-empty-blocks function slotState(SlotId slotId) public view override returns (SlotState) { diff --git a/contracts/TestVerifier.sol b/contracts/TestVerifier.sol index 074a827..862ffcd 100644 --- a/contracts/TestVerifier.sol +++ b/contracts/TestVerifier.sol @@ -1,9 +1,9 @@ // SPDX-License-Identifier: MIT pragma solidity 0.8.23; -import "./Verifier.sol"; +import "./Groth16.sol"; -contract TestVerifier is IVerifier { +contract TestVerifier is IGroth16Verifier { bool private _proofsAreValid; constructor() { @@ -14,10 +14,8 @@ contract TestVerifier is IVerifier { _proofsAreValid = proofsAreValid; } - function verifyProof( - uint[2] calldata, - uint[2][2] calldata, - uint[2] calldata, + function verify( + Groth16Proof calldata, uint[] calldata ) external view returns (bool) { return _proofsAreValid; diff --git a/contracts/Verifier.sol b/contracts/Verifier.sol deleted file mode 100644 index bacbce9..0000000 --- a/contracts/Verifier.sol +++ /dev/null @@ -1,11 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.23; - -interface IVerifier { - function verifyProof( - uint[2] calldata pA, - uint[2][2] calldata pB, - uint[2] calldata pC, - uint[] calldata pubSignals - ) external view returns (bool); -}