@crisp-e3/contracts 0.4.1 → 0.5.2

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.
@@ -11,100 +11,77 @@ import { IE3Program } from "@enclave-e3/contracts/contracts/interfaces/IE3Progra
11
11
  import { IEnclave } from "@enclave-e3/contracts/contracts/interfaces/IEnclave.sol";
12
12
  import { E3 } from "@enclave-e3/contracts/contracts/interfaces/IE3.sol";
13
13
  import { LazyIMTData, InternalLazyIMT, PoseidonT3 } from "@zk-kit/lazy-imt.sol/InternalLazyIMT.sol";
14
-
15
14
  import { HonkVerifier } from "./CRISPVerifier.sol";
16
15
 
17
16
  contract CRISPProgram is IE3Program, Ownable {
18
17
  using InternalLazyIMT for LazyIMTData;
19
- /// @notice a structure that holds the round data
18
+
19
+ /// @notice Struct to store all data related to a voting round
20
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;
21
+ uint256 merkleRoot;
22
+ bytes32 paramsHash;
23
+ mapping(address slot => uint40 index) voteSlots;
24
+ LazyIMTData votes;
27
25
  }
28
26
 
29
27
  // Constants
28
+ /// @notice Encryption scheme ID used for the CRISP program.
30
29
  bytes32 public constant ENCRYPTION_SCHEME_ID = keccak256("fhe.rs:BFV");
31
-
32
- // The depth of the input merkle tree
30
+ /// @notice The depth of the input Merkle tree.
33
31
  uint8 public constant TREE_DEPTH = 20;
32
+ /// @notice Half of the largest minimum degree used to fit votes inside the plaintext polynomial.
33
+ uint256 public constant HALF_LARGEST_MINIMUM_DEGREE = 28; // static, hardcoded in the circuit.
34
34
 
35
35
  // State variables
36
36
  IEnclave public enclave;
37
- IRiscZeroVerifier public verifier;
38
- HonkVerifier private immutable HONK_VERIFIER;
37
+ IRiscZeroVerifier public risc0Verifier;
39
38
  bytes32 public imageId;
40
-
41
- /// @notice the round data
42
- mapping(uint256 e3Id => RoundData roundData) public roundsData;
43
- /// @notice whether the round data has been set
44
- mapping(uint256 e3Id => bool isDataSet) public isRoundsDataSet;
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;
39
+ HonkVerifier private immutable honkVerifier;
49
40
 
50
41
  // Mappings
51
42
  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;
43
+ mapping(uint256 e3Id => RoundData) e3Data;
57
44
 
58
45
  // Errors
59
46
  error CallerNotAuthorized();
60
47
  error E3AlreadyInitialized();
61
48
  error E3DoesNotExist();
62
49
  error EnclaveAddressZero();
63
- error VerifierAddressZero();
64
-
65
- /// @notice The error emitted when the honk verifier address is invalid.
50
+ error Risc0VerifierAddressZero();
66
51
  error InvalidHonkVerifier();
67
- /// @notice The error emitted when the input data is empty.
68
52
  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
53
  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();
54
+ error InvalidMerkleRoot();
55
+ error MerkleRootAlreadySet();
77
56
 
78
- /// @notice The event emitted when an input is published.
57
+ // Events
79
58
  event InputPublished(uint256 indexed e3Id, bytes vote, uint256 index);
80
59
 
81
60
  /// @notice Initialize the contract, binding it to a specified RISC Zero verifier.
82
61
  /// @param _enclave The enclave address
83
- /// @param _verifier The RISC Zero verifier address
62
+ /// @param _risc0Verifier The RISC Zero verifier address
84
63
  /// @param _honkVerifier The honk verifier address
85
64
  /// @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());
65
+ constructor(IEnclave _enclave, IRiscZeroVerifier _risc0Verifier, HonkVerifier _honkVerifier, bytes32 _imageId) Ownable(msg.sender) {
66
+ if (address(_enclave) == address(0)) revert EnclaveAddressZero();
67
+ if (address(_risc0Verifier) == address(0)) revert Risc0VerifierAddressZero();
68
+ if (address(_honkVerifier) == address(0)) revert InvalidHonkVerifier();
90
69
 
91
70
  enclave = _enclave;
92
- verifier = _verifier;
93
- HONK_VERIFIER = _honkVerifier;
71
+ risc0Verifier = _risc0Verifier;
72
+ honkVerifier = _honkVerifier;
94
73
  authorizedContracts[address(_enclave)] = true;
95
74
  imageId = _imageId;
96
75
  }
97
76
 
98
- /// @notice Sets the Round data. Can only be set once.
77
+ /// @notice Sets the Merkle root for an E3 program. Can only be set once.
78
+ /// @param _e3Id The E3 program ID
99
79
  /// @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 _e3Id, uint256 _root, address _token, uint256 _balanceThreshold) external onlyOwner {
103
- if (isRoundsDataSet[_e3Id]) revert RoundDataAlreadySet();
104
-
105
- isRoundsDataSet[_e3Id] = true;
80
+ function setMerkleRoot(uint256 _e3Id, uint256 _root) external onlyOwner {
81
+ if (_root == 0) revert InvalidMerkleRoot();
82
+ if (e3Data[_e3Id].merkleRoot != 0) revert MerkleRootAlreadySet();
106
83
 
107
- roundsData[_e3Id] = RoundData({ token: _token, balanceThreshold: _balanceThreshold, censusMerkleRoot: _root });
84
+ e3Data[_e3Id].merkleRoot = _root;
108
85
  }
109
86
 
110
87
  /// @notice Set the Image ID for the guest program
@@ -113,30 +90,29 @@ contract CRISPProgram is IE3Program, Ownable {
113
90
  imageId = _imageId;
114
91
  }
115
92
 
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;
93
+ /// @notice Set the RISC Zero verifier.
94
+ /// @param _risc0Verifier The new RISC Zero verifier address
95
+ function setRisc0Verifier(IRiscZeroVerifier _risc0Verifier) external onlyOwner {
96
+ if (address(_risc0Verifier) == address(0)) revert Risc0VerifierAddressZero();
97
+ risc0Verifier = _risc0Verifier;
121
98
  }
122
99
 
123
100
  /// @notice Get the params hash for an E3 program
124
101
  /// @param e3Id The E3 program ID
125
102
  /// @return The params hash
126
103
  function getParamsHash(uint256 e3Id) public view returns (bytes32) {
127
- return paramsHashes[e3Id];
104
+ return e3Data[e3Id].paramsHash;
128
105
  }
129
106
 
130
- /// @notice Validate the E3 program parameters
131
- /// @param e3Id The E3 program ID
132
- /// @param e3ProgramParams The E3 program parameters
107
+ /// @inheritdoc IE3Program
133
108
  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);
109
+ if (!authorizedContracts[msg.sender] && msg.sender != owner()) revert CallerNotAuthorized();
110
+ if (e3Data[e3Id].paramsHash != bytes32(0)) revert E3AlreadyInitialized();
111
+
112
+ e3Data[e3Id].paramsHash = keccak256(e3ProgramParams);
137
113
 
138
- // we need to init the inputs merkle tree for this e3Id
139
- votes[e3Id]._init(TREE_DEPTH);
114
+ // Initialize the votes Merkle tree for this E3 ID.
115
+ e3Data[e3Id].votes._init(TREE_DEPTH);
140
116
 
141
117
  return ENCRYPTION_SCHEME_ID;
142
118
  }
@@ -144,9 +120,11 @@ contract CRISPProgram is IE3Program, Ownable {
144
120
  /// @inheritdoc IE3Program
145
121
  function validateInput(uint256 e3Id, address, bytes memory data) external {
146
122
  // it should only be called via Enclave for now
147
- require(authorizedContracts[msg.sender] || msg.sender == owner(), CallerNotAuthorized());
123
+ if (!authorizedContracts[msg.sender] && msg.sender != owner()) revert CallerNotAuthorized();
124
+
148
125
  // We need to ensure that the CRISP admin set the merkle root of the census.
149
- if (!isRoundsDataSet[e3Id]) revert RoundDataNotSet();
126
+ // TODO: Uncomment this when we make the merkle root a public input of the circuit.
127
+ // if (e3Data[e3Id].merkleRoot == 0) revert MerkleRootNotSet();
150
128
 
151
129
  if (data.length == 0) revert EmptyInputData();
152
130
 
@@ -156,47 +134,23 @@ contract CRISPProgram is IE3Program, Ownable {
156
134
 
157
135
  (uint40 voteIndex, bool isFirstVote) = _processVote(e3Id, slotAddress, voteBytes);
158
136
 
137
+ // Set public inputs for the proof. Order must match Noir circuit.
159
138
  bytes32[] memory noirPublicInputs = new bytes32[](2 + vote.length);
160
139
 
161
- // Set public inputs for the proof. Order must match Noir circuit.
162
140
  noirPublicInputs[0] = bytes32(uint256(uint160(slotAddress)));
163
- // Pass isFirstVote flag to verifier (1 = first vote, 0 = re-vote)
164
141
  noirPublicInputs[1] = bytes32(uint256(isFirstVote ? 1 : 0));
165
-
166
- // Set the encrypted vote to the noir public inputs.
167
142
  for (uint256 i = 0; i < vote.length; i++) {
168
143
  noirPublicInputs[i + 2] = vote[i];
169
144
  }
170
145
 
171
146
  // Check if the ciphertext was encrypted correctly
172
- if (!HONK_VERIFIER.verify(noirProof, noirPublicInputs)) {
147
+ if (!honkVerifier.verify(noirProof, noirPublicInputs)) {
173
148
  revert InvalidNoirProof();
174
149
  }
175
150
 
176
151
  emit InputPublished(e3Id, voteBytes, voteIndex);
177
152
  }
178
153
 
179
- /// @notice Process a vote: insert or update in the merkle tree depending
180
- /// on whether it's the first vote or an override.
181
- function _processVote(uint256 e3Id, address slotAddress, bytes memory vote) internal returns (uint40 voteIndex, bool isFirstVote) {
182
- uint40 storedIndexPlusOne = voteSlots[e3Id][slotAddress];
183
-
184
- // we treat the index 0 as not voted yet
185
- // any valid index will be index + 1
186
- if (storedIndexPlusOne == 0) {
187
- // FIRST VOTE
188
- isFirstVote = true;
189
- voteIndex = votes[e3Id].numberOfLeaves;
190
- voteSlots[e3Id][slotAddress] = voteIndex + 1;
191
- votes[e3Id]._insert(PoseidonT3.hash([uint256(keccak256(vote)), voteIndex]));
192
- } else {
193
- // RE-VOTE
194
- isFirstVote = false;
195
- voteIndex = storedIndexPlusOne - 1;
196
- votes[e3Id]._update(PoseidonT3.hash([uint256(keccak256(vote)), voteIndex]), voteIndex);
197
- }
198
- }
199
-
200
154
  /// @notice Decode the tally from the plaintext output
201
155
  /// @param e3Id The E3 program ID
202
156
  /// @return yes The number of yes votes
@@ -235,30 +189,49 @@ contract CRISPProgram is IE3Program, Ownable {
235
189
  return (yes, no);
236
190
  }
237
191
 
238
- /// @notice Verify the proof
239
- /// @param e3Id The E3 program ID
240
- /// @param ciphertextOutputHash The hash of the ciphertext output
241
- /// @param proof The proof to verify
192
+ /// @inheritdoc IE3Program
242
193
  function verify(uint256 e3Id, bytes32 ciphertextOutputHash, bytes memory proof) external view override returns (bool) {
243
- require(paramsHashes[e3Id] != bytes32(0), E3DoesNotExist());
244
- bytes32 inputRoot = bytes32(votes[e3Id]._root(TREE_DEPTH));
194
+ if (e3Data[e3Id].paramsHash == bytes32(0)) revert E3DoesNotExist();
195
+ bytes32 inputRoot = bytes32(e3Data[e3Id].votes._root(TREE_DEPTH));
245
196
  bytes memory journal = new bytes(396); // (32 + 1) * 4 * 3
246
197
 
247
- encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
248
- encodeLengthPrefixAndHash(journal, 132, paramsHashes[e3Id]);
249
- encodeLengthPrefixAndHash(journal, 264, inputRoot);
198
+ _encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
199
+ _encodeLengthPrefixAndHash(journal, 132, e3Data[e3Id].paramsHash);
200
+ _encodeLengthPrefixAndHash(journal, 264, inputRoot);
250
201
 
251
- verifier.verify(proof, imageId, sha256(journal));
202
+ risc0Verifier.verify(proof, imageId, sha256(journal));
252
203
  return true;
253
204
  }
254
205
 
206
+ /// @notice Process a vote: insert or update in the merkle tree depending
207
+ /// on whether it's the first vote or an override.
208
+ function _processVote(uint256 e3Id, address slotAddress, bytes memory vote) internal returns (uint40 voteIndex, bool isFirstVote) {
209
+ uint40 storedIndexPlusOne = e3Data[e3Id].voteSlots[slotAddress];
210
+
211
+ // we treat the index 0 as not voted yet
212
+ // any valid index will be index + 1
213
+ if (storedIndexPlusOne == 0) {
214
+ // FIRST VOTE
215
+ isFirstVote = true;
216
+ voteIndex = e3Data[e3Id].votes.numberOfLeaves;
217
+ e3Data[e3Id].voteSlots[slotAddress] = voteIndex + 1;
218
+ e3Data[e3Id].votes._insert(PoseidonT3.hash([uint256(keccak256(vote)), voteIndex]));
219
+ } else {
220
+ // RE-VOTE
221
+ isFirstVote = false;
222
+ voteIndex = storedIndexPlusOne - 1;
223
+ e3Data[e3Id].votes._update(PoseidonT3.hash([uint256(keccak256(vote)), voteIndex]), voteIndex);
224
+ }
225
+ }
226
+
255
227
  /// @notice Encode length prefix and hash
256
228
  /// @param journal The journal to encode into
257
229
  /// @param startIndex The start index in the journal
258
230
  /// @param hashVal The hash value to encode
259
- function encodeLengthPrefixAndHash(bytes memory journal, uint256 startIndex, bytes32 hashVal) internal pure {
231
+ function _encodeLengthPrefixAndHash(bytes memory journal, uint256 startIndex, bytes32 hashVal) internal pure {
260
232
  journal[startIndex] = 0x20;
261
233
  startIndex += 4;
234
+
262
235
  for (uint256 i = 0; i < 32; i++) {
263
236
  journal[startIndex + i * 4] = hashVal[i];
264
237
  }
@@ -8,7 +8,7 @@ pragma solidity >=0.8.21;
8
8
  uint256 constant N = 262144;
9
9
  uint256 constant LOG_N = 18;
10
10
  uint256 constant NUMBER_OF_PUBLIC_INPUTS = 2066;
11
- uint256 constant VK_HASH = 0x063c39e8bdbf5641b7e7da911a54f2e808b084120de6fec9cd1223ce6ef0da85;
11
+ uint256 constant VK_HASH = 0x20e20e306d8e4ab1bb38da9301dcff68986340a39ec3e45f3d680911b91aac9e;
12
12
  library HonkVerificationKey {
13
13
  function loadVerificationKey() internal pure returns (Honk.VerificationKey memory) {
14
14
  Honk.VerificationKey memory vk = Honk.VerificationKey({
@@ -16,116 +16,116 @@ library HonkVerificationKey {
16
16
  logCircuitSize: uint256(18),
17
17
  publicInputsSize: uint256(2066),
18
18
  ql: Honk.G1Point({
19
- x: uint256(0x246e058a5ff7d94b72e2d8f1d273265644d4795cfb2a427bab835b180a5509fb),
20
- y: uint256(0x221fc7eff2803fca12db033baa14646582610126778228307901d0db50748c5e)
19
+ x: uint256(0x1dddb42c5a7db46ec92d83144cb1fe8f8c1c862826388c25290ac8635ebc7dd7),
20
+ y: uint256(0x09549d2a558917eb071c0aaf4285d4827c566df2f1cd2d1e1b6ad5dab4afa499)
21
21
  }),
22
22
  qr: Honk.G1Point({
23
- x: uint256(0x08011cd886ba5fc7884098df37d58eecfa75f404fead14dbbe1708d9fba3a702),
24
- y: uint256(0x0a76efd12734f504e19bf349e90d20fc19400dd97c238195c67b118c14a35dc9)
23
+ x: uint256(0x0ed974b450a51723240b5e11b13e961951b95bf4c686c5be6c61b0e294b990ac),
24
+ y: uint256(0x10183cfef2d2ae78b9a83e62ed895bcfd086c7824be57c748914cf79b47627b1)
25
25
  }),
26
26
  qo: Honk.G1Point({
27
- x: uint256(0x03250bf94bfb59a296a7227fe1f7a6873ca063c4c581a7b725d6ab685c53440c),
28
- y: uint256(0x0663423bdefc067e3f531e2760b73340a76365a28d7753db11af572e71a7f395)
27
+ x: uint256(0x0376597dbc0a258dbe8691c07264e83d208bbab2795c9efba9f12f0cac99e773),
28
+ y: uint256(0x2704bee070b407f6d0956b82b1953f3697f7b0ebdd4cba286ba8a8aa876460be)
29
29
  }),
30
30
  q4: Honk.G1Point({
31
- x: uint256(0x1db2b3316d4bba2799898fc14a80ee1a12fd9af0b6cf261b86a0e725737dd091),
32
- y: uint256(0x0988609ce0b437fd1d5586ad60e05cc16f891e702fda15bfbbd79cf6ea306582)
31
+ x: uint256(0x26543108597623c48ef32ca11c95af0d3ea922ea43b41944ebc10940a76c67c3),
32
+ y: uint256(0x09c28d40fa91aeff40db0ce3e6b8b86f162ed9d65c3aaa1b74d25cf53999f847)
33
33
  }),
34
34
  qm: Honk.G1Point({
35
- x: uint256(0x1a1ddaefb0b108d39e35111ea8cab1f89d6fb1e6ca3d4d28ae1b6ad3acdb4c4f),
36
- y: uint256(0x110c07c930f3fd905e11cf35db1b4e8d267f016d94ffc9f0b3833dd27641fd4d)
35
+ x: uint256(0x0b63b4e86f99c000048a814e8579bbfb107bf06c473d3edc163e754ca41678d0),
36
+ y: uint256(0x1f3c3a429312accbf47a69871e0afe1c4902ec16747ee48914767cc3bb280d92)
37
37
  }),
38
38
  qc: Honk.G1Point({
39
- x: uint256(0x24a1d61ec991a93d44d50980c43acae82f4013d66126daf568a854cbc88c5a88),
40
- y: uint256(0x20327007b5abc8edee984da35d5c8b8f8565165d4708436627ba34a90ded8431)
39
+ x: uint256(0x27213ed6f59cc8fbc7380d04b4791900114e5f85f7c728fc98ae03fb4b7093da),
40
+ y: uint256(0x021c6a47a6d42a4f6c7a01c7b043c2bfc15b3a3ff270126510d60f1b86e01dac)
41
41
  }),
42
42
  qLookup: Honk.G1Point({
43
- x: uint256(0x205057a47479c3744023a35ca3d08d79c3499d9af48e264ecb31823713bbbca8),
44
- y: uint256(0x19b2541dcaae69df644bec1bb8ce13455719c73fcadac3763d81a6b1c70560f3)
43
+ x: uint256(0x056cab9e0cc90d6187f1504470e987376fb9d964f5e69f79d3dc50a3aba8b070),
44
+ y: uint256(0x2a0690805846bbbba0fe533d4ec11edc41678b77983bcba8f10a71ece5298fee)
45
45
  }),
46
46
  qArith: Honk.G1Point({
47
- x: uint256(0x06294dfc20c077df81e702e90386bca302a58eb9a8e42d116e88b4bcc7605f67),
48
- y: uint256(0x06a35b9ed28d64b5c79f08a6ed5698a501e9a45e71cd515d3a89455ea711489d)
47
+ x: uint256(0x02a42dcfc958fc3919cf2198202d7a6822c5ac22b5a20246ab7d35c549aeb1e0),
48
+ y: uint256(0x26cc1db7e7db68551027e647657bbbb1a4e5c6cc8d2778fb8d8833eaf0ec6c22)
49
49
  }),
50
50
  qDeltaRange: Honk.G1Point({
51
- x: uint256(0x21cb2b3ddedbbb4529c61e4586aa3913a975644620c112f1c892f1b0930633d8),
52
- y: uint256(0x2b2d51a0dc545770c23b83eceb33b488e8217ed4af94bbecd3233520f1a09e36)
51
+ x: uint256(0x1faf3b8674c17daf3e0f63e7c9356cacb82450ef207412dcf21922e73850f746),
52
+ y: uint256(0x07b754988b169db012bc034513ac4ee42746fe1bf233dc531af97d5bc2d0db0c)
53
53
  }),
54
54
  qElliptic: Honk.G1Point({
55
- x: uint256(0x0b33cc711a57329b7a8532a68b487c08afc55c25843549a9f80a1a954c946c62),
56
- y: uint256(0x068fcecd26322a8e6c9699ef0aaa0f2a0d309c5cb2bca51f14287d121c4694fa)
55
+ x: uint256(0x04788fb295f478b04078cab13188d28645677fda3a09ec788387afce8f2b5804),
56
+ y: uint256(0x29194accd4f5315943071685ff9c2b02480c0bff1c21915b16dd54ec561019fb)
57
57
  }),
58
58
  qMemory: Honk.G1Point({
59
- x: uint256(0x12924d915fd97341729ab4a38d431bdb22555119117c62a2ee78941855604328),
60
- y: uint256(0x253f6f540fea2a3f6d3b9f9d24d142b1e942c383fe5b45aea1306992c99fb094)
59
+ x: uint256(0x0bf2ec6066d37230ae861b2473bf67d5ea2fff4fe3b1ff276c71c0edab34c040),
60
+ y: uint256(0x230bbf4647507639fa136c8112d8682c34686c7d62fee9899fd8a456dc19c71b)
61
61
  }),
62
62
  qNnf: Honk.G1Point({
63
- x: uint256(0x23916c7db7b5ec14f6c4a90328e16fad9e03f0d36a7c22750a82bdb74925d008),
64
- y: uint256(0x0d2d9f771a28305ef54fd924dbab1b7fd294e39f2e64b1884fec624a151a78cc)
63
+ x: uint256(0x27b6e6deb48b8f1510b8bc376c0d0998166587549833fc86d6cc9eef0ed465c5),
64
+ y: uint256(0x072563066ec76ae4137df6971684a2904be8f2fef972f1f8ac9407bf41c044f6)
65
65
  }),
66
66
  qPoseidon2External: Honk.G1Point({
67
- x: uint256(0x277f77603f20d5d41e1149588bc2942ba2938ac9d9444622709ed99f1e207a4e),
68
- y: uint256(0x214d57941547e7acf0f4db0ba32b3a80ccf3bc7195673d43a58e36b735df60e6)
67
+ x: uint256(0x0412d71f1f86606512556c6d48194094f6f15e108cffeb5171ac5deafcf21ba5),
68
+ y: uint256(0x0d92c1ab9f5838fa6bda23289f6baab5abb7fafd2505b05412be72c98eed5784)
69
69
  }),
70
70
  qPoseidon2Internal: Honk.G1Point({
71
- x: uint256(0x09a0b5fbddaa5b0077595c630a0403f3de09ae31815145dae74c04ac7013cdd2),
72
- y: uint256(0x1c8f6eebb7992fbbd7b84ef11585538f876749bedd8e61b70fd86a22f3f0c47d)
71
+ x: uint256(0x03a37c8f14f04a0c47b55fc372681424912f79c3ae565a2afce81587846e00c5),
72
+ y: uint256(0x0dfb3edabbbd97745fea4c569bc99dd450bad935f50610a36639cae94acddf56)
73
73
  }),
74
74
  s1: Honk.G1Point({
75
- x: uint256(0x20dbaae9012430dfd99a8e15c41e5ce5e33abe943d4e6bcae2568196a6e2bf16),
76
- y: uint256(0x164b7d011804aa4dbb49c6d96cb28e3b16af2c3532c2407f0ce12c52be461956)
75
+ x: uint256(0x21d3dd322812fd84e7ba8137cdc4e181de182d713192f4a039cd825e5a7fc33d),
76
+ y: uint256(0x11d9b3e7533a65c73578f09d5950d207d615dbcbcba9ca10198af6994ea38d29)
77
77
  }),
78
78
  s2: Honk.G1Point({
79
- x: uint256(0x1da71ffed08aa0fa2b5a58ab3fc27ffb7e2cf131547842780a24963048e2d5aa),
80
- y: uint256(0x00ec65400c01cf5af83108cda478482094334d2ba594e370e47bb0bef1e30491)
79
+ x: uint256(0x245a672b0d35bdf197675411082a8ee1024aab2f95727039206a8403380fdaf8),
80
+ y: uint256(0x12abd51740375d49166bde266697381ca5260279174752c9e03ed6c466b86744)
81
81
  }),
82
82
  s3: Honk.G1Point({
83
- x: uint256(0x076d8ef37e5f6a8a967907da958538f3302bc2360c31614a5e1832a1f3ceb421),
84
- y: uint256(0x1c83f5d0dda79e6f11f5969084b6dcddd1432a426dca69f83a1b1cee21c9e208)
83
+ x: uint256(0x05d92f2af6e654e025fe6c6bab1a4e12b19f7a2cc149c33402472a0cc40cbaf7),
84
+ y: uint256(0x061370e345b4062d14f056256bbfa2c10eba7e585fb46ccead341f6811bd757a)
85
85
  }),
86
86
  s4: Honk.G1Point({
87
- x: uint256(0x0fbf35404645e90f9aa8a9a37507098b6a6332c214ba9b9d65f6e05a5ef82b26),
88
- y: uint256(0x0a6e47c369491fe21fc51c7b254dbaad6a9b6fba5f5fc1e1d66b11a05aca1eb5)
87
+ x: uint256(0x0860e07d6e2e6d913e58cd1eefa86a1d9a4cf4e29133f46c3d400b32dc9594af),
88
+ y: uint256(0x14c0357e31df310162c613184178967efe7a79c13f114a07ece96e80d301f082)
89
89
  }),
90
90
  t1: Honk.G1Point({
91
- x: uint256(0x08a5ba822823e5f21f5585f7d90f070aaad388561d817362c819850cccf82580),
92
- y: uint256(0x2d296fb3ec6c283d6f822a7e7f9edbe350516a4f9cba53be9dc8ac6240d0559c)
91
+ x: uint256(0x1f16b037f0b4c96ea2a30a118a44e139881c0db8a4d6c9fde7db5c1c1738e61f),
92
+ y: uint256(0x00c7781bda34afc32dedfb0d0f6b16c987c83375da000e180b44ad25685fc2ae)
93
93
  }),
94
94
  t2: Honk.G1Point({
95
- x: uint256(0x201b4ffc4068dd22cc3a99a1ef5bc10e2be7841ed934ad5ea5247f992687c29b),
96
- y: uint256(0x28351d4eacb149a545035052b1b2081b7e8c3ffa751c5bc31483b653f95cb6ca)
95
+ x: uint256(0x29345f914a28707887bee191c3a928191de584827a6d1a78ccce1d7629ca9dc0),
96
+ y: uint256(0x1920cebd0b33ac9713424e3bc03d53d79dc72f6afc24c90e56593094a213444c)
97
97
  }),
98
98
  t3: Honk.G1Point({
99
- x: uint256(0x0d1a271b6b84d9a2d8953885c3b2d13d10aa96a483eeb4c7a41d65c19d69d638),
100
- y: uint256(0x2a40aaa4bc03f75cbc60cc97a07b3e8885d4c99101b026f18219c82ee71443c4)
99
+ x: uint256(0x261c990958bc2ef77a45467d9639ab2c68cf787ff7bce55ce3074dfdaedc8f8f),
100
+ y: uint256(0x23c1c05424a40360a61e46f4deab04988a6f5b71dda351e0da608cff1f332ee0)
101
101
  }),
102
102
  t4: Honk.G1Point({
103
- x: uint256(0x18216d5e69c40817c81feefd02de1aa548f7bf9d9ce4d671e96b22f368709ed5),
104
- y: uint256(0x1e5e5f5acbdcd05a0ebffacea7a5426da9ec26a79cbb95692c9e9a499ff0155a)
103
+ x: uint256(0x2b651d2fd644b2972d72ec439dc69d3339d0b052a296bfc48c6a08396aaca078),
104
+ y: uint256(0x2d7e8c1ecb92e2490049b50efc811df63f1ca97e58d5e82852dbec0c29715d71)
105
105
  }),
106
106
  id1: Honk.G1Point({
107
- x: uint256(0x0fcc20437825949a4e696438aea909760b3db2d273bf5b17f5fbfe3a70f036fb),
108
- y: uint256(0x210603917536ed64abdd44a319ea9341a3f30a789789230a7e9cf6214b4bce7e)
107
+ x: uint256(0x2f22768b7d6cb9255a78d3045f4f91993c2b85d416d7368644777bb6a6aed6a4),
108
+ y: uint256(0x23a7f91dfce36352c68d9814946618220ada657d112dccac1d30f4b0e800cb78)
109
109
  }),
110
110
  id2: Honk.G1Point({
111
- x: uint256(0x1ecfabef38de6cd4881366a1b917f8ff9f99a024f4b5087c0c0f566d2c3ea36b),
112
- y: uint256(0x1dd57e4a465cfe220a85cafe9c22d18ea7500a73930a72a173882333424658fd)
111
+ x: uint256(0x0676ce7aa26b2303b49ab8eb603cd4d4d6cdde30dd8767a32e5a57cbad40d485),
112
+ y: uint256(0x0030ffd41d9c36d601af702ae6705909e05bdeae7a3668eda8a24b7f270e67a3)
113
113
  }),
114
114
  id3: Honk.G1Point({
115
- x: uint256(0x0c44be4c332daa7e6c1989b113af431bbbd1177a9e3b296182a038c4898c58b8),
116
- y: uint256(0x07c0db6714c5c1aae2d785402c39e9d18b09e367033cfcb2aecbb6991da057b1)
115
+ x: uint256(0x0d4104567de0b2e61083dfc969d70b67294cadb2bcae5e2fd5974fbfe3cdd025),
116
+ y: uint256(0x19beae607e8892176b30d2494fce1a7a9fcf9b367469413261bcb1db9d0c504b)
117
117
  }),
118
118
  id4: Honk.G1Point({
119
- x: uint256(0x24443689861aa88435f69edc9782024e9207b659aab7df6694958fb85f6d4d5a),
120
- y: uint256(0x19fa9eb82c3e56a745b7900324b2a205ab358a1dac89439011fc099408d21c96)
119
+ x: uint256(0x0927f450005fd2df96b3439570e88147f600d043616c8fb9a145ab6d25bc1b72),
120
+ y: uint256(0x12de11d953c0ba1f015f7363a5bfae565d2b5edefb6854eb60f71fe3e9950c7e)
121
121
  }),
122
122
  lagrangeFirst: Honk.G1Point({
123
123
  x: uint256(0x0000000000000000000000000000000000000000000000000000000000000001),
124
124
  y: uint256(0x0000000000000000000000000000000000000000000000000000000000000002)
125
125
  }),
126
126
  lagrangeLast: Honk.G1Point({
127
- x: uint256(0x11dc324ee0b909fa5ec049c5832d17f8ba51b9f2990874ea77384948fcaa6800),
128
- y: uint256(0x243280a04dd209d1241eb79b76c07ccc3bf501dd5b79fa218d4669cfb30316f8)
127
+ x: uint256(0x29b5224a64e89595783b8ed5c7d047e4d8a337cbf836ee2be2f20d7d47ce95b2),
128
+ y: uint256(0x13396543d3d0ccf426d918fd2199d9507eafa2a505f0cce2d958231b29af1e66)
129
129
  })
130
130
  });
131
131
  return vk;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crisp-e3/contracts",
3
- "version": "0.4.1",
3
+ "version": "0.5.2",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "contracts",
@@ -31,7 +31,7 @@
31
31
  "@zk-kit/lazy-imt.sol": "2.0.0-beta.12",
32
32
  "poseidon-solidity": "^0.0.5",
33
33
  "solady": "^0.1.13",
34
- "@enclave-e3/contracts": "0.1.5"
34
+ "@enclave-e3/contracts": "0.1.7"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@nomicfoundation/hardhat-ethers": "4",
@@ -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/zk-inputs": "^0.4.1",
63
- "@crisp-e3/sdk": "^0.4.1"
62
+ "@crisp-e3/sdk": "^0.5.2",
63
+ "@crisp-e3/zk-inputs": "^0.5.2"
64
64
  },
65
65
  "scripts": {
66
66
  "compile": "hardhat compile",
@@ -69,9 +69,9 @@
69
69
  "ciphernode:add:self": "hardhat ciphernode:add",
70
70
  "clean:deployments": "hardhat utils:clean-deployments",
71
71
  "deploy:contracts": "hardhat run deploy/deploy.ts",
72
- "deploy:contracts:full": "export DEPLOY_ENCLAVE=true && pnpm deploy:contracts",
73
- "deploy:contracts:full:mock": "export DEPLOY_ENCLAVE=true && export USE_MOCK_VERIFIER=true && export USE_MOCK_INPUT_VALIDATOR=true && pnpm deploy:contracts",
72
+ "deploy:contracts:full": "export DEPLOY_ENCLAVE=true USE_MOCK_VERIFIER=true && pnpm deploy:contracts",
74
73
  "test": "hardhat test mocha",
75
- "verify": "hardhat run deploy/verify.ts"
74
+ "verify": "hardhat run deploy/verify.ts",
75
+ "updateSubmissionWindow": "hardhat ciphernode:window"
76
76
  }
77
77
  }
@@ -1,214 +0,0 @@
1
- // SPDX-License-Identifier: LGPL-3.0-only
2
- //
3
- // This file is provided WITHOUT ANY WARRANTY;
4
- // without even the implied warranty of MERCHANTABILITY
5
- // or FITNESS FOR A PARTICULAR PURPOSE.
6
- pragma solidity >=0.8.27;
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";
14
-
15
- import { HonkVerifier } from "../CRISPVerifier.sol";
16
-
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;
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
172
- }
173
-
174
- // Reset weight for no votes
175
- weight = 2 ** (HALF_LARGEST_MINIMUM_DEGREE - 1);
176
-
177
- // Convert no votes
178
- for (uint256 i = START_INDEX_N; i < tally.length; i++) {
179
- no += tally[i] * weight;
180
- weight /= 2;
181
- }
182
-
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];
212
- }
213
- }
214
- }