From c0ca508a6b134524edde488ff2bed198ee768794 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 23 Jan 2024 12:50:14 +0100 Subject: [PATCH] Refactor verifier contract: public input as dynamic array --- contracts/Marketplace.sol | 8 +++++--- contracts/Proofs.sol | 2 +- contracts/TestProofs.sol | 2 +- contracts/TestVerifier.sol | 2 +- contracts/Verifier.sol | 2 +- contracts/verifiers/local/verifier_groth.sol | 8 ++------ verifier/template/verifier_groth.sol | 8 ++------ 7 files changed, 13 insertions(+), 19 deletions(-) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index e1fdbd1..73e2fe5 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -186,9 +186,11 @@ contract Marketplace is Proofs, StateRetrieval, Endian { ) public requestIsKnown(_slots[id].requestId) { Slot storage slot = _slots[id]; Request storage request = _requests[slot.requestId]; - uint256 challenge = _challengeToFieldElement(getChallenge(id)); - uint256 merkleRoot = _merkleRootToFieldElement(request.content.merkleRoot); - _proofReceived(id, proof, [challenge, merkleRoot, slot.slotIndex]); + uint256[] memory pubSignals = new uint256[](3); + pubSignals[0] = _challengeToFieldElement(getChallenge(id)); + pubSignals[1] = _merkleRootToFieldElement(request.content.merkleRoot); + pubSignals[2] = slot.slotIndex; + _proofReceived(id, proof, pubSignals); } function markProofAsMissing(SlotId slotId, Period period) public { diff --git a/contracts/Proofs.sol b/contracts/Proofs.sol index 2eebe74..e536402 100644 --- a/contracts/Proofs.sol +++ b/contracts/Proofs.sol @@ -112,7 +112,7 @@ abstract contract Proofs is Periods { function _proofReceived( SlotId id, Groth16Proof calldata proof, - uint[3] memory pubSignals + uint[] memory pubSignals ) internal { require(!_received[id][_blockPeriod()], "Proof already submitted"); require( diff --git a/contracts/TestProofs.sol b/contracts/TestProofs.sol index 9816708..7547b68 100644 --- a/contracts/TestProofs.sol +++ b/contracts/TestProofs.sol @@ -27,7 +27,7 @@ contract TestProofs is Proofs { function proofReceived( SlotId id, Groth16Proof calldata proof, - uint[3] memory pubSignals + uint[] memory pubSignals ) public { _proofReceived(id, proof, pubSignals); } diff --git a/contracts/TestVerifier.sol b/contracts/TestVerifier.sol index 9a80ba9..074a827 100644 --- a/contracts/TestVerifier.sol +++ b/contracts/TestVerifier.sol @@ -18,7 +18,7 @@ contract TestVerifier is IVerifier { uint[2] calldata, uint[2][2] calldata, uint[2] calldata, - uint[3] calldata + uint[] calldata ) external view returns (bool) { return _proofsAreValid; } diff --git a/contracts/Verifier.sol b/contracts/Verifier.sol index 051d14c..bacbce9 100644 --- a/contracts/Verifier.sol +++ b/contracts/Verifier.sol @@ -6,6 +6,6 @@ interface IVerifier { uint[2] calldata pA, uint[2][2] calldata pB, uint[2] calldata pC, - uint[3] calldata pubSignals + uint[] calldata pubSignals ) external view returns (bool); } diff --git a/contracts/verifiers/local/verifier_groth.sol b/contracts/verifiers/local/verifier_groth.sol index a6e2092..8d71e95 100644 --- a/contracts/verifiers/local/verifier_groth.sol +++ b/contracts/verifiers/local/verifier_groth.sol @@ -189,17 +189,13 @@ contract Verifier { uint[2] memory a, uint[2][2] memory b, uint[2] memory c, - uint[3] memory input + uint[] memory input ) public view returns (bool r) { Proof memory proof; proof.A = Pairing.G1Point(a[0], a[1]); proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); proof.C = Pairing.G1Point(c[0], c[1]); - uint[] memory inputValues = new uint[](input.length); - for(uint i = 0; i < input.length; i++){ - inputValues[i] = input[i]; - } - if (verify(inputValues, proof) == 0) { + if (verify(input, proof) == 0) { return true; } else { return false; diff --git a/verifier/template/verifier_groth.sol b/verifier/template/verifier_groth.sol index 29f87a0..8d71e95 100644 --- a/verifier/template/verifier_groth.sol +++ b/verifier/template/verifier_groth.sol @@ -189,17 +189,13 @@ contract Verifier { uint[2] memory a, uint[2][2] memory b, uint[2] memory c, - uint[<%vk_input_length%>] memory input + uint[] memory input ) public view returns (bool r) { Proof memory proof; proof.A = Pairing.G1Point(a[0], a[1]); proof.B = Pairing.G2Point([b[0][0], b[0][1]], [b[1][0], b[1][1]]); proof.C = Pairing.G1Point(c[0], c[1]); - uint[] memory inputValues = new uint[](input.length); - for(uint i = 0; i < input.length; i++){ - inputValues[i] = input[i]; - } - if (verify(inputValues, proof) == 0) { + if (verify(input, proof) == 0) { return true; } else { return false;