Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
45930 committed Aug 22, 2023
1 parent 8c01543 commit 362276d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snarkyjs-pack",
"version": "0.2.3-beta4",
"version": "0.2.3",
"description": "",
"author": "45930",
"license": "Apache-2.0",
Expand Down
49 changes: 49 additions & 0 deletions tests/provable/end_to_end.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { UInt32 } from 'snarkyjs';
import { Votes, VotesProgram, VotesProof } from './example_circuit';

describe('End to End Votes Test', () => {
const initVotesAux = [0n, 0n];
const initVotes = Votes.fromBigInts(initVotesAux);
let initProof: VotesProof;

beforeAll(async () => {
await VotesProgram.compile();
initProof = await VotesProgram.init(initVotes);
initProof.verify();
});

describe('Incrementing votes', () => {
// let initProof: VotesProof;
beforeAll(async () => {
// initProof = await VotesProgram.init(initVotes);
});

it('Increments the 0th index', async () => {
const proofAux = [1n, 0n];
const proofVotes = Votes.fromAuxiliary(Votes.fromBigInts(proofAux).aux);
const proof = await VotesProgram.incrementIndex0(proofVotes, initProof);
proof.verify();
proofVotes.packed.assertEquals(proof.publicInput.packed);
});

// it('Increments the 1st index', async () => {
// const proofAux = [UInt32.from(0), UInt32.from(1)];
// const proofVotes = Votes.fromAuxiliary(proofAux);
// const proof = await VotesProgram.incrementIndex1(proofVotes, initProof);
// proof.verify();
// proofVotes.packed.assertEquals(proof.publicInput.packed);
// })

// it('throws when verifying an invalid proof', async () => {
// const proofAux = [UInt32.from(0), UInt32.from(1)];
// const proofVotes = Votes.fromAuxiliary(proofAux);
// const proof = await VotesProgram.incrementIndex1(proofVotes, initProof);
// try {
// proof.verify();
// expect(0).toBe(1);
// } catch (e) {
// console.log(e);
// }
// })
});
});
39 changes: 39 additions & 0 deletions tests/provable/example_circuit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Experimental, SelfProof } from 'snarkyjs';
import { PackedUInt32Factory } from '../../src/lib/packed-types/PackedUInt32';

export class Votes extends PackedUInt32Factory(2) {}

export const VotesProgram = Experimental.ZkProgram({
publicInput: Votes,

methods: {
init: {
privateInputs: [],
method(state: Votes) {
const initState = Votes.fromAuxiliary(Votes.fromBigInts([0n, 0n]).aux);
state.assertEquals(initState);
},
},
incrementIndex0: {
privateInputs: [SelfProof],
method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
oldProof.verify();
const expectedAux = Votes.unpack(oldProof.publicInput.packed);
expectedAux[0] = expectedAux[0].add(1);
newState.packed.assertEquals(Votes.fromAuxiliary(expectedAux).packed);
},
},
incrementIndex1: {
privateInputs: [SelfProof],
method(newState: Votes, oldProof: SelfProof<Votes, Votes>) {
oldProof.verify();
const expectedAux = Votes.unpack(oldProof.publicInput.packed);
expectedAux[1] = expectedAux[1].add(1);
newState.assertEquals(Votes.fromAuxiliary(expectedAux));
},
},
},
});

export let VotesProof_ = Experimental.ZkProgram.Proof(VotesProgram);
export class VotesProof extends VotesProof_ {}

0 comments on commit 362276d

Please sign in to comment.