@crisp-e3/sdk 0.0.1-test → 0.0.2-test

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.
@@ -0,0 +1,365 @@
1
+ import { LeanIMTMerkleProof, LeanIMT } from '@zk-kit/lean-imt';
2
+ import { ProofData } from '@aztec/bb.js';
3
+
4
+ /**
5
+ * Get the merkle tree data from the CRISP server
6
+ * @param serverUrl - The base URL of the CRISP server
7
+ * @param e3Id - The e3Id of the round
8
+ */
9
+ declare const getTreeData: (serverUrl: string, e3Id: number) => Promise<bigint[]>;
10
+ /**
11
+ * Get the token balance at a specific block for a given address
12
+ * @param voterAddress - The address of the voter
13
+ * @param tokenAddress - The address of the token contract
14
+ * @param snapshotBlock - The block number at which to get the balance
15
+ * @param chainId - The chain ID of the network
16
+ * @returns The token balance as a bigint
17
+ */
18
+ declare const getBalanceAt: (voterAddress: string, tokenAddress: string, snapshotBlock: number, chainId: number) => Promise<bigint>;
19
+ /**
20
+ * Get the total supply of a ERC20Votes Token at a specific block
21
+ * @param tokenAddress The token address to query
22
+ * @param snapshotBlock The block number at which to get the total supply
23
+ * @param chainId The chain ID of the network
24
+ * @returns The total supply as a bigint
25
+ */
26
+ declare const getTotalSupplyAt: (tokenAddress: string, snapshotBlock: number, chainId: number) => Promise<bigint>;
27
+
28
+ /**
29
+ * Interface representing the details of a specific round returned by the CRISP server
30
+ */
31
+ interface IRoundDetailsResponse {
32
+ id: string;
33
+ chain_id: string;
34
+ enclave_address: string;
35
+ status: string;
36
+ vote_count: string;
37
+ start_time: string;
38
+ duration: string;
39
+ expiration: string;
40
+ start_block: string;
41
+ committee_public_key: string[];
42
+ emojis: [string, string];
43
+ token_address: string;
44
+ balance_threshold: string;
45
+ }
46
+ /**
47
+ * Interface representing the details of a specific round in a more convenient format
48
+ */
49
+ interface IRoundDetails {
50
+ e3Id: bigint;
51
+ chainId: bigint;
52
+ enclaveAddress: string;
53
+ status: string;
54
+ voteCount: bigint;
55
+ startTime: bigint;
56
+ duration: bigint;
57
+ expiration: bigint;
58
+ startBlock: bigint;
59
+ committeePublicKey: string[];
60
+ emojis: [string, string];
61
+ tokenAddress: string;
62
+ balanceThreshold: bigint;
63
+ }
64
+ /**
65
+ * Interface representing the token details required for participation in a round
66
+ */
67
+ interface ITokenDetails {
68
+ tokenAddress: string;
69
+ threshold: bigint;
70
+ snapshotBlock: bigint;
71
+ }
72
+ /**
73
+ * Interface representing a Merkle proof
74
+ */
75
+ interface IMerkleProof {
76
+ leaf: bigint;
77
+ index: number;
78
+ proof: LeanIMTMerkleProof<bigint>;
79
+ length: number;
80
+ indices: number[];
81
+ }
82
+ /**
83
+ * Enum representing the voting modes
84
+ */
85
+ declare enum VotingMode {
86
+ /**
87
+ * Governance voting requires to spend all credits on one option
88
+ they cannot be split
89
+ */
90
+ GOVERNANCE = "GOVERNANCE"
91
+ }
92
+ /**
93
+ * Interface representing a vote with power for 'yes' and 'no'
94
+ */
95
+ interface IVote {
96
+ /**
97
+ * The voting power for 'yes' votes
98
+ */
99
+ yes: bigint;
100
+ /**
101
+ * The voting power for 'no' votes
102
+ */
103
+ no: bigint;
104
+ }
105
+ /**
106
+ * Interface representing a vector with coefficients
107
+ */
108
+ interface Polynomial {
109
+ coefficients: string[];
110
+ }
111
+ /**
112
+ * Interface representing cryptographic parameters
113
+ */
114
+ interface GrecoCryptographicParams {
115
+ q_mod_t: string;
116
+ qis: string[];
117
+ k0is: string[];
118
+ }
119
+ /**
120
+ * Interface representing Greco bounds
121
+ */
122
+ interface GrecoBoundParams {
123
+ e_bound: string;
124
+ u_bound: string;
125
+ k1_low_bound: string;
126
+ k1_up_bound: string;
127
+ p1_bounds: string[];
128
+ p2_bounds: string[];
129
+ pk_bounds: string[];
130
+ r1_low_bounds: string[];
131
+ r1_up_bounds: string[];
132
+ r2_bounds: string[];
133
+ }
134
+ /**
135
+ * Interface representing Greco parameters
136
+ */
137
+ interface GrecoParams {
138
+ crypto: GrecoCryptographicParams;
139
+ bounds: GrecoBoundParams;
140
+ }
141
+ /**
142
+ * The inputs required for the CRISP circuit
143
+ */
144
+ interface CRISPCircuitInputs {
145
+ prev_ct0is: Polynomial[];
146
+ prev_ct1is: Polynomial[];
147
+ sum_ct0is: Polynomial[];
148
+ sum_ct1is: Polynomial[];
149
+ sum_r0is: Polynomial[];
150
+ sum_r1is: Polynomial[];
151
+ params: GrecoParams;
152
+ ct0is: Polynomial[];
153
+ ct1is: Polynomial[];
154
+ pk0is: Polynomial[];
155
+ pk1is: Polynomial[];
156
+ r1is: Polynomial[];
157
+ r2is: Polynomial[];
158
+ p1is: Polynomial[];
159
+ p2is: Polynomial[];
160
+ u: Polynomial;
161
+ e0: Polynomial;
162
+ e1: Polynomial;
163
+ k1: Polynomial;
164
+ public_key_x: string[];
165
+ public_key_y: string[];
166
+ signature: string[];
167
+ hashed_message: string[];
168
+ merkle_root: string;
169
+ merkle_proof_length: string;
170
+ merkle_proof_indices: string[];
171
+ merkle_proof_siblings: string[];
172
+ slot_address: string;
173
+ balance: string;
174
+ is_first_vote: boolean;
175
+ }
176
+ /**
177
+ * Interface representing the BFV parameters
178
+ */
179
+ interface BFVParams {
180
+ degree: number;
181
+ plaintextModulus: bigint;
182
+ moduli: BigInt64Array;
183
+ }
184
+ /**
185
+ * Interface representing the inputs for Noir signature verification
186
+ */
187
+ interface NoirSignatureInputs {
188
+ /**
189
+ * X coordinate of the public key
190
+ */
191
+ pub_key_x: Uint8Array;
192
+ /**
193
+ * Y coordinate of the public key
194
+ */
195
+ pub_key_y: Uint8Array;
196
+ /**
197
+ * The signature to verify
198
+ */
199
+ signature: Uint8Array;
200
+ /**
201
+ * The hashed message that was signed
202
+ */
203
+ hashed_message: Uint8Array;
204
+ }
205
+ /**
206
+ * Parameters for encryptVoteAndGenerateCRISPInputs function
207
+ */
208
+ interface EncryptVoteAndGenerateCRISPInputsParams {
209
+ encodedVote: string[];
210
+ publicKey: Uint8Array;
211
+ previousCiphertext: Uint8Array;
212
+ signature: `0x${string}`;
213
+ message: string;
214
+ merkleData: IMerkleProof;
215
+ balance: bigint;
216
+ bfvParams?: BFVParams;
217
+ slotAddress: string;
218
+ isFirstVote: boolean;
219
+ }
220
+
221
+ /**
222
+ * Get the details of a specific round
223
+ */
224
+ declare const getRoundDetails: (serverUrl: string, e3Id: number) => Promise<IRoundDetails>;
225
+ /**
226
+ * Get the token address, balance threshold and snapshot block for a specific round
227
+ * @param serverUrl - The base URL of the CRISP server
228
+ * @param e3Id - The e3Id of the round
229
+ * @returns The token address, balance threshold and snapshot block
230
+ */
231
+ declare const getRoundTokenDetails: (serverUrl: string, e3Id: number) => Promise<ITokenDetails>;
232
+
233
+ declare const CRISP_SERVER_TOKEN_TREE_ENDPOINT = "state/token-holders";
234
+ declare const CRISP_SERVER_STATE_LITE_ENDPOINT = "state/lite";
235
+ /**
236
+ * Half the minimum degree needed to support the maxium vote value
237
+ * If you change MAXIMUM_VOTE_VALUE, make sure to update this value too.
238
+ */
239
+ declare const HALF_LARGEST_MINIMUM_DEGREE = 28;
240
+ /**
241
+ * This is the maximum value for a vote (Yes or No). This is 2^28 - 1
242
+ * The minimum degree that BFV should use is 56 (to accommodate both Yes and No votes)
243
+ */
244
+ declare const MAXIMUM_VOTE_VALUE: bigint;
245
+ /**
246
+ * Default BFV parameters for the CRISP ZK inputs generator.
247
+ * These are the parameters used for the default testing purposes only.
248
+ */
249
+ declare const DEFAULT_BFV_PARAMS: BFVParams;
250
+ /**
251
+ * Mock message for masking signature
252
+ */
253
+ declare const MESSAGE = "Vote for round 0";
254
+
255
+ /**
256
+ * Hash a leaf node for the Merkle tree
257
+ * @param address The voter's address
258
+ * @param balance The voter's balance
259
+ * @returns The hashed leaf as a bigint
260
+ */
261
+ declare const hashLeaf: (address: string, balance: string) => bigint;
262
+ /**
263
+ * Generate a new LeanIMT with the leaves provided
264
+ * @param leaves The leaves of the Merkle tree
265
+ * @returns the generated Merkle tree
266
+ */
267
+ declare const generateMerkleTree: (leaves: bigint[]) => LeanIMT;
268
+ /**
269
+ * Generate a Merkle proof for a given address to prove inclusion in the voters' list
270
+ * @param threshold The minimum balance required to be eligible
271
+ * @param balance The voter's balance
272
+ * @param address The voter's address
273
+ * @param leaves The leaves of the Merkle tree
274
+ * @param maxDepth The maximum depth of the Merkle tree
275
+ */
276
+ declare const generateMerkleProof: (threshold: bigint, balance: bigint, address: string, leaves: bigint[], maxDepth: number) => IMerkleProof;
277
+ /**
278
+ * Convert a number to its binary representation
279
+ * @param number The number to convert to binary
280
+ * @returns The binary representation of the number as a string
281
+ */
282
+ declare const toBinary: (number: bigint) => string;
283
+
284
+ /**
285
+ * This utility function calculates the first valid index for vote options
286
+ * based on the total voting power and degree.
287
+ * @dev This is needed to calculate the decoded plaintext
288
+ * @dev Also, we will need to check in the circuit that anything within these indices is
289
+ * either 0 or 1.
290
+ * @param totalVotingPower The maximum vote amount (if a single voter had all of the power)
291
+ * @param degree The degree of the polynomial
292
+ */
293
+ declare const calculateValidIndicesForPlaintext: (totalVotingPower: bigint, degree: number) => {
294
+ yesIndex: number;
295
+ noIndex: number;
296
+ };
297
+ /**
298
+ * Encode a vote based on the voting mode
299
+ * @param vote The vote to encode
300
+ * @param votingMode The voting mode to use for encoding
301
+ * @param votingPower The voting power of the voter
302
+ * @param bfvParams The BFV parameters to use for encoding
303
+ * @returns The encoded vote as a string
304
+ */
305
+ declare const encodeVote: (vote: IVote, votingMode: VotingMode, votingPower: bigint, bfvParams?: BFVParams) => string[];
306
+ /**
307
+ * Given an encoded tally, decode it into its decimal representation
308
+ * @param tally The encoded tally to decode
309
+ * @param votingMode The voting mode
310
+ */
311
+ declare const decodeTally: (tally: string[], votingMode: VotingMode) => IVote;
312
+ /**
313
+ * Validate whether a vote is valid for a given voting mode
314
+ * @param votingMode The voting mode to validate against
315
+ * @param vote The vote to validate
316
+ * @param votingPower The voting power of the voter
317
+ */
318
+ declare const validateVote: (votingMode: VotingMode, vote: IVote, votingPower: bigint) => void;
319
+ /**
320
+ * This is a wrapper around enclave-e3/sdk encryption functions as CRISP circuit will require some more
321
+ * input values which generic Greco do not need.
322
+ * @param encodedVote The encoded vote as string array
323
+ * @param publicKey The public key to use for encryption
324
+ * @param previousCiphertext The previous ciphertext to use for addition operation
325
+ * @param bfvParams The BFV parameters to use for encryption
326
+ * @param merkleData The merkle proof data
327
+ * @param message The message that was signed
328
+ * @param signature The signature of the message
329
+ * @param balance The voter's balance
330
+ * @param slotAddress The voter's slot address
331
+ * @param isFirstVote Whether this is the first vote for this slot
332
+ * @returns The CRISP circuit inputs
333
+ */
334
+ declare const encryptVoteAndGenerateCRISPInputs: ({ encodedVote, publicKey, previousCiphertext, bfvParams, merkleData, message, signature, balance, slotAddress, isFirstVote, }: EncryptVoteAndGenerateCRISPInputsParams) => Promise<CRISPCircuitInputs>;
335
+ /**
336
+ * A function to generate the data required to mask a vote
337
+ * @param voter The voter's address
338
+ * @param publicKey The voter's public key
339
+ * @param previousCiphertext The previous ciphertext
340
+ * @param bfvParams The BFV parameters
341
+ * @param merkleRoot The merkle root of the census tree
342
+ * @param slotAddress The voter's slot address
343
+ * @param isFirstVote Whether this is the first vote for this slot
344
+ * @returns The CRISP circuit inputs for a mask vote
345
+ */
346
+ declare const generateMaskVote: (publicKey: Uint8Array, previousCiphertext: Uint8Array, bfvParams: BFVParams | undefined, merkleRoot: bigint, slotAddress: string, isFirstVote: boolean) => Promise<CRISPCircuitInputs>;
347
+ declare const generateProof: (crispInputs: CRISPCircuitInputs) => Promise<ProofData>;
348
+ declare const generateProofWithReturnValue: (crispInputs: CRISPCircuitInputs) => Promise<{
349
+ returnValue: unknown;
350
+ proof: ProofData;
351
+ }>;
352
+ declare const getCircuitOutputValue: (crispInputs: CRISPCircuitInputs) => Promise<{
353
+ returnValue: unknown;
354
+ }>;
355
+ declare const verifyProof: (proof: ProofData) => Promise<boolean>;
356
+
357
+ /**
358
+ * Given a message and its signed version, extract the signature components
359
+ * @param message The original message
360
+ * @param signedMessage The signed message (signature)
361
+ * @returns The extracted signature components
362
+ */
363
+ declare const extractSignature: (message: string, signedMessage: `0x${string}`) => Promise<NoirSignatureInputs>;
364
+
365
+ export { type CRISPCircuitInputs, CRISP_SERVER_STATE_LITE_ENDPOINT, CRISP_SERVER_TOKEN_TREE_ENDPOINT, DEFAULT_BFV_PARAMS, HALF_LARGEST_MINIMUM_DEGREE, type IMerkleProof, type IRoundDetails, type IRoundDetailsResponse, type ITokenDetails, type IVote, MAXIMUM_VOTE_VALUE, MESSAGE, type NoirSignatureInputs, VotingMode, calculateValidIndicesForPlaintext, decodeTally, encodeVote, encryptVoteAndGenerateCRISPInputs, extractSignature, generateMaskVote, generateMerkleProof, generateMerkleTree, generateProof, generateProofWithReturnValue, getBalanceAt, getCircuitOutputValue, getRoundDetails, getRoundTokenDetails, getTotalSupplyAt, getTreeData, hashLeaf, toBinary, validateVote, verifyProof };