Refactor verifier contract: remove unnecessary conversions

This commit is contained in:
Mark Spanbroek 2024-01-23 13:28:53 +01:00 committed by markspanbroek
parent 65655e3646
commit 70b22b241f
8 changed files with 18 additions and 46 deletions

View File

@ -16,3 +16,10 @@ struct Groth16Proof {
G2Point b;
G1Point c;
}
interface IGroth16Verifier {
function verify(
Groth16Proof calldata proof,
uint[] calldata pubSignals
) external view returns (bool);
}

View File

@ -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);
}
}

View File

@ -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_;

View File

@ -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);
}

View File

@ -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
{}

View File

@ -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) {

View File

@ -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;

View File

@ -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);
}