From 2ce3bfdb1de143782f9ef687e7f0fb4a4125cd17 Mon Sep 17 00:00:00 2001 From: TrueAlpha-spiral <199723968+TrueAlpha-spiral@users.noreply.github.com> Date: Tue, 17 Mar 2026 20:51:53 +0000 Subject: [PATCH] test: Add comprehensive tests for SimulatedZkProver Added a test suite for `SimulatedZkProver` in `packages/core/src/governance/zk-snark.ts` to improve test coverage and reliability. - Tests `generateProof` happy path to ensure correct proof generation based on valid inputs. - Tests `generateProof` error paths by simulating invalid public and private input structures. - Tests `verifyProof` happy path with structurally valid proofs. - Tests `verifyProof` negative paths for invalid proofs (incorrect protocol, incorrect curve, incorrect `pi_a` elements length). Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- packages/core/src/governance/zk-snark.test.ts | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 packages/core/src/governance/zk-snark.test.ts diff --git a/packages/core/src/governance/zk-snark.test.ts b/packages/core/src/governance/zk-snark.test.ts new file mode 100644 index 00000000000..c4a34c315bd --- /dev/null +++ b/packages/core/src/governance/zk-snark.test.ts @@ -0,0 +1,106 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect, beforeEach } from 'vitest'; +import { + SimulatedZkProver, + CircuitPublicInputs, + CircuitPrivateInputs, + SnarkProof, +} from './zk-snark.js'; + +describe('SimulatedZkProver', () => { + let prover: SimulatedZkProver; + let validPublicInputs: CircuitPublicInputs; + let validPrivateInputs: CircuitPrivateInputs; + let validProof: SnarkProof; + + beforeEach(() => { + prover = new SimulatedZkProver(); + validPublicInputs = { + genesis_hash: '0xgenesis', + gene_hash: '0xgene', + }; + validPrivateInputs = { + human_seed_sk: '0xsecret', + raw_prompt: 'Hello world', + biometric_sig: '0xsig', + }; + validProof = { + pi_a: ['0x123...', '0x456...', '0x789...'], + pi_b: [ + ['0xabc...', '0xdef...'], + ['0xghi...', '0xjkl...'], + ], + pi_c: ['0xmno...', '0xpqr...'], + protocol: 'groth16', + curve: 'bn128', + }; + }); + + describe('generateProof', () => { + it('returns a mocked proof for valid inputs', async () => { + const proof = await prover.generateProof( + validPublicInputs, + validPrivateInputs + ); + expect(proof).toEqual(validProof); + }); + + it('throws when genesis_hash is missing', async () => { + const publicInputs = { ...validPublicInputs, genesis_hash: '' }; + await expect( + prover.generateProof(publicInputs, validPrivateInputs) + ).rejects.toThrow('Invalid public inputs for ZK Circuit.'); + }); + + it('throws when gene_hash is missing', async () => { + const publicInputs = { ...validPublicInputs, gene_hash: '' }; + await expect( + prover.generateProof(publicInputs, validPrivateInputs) + ).rejects.toThrow('Invalid public inputs for ZK Circuit.'); + }); + + it('throws when human_seed_sk is missing', async () => { + const privateInputs = { ...validPrivateInputs, human_seed_sk: '' }; + await expect( + prover.generateProof(validPublicInputs, privateInputs) + ).rejects.toThrow('Invalid private inputs for ZK Circuit.'); + }); + + it('throws when raw_prompt is missing', async () => { + const privateInputs = { ...validPrivateInputs, raw_prompt: '' }; + await expect( + prover.generateProof(validPublicInputs, privateInputs) + ).rejects.toThrow('Invalid private inputs for ZK Circuit.'); + }); + }); + + describe('verifyProof', () => { + it('returns true for a valid proof structure', async () => { + const result = await prover.verifyProof(validProof, validPublicInputs); + expect(result).toBe(true); + }); + + it('returns false when protocol is not groth16', async () => { + const invalidProof: SnarkProof = { ...validProof, protocol: 'plonk' }; + const result = await prover.verifyProof(invalidProof, validPublicInputs); + expect(result).toBe(false); + }); + + it('returns false when curve is not bn128', async () => { + const invalidProof: SnarkProof = { ...validProof, curve: 'bls12-381' }; + const result = await prover.verifyProof(invalidProof, validPublicInputs); + expect(result).toBe(false); + }); + + it('returns false when pi_a does not have exactly 3 elements', async () => { + const invalidProof: SnarkProof = { ...validProof, pi_a: ['0x1'] }; + const result = await prover.verifyProof(invalidProof, validPublicInputs); + expect(result).toBe(false); + }); + }); +});