starkshield 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -0
- package/bin/circom +0 -0
- package/bin/cli.js +36 -0
- package/bin/index.js +13 -0
- package/circomlib/.vscode/launch.json +63 -0
- package/circomlib/README.md +18 -0
- package/circomlib/circuits/README.md +830 -0
- package/circomlib/circuits/aliascheck.circom +33 -0
- package/circomlib/circuits/babyjub.circom +107 -0
- package/circomlib/circuits/binsub.circom +74 -0
- package/circomlib/circuits/binsum.circom +101 -0
- package/circomlib/circuits/bitify.circom +106 -0
- package/circomlib/circuits/comparators.circom +141 -0
- package/circomlib/circuits/compconstant.circom +74 -0
- package/circomlib/circuits/eddsa.circom +139 -0
- package/circomlib/circuits/eddsamimc.circom +124 -0
- package/circomlib/circuits/eddsamimcsponge.circom +124 -0
- package/circomlib/circuits/eddsaposeidon.circom +123 -0
- package/circomlib/circuits/escalarmul.circom +166 -0
- package/circomlib/circuits/escalarmulany.circom +197 -0
- package/circomlib/circuits/escalarmulfix.circom +299 -0
- package/circomlib/circuits/escalarmulw4table.circom +52 -0
- package/circomlib/circuits/gates.circom +96 -0
- package/circomlib/circuits/mimc.circom +156 -0
- package/circomlib/circuits/mimcsponge.circom +293 -0
- package/circomlib/circuits/montgomery.circom +142 -0
- package/circomlib/circuits/multiplexer.circom +115 -0
- package/circomlib/circuits/mux1.circom +48 -0
- package/circomlib/circuits/mux2.circom +63 -0
- package/circomlib/circuits/mux3.circom +75 -0
- package/circomlib/circuits/mux4.circom +119 -0
- package/circomlib/circuits/pedersen.circom +257 -0
- package/circomlib/circuits/pedersen_old.circom +68 -0
- package/circomlib/circuits/pointbits.circom +164 -0
- package/circomlib/circuits/poseidon.circom +208 -0
- package/circomlib/circuits/poseidon_constants.circom +24959 -0
- package/circomlib/circuits/poseidon_constants_old.circom +252 -0
- package/circomlib/circuits/poseidon_old.circom +97 -0
- package/circomlib/circuits/sha256/ch.circom +47 -0
- package/circomlib/circuits/sha256/constants.circom +53 -0
- package/circomlib/circuits/sha256/main.circom +35 -0
- package/circomlib/circuits/sha256/maj.circom +45 -0
- package/circomlib/circuits/sha256/rotate.circom +28 -0
- package/circomlib/circuits/sha256/sha256.circom +81 -0
- package/circomlib/circuits/sha256/sha256_2.circom +91 -0
- package/circomlib/circuits/sha256/sha256compression.circom +166 -0
- package/circomlib/circuits/sha256/sha256compression_function.circom +112 -0
- package/circomlib/circuits/sha256/shift.circom +33 -0
- package/circomlib/circuits/sha256/sigma.circom +77 -0
- package/circomlib/circuits/sha256/sigmaplus.circom +50 -0
- package/circomlib/circuits/sha256/t1.circom +58 -0
- package/circomlib/circuits/sha256/t2.circom +51 -0
- package/circomlib/circuits/sha256/xor3.circom +45 -0
- package/circomlib/circuits/sign.circom +36 -0
- package/circomlib/circuits/smt/smthash_mimc.circom +58 -0
- package/circomlib/circuits/smt/smthash_poseidon.circom +57 -0
- package/circomlib/circuits/smt/smtlevins.circom +103 -0
- package/circomlib/circuits/smt/smtprocessor.circom +261 -0
- package/circomlib/circuits/smt/smtprocessorlevel.circom +95 -0
- package/circomlib/circuits/smt/smtprocessorsm.circom +165 -0
- package/circomlib/circuits/smt/smtverifier.circom +138 -0
- package/circomlib/circuits/smt/smtverifierlevel.circom +71 -0
- package/circomlib/circuits/smt/smtverifiersm.circom +106 -0
- package/circomlib/circuits/switcher.circom +42 -0
- package/circomlib/doc/root_transfer.monopic +0 -0
- package/circomlib/doc/smt_diagram_0.monopic +0 -0
- package/circomlib/doc/smt_diagram_1.monopic +0 -0
- package/circomlib/doc/smt_hash.monopic +0 -0
- package/circomlib/doc/smt_levins.monopic +0 -0
- package/circomlib/doc/smt_sm.monopic +0 -0
- package/circomlib/doc/smt_verifier_sm.monopic +0 -0
- package/circomlib/doc/voting.monopic +0 -0
- package/circomlib/doc/window.monopic +0 -0
- package/circomlib/doc/window_chain.monopic +0 -0
- package/circomlib/index.js +2 -0
- package/circomlib/package.json +33 -0
- package/circomlib/test/aliascheck.js +77 -0
- package/circomlib/test/babyjub.js +118 -0
- package/circomlib/test/binsub.js +52 -0
- package/circomlib/test/binsum.js +38 -0
- package/circomlib/test/circuits/aliascheck_test.circom +4 -0
- package/circomlib/test/circuits/babyadd_tester.circom +4 -0
- package/circomlib/test/circuits/babycheck_test.circom +4 -0
- package/circomlib/test/circuits/babypbk_test.circom +4 -0
- package/circomlib/test/circuits/binsub_test.circom +33 -0
- package/circomlib/test/circuits/constants_test.circom +20 -0
- package/circomlib/test/circuits/eddsa_test.circom +5 -0
- package/circomlib/test/circuits/eddsamimc_test.circom +5 -0
- package/circomlib/test/circuits/eddsaposeidon_test.circom +5 -0
- package/circomlib/test/circuits/edwards2montgomery.circom +5 -0
- package/circomlib/test/circuits/escalarmul_min_test.circom +27 -0
- package/circomlib/test/circuits/escalarmul_test.circom +33 -0
- package/circomlib/test/circuits/escalarmul_test_min.circom +28 -0
- package/circomlib/test/circuits/escalarmulany_test.circom +30 -0
- package/circomlib/test/circuits/escalarmulfix_test.circom +31 -0
- package/circomlib/test/circuits/escalarmulw4table.circom +20 -0
- package/circomlib/test/circuits/escalarmulw4table_test.circom +19 -0
- package/circomlib/test/circuits/escalarmulw4table_test3.circom +19 -0
- package/circomlib/test/circuits/greatereqthan.circom +5 -0
- package/circomlib/test/circuits/greaterthan.circom +5 -0
- package/circomlib/test/circuits/isequal.circom +5 -0
- package/circomlib/test/circuits/iszero.circom +5 -0
- package/circomlib/test/circuits/lesseqthan.circom +5 -0
- package/circomlib/test/circuits/lessthan.circom +5 -0
- package/circomlib/test/circuits/mimc_sponge_hash_test.circom +5 -0
- package/circomlib/test/circuits/mimc_sponge_test.circom +5 -0
- package/circomlib/test/circuits/mimc_test.circom +5 -0
- package/circomlib/test/circuits/montgomery2edwards.circom +5 -0
- package/circomlib/test/circuits/montgomeryadd.circom +5 -0
- package/circomlib/test/circuits/montgomerydouble.circom +5 -0
- package/circomlib/test/circuits/mux1_1.circom +33 -0
- package/circomlib/test/circuits/mux2_1.circom +37 -0
- package/circomlib/test/circuits/mux3_1.circom +41 -0
- package/circomlib/test/circuits/mux4_1.circom +56 -0
- package/circomlib/test/circuits/pedersen2_test.circom +34 -0
- package/circomlib/test/circuits/pedersen_test.circom +31 -0
- package/circomlib/test/circuits/pointbits_loopback.circom +25 -0
- package/circomlib/test/circuits/poseidon3_test.circom +5 -0
- package/circomlib/test/circuits/poseidon6_test.circom +5 -0
- package/circomlib/test/circuits/poseidonex_test.circom +5 -0
- package/circomlib/test/circuits/sha256_2_test.circom +17 -0
- package/circomlib/test/circuits/sha256_test448.circom +5 -0
- package/circomlib/test/circuits/sha256_test512.circom +5 -0
- package/circomlib/test/circuits/sign_test.circom +5 -0
- package/circomlib/test/circuits/smtprocessor10_test.circom +5 -0
- package/circomlib/test/circuits/smtverifier10_test.circom +5 -0
- package/circomlib/test/circuits/sum_test.circom +33 -0
- package/circomlib/test/comparators.js +187 -0
- package/circomlib/test/eddsa.js +75 -0
- package/circomlib/test/eddsamimc.js +102 -0
- package/circomlib/test/eddsaposeidon.js +103 -0
- package/circomlib/test/escalarmul.js +121 -0
- package/circomlib/test/escalarmulany.js +51 -0
- package/circomlib/test/escalarmulfix.js +95 -0
- package/circomlib/test/helpers/printsignal.js +22 -0
- package/circomlib/test/helpers/sha256.js +178 -0
- package/circomlib/test/mimccircuit.js +27 -0
- package/circomlib/test/mimcspongecircuit.js +47 -0
- package/circomlib/test/montgomery.js +101 -0
- package/circomlib/test/multiplexer.js +101 -0
- package/circomlib/test/pedersen.js +83 -0
- package/circomlib/test/pedersen2.js +56 -0
- package/circomlib/test/point2bits.js +30 -0
- package/circomlib/test/poseidoncircuit.js +80 -0
- package/circomlib/test/sha256.js +118 -0
- package/circomlib/test/sign.js +82 -0
- package/circomlib/test/smtprocessor.js +219 -0
- package/circomlib/test/smtverifier.js +141 -0
- package/lib/compile.js +82 -0
- package/lib/deploy.js +391 -0
- package/lib/test.js +47 -0
- package/lib/verify.js +153 -0
- package/package.json +43 -0
- package/pot12_0000.ptau +0 -0
- package/pot12_0001.ptau +0 -0
- package/ptau/pot12_final.ptau +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
pragma circom 2.0.0;
|
|
20
|
+
|
|
21
|
+
include "compconstant.circom";
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
template AliasCheck() {
|
|
25
|
+
|
|
26
|
+
signal input in[254];
|
|
27
|
+
|
|
28
|
+
component compConstant = CompConstant(-1);
|
|
29
|
+
|
|
30
|
+
for (var i=0; i<254; i++) in[i] ==> compConstant.in[i];
|
|
31
|
+
|
|
32
|
+
compConstant.out === 0;
|
|
33
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
pragma circom 2.0.0;
|
|
20
|
+
|
|
21
|
+
include "bitify.circom";
|
|
22
|
+
include "escalarmulfix.circom";
|
|
23
|
+
|
|
24
|
+
template BabyAdd() {
|
|
25
|
+
signal input x1;
|
|
26
|
+
signal input y1;
|
|
27
|
+
signal input x2;
|
|
28
|
+
signal input y2;
|
|
29
|
+
signal output xout;
|
|
30
|
+
signal output yout;
|
|
31
|
+
|
|
32
|
+
signal beta;
|
|
33
|
+
signal gamma;
|
|
34
|
+
signal delta;
|
|
35
|
+
signal tau;
|
|
36
|
+
|
|
37
|
+
var a = 168700;
|
|
38
|
+
var d = 168696;
|
|
39
|
+
|
|
40
|
+
beta <== x1*y2;
|
|
41
|
+
gamma <== y1*x2;
|
|
42
|
+
delta <== (-a*x1+y1)*(x2 + y2);
|
|
43
|
+
tau <== beta * gamma;
|
|
44
|
+
|
|
45
|
+
xout <-- (beta + gamma) / (1+ d*tau);
|
|
46
|
+
(1+ d*tau) * xout === (beta + gamma);
|
|
47
|
+
|
|
48
|
+
yout <-- (delta + a*beta - gamma) / (1-d*tau);
|
|
49
|
+
(1-d*tau)*yout === (delta + a*beta - gamma);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
template BabyDbl() {
|
|
53
|
+
signal input x;
|
|
54
|
+
signal input y;
|
|
55
|
+
signal output xout;
|
|
56
|
+
signal output yout;
|
|
57
|
+
|
|
58
|
+
component adder = BabyAdd();
|
|
59
|
+
adder.x1 <== x;
|
|
60
|
+
adder.y1 <== y;
|
|
61
|
+
adder.x2 <== x;
|
|
62
|
+
adder.y2 <== y;
|
|
63
|
+
|
|
64
|
+
adder.xout ==> xout;
|
|
65
|
+
adder.yout ==> yout;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
template BabyCheck() {
|
|
70
|
+
signal input x;
|
|
71
|
+
signal input y;
|
|
72
|
+
|
|
73
|
+
signal x2;
|
|
74
|
+
signal y2;
|
|
75
|
+
|
|
76
|
+
var a = 168700;
|
|
77
|
+
var d = 168696;
|
|
78
|
+
|
|
79
|
+
x2 <== x*x;
|
|
80
|
+
y2 <== y*y;
|
|
81
|
+
|
|
82
|
+
a*x2 + y2 === 1 + d*x2*y2;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Extracts the public key from private key
|
|
86
|
+
template BabyPbk() {
|
|
87
|
+
signal input in;
|
|
88
|
+
signal output Ax;
|
|
89
|
+
signal output Ay;
|
|
90
|
+
|
|
91
|
+
var BASE8[2] = [
|
|
92
|
+
5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
|
93
|
+
16950150798460657717958625567821834550301663161624707787222815936182638968203
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
component pvkBits = Num2Bits(253);
|
|
97
|
+
pvkBits.in <== in;
|
|
98
|
+
|
|
99
|
+
component mulFix = EscalarMulFix(253, BASE8);
|
|
100
|
+
|
|
101
|
+
var i;
|
|
102
|
+
for (i=0; i<253; i++) {
|
|
103
|
+
mulFix.e[i] <== pvkBits.out[i];
|
|
104
|
+
}
|
|
105
|
+
Ax <== mulFix.out[0];
|
|
106
|
+
Ay <== mulFix.out[1];
|
|
107
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
This component creates a binary substraction.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
Main Constraint:
|
|
25
|
+
(in[0][0] * 2^0 + in[0][1] * 2^1 + ..... + in[0][n-1] * 2^(n-1)) +
|
|
26
|
+
+ 2^n
|
|
27
|
+
- (in[1][0] * 2^0 + in[1][1] * 2^1 + ..... + in[1][n-1] * 2^(n-1))
|
|
28
|
+
===
|
|
29
|
+
out[0] * 2^0 + out[1] * 2^1 + + out[n-1] *2^(n-1) + aux
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
out[0] * (out[0] - 1) === 0
|
|
33
|
+
out[1] * (out[0] - 1) === 0
|
|
34
|
+
.
|
|
35
|
+
.
|
|
36
|
+
.
|
|
37
|
+
out[n-1] * (out[n-1] - 1) === 0
|
|
38
|
+
aux * (aux-1) == 0
|
|
39
|
+
|
|
40
|
+
*/
|
|
41
|
+
pragma circom 2.0.0;
|
|
42
|
+
|
|
43
|
+
template BinSub(n) {
|
|
44
|
+
signal input in[2][n];
|
|
45
|
+
signal output out[n];
|
|
46
|
+
|
|
47
|
+
signal aux;
|
|
48
|
+
|
|
49
|
+
var lin = 2**n;
|
|
50
|
+
var lout = 0;
|
|
51
|
+
|
|
52
|
+
var i;
|
|
53
|
+
|
|
54
|
+
for (i=0; i<n; i++) {
|
|
55
|
+
lin = lin + in[0][i]*(2**i);
|
|
56
|
+
lin = lin - in[1][i]*(2**i);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (i=0; i<n; i++) {
|
|
60
|
+
out[i] <-- (lin >> i) & 1;
|
|
61
|
+
|
|
62
|
+
// Ensure out is binary
|
|
63
|
+
out[i] * (out[i] - 1) === 0;
|
|
64
|
+
|
|
65
|
+
lout = lout + out[i]*(2**i);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
aux <-- (lin >> n) & 1;
|
|
69
|
+
aux*(aux-1) === 0;
|
|
70
|
+
lout = lout + aux*(2**n);
|
|
71
|
+
|
|
72
|
+
// Ensure the sum;
|
|
73
|
+
lin === lout;
|
|
74
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
|
|
22
|
+
Binary Sum
|
|
23
|
+
==========
|
|
24
|
+
|
|
25
|
+
This component creates a binary sum componet of ops operands and n bits each operand.
|
|
26
|
+
|
|
27
|
+
e is Number of carries: Depends on the number of operands in the input.
|
|
28
|
+
|
|
29
|
+
Main Constraint:
|
|
30
|
+
in[0][0] * 2^0 + in[0][1] * 2^1 + ..... + in[0][n-1] * 2^(n-1) +
|
|
31
|
+
+ in[1][0] * 2^0 + in[1][1] * 2^1 + ..... + in[1][n-1] * 2^(n-1) +
|
|
32
|
+
+ ..
|
|
33
|
+
+ in[ops-1][0] * 2^0 + in[ops-1][1] * 2^1 + ..... + in[ops-1][n-1] * 2^(n-1) +
|
|
34
|
+
===
|
|
35
|
+
out[0] * 2^0 + out[1] * 2^1 + + out[n+e-1] *2(n+e-1)
|
|
36
|
+
|
|
37
|
+
To waranty binary outputs:
|
|
38
|
+
|
|
39
|
+
out[0] * (out[0] - 1) === 0
|
|
40
|
+
out[1] * (out[0] - 1) === 0
|
|
41
|
+
.
|
|
42
|
+
.
|
|
43
|
+
.
|
|
44
|
+
out[n+e-1] * (out[n+e-1] - 1) == 0
|
|
45
|
+
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
This function calculates the number of extra bits in the output to do the full sum.
|
|
51
|
+
*/
|
|
52
|
+
pragma circom 2.0.0;
|
|
53
|
+
|
|
54
|
+
function nbits(a) {
|
|
55
|
+
var n = 1;
|
|
56
|
+
var r = 0;
|
|
57
|
+
while (n-1<a) {
|
|
58
|
+
r++;
|
|
59
|
+
n *= 2;
|
|
60
|
+
}
|
|
61
|
+
return r;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
template BinSum(n, ops) {
|
|
66
|
+
var nout = nbits((2**n -1)*ops);
|
|
67
|
+
signal input in[ops][n];
|
|
68
|
+
signal output out[nout];
|
|
69
|
+
|
|
70
|
+
var lin = 0;
|
|
71
|
+
var lout = 0;
|
|
72
|
+
|
|
73
|
+
var k;
|
|
74
|
+
var j;
|
|
75
|
+
|
|
76
|
+
var e2;
|
|
77
|
+
|
|
78
|
+
e2 = 1;
|
|
79
|
+
for (k=0; k<n; k++) {
|
|
80
|
+
for (j=0; j<ops; j++) {
|
|
81
|
+
lin += in[j][k] * e2;
|
|
82
|
+
}
|
|
83
|
+
e2 = e2 + e2;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
e2 = 1;
|
|
87
|
+
for (k=0; k<nout; k++) {
|
|
88
|
+
out[k] <-- (lin >> k) & 1;
|
|
89
|
+
|
|
90
|
+
// Ensure out is binary
|
|
91
|
+
out[k] * (out[k] - 1) === 0;
|
|
92
|
+
|
|
93
|
+
lout += out[k] * e2;
|
|
94
|
+
|
|
95
|
+
e2 = e2+e2;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Ensure the sum;
|
|
99
|
+
|
|
100
|
+
lin === lout;
|
|
101
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
pragma circom 2.0.0;
|
|
20
|
+
|
|
21
|
+
include "comparators.circom";
|
|
22
|
+
include "aliascheck.circom";
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
template Num2Bits(n) {
|
|
26
|
+
signal input in;
|
|
27
|
+
signal output out[n];
|
|
28
|
+
var lc1=0;
|
|
29
|
+
|
|
30
|
+
var e2=1;
|
|
31
|
+
for (var i = 0; i<n; i++) {
|
|
32
|
+
out[i] <-- (in >> i) & 1;
|
|
33
|
+
out[i] * (out[i] -1 ) === 0;
|
|
34
|
+
lc1 += out[i] * e2;
|
|
35
|
+
e2 = e2+e2;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
lc1 === in;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
template Num2Bits_strict() {
|
|
42
|
+
signal input in;
|
|
43
|
+
signal output out[254];
|
|
44
|
+
|
|
45
|
+
component aliasCheck = AliasCheck();
|
|
46
|
+
component n2b = Num2Bits(254);
|
|
47
|
+
in ==> n2b.in;
|
|
48
|
+
|
|
49
|
+
for (var i=0; i<254; i++) {
|
|
50
|
+
n2b.out[i] ==> out[i];
|
|
51
|
+
n2b.out[i] ==> aliasCheck.in[i];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
template Bits2Num(n) {
|
|
56
|
+
signal input in[n];
|
|
57
|
+
signal output out;
|
|
58
|
+
var lc1=0;
|
|
59
|
+
|
|
60
|
+
var e2 = 1;
|
|
61
|
+
for (var i = 0; i<n; i++) {
|
|
62
|
+
lc1 += in[i] * e2;
|
|
63
|
+
e2 = e2 + e2;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
lc1 ==> out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
template Bits2Num_strict() {
|
|
70
|
+
signal input in[254];
|
|
71
|
+
signal output out;
|
|
72
|
+
|
|
73
|
+
component aliasCheck = AliasCheck();
|
|
74
|
+
component b2n = Bits2Num(254);
|
|
75
|
+
|
|
76
|
+
for (var i=0; i<254; i++) {
|
|
77
|
+
in[i] ==> b2n.in[i];
|
|
78
|
+
in[i] ==> aliasCheck.in[i];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
b2n.out ==> out;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
template Num2BitsNeg(n) {
|
|
85
|
+
signal input in;
|
|
86
|
+
signal output out[n];
|
|
87
|
+
var lc1=0;
|
|
88
|
+
|
|
89
|
+
component isZero;
|
|
90
|
+
|
|
91
|
+
isZero = IsZero();
|
|
92
|
+
|
|
93
|
+
var neg = n == 0 ? 0 : 2**n - in;
|
|
94
|
+
|
|
95
|
+
for (var i = 0; i<n; i++) {
|
|
96
|
+
out[i] <-- (neg >> i) & 1;
|
|
97
|
+
out[i] * (out[i] -1 ) === 0;
|
|
98
|
+
lc1 += out[i] * 2**i;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
in ==> isZero.in;
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
lc1 + isZero.out * 2**n === 2**n - in;
|
|
106
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
pragma circom 2.0.0;
|
|
20
|
+
|
|
21
|
+
include "bitify.circom";
|
|
22
|
+
include "binsum.circom";
|
|
23
|
+
|
|
24
|
+
template IsZero() {
|
|
25
|
+
signal input in;
|
|
26
|
+
signal output out;
|
|
27
|
+
|
|
28
|
+
signal inv;
|
|
29
|
+
|
|
30
|
+
inv <-- in!=0 ? 1/in : 0;
|
|
31
|
+
|
|
32
|
+
out <== -in*inv +1;
|
|
33
|
+
in*out === 0;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
template IsEqual() {
|
|
38
|
+
signal input in[2];
|
|
39
|
+
signal output out;
|
|
40
|
+
|
|
41
|
+
component isz = IsZero();
|
|
42
|
+
|
|
43
|
+
in[1] - in[0] ==> isz.in;
|
|
44
|
+
|
|
45
|
+
isz.out ==> out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
template ForceEqualIfEnabled() {
|
|
49
|
+
signal input enabled;
|
|
50
|
+
signal input in[2];
|
|
51
|
+
|
|
52
|
+
component isz = IsZero();
|
|
53
|
+
|
|
54
|
+
in[1] - in[0] ==> isz.in;
|
|
55
|
+
|
|
56
|
+
(1 - isz.out)*enabled === 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/*
|
|
60
|
+
// N is the number of bits the input have.
|
|
61
|
+
// The MSF is the sign bit.
|
|
62
|
+
template LessThan(n) {
|
|
63
|
+
signal input in[2];
|
|
64
|
+
signal output out;
|
|
65
|
+
|
|
66
|
+
component num2Bits0;
|
|
67
|
+
component num2Bits1;
|
|
68
|
+
|
|
69
|
+
component adder;
|
|
70
|
+
|
|
71
|
+
adder = BinSum(n, 2);
|
|
72
|
+
|
|
73
|
+
num2Bits0 = Num2Bits(n);
|
|
74
|
+
num2Bits1 = Num2BitsNeg(n);
|
|
75
|
+
|
|
76
|
+
in[0] ==> num2Bits0.in;
|
|
77
|
+
in[1] ==> num2Bits1.in;
|
|
78
|
+
|
|
79
|
+
var i;
|
|
80
|
+
for (i=0;i<n;i++) {
|
|
81
|
+
num2Bits0.out[i] ==> adder.in[0][i];
|
|
82
|
+
num2Bits1.out[i] ==> adder.in[1][i];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
adder.out[n-1] ==> out;
|
|
86
|
+
}
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
template LessThan(n) {
|
|
90
|
+
assert(n <= 252);
|
|
91
|
+
signal input in[2];
|
|
92
|
+
signal output out;
|
|
93
|
+
|
|
94
|
+
component n2b = Num2Bits(n+1);
|
|
95
|
+
|
|
96
|
+
n2b.in <== in[0]+ (1<<n) - in[1];
|
|
97
|
+
|
|
98
|
+
out <== 1-n2b.out[n];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
// N is the number of bits the input have.
|
|
104
|
+
// The MSF is the sign bit.
|
|
105
|
+
template LessEqThan(n) {
|
|
106
|
+
signal input in[2];
|
|
107
|
+
signal output out;
|
|
108
|
+
|
|
109
|
+
component lt = LessThan(n);
|
|
110
|
+
|
|
111
|
+
lt.in[0] <== in[0];
|
|
112
|
+
lt.in[1] <== in[1]+1;
|
|
113
|
+
lt.out ==> out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// N is the number of bits the input have.
|
|
117
|
+
// The MSF is the sign bit.
|
|
118
|
+
template GreaterThan(n) {
|
|
119
|
+
signal input in[2];
|
|
120
|
+
signal output out;
|
|
121
|
+
|
|
122
|
+
component lt = LessThan(n);
|
|
123
|
+
|
|
124
|
+
lt.in[0] <== in[1];
|
|
125
|
+
lt.in[1] <== in[0];
|
|
126
|
+
lt.out ==> out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// N is the number of bits the input have.
|
|
130
|
+
// The MSF is the sign bit.
|
|
131
|
+
template GreaterEqThan(n) {
|
|
132
|
+
signal input in[2];
|
|
133
|
+
signal output out;
|
|
134
|
+
|
|
135
|
+
component lt = LessThan(n);
|
|
136
|
+
|
|
137
|
+
lt.in[0] <== in[1];
|
|
138
|
+
lt.in[1] <== in[0]+1;
|
|
139
|
+
lt.out ==> out;
|
|
140
|
+
}
|
|
141
|
+
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 0KIMS association.
|
|
3
|
+
|
|
4
|
+
This file is part of circom (Zero Knowledge Circuit Compiler).
|
|
5
|
+
|
|
6
|
+
circom is a free software: you can redistribute it and/or modify it
|
|
7
|
+
under the terms of the GNU General Public License as published by
|
|
8
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+
(at your option) any later version.
|
|
10
|
+
|
|
11
|
+
circom is distributed in the hope that it will be useful, but WITHOUT
|
|
12
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
13
|
+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
|
14
|
+
License for more details.
|
|
15
|
+
|
|
16
|
+
You should have received a copy of the GNU General Public License
|
|
17
|
+
along with circom. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
*/
|
|
19
|
+
pragma circom 2.0.0;
|
|
20
|
+
|
|
21
|
+
include "bitify.circom";
|
|
22
|
+
|
|
23
|
+
// Returns 1 if in (in binary) > ct
|
|
24
|
+
|
|
25
|
+
template CompConstant(ct) {
|
|
26
|
+
signal input in[254];
|
|
27
|
+
signal output out;
|
|
28
|
+
|
|
29
|
+
signal parts[127];
|
|
30
|
+
signal sout;
|
|
31
|
+
|
|
32
|
+
var clsb;
|
|
33
|
+
var cmsb;
|
|
34
|
+
var slsb;
|
|
35
|
+
var smsb;
|
|
36
|
+
|
|
37
|
+
var sum=0;
|
|
38
|
+
|
|
39
|
+
var b = (1 << 128) -1;
|
|
40
|
+
var a = 1;
|
|
41
|
+
var e = 1;
|
|
42
|
+
var i;
|
|
43
|
+
|
|
44
|
+
for (i=0;i<127; i++) {
|
|
45
|
+
clsb = (ct >> (i*2)) & 1;
|
|
46
|
+
cmsb = (ct >> (i*2+1)) & 1;
|
|
47
|
+
slsb = in[i*2];
|
|
48
|
+
smsb = in[i*2+1];
|
|
49
|
+
|
|
50
|
+
if ((cmsb==0)&&(clsb==0)) {
|
|
51
|
+
parts[i] <== -b*smsb*slsb + b*smsb + b*slsb;
|
|
52
|
+
} else if ((cmsb==0)&&(clsb==1)) {
|
|
53
|
+
parts[i] <== a*smsb*slsb - a*slsb + b*smsb - a*smsb + a;
|
|
54
|
+
} else if ((cmsb==1)&&(clsb==0)) {
|
|
55
|
+
parts[i] <== b*smsb*slsb - a*smsb + a;
|
|
56
|
+
} else {
|
|
57
|
+
parts[i] <== -a*smsb*slsb + a;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
sum = sum + parts[i];
|
|
61
|
+
|
|
62
|
+
b = b -e;
|
|
63
|
+
a = a +e;
|
|
64
|
+
e = e*2;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
sout <== sum;
|
|
68
|
+
|
|
69
|
+
component num2bits = Num2Bits(135);
|
|
70
|
+
|
|
71
|
+
num2bits.in <== sout;
|
|
72
|
+
|
|
73
|
+
out <== num2bits.out[127];
|
|
74
|
+
}
|