From e4e679d184ef16b8829b488f28fd9cf8f6e49d7c Mon Sep 17 00:00:00 2001 From: ctrlc03 <93448202+ctrlc03@users.noreply.github.com> Date: Thu, 6 Nov 2025 18:20:27 +0000 Subject: [PATCH] test: crisp circuit tests --- .../packages/crisp-sdk/tests/vote.test.ts | 52 +++++++++++++++---- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/examples/CRISP/packages/crisp-sdk/tests/vote.test.ts b/examples/CRISP/packages/crisp-sdk/tests/vote.test.ts index 64243ecd25..de399e0c0a 100644 --- a/examples/CRISP/packages/crisp-sdk/tests/vote.test.ts +++ b/examples/CRISP/packages/crisp-sdk/tests/vote.test.ts @@ -375,9 +375,7 @@ describe('Vote', () => { // invalidate signature inputs.signature[0] = '0' - expect(async () => { - await getCircuitOutputValue(inputs) - }).rejects.toThrow() + await expect(getCircuitOutputValue(inputs)).rejects.toThrow() }) it('should throw when the merkle tree inclusion proof is invalid and it is a vote (no masking)', { timeout: 100000 }, async () => { @@ -403,12 +401,48 @@ describe('Vote', () => { isFirstVote: false, }) - // invalidate signature + // invalidate merkle root inputs.merkle_root = '0' - expect(async () => { - await getCircuitOutputValue(inputs) - }).rejects.toThrow() + await expect(getCircuitOutputValue(inputs)).rejects.toThrow() + }) + + it('should succeed when the vote is the maximum value supported', { timeout: 100000 }, async () => { + const MAXIMUM_VOTE_VALUE = BigInt(Math.pow(2, 28) - 1) // 268,435,455 + const votingPowerLeaf = MAXIMUM_VOTE_VALUE // Balance at the limit + + // Vote exactly at the maximum + const VOTE = { + yes: MAXIMUM_VOTE_VALUE, + no: 0n, + } + + const encodedVote = encodeVote(VOTE, VotingMode.GOVERNANCE, votingPowerLeaf) + + // hardhat default private key + const privateKey = '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80' + const account = privateKeyToAccount(privateKey) + const signature = await account.signMessage({ message: MESSAGE }) + const leaf = hashLeaf(account.address.toLowerCase(), votingPowerLeaf.toString()) + const leaves = [...LEAVES, leaf] + const merkleProof = generateMerkleProof(0n, votingPowerLeaf, account.address.toLowerCase(), leaves, 20) + + const inputs = await encryptVoteAndGenerateCRISPInputs({ + encodedVote, + publicKey, + previousCiphertext, + signature, + message: MESSAGE, + merkleData: merkleProof, + balance: votingPowerLeaf, + slotAddress: account.address.toLowerCase(), + isFirstVote: false, + }) + + // This should pass - vote equals balance + const proof = await generateProof(inputs) + const isValid = await verifyProof(proof) + expect(isValid).toBe(true) }) it('should throw when the vote is > balance', { timeout: 100000 }, async () => { @@ -437,9 +471,7 @@ describe('Vote', () => { // set balance to 0 inputs.balance = '0' - expect(async () => { - await getCircuitOutputValue(inputs) - }).rejects.toThrow() + await expect(getCircuitOutputValue(inputs)).rejects.toThrow() }) }) })