Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update verifier contract templates #963

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions book/developers/building-plonk-artifacts.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# Building Plonk BN254 Artifacts

To build the Plonk Bn254 artifacts from scratch, you can use the `Makefile` inside the `prover` directory.
To build the production Plonk Bn254 artifacts from scratch, you can use the `Makefile` inside the `prover` directory.

```shell,noplayground
RUST_LOG=info make plonk-bn254
```
cd prover
RUST_LOG=info make build-plonk-bn254
```

## Non-production builds

For quickly building the plonk artifacts, you can run `cargo test` with additional flags to speed up the build process.

```shell,noplayground
SP1_DEV=true FRI_QUERIES=1 cargo test --release test_e2e_prove_plonk
```

The generated artifacts should only be used for development and testing purposes.
20 changes: 11 additions & 9 deletions recursion/gnark-ffi/assets/ISP1Verifier.txt
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;

/// @title SP1 Verifier Interface
/// @author Succinct Labs
/// @notice This contract is the interface for the SP1 Verifier.
interface ISP1Verifier {
/// @notice Returns the version of SP1 this verifier corresponds to.
function VERSION() external pure returns (string memory);

/// @notice Returns the hash of the verification key.
function VKEY_HASH() external pure returns (bytes32);

/// @notice Verifies a proof with given public values and vkey.
/// @param vkey The verification key for the RISC-V program.
/// @dev It is expected that the first 4 bytes of proofBytes must match the first 4 bytes of
/// target verifier's VERIFIER_HASH.
/// @param programVKey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32 vkey,
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) external view;
}

interface ISP1VerifierWithHash is ISP1Verifier {
/// @notice Returns the SHA-256 hash of the verifier.
/// @dev This is automatically generated by taking hash of the VKey file.
function VERIFIER_HASH() external pure returns (bytes32);
}
14 changes: 3 additions & 11 deletions recursion/gnark-ffi/assets/SP1MockVerifier.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;

import {ISP1Verifier} from "./ISP1Verifier.sol";

/// @title SP1 Mock Verifier
/// @author Succinct Labs
/// @notice This contracts implements a Mock solidity verifier for SP1.
contract SP1MockVerifier is ISP1Verifier {
function VERSION() external pure returns (string memory) {
return "{SP1_CIRCUIT_VERSION}";
}

function VKEY_HASH() external pure returns (bytes32) {
return bytes32(0);
}

/// @notice Verifies a mock proof with given public values and vkey.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32,
bytes memory,
bytes memory proofBytes
bytes calldata,
bytes calldata proofBytes
) external pure {
assert(proofBytes.length == 0);
}
Expand Down
36 changes: 20 additions & 16 deletions recursion/gnark-ffi/assets/SP1Verifier.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;

import {ISP1Verifier} from "./ISP1Verifier.sol";
import {ISP1Verifier, ISP1VerifierWithHash} from "../ISP1Verifier.sol";
import {PlonkVerifier} from "./PlonkVerifier.sol";

/// @title SP1 Verifier
/// @author Succinct Labs
/// @notice This contracts implements a solidity verifier for SP1.
contract SP1Verifier is PlonkVerifier {
error WrongVersionProof();
contract SP1Verifier is PlonkVerifier, ISP1VerifierWithHash {
/// @notice Thrown when the verifier selector from this proof does not match the one in this
/// verifier. This indicates that this proof was sent to the wrong verifier.
/// @param received The verifier selector from the first 4 bytes of the proof.
/// @param expected The verifier selector from the first 4 bytes of the VERIFIER_HASH().
error WrongVerifierSelector(bytes4 received, bytes4 expected);

function VERSION() external pure returns (string memory) {
return "{SP1_CIRCUIT_VERSION}";
}

function VKEY_HASH() public pure returns (bytes32) {
return {VKEY_HASH};
/// @inheritdoc ISP1VerifierWithHash
function VERIFIER_HASH() public pure returns (bytes32) {
return {VERIFIER_HASH};
}

/// @notice Hashes the public values to a field elements inside Bn254.
Expand All @@ -27,25 +32,24 @@ contract SP1Verifier is PlonkVerifier {
}

/// @notice Verifies a proof with given public values and vkey.
/// @param vkey The verification key for the RISC-V program.
/// @param programVKey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32 vkey,
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) public view {
// To ensure the proof corresponds to this verifier, we check that the first 4 bytes of
// proofBytes match the first 4 bytes of VKEY_HASH.
bytes4 proofBytesPrefix = bytes4(proofBytes[:4]);
if (proofBytesPrefix != bytes4(VKEY_HASH())) {
revert WrongVersionProof();
) external view {
bytes4 recievedSelector = bytes4(proofBytes[:4]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: received

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

bytes4 expectedSelector = bytes4(VERIFIER_HASH());
if (recievedSelector != expectedSelector) {
revert WrongVerifierSelector(recievedSelector, expectedSelector);
}

bytes32 publicValuesDigest = hashPublicValues(publicValues);
uint256[] memory inputs = new uint256[](2);
inputs[0] = uint256(vkey);
inputs[0] = uint256(programVKey);
inputs[1] = uint256(publicValuesDigest);
this.Verify(proofBytes[4:], inputs);
}
}
}
2 changes: 1 addition & 1 deletion recursion/gnark-ffi/src/plonk_bn254.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl PlonkBn254Prover {
let sp1_verifier_str = include_str!("../assets/SP1Verifier.txt")
.replace("{SP1_CIRCUIT_VERSION}", SP1_CIRCUIT_VERSION)
.replace(
"{VKEY_HASH}",
"{VERIFIER_HASH}",
format!("0x{}", hex::encode(vkey_hash)).as_str(),
);
let mut sp1_verifier_file = File::create(sp1_verifier_path).unwrap();
Expand Down
Loading