Skip to content

Commit

Permalink
Merge pull request #1004 from privacy-scaling-explorations/test/circuits
Browse files Browse the repository at this point in the history
test(circuits): refactor and add tests for the circom circuits
  • Loading branch information
ctrlc03 authored Jan 11, 2024
2 parents d6a748f + 4d6419b commit dc071dc
Show file tree
Hide file tree
Showing 26 changed files with 1,665 additions and 5,984 deletions.
15 changes: 4 additions & 11 deletions circuits/circom/tallyVotes.circom
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ include "./hasherSha256.circom";
include "./hasherPoseidon.circom";
include "./unpackElement.circom";

/*
* Tally votes in the ballots, batch by batch.
*/
// Tally votes in the ballots, batch by batch.
template TallyVotes(
stateTreeDepth,
intStateTreeDepth,
Expand Down Expand Up @@ -192,10 +190,8 @@ template TallyVotes(
}
}

/*
* Verifies the commitment to the current results. Also computes and outputs a
* commitment to the new results.
*/
// Verifies the commitment to the current results. Also computes and outputs a
// commitment to the new results.
template ResultCommitmentVerifier(voteOptionTreeDepth) {
var TREE_ARITY = 5;
var numVoteOptions = TREE_ARITY ** voteOptionTreeDepth;
Expand Down Expand Up @@ -257,7 +253,7 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
currentTallyCommitmentHasher.in[1] <== currentSpentVoiceCreditsCommitment.hash;
currentTallyCommitmentHasher.in[2] <== currentPerVOSpentVoiceCreditsCommitment.hash;

/*currentTallyCommitmentHasher.hash === currentTallyCommitment;*/
// currentTallyCommitmentHasher.hash === currentTallyCommitment
// Check if the current tally commitment is correct only if this is not the first batch
component iz = IsZero();
iz.in <== isFirstBatch;
Expand Down Expand Up @@ -305,9 +301,6 @@ template ResultCommitmentVerifier(voteOptionTreeDepth) {
newTallyCommitmentHasher.in[1] <== newSpentVoiceCreditsCommitment.hash;
newTallyCommitmentHasher.in[2] <== newPerVOSpentVoiceCreditsCommitment.hash;

/*log(newResultsCommitment.hash);*/
/*log(newSpentVoiceCreditsCommitment.hash);*/
/*log(newPerVOSpentVoiceCreditsCommitment.hash);*/
newTallyCommitmentHasher.hash === newTallyCommitment;
}

Expand Down
36 changes: 2 additions & 34 deletions circuits/circom/trees/checkRoot.circom
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
pragma circom 2.0.0;
include "../hasherPoseidon.circom";

/*template QuinCheckRootWithSha256(levels, subLevels) {*/
/*// Given a quin Merkle root and a list of leaves, check if the root is the*/
/*// correct result of inserting all the leaves into the tree in the given*/
/*// order. Uses SHA256 instead of Poseidon to hash levels up to subLevels*/

/*assert(levels > 0);*/
/*assert(subLevels <= levels);*/
/*var LEAVES_PER_NODE = 5;*/

/*// The total number of leaves*/
/*var totalLeaves = LEAVES_PER_NODE ** levels;*/

/*signal input leaves[totalLeaves];*/
/*signal output root;*/

/*// The total number of hashers*/
/*var numHashers = 0;*/
/*for (var i = 0; i < levels; i ++) {*/
/*numHashers += LEAVES_PER_NODE ** i;*/
/*}*/

/*// The number of SHA256 hashers*/
/*var numShaHashers = 0;*/
/*for (var i = 0; i < subLevels; i ++) {*/
/*numShaHashers += LEAVES_PER_NODE ** i;*/
/*}*/

/*var numPoseidonHashers = numHashers - numShaHashers;*/


/*}*/

// Given a quin Merkle root and a list of leaves, check if the root is the
// correct result of inserting all the leaves into the tree in the given
// Given a list of leaves, compute the root of the merkle tree
// by inserting all the leaves into the tree in the given
// order.
template QuinCheckRoot(levels) {
var LEAVES_PER_NODE = 5;
Expand Down
10 changes: 5 additions & 5 deletions circuits/circom/trees/incrementalMerkleTree.circom
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pragma circom 2.0.0;
include "../../node_modules/circomlib/circuits/mux1.circom";
include "../hasherPoseidon.circom";

// recompute a merkle root from a leaf and a path
template MerkleTreeInclusionProof(n_levels) {
signal input leaf;
signal input path_index[n_levels];
Expand Down Expand Up @@ -41,8 +42,8 @@ template MerkleTreeInclusionProof(n_levels) {
root <== levelHashes[n_levels];
}

// Ensures that a leaf exists within a merkletree with given `root`
template LeafExists(levels){
// Ensures that a leaf exists within a merkletree with given `root`

// levels is depth of tree
signal input leaf;
Expand All @@ -62,11 +63,10 @@ template LeafExists(levels){
root === merkletree.root;
}

// Given a Merkle root and a list of leaves, check if the root is the
// correct result of inserting all the leaves into the tree (in the given
// order)
template CheckRoot(levels) {
// Given a Merkle root and a list of leaves, check if the root is the
// correct result of inserting all the leaves into the tree (in the given
// order)

// Circom has some perticularities which limit the code patterns we can
// use.

Expand Down
25 changes: 13 additions & 12 deletions circuits/circom/trees/incrementalQuinTree.circom
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ include "../utils.circom";
// circuit to hash leaves, which supports up to 5 input elements.

/*
Note: circom has some particularities which limit the code patterns we can use.
- You can only assign a value to a signal once.
- A component's input signal must only be wired to another component's output
signal.
- Variables can store linear combinations, and can also be used for loops,
declaring sizes of things, and anything that is not related to inputs of a
circuit.
- The compiler fails whenever you try to mix invalid elements.
- You can't use a signal as a list index.
Note: circom has some particularities which limit the code patterns we can use.
- You can only assign a value to a signal once.
- A component's input signal must only be wired to another component's output
signal.
- Variables can store linear combinations, and can also be used for loops,
declaring sizes of things, and anything that is not related to inputs of a
circuit.
- The compiler fails whenever you try to mix invalid elements.
- You can't use a signal as a list index.
*/

// Given a list of items and an index, output the item at the position denoted
Expand Down Expand Up @@ -137,6 +137,8 @@ template Splicer(numItems) {
}
}

// Given a list of leaves, as well as the path to the root,
// compute the root
template QuinTreeInclusionProof(levels) {
// Each node has 5 leaves
var LEAVES_PER_NODE = 5;
Expand Down Expand Up @@ -185,9 +187,8 @@ template QuinTreeInclusionProof(levels) {
root <== hashers[levels - 1].hash;
}

// Ensures that a leaf exists within a quintree with given `root`
template QuinLeafExists(levels){
// Ensures that a leaf exists within a quintree with given `root`

var LEAVES_PER_NODE = 5;
var LEAVES_PER_PATH_LEVEL = LEAVES_PER_NODE - 1;

Expand Down
Loading

0 comments on commit dc071dc

Please sign in to comment.