-
Notifications
You must be signed in to change notification settings - Fork 61
/
EvenNumber.sol
58 lines (51 loc) · 2.52 KB
/
EvenNumber.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2024 RISC Zero, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;
import {IRiscZeroVerifier} from "risc0/IRiscZeroVerifier.sol";
import {ImageID} from "./ImageID.sol"; // auto-generated contract after running `cargo build`.
/// @title A starter application using RISC Zero.
/// @notice This basic application holds a number, guaranteed to be even.
/// @dev This contract demonstrates one pattern for offloading the computation of an expensive
/// or difficult to implement function to a RISC Zero guest running on the zkVM.
contract EvenNumber {
/// @notice RISC Zero verifier contract address.
IRiscZeroVerifier public immutable verifier;
/// @notice Image ID of the only zkVM binary to accept verification from.
/// The image ID is similar to the address of a smart contract.
/// It uniquely represents the logic of that guest program,
/// ensuring that only proofs generated from a pre-defined guest program
/// (in this case, checking if a number is even) are considered valid.
bytes32 public constant imageId = ImageID.IS_EVEN_ID;
/// @notice A number that is guaranteed, by the RISC Zero zkVM, to be even.
/// It can be set by calling the `set` function.
uint256 public number;
/// @notice Initialize the contract, binding it to a specified RISC Zero verifier.
constructor(IRiscZeroVerifier _verifier) {
verifier = _verifier;
number = 0;
}
/// @notice Set the even number stored on the contract. Requires a RISC Zero proof that the number is even.
function set(uint256 x, bytes calldata seal) public {
// Construct the expected journal data. Verify will fail if journal does not match.
bytes memory journal = abi.encode(x);
verifier.verify(seal, imageId, sha256(journal));
number = x;
}
/// @notice Returns the number stored.
function get() public view returns (uint256) {
return number;
}
}