@crisp-e3/contracts 0.4.0 → 0.4.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.
- package/contracts/CRISPProgram.sol +79 -106
- package/contracts/CRISPVerifier.sol +55 -55
- package/package.json +3 -4
- package/contracts/Mocks/MockCRISPProgram.sol +0 -214
|
@@ -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
|
-
|
|
18
|
+
|
|
19
|
+
/// @notice Struct to store all data related to a voting round
|
|
20
20
|
struct RoundData {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
|
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 =>
|
|
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
|
|
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
|
-
|
|
74
|
-
error
|
|
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
|
-
|
|
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
|
|
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
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
93
|
-
|
|
71
|
+
risc0Verifier = _risc0Verifier;
|
|
72
|
+
honkVerifier = _honkVerifier;
|
|
94
73
|
authorizedContracts[address(_enclave)] = true;
|
|
95
74
|
imageId = _imageId;
|
|
96
75
|
}
|
|
97
76
|
|
|
98
|
-
/// @notice Sets the
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
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
|
|
117
|
-
/// @param
|
|
118
|
-
function
|
|
119
|
-
if (address(
|
|
120
|
-
|
|
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
|
|
104
|
+
return e3Data[e3Id].paramsHash;
|
|
128
105
|
}
|
|
129
106
|
|
|
130
|
-
/// @
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
//
|
|
139
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (!
|
|
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
|
-
/// @
|
|
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
|
-
|
|
244
|
-
bytes32 inputRoot = bytes32(
|
|
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
|
-
|
|
248
|
-
|
|
249
|
-
|
|
198
|
+
_encodeLengthPrefixAndHash(journal, 0, ciphertextOutputHash);
|
|
199
|
+
_encodeLengthPrefixAndHash(journal, 132, e3Data[e3Id].paramsHash);
|
|
200
|
+
_encodeLengthPrefixAndHash(journal, 264, inputRoot);
|
|
250
201
|
|
|
251
|
-
|
|
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
|
|
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 =
|
|
11
|
+
uint256 constant VK_HASH = 0x1928c21699c7e0ea8193b35638ffffb6e95f2a8d572fa86a4f16f9df74c379fd;
|
|
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(
|
|
20
|
-
y: uint256(
|
|
19
|
+
x: uint256(0x04e9b837eb4859cf6d88b9cffdc3cb147c3924ba2c2a70d822f587ffdc922c7d),
|
|
20
|
+
y: uint256(0x1a985ef8bd818ab1a4465f284ae97817916527b7ba8fb601b02c336fdf677523)
|
|
21
21
|
}),
|
|
22
22
|
qr: Honk.G1Point({
|
|
23
|
-
x: uint256(
|
|
24
|
-
y: uint256(
|
|
23
|
+
x: uint256(0x0025964752de7d9ce88a9105961e89c5d54508867be3d862ccd5d7df3282f71c),
|
|
24
|
+
y: uint256(0x262f9a1cbe392f2d7802bdb647edce2906220ed344b999a15d453c4cbc72fff1)
|
|
25
25
|
}),
|
|
26
26
|
qo: Honk.G1Point({
|
|
27
|
-
x: uint256(
|
|
28
|
-
y: uint256(
|
|
27
|
+
x: uint256(0x199b3105035a711288994d4a46db5df5ce7f77173d9b39e2c6c7dee7a045ab2d),
|
|
28
|
+
y: uint256(0x2384d70aa447ed87273b69abef7e65cf56e9e1f1781cc5d47627971cd8a20a6d)
|
|
29
29
|
}),
|
|
30
30
|
q4: Honk.G1Point({
|
|
31
|
-
x: uint256(
|
|
32
|
-
y: uint256(
|
|
31
|
+
x: uint256(0x2040a147ace2b079e7da66f09c4a8a790c25155e9112f6d359edb1e7a1c14bd9),
|
|
32
|
+
y: uint256(0x01f4c95f4a958654488f52a471557b7a9248d688eba93dd73e7b42b5f65d395b)
|
|
33
33
|
}),
|
|
34
34
|
qm: Honk.G1Point({
|
|
35
|
-
x: uint256(
|
|
36
|
-
y: uint256(
|
|
35
|
+
x: uint256(0x249e1e9e7b76099e6ae659cff1012d9ea22fa00702d2c201588ef753a39027a1),
|
|
36
|
+
y: uint256(0x0fe03c280de4af4b50cb49b818a4ceb6b16d71123933670a8fb97db1f91f1cb9)
|
|
37
37
|
}),
|
|
38
38
|
qc: Honk.G1Point({
|
|
39
|
-
x: uint256(
|
|
40
|
-
y: uint256(
|
|
39
|
+
x: uint256(0x0b87629543c1951caf6237bd2c13fa9e8d0d9dbcf683448e36b83efdb37ba822),
|
|
40
|
+
y: uint256(0x2b160fd59507419036359b4dad926d33bb7212ebfa265020a16d9860b77a4326)
|
|
41
41
|
}),
|
|
42
42
|
qLookup: Honk.G1Point({
|
|
43
|
-
x: uint256(
|
|
44
|
-
y: uint256(
|
|
43
|
+
x: uint256(0x056cab9e0cc90d6187f1504470e987376fb9d964f5e69f79d3dc50a3aba8b070),
|
|
44
|
+
y: uint256(0x2a0690805846bbbba0fe533d4ec11edc41678b77983bcba8f10a71ece5298fee)
|
|
45
45
|
}),
|
|
46
46
|
qArith: Honk.G1Point({
|
|
47
|
-
x: uint256(
|
|
48
|
-
y: uint256(
|
|
47
|
+
x: uint256(0x1be80044ada3428b16f44b8deb186189654facebd948778500a35ff70d539f80),
|
|
48
|
+
y: uint256(0x16c4b4c9d038147adf1ee72f02840837732841340f45216ee0c7b26c44e6c598)
|
|
49
49
|
}),
|
|
50
50
|
qDeltaRange: Honk.G1Point({
|
|
51
|
-
x: uint256(
|
|
52
|
-
y: uint256(
|
|
51
|
+
x: uint256(0x0e4f6b5ad66313663f1f68b7bdbd37fd9ae4fb0a30a7a26bc09b3624340acb94),
|
|
52
|
+
y: uint256(0x0f76c694a09644e9b7e9ab35d64c7fd37d6299e442b7d2c3ff159e04ede31b7d)
|
|
53
53
|
}),
|
|
54
54
|
qElliptic: Honk.G1Point({
|
|
55
|
-
x: uint256(
|
|
56
|
-
y: uint256(
|
|
55
|
+
x: uint256(0x1ba257d8ac16fc3c4b3694a6f31b1c5f5ac6beb4e9eb38297180c454f66ec30e),
|
|
56
|
+
y: uint256(0x2ff820c29d349cff8bac8e6a969c845b9ceb741103c1d07ac891d513a6bd0901)
|
|
57
57
|
}),
|
|
58
58
|
qMemory: Honk.G1Point({
|
|
59
|
-
x: uint256(
|
|
60
|
-
y: uint256(
|
|
59
|
+
x: uint256(0x160f9a682f385226627287244e1b6fffd5ad050d682304707917ff5999819b1b),
|
|
60
|
+
y: uint256(0x1e9891b752101a03dfaf1bbc35822d7bb037c71ba669c063374d84f4bb47b56a)
|
|
61
61
|
}),
|
|
62
62
|
qNnf: Honk.G1Point({
|
|
63
|
-
x: uint256(
|
|
64
|
-
y: uint256(
|
|
63
|
+
x: uint256(0x1f0fa4f6a3a36136a0e0ebd90abd6ac2e2574079c13c04a6371dc01b0ea5a9c7),
|
|
64
|
+
y: uint256(0x0cc5647711047db7fff873f47fa26d7d3d67c07ec5917a306646b5cfcd8e87e1)
|
|
65
65
|
}),
|
|
66
66
|
qPoseidon2External: Honk.G1Point({
|
|
67
|
-
x: uint256(
|
|
68
|
-
y: uint256(
|
|
67
|
+
x: uint256(0x127934fc35e05404db26f9f8c6822686cde6713bbd81c53e3662078f16db8f85),
|
|
68
|
+
y: uint256(0x154ef0a5e35291de0be60d5f445a17f8e76cd641f7d131790b4ab2009c075705)
|
|
69
69
|
}),
|
|
70
70
|
qPoseidon2Internal: Honk.G1Point({
|
|
71
|
-
x: uint256(
|
|
72
|
-
y: uint256(
|
|
71
|
+
x: uint256(0x21c3559c733cc1fb3e84d2b4a5e5147c25143a7a846505773bf9fade3c363004),
|
|
72
|
+
y: uint256(0x1deac820a1292780994b6b49e4ec046f0674a1f5e1e09fa72c4dfe863a8da4b7)
|
|
73
73
|
}),
|
|
74
74
|
s1: Honk.G1Point({
|
|
75
|
-
x: uint256(
|
|
76
|
-
y: uint256(
|
|
75
|
+
x: uint256(0x0485a119a5e9393108425df7916013414c2397f9b633ae60a1dc852ece837a96),
|
|
76
|
+
y: uint256(0x04ec4a89caab0203e7d3ed4876bf3ff1a5fad1ffb11c44ea4fb722a52147d3c8)
|
|
77
77
|
}),
|
|
78
78
|
s2: Honk.G1Point({
|
|
79
|
-
x: uint256(
|
|
80
|
-
y: uint256(
|
|
79
|
+
x: uint256(0x013f7d7e0f3d0416b38be435440a072e8f55964c09a8126276be2fc6f4fd6f9f),
|
|
80
|
+
y: uint256(0x1595883ac9ceeb4fad5fe4ec277c354f87b25b8f4737ef1f4aab5ce1efa7d4ab)
|
|
81
81
|
}),
|
|
82
82
|
s3: Honk.G1Point({
|
|
83
|
-
x: uint256(
|
|
84
|
-
y: uint256(
|
|
83
|
+
x: uint256(0x1edfd7bd7cb7d31d82d7e5711cf8144e54def3b2a868882ea55a9c9bb67ec8ab),
|
|
84
|
+
y: uint256(0x0ff877cf80a59cefdda50fd9846b591c74cfecb8e72f9b2c1c2a9e6ea2e15ef3)
|
|
85
85
|
}),
|
|
86
86
|
s4: Honk.G1Point({
|
|
87
|
-
x: uint256(
|
|
88
|
-
y: uint256(
|
|
87
|
+
x: uint256(0x20487f6814f872cef8047d4e2a8ceebfcdb02173231ef2e3b5fdb81a7a4d02cd),
|
|
88
|
+
y: uint256(0x14c15fece64e528803e437e2b6ba8557cf484c02b43589880178ef3a7035eb84)
|
|
89
89
|
}),
|
|
90
90
|
t1: Honk.G1Point({
|
|
91
|
-
x: uint256(
|
|
92
|
-
y: uint256(
|
|
91
|
+
x: uint256(0x1f16b037f0b4c96ea2a30a118a44e139881c0db8a4d6c9fde7db5c1c1738e61f),
|
|
92
|
+
y: uint256(0x00c7781bda34afc32dedfb0d0f6b16c987c83375da000e180b44ad25685fc2ae)
|
|
93
93
|
}),
|
|
94
94
|
t2: Honk.G1Point({
|
|
95
|
-
x: uint256(
|
|
96
|
-
y: uint256(
|
|
95
|
+
x: uint256(0x29345f914a28707887bee191c3a928191de584827a6d1a78ccce1d7629ca9dc0),
|
|
96
|
+
y: uint256(0x1920cebd0b33ac9713424e3bc03d53d79dc72f6afc24c90e56593094a213444c)
|
|
97
97
|
}),
|
|
98
98
|
t3: Honk.G1Point({
|
|
99
|
-
x: uint256(
|
|
100
|
-
y: uint256(
|
|
99
|
+
x: uint256(0x261c990958bc2ef77a45467d9639ab2c68cf787ff7bce55ce3074dfdaedc8f8f),
|
|
100
|
+
y: uint256(0x23c1c05424a40360a61e46f4deab04988a6f5b71dda351e0da608cff1f332ee0)
|
|
101
101
|
}),
|
|
102
102
|
t4: Honk.G1Point({
|
|
103
|
-
x: uint256(
|
|
104
|
-
y: uint256(
|
|
103
|
+
x: uint256(0x2b651d2fd644b2972d72ec439dc69d3339d0b052a296bfc48c6a08396aaca078),
|
|
104
|
+
y: uint256(0x2d7e8c1ecb92e2490049b50efc811df63f1ca97e58d5e82852dbec0c29715d71)
|
|
105
105
|
}),
|
|
106
106
|
id1: Honk.G1Point({
|
|
107
|
-
x: uint256(
|
|
108
|
-
y: uint256(
|
|
107
|
+
x: uint256(0x0fd29f94279ade1203624e978608aa8d4cdd56a7b2f83291f86e990871748c9d),
|
|
108
|
+
y: uint256(0x0eba8e7e417161e496fa362877b6463f892dced77d5d486611447ba378a5c7a6)
|
|
109
109
|
}),
|
|
110
110
|
id2: Honk.G1Point({
|
|
111
|
-
x: uint256(
|
|
112
|
-
y: uint256(
|
|
111
|
+
x: uint256(0x23cfb55bc0f52f50da0a01ca6fefad0af11ca20e6ad94e002afaab2b57f4e7c7),
|
|
112
|
+
y: uint256(0x10c9ad32e742902c5d75712b94a91faec6a2a0acded64159ca96f7cf3314b081)
|
|
113
113
|
}),
|
|
114
114
|
id3: Honk.G1Point({
|
|
115
|
-
x: uint256(
|
|
116
|
-
y: uint256(
|
|
115
|
+
x: uint256(0x1031f8cc78cc9d0e75484eef90799dafa081faa2b2a15288f7e4b6e4df21642f),
|
|
116
|
+
y: uint256(0x180f20c9501bae17e5ba23fada7a49bb85004de66bb62df79cb2558690e92ef7)
|
|
117
117
|
}),
|
|
118
118
|
id4: Honk.G1Point({
|
|
119
|
-
x: uint256(
|
|
120
|
-
y: uint256(
|
|
119
|
+
x: uint256(0x284a583a1744d54bd7abd4b196d05620f8d17901400c8d203b45dd26e9bae43e),
|
|
120
|
+
y: uint256(0x0252954267d54f35fc76a5e93f5350d55c3cb488e2817a6dc191ba40999a4800)
|
|
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(
|
|
128
|
-
y: uint256(
|
|
127
|
+
x: uint256(0x205b95c6359cfde0becec8f8b0fcd3a7a24b84725ea23c684f4f04d1add85998),
|
|
128
|
+
y: uint256(0x1db6e6ea5150485a98eebd181652c378c6a799a1d38553dc70bfddd39251c03b)
|
|
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.
|
|
3
|
+
"version": "0.4.2",
|
|
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/zk-inputs": "^0.4.
|
|
63
|
-
"@crisp-e3/sdk": "^0.4.
|
|
62
|
+
"@crisp-e3/zk-inputs": "^0.4.2",
|
|
63
|
+
"@crisp-e3/sdk": "^0.4.2"
|
|
64
64
|
},
|
|
65
65
|
"scripts": {
|
|
66
66
|
"compile": "hardhat compile",
|
|
@@ -70,7 +70,6 @@
|
|
|
70
70
|
"clean:deployments": "hardhat utils:clean-deployments",
|
|
71
71
|
"deploy:contracts": "hardhat run deploy/deploy.ts",
|
|
72
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",
|
|
74
73
|
"test": "hardhat test mocha",
|
|
75
74
|
"verify": "hardhat run deploy/verify.ts"
|
|
76
75
|
}
|
|
@@ -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
|
-
}
|