-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(circuits): add poll joined circuit #2062
Open
0xmad
wants to merge
1
commit into
dev
Choose a base branch
from
feature/poll-join-circuit
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,43 @@ template PollJoining(stateTreeDepth) { | |
|
||
calculatedRoot === stateRoot; | ||
} | ||
|
||
template PollJoined(stateTreeDepth) { | ||
// Constants defining the tree structure | ||
var STATE_TREE_ARITY = 2; | ||
|
||
// User's private key | ||
signal input privKey; | ||
// Poll's private key | ||
signal input pollPrivKey; | ||
// Poll's public key | ||
signal input pollPubKey[2]; | ||
// User's voice credits balance | ||
signal input voiceCreditsBalance; | ||
// Poll's joined timestamp | ||
signal input joinTimestamp; | ||
// Path elements | ||
signal input pathElements[stateTreeDepth][STATE_TREE_ARITY - 1]; | ||
// Path indices | ||
signal input pathIndices[stateTreeDepth]; | ||
// Poll State tree root which proves the user is joined | ||
signal input stateRoot; | ||
// The poll id | ||
signal input pollId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pollId is not needed |
||
|
||
// Poll private to public key to verify the correct one is used to join the poll (public input) | ||
var derivedPollPubKey[2] = PrivToPubKey()(pollPrivKey); | ||
derivedPollPubKey[0] === pollPubKey[0]; | ||
derivedPollPubKey[1] === pollPubKey[1]; | ||
|
||
var stateLeaf = PoseidonHasher(4)([derivedPollPubKey[0], derivedPollPubKey[1], voiceCreditsBalance, joinTimestamp]); | ||
|
||
// Inclusion proof | ||
var stateLeafQip = MerkleTreeInclusionProof(stateTreeDepth)( | ||
stateLeaf, | ||
pathIndices, | ||
pathElements | ||
); | ||
|
||
stateLeafQip === stateRoot; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { type WitnessTester } from "circomkit"; | ||
import { MaciState, Poll } from "maci-core"; | ||
import { poseidon } from "maci-crypto"; | ||
import { Keypair, Message, PCommand } from "maci-domainobjs"; | ||
|
||
import type { IPollJoinedInputs } from "../types"; | ||
|
||
import { STATE_TREE_DEPTH, duration, messageBatchSize, treeDepths, voiceCreditBalance } from "./utils/constants"; | ||
import { circomkitInstance } from "./utils/utils"; | ||
|
||
describe("Poll Joined circuit", function test() { | ||
this.timeout(900000); | ||
const NUM_USERS = 50; | ||
|
||
const coordinatorKeypair = new Keypair(); | ||
|
||
type PollJoinedCircuitInputs = [ | ||
"privKey", | ||
"pollPrivKey", | ||
"pollPubKey", | ||
"voiceCreditsBalance", | ||
"joinTimestamp", | ||
"stateLeaf", | ||
"pathElements", | ||
"pathIndices", | ||
"stateRoot", | ||
"pollId", | ||
]; | ||
|
||
let circuit: WitnessTester<PollJoinedCircuitInputs>; | ||
|
||
before(async () => { | ||
circuit = await circomkitInstance.WitnessTester("pollJoined", { | ||
file: "./anon/poll", | ||
template: "PollJoined", | ||
params: [STATE_TREE_DEPTH], | ||
}); | ||
}); | ||
|
||
describe(`${NUM_USERS} users, 1 join`, () => { | ||
const maciState = new MaciState(STATE_TREE_DEPTH); | ||
let pollId: bigint; | ||
let poll: Poll; | ||
let users: Keypair[]; | ||
const { privKey: pollPrivKey, pubKey: pollPubKey } = new Keypair(); | ||
const messages: Message[] = []; | ||
const commands: PCommand[] = []; | ||
|
||
const timestamp = BigInt(Math.floor(Date.now() / 1000)); | ||
|
||
before(() => { | ||
// Sign up | ||
users = new Array(NUM_USERS).fill(0).map(() => new Keypair()); | ||
|
||
users.forEach((userKeypair) => { | ||
maciState.signUp(userKeypair.pubKey); | ||
}); | ||
|
||
pollId = maciState.deployPoll(timestamp + BigInt(duration), treeDepths, messageBatchSize, coordinatorKeypair); | ||
|
||
poll = maciState.polls.get(pollId)!; | ||
poll.updatePoll(BigInt(maciState.pubKeys.length)); | ||
|
||
// Join the poll | ||
const { privKey } = users[0]; | ||
|
||
const nullifier = poseidon([BigInt(privKey.rawPrivKey.toString())]); | ||
|
||
const stateIndex = BigInt(poll.joinPoll(nullifier, pollPubKey, voiceCreditBalance, timestamp)); | ||
|
||
// First command (valid) | ||
const command = new PCommand( | ||
stateIndex, | ||
pollPubKey, | ||
BigInt(0), // voteOptionIndex, | ||
BigInt(9), // vote weight | ||
BigInt(1), // nonce | ||
BigInt(pollId), | ||
); | ||
|
||
const signature = command.sign(pollPrivKey); | ||
|
||
const ecdhKeypair = new Keypair(); | ||
const sharedKey = Keypair.genEcdhSharedKey(ecdhKeypair.privKey, coordinatorKeypair.pubKey); | ||
const message = command.encrypt(signature, sharedKey); | ||
messages.push(message); | ||
commands.push(command); | ||
|
||
poll.publishMessage(message, ecdhKeypair.pubKey); | ||
|
||
// Process messages | ||
poll.processMessages(pollId); | ||
}); | ||
|
||
it("should produce a proof", async () => { | ||
const privateKey = users[0].privKey; | ||
const nullifier = poseidon([BigInt(privateKey.asCircuitInputs()), poll.pollId]); | ||
|
||
const stateLeafIndex = poll.joinPoll(nullifier, pollPubKey, voiceCreditBalance, timestamp); | ||
|
||
const inputs = poll.joinedCircuitInputs({ | ||
maciPrivKey: privateKey, | ||
stateLeafIndex: BigInt(stateLeafIndex), | ||
pollPrivKey, | ||
pollPubKey, | ||
voiceCreditsBalance: voiceCreditBalance, | ||
joinTimestamp: timestamp, | ||
}) as unknown as IPollJoinedInputs; | ||
|
||
const witness = await circuit.calculateWitness(inputs); | ||
await circuit.expectConstraintPass(witness); | ||
}); | ||
|
||
it("should fail for fake witness", async () => { | ||
const privateKey = users[1].privKey; | ||
const nullifier = poseidon([BigInt(privateKey.asCircuitInputs()), poll.pollId]); | ||
|
||
const stateLeafIndex = poll.joinPoll(nullifier, pollPubKey, voiceCreditBalance, timestamp); | ||
|
||
const inputs = poll.joinedCircuitInputs({ | ||
maciPrivKey: privateKey, | ||
stateLeafIndex: BigInt(stateLeafIndex), | ||
pollPrivKey, | ||
pollPubKey, | ||
voiceCreditsBalance: voiceCreditBalance, | ||
joinTimestamp: timestamp, | ||
}) as unknown as IPollJoinedInputs; | ||
const witness = await circuit.calculateWitness(inputs); | ||
|
||
const fakeWitness = Array(witness.length).fill(1n) as bigint[]; | ||
await circuit.expectConstraintFail(fakeWitness); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed, see updates to the pollJoin circuit