@crisp-e3/contracts 0.3.0-test → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,225 +5,210 @@
5
5
  // or FITNESS FOR A PARTICULAR PURPOSE.
6
6
  pragma solidity >=0.8.27;
7
7
 
8
- import {IRiscZeroVerifier} from "risc0/IRiscZeroVerifier.sol";
9
- import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
10
- import {IE3Program} from "@enclave-e3/contracts/contracts/interfaces/IE3Program.sol";
11
- import {IEnclave} from "@enclave-e3/contracts/contracts/interfaces/IEnclave.sol";
12
- import {E3} from "@enclave-e3/contracts/contracts/interfaces/IE3.sol";
13
- import {LazyIMTData, InternalLazyIMT} from "@zk-kit/lazy-imt.sol/InternalLazyIMT.sol";
8
+ import { IRiscZeroVerifier } from "risc0/IRiscZeroVerifier.sol";
9
+ import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
10
+ import { IE3Program } from "@enclave-e3/contracts/contracts/interfaces/IE3Program.sol";
11
+ import { IEnclave } from "@enclave-e3/contracts/contracts/interfaces/IEnclave.sol";
12
+ import { E3 } from "@enclave-e3/contracts/contracts/interfaces/IE3.sol";
13
+ import { LazyIMTData, InternalLazyIMT } from "@zk-kit/lazy-imt.sol/InternalLazyIMT.sol";
14
14
 
15
- import {HonkVerifier} from "../CRISPVerifier.sol";
15
+ import { HonkVerifier } from "../CRISPVerifier.sol";
16
16
 
17
17
  contract MockCRISPProgram is IE3Program, Ownable {
18
- using InternalLazyIMT for LazyIMTData;
19
- /// @notice a structure that holds the round data
20
- struct RoundData {
21
- /// @notice The governance token address.
22
- address token;
23
- /// @notice The minimum balance required to pass the validation.
24
- uint256 balanceThreshold;
25
- /// @notice The Merkle root of the census.
26
- uint256 censusMerkleRoot;
18
+ using InternalLazyIMT for LazyIMTData;
19
+ /// @notice a structure that holds the round data
20
+ struct RoundData {
21
+ /// @notice The governance token address.
22
+ address token;
23
+ /// @notice The minimum balance required to pass the validation.
24
+ uint256 balanceThreshold;
25
+ /// @notice The Merkle root of the census.
26
+ uint256 censusMerkleRoot;
27
+ }
28
+
29
+ // Constants
30
+ bytes32 public constant ENCRYPTION_SCHEME_ID = keccak256("fhe.rs:BFV");
31
+
32
+ // The depth of the input merkle tree
33
+ uint8 public constant TREE_DEPTH = 20;
34
+
35
+ // State variables
36
+ IEnclave public enclave;
37
+ IRiscZeroVerifier public verifier;
38
+ HonkVerifier private immutable HONK_VERIFIER;
39
+ bytes32 public imageId;
40
+
41
+ /// @notice the round data
42
+ RoundData public roundData;
43
+ /// @notice whether the round data has been set
44
+ bool public isDataSet;
45
+
46
+ /// @notice Half of the largest minimum degree used to fit votes
47
+ /// inside the plaintext polynomial
48
+ uint256 public constant HALF_LARGEST_MINIMUM_DEGREE = 28;
49
+
50
+ // Mappings
51
+ mapping(address => bool) public authorizedContracts;
52
+ mapping(uint256 e3Id => bytes32 paramsHash) public paramsHashes;
53
+ /// @notice Mapping to store votes slot indices. Each eligible voter has their own slot
54
+ /// to store their vote inside the merkle tree.
55
+ mapping(uint256 e3Id => mapping(address slot => uint40 index)) public voteSlots;
56
+ mapping(uint256 e3Id => LazyIMTData) public votes;
57
+
58
+ // Errors
59
+ error CallerNotAuthorized();
60
+ error E3AlreadyInitialized();
61
+ error E3DoesNotExist();
62
+ error EnclaveAddressZero();
63
+ error VerifierAddressZero();
64
+
65
+ /// @notice The error emitted when the honk verifier address is invalid.
66
+ error InvalidHonkVerifier();
67
+ /// @notice The error emitted when the input data is empty.
68
+ error EmptyInputData();
69
+ /// @notice The error emitted when the input data is invalid.
70
+ error InvalidInputData(bytes reason);
71
+ /// @notice The error emitted when the Noir proof is invalid.
72
+ error InvalidNoirProof();
73
+ /// @notice The error emitted when the round data is not set.
74
+ error RoundDataNotSet();
75
+ /// @notice The error emitted when trying to set the round data more than once.
76
+ error RoundDataAlreadySet();
77
+
78
+ /// @notice The event emitted when an input is published.
79
+ event InputPublished(uint256 indexed e3Id, bytes vote, uint256 index);
80
+
81
+ /// @notice Initialize the contract, binding it to a specified RISC Zero verifier.
82
+ /// @param _enclave The enclave address
83
+ /// @param _verifier The RISC Zero verifier address
84
+ /// @param _honkVerifier The honk verifier address
85
+ /// @param _imageId The image ID for the guest program
86
+ constructor(IEnclave _enclave, IRiscZeroVerifier _verifier, HonkVerifier _honkVerifier, bytes32 _imageId) Ownable(msg.sender) {
87
+ require(address(_enclave) != address(0), EnclaveAddressZero());
88
+ require(address(_verifier) != address(0), VerifierAddressZero());
89
+ require(address(_honkVerifier) != address(0), InvalidHonkVerifier());
90
+
91
+ enclave = _enclave;
92
+ verifier = _verifier;
93
+ HONK_VERIFIER = _honkVerifier;
94
+ authorizedContracts[address(_enclave)] = true;
95
+ imageId = _imageId;
96
+ }
97
+
98
+ /// @notice Sets the Round data. Can only be set once.
99
+ /// @param _root The Merkle root to set.
100
+ /// @param _token The governance token address.
101
+ /// @param _balanceThreshold The minimum balance required.
102
+ function setRoundData(uint256 _root, address _token, uint256 _balanceThreshold) external onlyOwner {
103
+ if (isDataSet) revert RoundDataAlreadySet();
104
+
105
+ isDataSet = true;
106
+
107
+ roundData = RoundData({ token: _token, balanceThreshold: _balanceThreshold, censusMerkleRoot: _root });
108
+ }
109
+
110
+ /// @notice Set the Image ID for the guest program
111
+ /// @param _imageId The new image ID.
112
+ function setImageId(bytes32 _imageId) external onlyOwner {
113
+ imageId = _imageId;
114
+ }
115
+
116
+ /// @notice Set the RISC Zero verifier address
117
+ /// @param _verifier The new RISC Zero verifier address
118
+ function setVerifier(IRiscZeroVerifier _verifier) external onlyOwner {
119
+ if (address(_verifier) == address(0)) revert VerifierAddressZero();
120
+ verifier = _verifier;
121
+ }
122
+
123
+ /// @notice Get the params hash for an E3 program
124
+ /// @param e3Id The E3 program ID
125
+ /// @return The params hash
126
+ function getParamsHash(uint256 e3Id) public view returns (bytes32) {
127
+ return paramsHashes[e3Id];
128
+ }
129
+
130
+ /// @notice Validate the E3 program parameters
131
+ /// @param e3Id The E3 program ID
132
+ /// @param e3ProgramParams The E3 program parameters
133
+ function validate(uint256 e3Id, uint256, bytes calldata e3ProgramParams, bytes calldata) external returns (bytes32) {
134
+ require(authorizedContracts[msg.sender] || msg.sender == owner(), CallerNotAuthorized());
135
+ require(paramsHashes[e3Id] == bytes32(0), E3AlreadyInitialized());
136
+ paramsHashes[e3Id] = keccak256(e3ProgramParams);
137
+
138
+ return ENCRYPTION_SCHEME_ID;
139
+ }
140
+
141
+ /// @inheritdoc IE3Program
142
+ function validateInput(uint256 e3Id, address, bytes memory data) external {
143
+ if (data.length == 0) revert EmptyInputData();
144
+
145
+ (, , bytes memory vote, ) = abi.decode(data, (bytes, bytes32[], bytes, address));
146
+ }
147
+
148
+ /// @notice Decode the tally from the plaintext output
149
+ /// @param e3Id The E3 program ID
150
+ /// @return yes The number of yes votes
151
+ /// @return no The number of no votes
152
+ function decodeTally(uint256 e3Id) public view returns (uint256 yes, uint256 no) {
153
+ // fetch from enclave
154
+ E3 memory e3 = enclave.getE3(e3Id);
155
+
156
+ // abi decode it into an array of uint256
157
+ uint256[] memory tally = abi.decode(e3.plaintextOutput, (uint256[]));
158
+
159
+ /// @notice We want to completely ignore anything outside of the coefficients
160
+ /// we agreed to store out votes on.
161
+ uint256 halfD = tally.length / 2;
162
+ uint256 START_INDEX_Y = halfD - HALF_LARGEST_MINIMUM_DEGREE;
163
+ uint256 START_INDEX_N = tally.length - HALF_LARGEST_MINIMUM_DEGREE;
164
+
165
+ // first weight (we are converting back from bits to integer)
166
+ uint256 weight = 2 ** (HALF_LARGEST_MINIMUM_DEGREE - 1);
167
+
168
+ // Convert yes votes
169
+ for (uint256 i = START_INDEX_Y; i < halfD; i++) {
170
+ yes += tally[i] * weight;
171
+ weight /= 2; // Right shift equivalent
27
172
  }
28
173
 
29
- // Constants
30
- bytes32 public constant ENCRYPTION_SCHEME_ID = keccak256("fhe.rs:BFV");
31
-
32
- // The depth of the input merkle tree
33
- uint8 public constant TREE_DEPTH = 20;
34
-
35
- // State variables
36
- IEnclave public enclave;
37
- IRiscZeroVerifier public verifier;
38
- HonkVerifier private immutable HONK_VERIFIER;
39
- bytes32 public imageId;
40
-
41
- /// @notice the round data
42
- RoundData public roundData;
43
- /// @notice whether the round data has been set
44
- bool public isDataSet;
45
-
46
- /// @notice Half of the largest minimum degree used to fit votes
47
- /// inside the plaintext polynomial
48
- uint256 public constant HALF_LARGEST_MINIMUM_DEGREE = 28;
49
-
50
- // Mappings
51
- mapping(address => bool) public authorizedContracts;
52
- mapping(uint256 e3Id => bytes32 paramsHash) public paramsHashes;
53
- /// @notice Mapping to store votes slot indices. Each eligible voter has their own slot
54
- /// to store their vote inside the merkle tree.
55
- mapping(uint256 e3Id => mapping(address slot => uint40 index)) public voteSlots;
56
- mapping(uint256 e3Id => LazyIMTData) public votes;
57
-
58
- // Errors
59
- error CallerNotAuthorized();
60
- error E3AlreadyInitialized();
61
- error E3DoesNotExist();
62
- error EnclaveAddressZero();
63
- error VerifierAddressZero();
64
-
65
- /// @notice The error emitted when the honk verifier address is invalid.
66
- error InvalidHonkVerifier();
67
- /// @notice The error emitted when the input data is empty.
68
- error EmptyInputData();
69
- /// @notice The error emitted when the input data is invalid.
70
- error InvalidInputData(bytes reason);
71
- /// @notice The error emitted when the Noir proof is invalid.
72
- error InvalidNoirProof();
73
- /// @notice The error emitted when the round data is not set.
74
- error RoundDataNotSet();
75
- /// @notice The error emitted when trying to set the round data more than once.
76
- error RoundDataAlreadySet();
77
-
78
- /// @notice The event emitted when an input is published.
79
- event InputPublished(uint256 indexed e3Id, bytes vote, uint256 index);
80
-
81
- /// @notice Initialize the contract, binding it to a specified RISC Zero verifier.
82
- /// @param _enclave The enclave address
83
- /// @param _verifier The RISC Zero verifier address
84
- /// @param _honkVerifier The honk verifier address
85
- /// @param _imageId The image ID for the guest program
86
- constructor(IEnclave _enclave, IRiscZeroVerifier _verifier, HonkVerifier _honkVerifier, bytes32 _imageId)
87
- Ownable(msg.sender)
88
- {
89
- require(address(_enclave) != address(0), EnclaveAddressZero());
90
- require(address(_verifier) != address(0), VerifierAddressZero());
91
- require(address(_honkVerifier) != address(0), InvalidHonkVerifier());
92
-
93
- enclave = _enclave;
94
- verifier = _verifier;
95
- HONK_VERIFIER = _honkVerifier;
96
- authorizedContracts[address(_enclave)] = true;
97
- imageId = _imageId;
98
- }
99
-
100
- /// @notice Sets the Round data. Can only be set once.
101
- /// @param _root The Merkle root to set.
102
- /// @param _token The governance token address.
103
- /// @param _balanceThreshold The minimum balance required.
104
- function setRoundData(uint256 _root, address _token, uint256 _balanceThreshold)
105
- external
106
- onlyOwner
107
- {
108
- if (isDataSet) revert RoundDataAlreadySet();
109
-
110
- isDataSet = true;
111
-
112
- roundData = RoundData({
113
- token: _token,
114
- balanceThreshold: _balanceThreshold,
115
- censusMerkleRoot: _root
116
- });
117
- }
118
-
119
- /// @notice Set the Image ID for the guest program
120
- /// @param _imageId The new image ID.
121
- function setImageId(bytes32 _imageId) external onlyOwner {
122
- imageId = _imageId;
123
- }
124
-
125
- /// @notice Set the RISC Zero verifier address
126
- /// @param _verifier The new RISC Zero verifier address
127
- function setVerifier(IRiscZeroVerifier _verifier) external onlyOwner {
128
- if (address(_verifier) == address(0)) revert VerifierAddressZero();
129
- verifier = _verifier;
130
- }
131
-
132
- /// @notice Get the params hash for an E3 program
133
- /// @param e3Id The E3 program ID
134
- /// @return The params hash
135
- function getParamsHash(uint256 e3Id) public view returns (bytes32) {
136
- return paramsHashes[e3Id];
137
- }
138
-
139
- /// @notice Validate the E3 program parameters
140
- /// @param e3Id The E3 program ID
141
- /// @param e3ProgramParams The E3 program parameters
142
- function validate(uint256 e3Id, uint256, bytes calldata e3ProgramParams, bytes calldata)
143
- external
144
- returns (bytes32)
145
- {
146
- require(authorizedContracts[msg.sender] || msg.sender == owner(), CallerNotAuthorized());
147
- require(paramsHashes[e3Id] == bytes32(0), E3AlreadyInitialized());
148
- paramsHashes[e3Id] = keccak256(e3ProgramParams);
149
-
150
- return ENCRYPTION_SCHEME_ID;
151
- }
152
-
153
- /// @inheritdoc IE3Program
154
- function validateInput(uint256 e3Id, address, bytes memory data) external {
155
- if (data.length == 0) revert EmptyInputData();
156
- }
157
-
158
- /// @notice Decode the tally from the plaintext output
159
- /// @param e3Id The E3 program ID
160
- /// @return yes The number of yes votes
161
- /// @return no The number of no votes
162
- function decodeTally(uint256 e3Id) public view returns (uint256 yes, uint256 no) {
163
- // fetch from enclave
164
- E3 memory e3 = enclave.getE3(e3Id);
165
-
166
- // abi decode it into an array of uint256
167
- uint256[] memory tally = abi.decode(e3.plaintextOutput, (uint256[]));
168
-
169
- /// @notice We want to completely ignore anything outside of the coefficients
170
- /// we agreed to store out votes on.
171
- uint256 halfD = tally.length / 2;
172
- uint256 START_INDEX_Y = halfD - HALF_LARGEST_MINIMUM_DEGREE;
173
- uint256 START_INDEX_N = tally.length - HALF_LARGEST_MINIMUM_DEGREE;
174
-
175
- // first weight (we are converting back from bits to integer)
176
- uint256 weight = 2 ** (HALF_LARGEST_MINIMUM_DEGREE - 1);
177
-
178
- // Convert yes votes
179
- for (uint256 i = START_INDEX_Y; i < halfD; i++) {
180
- yes += tally[i] * weight;
181
- weight /= 2; // Right shift equivalent
182
- }
183
-
184
- // Reset weight for no votes
185
- weight = 2 ** (HALF_LARGEST_MINIMUM_DEGREE - 1);
186
-
187
- // Convert no votes
188
- for (uint256 i = START_INDEX_N; i < tally.length; i++) {
189
- no += tally[i] * weight;
190
- weight /= 2;
191
- }
192
-
193
- return (yes, no);
194
- }
174
+ // Reset weight for no votes
175
+ weight = 2 ** (HALF_LARGEST_MINIMUM_DEGREE - 1);
195
176
 
196
- /// @notice Verify the proof
197
- /// @param e3Id The E3 program ID
198
- /// @param ciphertextOutputHash The hash of the ciphertext output
199
- /// @param proof The proof to verify
200
- function verify(uint256 e3Id, bytes32 ciphertextOutputHash, bytes memory proof)
201
- external
202
- view
203
- override
204
- returns (bool)
205
- {
206
- require(paramsHashes[e3Id] != bytes32(0), E3DoesNotExist());
207
- bytes32 inputRoot = bytes32(votes[e3Id]._root(TREE_DEPTH));
208
- bytes memory journal = new bytes(396); // (32 + 1) * 4 * 3
209
-
210
- encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
211
- encodeLengthPrefixAndHash(journal, 132, paramsHashes[e3Id]);
212
- encodeLengthPrefixAndHash(journal, 264, inputRoot);
213
-
214
- verifier.verify(proof, imageId, sha256(journal));
215
- return true;
177
+ // Convert no votes
178
+ for (uint256 i = START_INDEX_N; i < tally.length; i++) {
179
+ no += tally[i] * weight;
180
+ weight /= 2;
216
181
  }
217
182
 
218
- /// @notice Encode length prefix and hash
219
- /// @param journal The journal to encode into
220
- /// @param startIndex The start index in the journal
221
- /// @param hashVal The hash value to encode
222
- function encodeLengthPrefixAndHash(bytes memory journal, uint256 startIndex, bytes32 hashVal) internal pure {
223
- journal[startIndex] = 0x20;
224
- startIndex += 4;
225
- for (uint256 i = 0; i < 32; i++) {
226
- journal[startIndex + i * 4] = hashVal[i];
227
- }
183
+ return (yes, no);
184
+ }
185
+
186
+ /// @notice Verify the proof
187
+ /// @param e3Id The E3 program ID
188
+ /// @param ciphertextOutputHash The hash of the ciphertext output
189
+ /// @param proof The proof to verify
190
+ function verify(uint256 e3Id, bytes32 ciphertextOutputHash, bytes memory proof) external view override returns (bool) {
191
+ require(paramsHashes[e3Id] != bytes32(0), E3DoesNotExist());
192
+ bytes32 inputRoot = bytes32(votes[e3Id]._root(TREE_DEPTH));
193
+ bytes memory journal = new bytes(396); // (32 + 1) * 4 * 3
194
+
195
+ encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
196
+ encodeLengthPrefixAndHash(journal, 132, paramsHashes[e3Id]);
197
+ encodeLengthPrefixAndHash(journal, 264, inputRoot);
198
+
199
+ verifier.verify(proof, imageId, sha256(journal));
200
+ return true;
201
+ }
202
+
203
+ /// @notice Encode length prefix and hash
204
+ /// @param journal The journal to encode into
205
+ /// @param startIndex The start index in the journal
206
+ /// @param hashVal The hash value to encode
207
+ function encodeLengthPrefixAndHash(bytes memory journal, uint256 startIndex, bytes32 hashVal) internal pure {
208
+ journal[startIndex] = 0x20;
209
+ startIndex += 4;
210
+ for (uint256 i = 0; i < 32; i++) {
211
+ journal[startIndex + i * 4] = hashVal[i];
228
212
  }
213
+ }
229
214
  }
@@ -5,9 +5,9 @@
5
5
  // or FITNESS FOR A PARTICULAR PURPOSE.
6
6
  pragma solidity >=0.8.27;
7
7
 
8
- import { E3 } from '@enclave-e3/contracts/contracts/interfaces/IE3.sol';
9
- import { IE3Program } from '@enclave-e3/contracts/contracts/interfaces/IE3Program.sol';
10
- import { IDecryptionVerifier } from '@enclave-e3/contracts/contracts/interfaces/IDecryptionVerifier.sol';
8
+ import { E3 } from "@enclave-e3/contracts/contracts/interfaces/IE3.sol";
9
+ import { IE3Program } from "@enclave-e3/contracts/contracts/interfaces/IE3Program.sol";
10
+ import { IDecryptionVerifier } from "@enclave-e3/contracts/contracts/interfaces/IDecryptionVerifier.sol";
11
11
 
12
12
  contract MockEnclave {
13
13
  bytes public plaintextOutput;
@@ -27,8 +27,8 @@ contract MockEnclave {
27
27
  expiration: 0,
28
28
  encryptionSchemeId: bytes32(0),
29
29
  e3Program: IE3Program(address(0)),
30
- e3ProgramParams: bytes(''),
31
- customParams: bytes(''),
30
+ e3ProgramParams: bytes(""),
31
+ customParams: bytes(""),
32
32
  decryptionVerifier: IDecryptionVerifier(address(0)),
33
33
  committeePublicKey: bytes32(0),
34
34
  ciphertextOutput: bytes32(0),
@@ -5,10 +5,10 @@
5
5
  // or FITNESS FOR A PARTICULAR PURPOSE.
6
6
  pragma solidity ^0.8.27;
7
7
 
8
- import {IRiscZeroVerifier, Receipt} from "risc0/IRiscZeroVerifier.sol";
8
+ import { IRiscZeroVerifier, Receipt } from "risc0/IRiscZeroVerifier.sol";
9
9
 
10
10
  contract MockRISC0Verifier is IRiscZeroVerifier {
11
- function verify(bytes calldata seal, bytes32 imageId, bytes32 journalDigest) public view override {}
11
+ function verify(bytes calldata seal, bytes32 imageId, bytes32 journalDigest) public view override {}
12
12
 
13
- function verifyIntegrity(Receipt calldata receipt) external view override {}
13
+ function verifyIntegrity(Receipt calldata receipt) external view override {}
14
14
  }
@@ -5,9 +5,9 @@
5
5
  // or FITNESS FOR A PARTICULAR PURPOSE.
6
6
  pragma solidity >=0.8.27;
7
7
 
8
- import {RiscZeroGroth16Verifier as RiscZero} from "risc0/groth16/RiscZeroGroth16Verifier.sol";
9
- import {ControlID} from "risc0/groth16/ControlID.sol";
8
+ import { RiscZeroGroth16Verifier as RiscZero } from "risc0/groth16/RiscZeroGroth16Verifier.sol";
9
+ import { ControlID } from "risc0/groth16/ControlID.sol";
10
10
 
11
11
  contract RiscZeroGroth16Verifier is RiscZero {
12
- constructor() RiscZero(ControlID.CONTROL_ROOT, ControlID.BN254_CONTROL_ID) {}
12
+ constructor() RiscZero(ControlID.CONTROL_ROOT, ControlID.BN254_CONTROL_ID) {}
13
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crisp-e3/contracts",
3
- "version": "0.3.0-test",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "contracts",
@@ -59,8 +59,8 @@
59
59
  "typechain": "^8.3.0",
60
60
  "typescript": "5.8.3",
61
61
  "viem": "2.30.6",
62
- "@crisp-e3/sdk": "^0.3.0-test",
63
- "@crisp-e3/zk-inputs": "^0.3.0-test"
62
+ "@crisp-e3/zk-inputs": "^0.4.0",
63
+ "@crisp-e3/sdk": "^0.4.0"
64
64
  },
65
65
  "scripts": {
66
66
  "compile": "hardhat compile",