11// SPDX-License-Identifier: MIT
22pragma solidity ^ 0.8.20 ;
33
4- import {ISP1Verifier, ISP1VerifierWithHash} from " ./ISP1Verifier.sol " ;
5- import {Groth16Verifier} from " ./Groth16Verifier.sol " ;
4+ import { ISP1Verifier, ISP1VerifierWithHash } from ' ./ISP1Verifier.sol ' ;
5+ import { Groth16Verifier } from ' ./Groth16Verifier.sol ' ;
66
77/// @title SP1 Verifier
88/// @author Succinct Labs
99/// @notice This contracts implements a solidity verifier for SP1.
1010contract SP1Verifier is Groth16Verifier , ISP1VerifierWithHash {
11- /// @notice Thrown when the verifier selector from this proof does not match the one in this
12- /// verifier. This indicates that this proof was sent to the wrong verifier.
13- /// @param received The verifier selector from the first 4 bytes of the proof.
14- /// @param expected The verifier selector from the first 4 bytes of the VERIFIER_HASH().
15- error WrongVerifierSelector (bytes4 received , bytes4 expected );
16-
17- /// @notice Thrown when the proof is invalid.
18- error InvalidProof ();
19-
20- function VERSION () external pure returns (string memory ) {
21- return "v4.0.0-rc.3 " ;
22- }
23-
24- /// @inheritdoc ISP1VerifierWithHash
25- function VERIFIER_HASH () public pure returns (bytes32 ) {
26- return 0x11b6a09d63d255ad425ee3a7f6211d5ec63fbde9805b40551c3136275b6f4eb4 ;
11+ /// @notice Thrown when the verifier selector from this proof does not match the one in this
12+ /// verifier. This indicates that this proof was sent to the wrong verifier.
13+ /// @param received The verifier selector from the first 4 bytes of the proof.
14+ /// @param expected The verifier selector from the first 4 bytes of the VERIFIER_HASH().
15+ error WrongVerifierSelector (bytes4 received , bytes4 expected );
16+
17+ /// @notice Thrown when the proof is invalid.
18+ error InvalidProof ();
19+
20+ function VERSION () external pure returns (string memory ) {
21+ return 'v5.0.0 ' ;
22+ }
23+
24+ /// @inheritdoc ISP1VerifierWithHash
25+ function VERIFIER_HASH () public pure returns (bytes32 ) {
26+ return 0xa4594c59bbc142f3b81c3ecb7f50a7c34bc9af7c4c444b5d48b795427e285913 ;
27+ }
28+
29+ /// @notice Hashes the public values to a field elements inside Bn254.
30+ /// @param publicValues The public values.
31+ function hashPublicValues (bytes calldata publicValues ) public pure returns (bytes32 ) {
32+ return sha256 (publicValues) & bytes32 (uint256 ((1 << 253 ) - 1 ));
33+ }
34+
35+ /// @notice Verifies a proof with given public values and vkey.
36+ /// @param programVKey The verification key for the RISC-V program.
37+ /// @param publicValues The public values encoded as bytes.
38+ /// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
39+ function verifyProof (bytes32 programVKey , bytes calldata publicValues , bytes calldata proofBytes ) external view {
40+ bytes4 receivedSelector = bytes4 (proofBytes[:4 ]);
41+ bytes4 expectedSelector = bytes4 (VERIFIER_HASH ());
42+ if (receivedSelector != expectedSelector) {
43+ revert WrongVerifierSelector (receivedSelector, expectedSelector);
2744 }
2845
29- /// @notice Hashes the public values to a field elements inside Bn254.
30- /// @param publicValues The public values.
31- function hashPublicValues (bytes calldata publicValues ) public pure returns (bytes32 ) {
32- return sha256 (publicValues) & bytes32 (uint256 ((1 << 253 ) - 1 ));
33- }
34-
35- /// @notice Verifies a proof with given public values and vkey.
36- /// @param programVKey The verification key for the RISC-V program.
37- /// @param publicValues The public values encoded as bytes.
38- /// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
39- function verifyProof (
40- bytes32 programVKey ,
41- bytes calldata publicValues ,
42- bytes calldata proofBytes
43- ) external view {
44- bytes4 receivedSelector = bytes4 (proofBytes[:4 ]);
45- bytes4 expectedSelector = bytes4 (VERIFIER_HASH ());
46- if (receivedSelector != expectedSelector) {
47- revert WrongVerifierSelector (receivedSelector, expectedSelector);
48- }
49-
50- bytes32 publicValuesDigest = hashPublicValues (publicValues);
51- uint256 [2 ] memory inputs;
52- inputs[0 ] = uint256 (programVKey);
53- inputs[1 ] = uint256 (publicValuesDigest);
54- uint256 [8 ] memory proof = abi.decode (proofBytes[4 :], (uint256 [8 ]));
55- this .Verify (proof, inputs);
56- }
57- }
46+ bytes32 publicValuesDigest = hashPublicValues (publicValues);
47+ uint256 [2 ] memory inputs;
48+ inputs[0 ] = uint256 (programVKey);
49+ inputs[1 ] = uint256 (publicValuesDigest);
50+ uint256 [8 ] memory proof = abi.decode (proofBytes[4 :], (uint256 [8 ]));
51+ this .Verify (proof, inputs);
52+ }
53+ }
0 commit comments