refactor select() into a generic multiplexer

This commit is contained in:
poma 2019-11-02 04:55:25 +03:00
parent 7193655e49
commit 07168f9816

View File

@ -15,30 +15,15 @@ template HashLeftRight() {
hash <== hasher.outs[0]; hash <== hasher.outs[0];
} }
// if pathIndex == 0 returns (left = inputElement, right = pathElement) // if s == 0 returns [in[0], in[1]]
// if pathIndex == 1 returns (left = pathElement, right = inputElement) // if s == 1 returns [in[1], in[0]]
template Selector() { template Mux() {
signal input inputElement; signal input in[2];
signal input pathElement; signal input s;
signal input pathIndex; signal output out[2];
signal output left; out[0] <== (in[1] - in[0])*s + in[0];
signal output right; out[1] <== (in[0] - in[1])*s + in[1];
signal leftSelector1;
signal leftSelector2;
signal rightSelector1;
signal rightSelector2;
pathIndex * (1-pathIndex) === 0
leftSelector1 <== (1 - pathIndex) * inputElement;
leftSelector2 <== (pathIndex) * pathElement;
rightSelector1 <== (pathIndex) * inputElement;
rightSelector2 <== (1 - pathIndex) * pathElement;
left <== leftSelector1 + leftSelector2;
right <== rightSelector1 + rightSelector2;
} }
// Verifies that merkle proof is correct for given merkle root and a leaf // Verifies that merkle proof is correct for given merkle root and a leaf
@ -53,20 +38,20 @@ template MerkleTree(levels) {
component hashers[levels]; component hashers[levels];
for (var i = 0; i < levels; i++) { for (var i = 0; i < levels; i++) {
selectors[i] = Selector(); selectors[i] = Mux();
hashers[i] = HashLeftRight(); hashers[i] = HashLeftRight();
selectors[i].pathElement <== pathElements[i]; selectors[i].in[1] <== pathElements[i];
selectors[i].pathIndex <== pathIndex[i]; selectors[i].s <== pathIndex[i];
hashers[i].left <== selectors[i].left; hashers[i].left <== selectors[i].out[0];
hashers[i].right <== selectors[i].right; hashers[i].right <== selectors[i].out[1];
} }
selectors[0].inputElement <== leaf; selectors[0].in[0] <== leaf;
for (var i = 1; i < levels; i++) { for (var i = 1; i < levels; i++) {
selectors[i].inputElement <== hashers[i-1].hash; selectors[i].in[0] <== hashers[i-1].hash;
} }
root === hashers[levels - 1].hash; root === hashers[levels - 1].hash;