From d93a6d62981ee901948187ee1a52bbc61dac5ae1 Mon Sep 17 00:00:00 2001 From: poma Date: Tue, 24 Aug 2021 19:19:26 +0300 Subject: [PATCH] Use switcher from circomlib --- circuits/merkleTree.circom | 46 +++++++++---------------------------- circuits/treeUpdater.circom | 8 +++---- 2 files changed, 15 insertions(+), 39 deletions(-) diff --git a/circuits/merkleTree.circom b/circuits/merkleTree.circom index 302c0d7..feeacb0 100644 --- a/circuits/merkleTree.circom +++ b/circuits/merkleTree.circom @@ -1,28 +1,5 @@ include "../node_modules/circomlib/circuits/poseidon.circom"; - -// Computes MiMC([left, right]) -template HashLeftRight() { - signal input left; - signal input right; - signal output hash; - - component hasher = Poseidon(2); - hasher.inputs[0] <== left; - hasher.inputs[1] <== right; - hash <== hasher.out; -} - -// if s == 0 returns [in[0], in[1]] -// if s == 1 returns [in[1], in[0]] -template DualMux() { - signal input in[2]; - signal input s; - signal output out[2]; - - s * (1 - s) === 0 - out[0] <== (in[1] - in[0])*s + in[0]; - out[1] <== (in[0] - in[1])*s + in[1]; -} +include "../node_modules/circomlib/circuits/switcher.circom"; // Verifies that merkle proof is correct for given merkle root and a leaf // pathIndices input is an array of 0/1 selectors telling whether given pathElement is on the left or right side of merkle path @@ -30,25 +7,24 @@ template MerkleTree(levels) { signal input leaf; signal input pathElements[levels]; signal input pathIndices; - signal output root; - component selectors[levels]; - component hashers[levels]; + component switcher[levels]; + component hasher[levels]; component indexBits = Num2Bits(levels); indexBits.in <== pathIndices; for (var i = 0; i < levels; i++) { - selectors[i] = DualMux(); - selectors[i].in[0] <== i == 0 ? leaf : hashers[i - 1].hash; - selectors[i].in[1] <== pathElements[i]; - selectors[i].s <== indexBits.out[i]; + switcher[i] = Switcher(); + switcher[i].L <== i == 0 ? leaf : hasher[i - 1].out; + switcher[i].R <== pathElements[i]; + switcher[i].sel <== indexBits.out[i]; - hashers[i] = HashLeftRight(); - hashers[i].left <== selectors[i].out[0]; - hashers[i].right <== selectors[i].out[1]; + hasher[i] = Poseidon(2); + hasher[i].inputs[0] <== switcher[i].outL; + hasher[i].inputs[1] <== switcher[i].outR; } - root <== hashers[levels - 1].hash; + root <== hasher[levels - 1].out; } diff --git a/circuits/treeUpdater.circom b/circuits/treeUpdater.circom index c77ae7a..6495a1e 100644 --- a/circuits/treeUpdater.circom +++ b/circuits/treeUpdater.circom @@ -17,9 +17,9 @@ template TreeUpdater(levels, subtreeLevels, zeroSubtreeRoot) { // calculate subtree root // todo: make it work with arbitrary subtree levels // currently it works only with 1-level subtrees - component leafPair = HashLeftRight(); - leafPair.left <== leaf[0]; - leafPair.right <== leaf[1]; + component leafPair = Poseidon(2); + leafPair.inputs[0] <== leaf[0]; + leafPair.inputs[1] <== leaf[1]; component treeBefore = MerkleTree(remainingLevels); for(var i = 0; i < remainingLevels; i++) { @@ -34,6 +34,6 @@ template TreeUpdater(levels, subtreeLevels, zeroSubtreeRoot) { treeAfter.pathElements[i] <== pathElements[i]; } treeAfter.pathIndices <== pathIndices; - treeAfter.leaf <== leafPair.hash; + treeAfter.leaf <== leafPair.out; treeAfter.root === newRoot; }