Skip to content
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

Mux6 and Mux5 template #67

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions circuits/mux5.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2018 0KIMS association.

This file is part of circom (Zero Knowledge Circuit Compiler).

circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.

You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
pragma circom 2.0.0;

include "mux2.circom";
include "mux3.circom";

template MultiMux5(n) {
signal input c[n][32]; // Constants
signal input s[5]; // Selector
signal output out[n];

component leafs[n][8];
component root[n];
for (var i=0; i<n; i++) {
// Make a tree (with depth 2). Leaf nodes are Mux3 components (with 4 leaf node) and one Mux2 root node.

// Leaf components
for (var j=0; j<4; j++) {
leafs[i][j] = Mux3();

// Set the leaf component's constants
for (var k=0; k<8; k++) {
leafs[i][j].c[k] <== c[i][8 * j + k];
}

// Set the leaf component's selector (the least significant 3 bits of the selector);
leafs[i][j].s[0] <== s[0];
leafs[i][j].s[1] <== s[1];
leafs[i][j].s[2] <== s[2];
}

// Root component
root[i] = Mux2();

// Set the root component's constants (the output of it's child nodes).
for (var j=0; j<4; j++) {
root[i].c[j] <== leafs[i][j].out;
}

// Set the root component's selector (the most significant 2 bits of the selector);
root[i].s[0] <== s[3];
root[i].s[1] <== s[4];

out[i] <== root[i].out;
}
}

template Mux5() {
var i;
signal input c[32]; // Constants
signal input s[5]; // Selector
signal output out;

component mux = MultiMux5(1);

for (i=0; i<32; i++) {
mux.c[0][i] <== c[i];
}

for (i=0; i<5; i++) {
s[i] ==> mux.s[i];
}

mux.out[0] ==> out;
}
82 changes: 82 additions & 0 deletions circuits/mux6.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2018 0KIMS association.

This file is part of circom (Zero Knowledge Circuit Compiler).

circom is a free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

circom is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.

You should have received a copy of the GNU General Public License
along with circom. If not, see <https://www.gnu.org/licenses/>.
*/
pragma circom 2.0.0;

include "mux3.circom";

template MultiMux6(n) {
signal input c[n][64]; // Constants
signal input s[6]; // Selector
signal output out[n];

component leafs[n][8];
component root[n];
for (var i=0; i<n; i++) {
// Make a tree (with depth 2) of Mux3 components, with 8 leaf node and one root node.

// Leaf components
for (var j=0; j<8; j++) {
leafs[i][j] = Mux3();

// Set the leaf component's constants
for (var k=0; k<8; k++) {
leafs[i][j].c[k] <== c[i][8 * j + k];
}

// Set the leaf component's selector (the least significant 3 bits of the selector);
leafs[i][j].s[0] <== s[0];
leafs[i][j].s[1] <== s[1];
leafs[i][j].s[2] <== s[2];
}

// Root component
root[i] = Mux3();

// Set the root component's constants (the output of it's child nodes).
for (var j=0; j<8; j++) {
root[i].c[j] <== leafs[i][j].out;
}

// Set the root component's selector (the most significant 3 bits of the selector);
root[i].s[0] <== s[3];
root[i].s[1] <== s[4];
root[i].s[2] <== s[5];

out[i] <== root[i].out;
}
}

template Mux6() {
var i;
signal input c[64]; // Constants
signal input s[6]; // Selector
signal output out;

component mux = MultiMux6(1);

for (i=0; i<64; i++) {
mux.c[0][i] <== c[i];
}

for (i=0; i<6; i++) {
s[i] ==> mux.s[i];
}

mux.out[0] ==> out;
}
66 changes: 66 additions & 0 deletions test/circuits/mux5_1.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pragma circom 2.0.0;

include "../../circuits/mux5.circom";
include "../../circuits/bitify.circom";


template Constants() {
var i;
signal output out[32];

out[0] <== 1230;
out[1] <== 4560;
out[2] <== 7890;
out[3] <== 0120;
out[4] <== 1110;
out[5] <== 2220;
out[6] <== 3330;
out[7] <== 45460;
out[8] <== 1345230;
out[9] <== 443560;
out[10] <== 156230;
out[11] <== 45660;
out[12] <== 12230;
out[13] <== 45460;
out[14] <== 42560;
out[15] <== 44560;

out[16] <== 1231;
out[17] <== 4561;
out[18] <== 7891;
out[19] <== 0121;
out[20] <== 1111;
out[21] <== 2221;
out[22] <== 3331;
out[23] <== 45461;
out[24] <== 1345231;
out[25] <== 443561;
out[26] <== 156231;
out[27] <== 45661;
out[28] <== 12231;
out[29] <== 45461;
out[30] <== 42561;
out[31] <== 44561;
}

template Main() {
var i;
signal input selector;//private
signal output out;

component mux = Mux5();
component n2b = Num2Bits(5);
component cst = Constants();

selector ==> n2b.in;
for (i=0; i<5; i++) {
n2b.out[i] ==> mux.s[i];
}
for (i=0; i<32; i++) {
cst.out[i] ==> mux.c[i];
}

mux.out ==> out;
}

component main = Main();
100 changes: 100 additions & 0 deletions test/circuits/mux6_1.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
pragma circom 2.0.0;

include "../../circuits/mux6.circom";
include "../../circuits/bitify.circom";


template Constants() {
var i;
signal output out[64];

out[0] <== 1230;
out[1] <== 4560;
out[2] <== 7890;
out[3] <== 0120;
out[4] <== 1110;
out[5] <== 2220;
out[6] <== 3330;
out[7] <== 45460;
out[8] <== 1345230;
out[9] <== 443560;
out[10] <== 156230;
out[11] <== 45660;
out[12] <== 12230;
out[13] <== 45460;
out[14] <== 42560;
out[15] <== 44560;

out[16] <== 1231;
out[17] <== 4561;
out[18] <== 7891;
out[19] <== 0121;
out[20] <== 1111;
out[21] <== 2221;
out[22] <== 3331;
out[23] <== 45461;
out[24] <== 1345231;
out[25] <== 443561;
out[26] <== 156231;
out[27] <== 45661;
out[28] <== 12231;
out[29] <== 45461;
out[30] <== 42561;
out[31] <== 44561;

out[32] <== 1232;
out[33] <== 4562;
out[34] <== 7892;
out[35] <== 0122;
out[36] <== 1112;
out[37] <== 2222;
out[38] <== 3332;
out[39] <== 45462;
out[40] <== 1345232;
out[41] <== 443562;
out[42] <== 156232;
out[43] <== 45662;
out[44] <== 12232;
out[45] <== 45462;
out[46] <== 42562;
out[47] <== 44562;

out[48] <== 1233;
out[49] <== 4563;
out[50] <== 7893;
out[51] <== 0123;
out[52] <== 1113;
out[53] <== 2223;
out[54] <== 3333;
out[55] <== 45463;
out[56] <== 1345233;
out[57] <== 443563;
out[58] <== 156233;
out[59] <== 45663;
out[60] <== 12233;
out[61] <== 45463;
out[62] <== 42563;
out[63] <== 44563;
}

template Main() {
var i;
signal input selector;//private
signal output out;

component mux = Mux6();
component n2b = Num2Bits(6);
component cst = Constants();

selector ==> n2b.in;
for (i=0; i<6; i++) {
n2b.out[i] ==> mux.s[i];
}
for (i=0; i<64; i++) {
cst.out[i] ==> mux.c[i];
}

mux.out ==> out;
}

component main = Main();
2 changes: 1 addition & 1 deletion test/escalarmul.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}

describe("Exponentioation test", function () {
describe("Exponentiation test", function () {
let babyJub;
let Fr;
this.timeout(100000);
Expand Down
Loading