Skip to content

Commit

Permalink
Ensuring proof value handled as hex for anonymous circuits inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Sep 12, 2023
1 parent 49bb74a commit 871bb1e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Ensuring proof `value` handled as hex for anonymous circuits inputs.

### Added

- Census3 supported chains information.
Expand Down
4 changes: 2 additions & 2 deletions src/services/anonymous.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service, ServiceProperties } from './service';
import { ChainAPI } from '../api';
import invariant from 'tiny-invariant';
import { strip0x } from '../util/common';
import { ensure0x, strip0x } from '../util/common';
import { sha256 } from '@ethersproject/sha2';
import { groth16 } from 'snarkjs';
import { hexlify } from '@ethersproject/bytes';
Expand Down Expand Up @@ -211,7 +211,7 @@ export class AnonymousService extends Service implements AnonymousServicePropert

return Promise.all([
AnonymousService.calcNullifier(ffsignature, ffpassword, arboElectionId),
AnonymousService.arbo_utils.toHash(AnonymousService.hex_utils.fromBigInt(BigInt(availableWeight))),
AnonymousService.arbo_utils.toHash(AnonymousService.hex_utils.fromBigInt(BigInt(ensure0x(availableWeight)))),
]).then((data) => ({
electionId: arboElectionId,
nullifier: data[0].toString(),
Expand Down
81 changes: 80 additions & 1 deletion test/integration/zk.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @ts-ignore
import { clientParams } from './util/client.params';
import { Election, PlainCensus, VocdoniSDKClient, Vote } from '../../src';
import { Election, PlainCensus, WeightedCensus, VocdoniSDKClient, Vote } from '../../src';
import { Wallet } from '@ethersproject/wallet';
// @ts-ignore
import { waitForElectionReady } from './util/client.utils';
Expand Down Expand Up @@ -94,6 +94,85 @@ describe('zkSNARK test', () => {
.then(() => {
const vote = new Vote([1]);
return client.submitVote(vote);
})
.then(() => client.fetchElection())
.then((election) => {
expect(election.electionType.anonymous).toBeTruthy();
expect(election.voteCount).toEqual(3);
expect(election.results[0][0]).toEqual('2');
expect(election.results[0][1]).toEqual('1');
expect(election.census.size).toEqual(3);
expect(election.census.weight).toEqual(BigInt(3));
});
}, 285000);
it('should create a weighted anonymous election and vote successfully', async () => {
const census = new WeightedCensus();
const voter1 = Wallet.createRandom();
const voter2 = Wallet.createRandom();
// User that votes with account with SIK
census.add({
key: (client.wallet as Wallet).address,
weight: 12n,
});
// User that votes and has no account
census.add({
key: voter1.address,
weight: 120n,
});
// User that votes with account without SIK
census.add({
key: voter2.address,
weight: 1200n,
});

const election = createElection(census, {
anonymous: true,
});

await client.createAccount({
sik: true,
password: 'password123',
});

await client
.createElection(election)
.then((electionId) => {
expect(electionId).toMatch(/^[0-9a-fA-F]{64}$/);
client.setElectionId(electionId);
return client.fetchElection();
})
.then((publishedElection) => {
expect(publishedElection.electionType.anonymous).toBeTruthy();
return waitForElectionReady(client, publishedElection.id);
})
.then(async () => {
await expect(async () => {
await client.submitVote(new Vote([0]));
}).rejects.toThrow();
const vote = new AnonymousVote([0], 'password123');
return client.submitVote(vote);
})
.then(() => {
client.wallet = voter1;
const vote = new AnonymousVote([0], 'password456');
return client.submitVote(vote);
})
.then(() => {
client.wallet = voter2;
return client.createAccount({ sik: false });
})
.then(() => {
const vote = new Vote([1]);
return client.submitVote(vote);
})
.then(() => client.fetchElection())
.then((election) => {
expect(election.electionType.anonymous).toBeTruthy();
expect(election.voteCount).toEqual(3);
expect(election.results[0][0]).toEqual('132');
expect(election.results[0][1]).toEqual('1200');
expect(election.census.size).toEqual(3);
expect(election.census.weight).toEqual(BigInt(12n + 120n + 1200n));
});
}, 285000);
it('should create an anonymous election with 12 participants and each of them should vote correctly', async () => {
Expand Down

0 comments on commit 871bb1e

Please sign in to comment.