Skip to content

Commit

Permalink
test: Verifier: Add test for permutation circuit
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Feb 19, 2025
1 parent f48aaea commit 6bdd67d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/Verifier.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,35 @@ contract VerifierTest is VerifierTestUtils {
bool res = verifier.verify(proof, publicInputs, vkey);
require(res, "Proof verification should have succeeded");
}

/// @notice Test the verifier against a reference implementation on the permutation circuit
function testVerifierPermutation() public {
uint256 N = 5;
// First generate the verification key for the circuit
compileRustBinary("test/rust-reference-impls/verifier/Cargo.toml");
VerificationKey memory vkey = getPermutationVkey();

// Generate a random statement and witness
uint256[5] memory statement;
uint256[5] memory witness;
for (uint256 i = 0; i < N; i++) {
uint256 val = randomFelt();
statement[i] = val;
witness[N - i - 1] = val; // A simple reverse permutation
}

// Get the proof
uint256 randomChallenge = randomFelt();
PlonkProof memory proof = getPermutationProof(randomChallenge, statement, witness);

// Verify the proof
BN254.ScalarField[] memory publicInputs = new BN254.ScalarField[](N + 1);
publicInputs[0] = BN254.ScalarField.wrap(randomChallenge);
for (uint256 i = 0; i < N; i++) {
publicInputs[i + 1] = BN254.ScalarField.wrap(statement[i]);
}

bool res = verifier.verify(proof, publicInputs, vkey);
require(res, "Proof verification should have succeeded");
}
}
43 changes: 43 additions & 0 deletions test/utils/VerifierTestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,47 @@ contract VerifierTestUtils is TestUtils {
string memory response = runBinaryGetResponse(args);
return (expected, abi.decode(vm.parseBytes(response), (PlonkProof)));
}

/// @dev Run the reference implementation to generate a vkey for the permutation circuit
function getPermutationVkey() internal returns (VerificationKey memory) {
string[] memory args = new string[](3);
args[0] = "./test/rust-reference-impls/target/debug/verifier";
args[1] = "permutation";
args[2] = "print-vkey";

string memory response = runBinaryGetResponse(args);
return abi.decode(vm.parseBytes(response), (VerificationKey));
}

/// @dev Run the reference implementation to generate a proof for the permutation circuit
function getPermutationProof(
uint256 randomChallenge,
uint256[5] memory statement,
uint256[5] memory witness
)
internal
returns (PlonkProof memory)
{
string[] memory args = new string[](17);
args[0] = "./test/rust-reference-impls/target/debug/verifier";
args[1] = "permutation";
args[2] = "prove";
args[3] = "--random-challenge";
args[4] = Strings.toString(randomChallenge);

// Encode statement elements
args[5] = "--values";
for (uint256 i = 0; i < statement.length; i++) {
args[6 + i] = Strings.toString(statement[i]);
}

// Encode witness elements
args[11] = "--permuted-values";
for (uint256 i = 0; i < witness.length; i++) {
args[12 + i] = Strings.toString(witness[i]);
}

string memory response = runBinaryGetResponse(args);
return abi.decode(vm.parseBytes(response), (PlonkProof));
}
}

0 comments on commit 6bdd67d

Please sign in to comment.