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,261 @@
|
|
|
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
|
+
SMTProcessor: Sparse Merkle Tree processor is a component to verify an insert/update/delete elements
|
|
23
|
+
into the Sparse Merkle tree.
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
Insert to an empty leaf
|
|
27
|
+
=======================
|
|
28
|
+
|
|
29
|
+
STATE OLD STATE NEW STATE
|
|
30
|
+
===== ========= =========
|
|
31
|
+
|
|
32
|
+
oldRoot newRoot
|
|
33
|
+
▲ ▲
|
|
34
|
+
│ │
|
|
35
|
+
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
|
|
36
|
+
top │Sibling├────▶┃ Hash ┃◀─┐ │Sibling├────▶┃ Hash ┃◀─┐
|
|
37
|
+
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
|
|
38
|
+
│ │
|
|
39
|
+
│ │
|
|
40
|
+
┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓ ┌───────┐
|
|
41
|
+
top ┌─────▶┃ Hash ┃◀──┤Sibling│ ┌─────▶┃ Hash ┃◀──┤Sibling│
|
|
42
|
+
│ ┗━━━━━━━┛ └───────┘ │ ┗━━━━━━━┛ └───────┘
|
|
43
|
+
│ │
|
|
44
|
+
│ │
|
|
45
|
+
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
|
|
46
|
+
top │Sibling├──▶┃ Hash ┃◀─────┐ │Sibling├──▶┃ Hash ┃◀─────┐
|
|
47
|
+
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
|
|
48
|
+
│ │
|
|
49
|
+
│ │
|
|
50
|
+
┌────┴────┐ ┌────┴────┐
|
|
51
|
+
old0 │ 0 │ │New1Leaf │
|
|
52
|
+
└─────────┘ └─────────┘
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
┏━━━━━━━┓ ┏━━━━━━━┓
|
|
56
|
+
na ┃ Hash ┃ ┃ Hash ┃
|
|
57
|
+
┗━━━━━━━┛ ┗━━━━━━━┛
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
┏━━━━━━━┓ ┏━━━━━━━┓
|
|
61
|
+
na ┃ Hash ┃ ┃ Hash ┃
|
|
62
|
+
┗━━━━━━━┛ ┗━━━━━━━┛
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
Insert to a used leaf.
|
|
67
|
+
=====================
|
|
68
|
+
|
|
69
|
+
STATE OLD STATE NEW STATE
|
|
70
|
+
===== ========= =========
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
oldRoot newRoot
|
|
74
|
+
▲ ▲
|
|
75
|
+
│ │
|
|
76
|
+
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
|
|
77
|
+
top │Sibling├────▶┃ Hash ┃◀─┐ │Sibling├────▶┃ Hash ┃◀─┐
|
|
78
|
+
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
|
|
79
|
+
│ │
|
|
80
|
+
│ │
|
|
81
|
+
┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓ ┌───────┐
|
|
82
|
+
top ┌─────▶┃ Hash ┃◀──┤Sibling│ ┌─────▶┃ Hash ┃◀──┤Sibling│
|
|
83
|
+
│ ┗━━━━━━━┛ └───────┘ │ ┗━━━━━━━┛ └───────┘
|
|
84
|
+
│ │
|
|
85
|
+
│ │
|
|
86
|
+
┌───────┐ ┏━━━┻━━━┓ ┌───────┐ ┏━━━┻━━━┓
|
|
87
|
+
top │Sibling├──▶┃ Hash ┃◀─────┐ │Sibling├──▶┃ Hash ┃◀─────┐
|
|
88
|
+
└───────┘ ┗━━━━━━━┛ │ └───────┘ ┗━━━━━━━┛ │
|
|
89
|
+
│ │
|
|
90
|
+
│ │
|
|
91
|
+
┌────┴────┐ ┏━━━┻━━━┓ ┌───────┐
|
|
92
|
+
bot │Old1Leaf │ ┌─────▶┃ Hash ┃◀──┼─ 0 │
|
|
93
|
+
└─────────┘ │ ┗━━━━━━━┛ └───────┘
|
|
94
|
+
│
|
|
95
|
+
│
|
|
96
|
+
┏━━━━━━━┓ ┌───────┐ ┏━━━┻━━━┓
|
|
97
|
+
bot ┃ Hash ┃ │ 0 ─┼──▶┃ Hash ┃◀─────┐
|
|
98
|
+
┗━━━━━━━┛ └───────┘ ┗━━━━━━━┛ │
|
|
99
|
+
│
|
|
100
|
+
│
|
|
101
|
+
┏━━━━━━━┓ ┏━━━┻━━━┓ ┌───────┐
|
|
102
|
+
bot ┃ Hash ┃ ┌─────▶┃ Hash ┃◀──│ 0 │
|
|
103
|
+
┗━━━━━━━┛ │ ┗━━━━━━━┛ └───────┘
|
|
104
|
+
│
|
|
105
|
+
│
|
|
106
|
+
┏━━━━━━━┓ ┌─────────┐ ┏━━━┻━━━┓ ┌─────────┐
|
|
107
|
+
new1 ┃ Hash ┃ │Old1Leaf ├──▶┃ Hash ┃◀──│New1Leaf │
|
|
108
|
+
┗━━━━━━━┛ └─────────┘ ┗━━━━━━━┛ └─────────┘
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
┏━━━━━━━┓ ┏━━━━━━━┓
|
|
112
|
+
na ┃ Hash ┃ ┃ Hash ┃
|
|
113
|
+
┗━━━━━━━┛ ┗━━━━━━━┛
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
┏━━━━━━━┓ ┏━━━━━━━┓
|
|
117
|
+
na ┃ Hash ┃ ┃ Hash ┃
|
|
118
|
+
┗━━━━━━━┛ ┗━━━━━━━┛
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
Fnction
|
|
122
|
+
fnc[0] fnc[1]
|
|
123
|
+
0 0 NOP
|
|
124
|
+
0 1 UPDATE
|
|
125
|
+
1 0 INSERT
|
|
126
|
+
1 1 DELETE
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
***************************************************************************************************/
|
|
130
|
+
pragma circom 2.0.0;
|
|
131
|
+
|
|
132
|
+
include "../gates.circom";
|
|
133
|
+
include "../bitify.circom";
|
|
134
|
+
include "../comparators.circom";
|
|
135
|
+
include "../switcher.circom";
|
|
136
|
+
include "smtlevins.circom";
|
|
137
|
+
include "smtprocessorlevel.circom";
|
|
138
|
+
include "smtprocessorsm.circom";
|
|
139
|
+
include "smthash_poseidon.circom";
|
|
140
|
+
|
|
141
|
+
template SMTProcessor(nLevels) {
|
|
142
|
+
signal input oldRoot;
|
|
143
|
+
signal output newRoot;
|
|
144
|
+
signal input siblings[nLevels];
|
|
145
|
+
signal input oldKey;
|
|
146
|
+
signal input oldValue;
|
|
147
|
+
signal input isOld0;
|
|
148
|
+
signal input newKey;
|
|
149
|
+
signal input newValue;
|
|
150
|
+
signal input fnc[2];
|
|
151
|
+
|
|
152
|
+
signal enabled;
|
|
153
|
+
|
|
154
|
+
var i;
|
|
155
|
+
|
|
156
|
+
enabled <== fnc[0] + fnc[1] - fnc[0]*fnc[1];
|
|
157
|
+
|
|
158
|
+
component hash1Old = SMTHash1();
|
|
159
|
+
hash1Old.key <== oldKey;
|
|
160
|
+
hash1Old.value <== oldValue;
|
|
161
|
+
|
|
162
|
+
component hash1New = SMTHash1();
|
|
163
|
+
hash1New.key <== newKey;
|
|
164
|
+
hash1New.value <== newValue;
|
|
165
|
+
|
|
166
|
+
component n2bOld = Num2Bits_strict();
|
|
167
|
+
component n2bNew = Num2Bits_strict();
|
|
168
|
+
|
|
169
|
+
n2bOld.in <== oldKey;
|
|
170
|
+
n2bNew.in <== newKey;
|
|
171
|
+
|
|
172
|
+
component smtLevIns = SMTLevIns(nLevels);
|
|
173
|
+
for (i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
|
|
174
|
+
smtLevIns.enabled <== enabled;
|
|
175
|
+
|
|
176
|
+
component xors[nLevels];
|
|
177
|
+
for (i=0; i<nLevels; i++) {
|
|
178
|
+
xors[i] = XOR();
|
|
179
|
+
xors[i].a <== n2bOld.out[i];
|
|
180
|
+
xors[i].b <== n2bNew.out[i];
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
component sm[nLevels];
|
|
184
|
+
for (i=0; i<nLevels; i++) {
|
|
185
|
+
sm[i] = SMTProcessorSM();
|
|
186
|
+
if (i==0) {
|
|
187
|
+
sm[i].prev_top <== enabled;
|
|
188
|
+
sm[i].prev_old0 <== 0;
|
|
189
|
+
sm[i].prev_bot <== 0;
|
|
190
|
+
sm[i].prev_new1 <== 0;
|
|
191
|
+
sm[i].prev_na <== 1-enabled;
|
|
192
|
+
sm[i].prev_upd <== 0;
|
|
193
|
+
} else {
|
|
194
|
+
sm[i].prev_top <== sm[i-1].st_top;
|
|
195
|
+
sm[i].prev_old0 <== sm[i-1].st_old0;
|
|
196
|
+
sm[i].prev_bot <== sm[i-1].st_bot;
|
|
197
|
+
sm[i].prev_new1 <== sm[i-1].st_new1;
|
|
198
|
+
sm[i].prev_na <== sm[i-1].st_na;
|
|
199
|
+
sm[i].prev_upd <== sm[i-1].st_upd;
|
|
200
|
+
}
|
|
201
|
+
sm[i].is0 <== isOld0;
|
|
202
|
+
sm[i].xor <== xors[i].out;
|
|
203
|
+
sm[i].fnc[0] <== fnc[0];
|
|
204
|
+
sm[i].fnc[1] <== fnc[1];
|
|
205
|
+
sm[i].levIns <== smtLevIns.levIns[i];
|
|
206
|
+
}
|
|
207
|
+
sm[nLevels-1].st_na + sm[nLevels-1].st_new1 + sm[nLevels-1].st_old0 +sm[nLevels-1].st_upd === 1;
|
|
208
|
+
|
|
209
|
+
component levels[nLevels];
|
|
210
|
+
for (i=nLevels-1; i != -1; i--) {
|
|
211
|
+
levels[i] = SMTProcessorLevel();
|
|
212
|
+
|
|
213
|
+
levels[i].st_top <== sm[i].st_top;
|
|
214
|
+
levels[i].st_old0 <== sm[i].st_old0;
|
|
215
|
+
levels[i].st_bot <== sm[i].st_bot;
|
|
216
|
+
levels[i].st_new1 <== sm[i].st_new1;
|
|
217
|
+
levels[i].st_na <== sm[i].st_na;
|
|
218
|
+
levels[i].st_upd <== sm[i].st_upd;
|
|
219
|
+
|
|
220
|
+
levels[i].sibling <== siblings[i];
|
|
221
|
+
levels[i].old1leaf <== hash1Old.out;
|
|
222
|
+
levels[i].new1leaf <== hash1New.out;
|
|
223
|
+
|
|
224
|
+
levels[i].newlrbit <== n2bNew.out[i];
|
|
225
|
+
if (i==nLevels-1) {
|
|
226
|
+
levels[i].oldChild <== 0;
|
|
227
|
+
levels[i].newChild <== 0;
|
|
228
|
+
} else {
|
|
229
|
+
levels[i].oldChild <== levels[i+1].oldRoot;
|
|
230
|
+
levels[i].newChild <== levels[i+1].newRoot;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
component topSwitcher = Switcher();
|
|
235
|
+
|
|
236
|
+
topSwitcher.sel <== fnc[0]*fnc[1];
|
|
237
|
+
topSwitcher.L <== levels[0].oldRoot;
|
|
238
|
+
topSwitcher.R <== levels[0].newRoot;
|
|
239
|
+
|
|
240
|
+
component checkOldInput = ForceEqualIfEnabled();
|
|
241
|
+
checkOldInput.enabled <== enabled;
|
|
242
|
+
checkOldInput.in[0] <== oldRoot;
|
|
243
|
+
checkOldInput.in[1] <== topSwitcher.outL;
|
|
244
|
+
|
|
245
|
+
newRoot <== enabled * (topSwitcher.outR - oldRoot) + oldRoot;
|
|
246
|
+
|
|
247
|
+
// topSwitcher.outL === oldRoot*enabled;
|
|
248
|
+
// topSwitcher.outR === newRoot*enabled;
|
|
249
|
+
|
|
250
|
+
// Ckeck keys are equal if updating
|
|
251
|
+
component areKeyEquals = IsEqual();
|
|
252
|
+
areKeyEquals.in[0] <== oldKey;
|
|
253
|
+
areKeyEquals.in[1] <== newKey;
|
|
254
|
+
|
|
255
|
+
component keysOk = MultiAND(3);
|
|
256
|
+
keysOk.in[0] <== 1-fnc[0];
|
|
257
|
+
keysOk.in[1] <== fnc[1];
|
|
258
|
+
keysOk.in[2] <== 1-areKeyEquals.out;
|
|
259
|
+
|
|
260
|
+
keysOk.out === 0;
|
|
261
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
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
|
+
SMTProcessorLevel
|
|
23
|
+
|
|
24
|
+
This circuit has 2 hash
|
|
25
|
+
|
|
26
|
+
Outputs according to the state.
|
|
27
|
+
|
|
28
|
+
State oldRoot newRoot
|
|
29
|
+
===== ======= =======
|
|
30
|
+
top H'(oldChild, sibling) H'(newChild, sibling)
|
|
31
|
+
old0 0 new1leaf
|
|
32
|
+
bot old1leaf H'(newChild, 0)
|
|
33
|
+
new1 old1leaf H'(new1leaf, old1leaf)
|
|
34
|
+
na 0 0
|
|
35
|
+
|
|
36
|
+
upd old1leaf new1leaf
|
|
37
|
+
|
|
38
|
+
H' is the Hash function with the inputs shifted acordingly.
|
|
39
|
+
|
|
40
|
+
*****/
|
|
41
|
+
pragma circom 2.0.0;
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
template SMTProcessorLevel() {
|
|
45
|
+
signal input st_top;
|
|
46
|
+
signal input st_old0;
|
|
47
|
+
signal input st_bot;
|
|
48
|
+
signal input st_new1;
|
|
49
|
+
signal input st_na;
|
|
50
|
+
signal input st_upd;
|
|
51
|
+
|
|
52
|
+
signal output oldRoot;
|
|
53
|
+
signal output newRoot;
|
|
54
|
+
signal input sibling;
|
|
55
|
+
signal input old1leaf;
|
|
56
|
+
signal input new1leaf;
|
|
57
|
+
signal input newlrbit;
|
|
58
|
+
signal input oldChild;
|
|
59
|
+
signal input newChild;
|
|
60
|
+
|
|
61
|
+
signal aux[4];
|
|
62
|
+
|
|
63
|
+
component oldProofHash = SMTHash2();
|
|
64
|
+
component newProofHash = SMTHash2();
|
|
65
|
+
|
|
66
|
+
component oldSwitcher = Switcher();
|
|
67
|
+
component newSwitcher = Switcher();
|
|
68
|
+
|
|
69
|
+
// Old side
|
|
70
|
+
|
|
71
|
+
oldSwitcher.L <== oldChild;
|
|
72
|
+
oldSwitcher.R <== sibling;
|
|
73
|
+
|
|
74
|
+
oldSwitcher.sel <== newlrbit;
|
|
75
|
+
oldProofHash.L <== oldSwitcher.outL;
|
|
76
|
+
oldProofHash.R <== oldSwitcher.outR;
|
|
77
|
+
|
|
78
|
+
aux[0] <== old1leaf * (st_bot + st_new1 + st_upd);
|
|
79
|
+
oldRoot <== aux[0] + oldProofHash.out * st_top;
|
|
80
|
+
|
|
81
|
+
// New side
|
|
82
|
+
|
|
83
|
+
aux[1] <== newChild * ( st_top + st_bot);
|
|
84
|
+
newSwitcher.L <== aux[1] + new1leaf*st_new1;
|
|
85
|
+
|
|
86
|
+
aux[2] <== sibling*st_top;
|
|
87
|
+
newSwitcher.R <== aux[2] + old1leaf*st_new1;
|
|
88
|
+
|
|
89
|
+
newSwitcher.sel <== newlrbit;
|
|
90
|
+
newProofHash.L <== newSwitcher.outL;
|
|
91
|
+
newProofHash.R <== newSwitcher.outR;
|
|
92
|
+
|
|
93
|
+
aux[3] <== newProofHash.out * (st_top + st_bot + st_new1);
|
|
94
|
+
newRoot <== aux[3] + new1leaf * (st_old0 + st_upd);
|
|
95
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
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
|
+
Each level on a SMTProcessor has a state.
|
|
22
|
+
|
|
23
|
+
The state of the level depends on the state of te botom level and on `xor` and
|
|
24
|
+
`is0` signals.
|
|
25
|
+
|
|
26
|
+
`isOldLev` 1 when is the level where oldLeaf is.
|
|
27
|
+
|
|
28
|
+
`xor` signal is 0 if the index bit at the current level is the same in the old
|
|
29
|
+
and the new index, and 1 if it is different.
|
|
30
|
+
|
|
31
|
+
`is0` signal, is 1 if we are inserting/deleting in an empty leaf and 0 if we
|
|
32
|
+
are inserting/deleting in a leaf that contains an element.
|
|
33
|
+
|
|
34
|
+
The states are:
|
|
35
|
+
|
|
36
|
+
top: While the index bits of the old and new insex in the top level is the same, whe are in the top state.
|
|
37
|
+
old0: When the we reach insert level, we go to old0 state
|
|
38
|
+
if `is0`=1.
|
|
39
|
+
btn: Once in insert level and `is0` =0 we go to btn or new1 level if xor=1
|
|
40
|
+
new1: This level is reached when xor=1. Here is where we insert/delete the hash of the
|
|
41
|
+
old and the new trees with just one element.
|
|
42
|
+
na: Not appliable. After processing it, we go to the na level.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
Fnction
|
|
46
|
+
fnc[0] fnc[1]
|
|
47
|
+
0 0 NOP
|
|
48
|
+
0 1 UPDATE
|
|
49
|
+
1 0 INSERT
|
|
50
|
+
1 1 DELETE
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
###########
|
|
54
|
+
# #
|
|
55
|
+
┌────────────────────────────▶# upd #─────────────────────┐
|
|
56
|
+
│ ## ## │
|
|
57
|
+
│ ######### │
|
|
58
|
+
levIns=1 │ │
|
|
59
|
+
fnc[0]=0 │ │ any
|
|
60
|
+
│ │
|
|
61
|
+
│ │
|
|
62
|
+
│ │
|
|
63
|
+
│ ########### │
|
|
64
|
+
│ levIns=1 # # │
|
|
65
|
+
levIns=0 │ is0=1 ┌────────────▶# old0 #────────┐ │ any
|
|
66
|
+
┌─────┐ │ fnc[0]=1│ ## ## │ │ ┌──────┐
|
|
67
|
+
│ │ │ │ ######### │ any │ │ │
|
|
68
|
+
│ ▼ │ │ │ ▼ ▼ │
|
|
69
|
+
│ ########### │ │ ########### │
|
|
70
|
+
│ # # ────────────┘ └────────▶# #│
|
|
71
|
+
└──# top # # na #
|
|
72
|
+
## ## ───────────────────┐ levIns=1 ┌──▶## ##
|
|
73
|
+
######### │ is0=0 │ #########
|
|
74
|
+
│ │ fnc[0]=1 │
|
|
75
|
+
│ │ xor=1 ########### │ any
|
|
76
|
+
│ └──────────────────▶# # │
|
|
77
|
+
│ # new1 #──┘
|
|
78
|
+
│ ## ##
|
|
79
|
+
└────────────────────────────────┐ #########
|
|
80
|
+
levIns=1 │ ▲
|
|
81
|
+
is0=0 │ ┌─────┘
|
|
82
|
+
fnc[0]=1 │ ###########│ xor=1
|
|
83
|
+
xor=0 │ # #
|
|
84
|
+
▼# btn #
|
|
85
|
+
## ##
|
|
86
|
+
#########◀───────┐
|
|
87
|
+
│ │
|
|
88
|
+
│ │
|
|
89
|
+
└────────────┘
|
|
90
|
+
xor=0
|
|
91
|
+
|
|
92
|
+
***************************************************************************************************/
|
|
93
|
+
pragma circom 2.0.0;
|
|
94
|
+
|
|
95
|
+
template SMTProcessorSM() {
|
|
96
|
+
signal input xor;
|
|
97
|
+
signal input is0;
|
|
98
|
+
signal input levIns;
|
|
99
|
+
signal input fnc[2];
|
|
100
|
+
|
|
101
|
+
signal input prev_top;
|
|
102
|
+
signal input prev_old0;
|
|
103
|
+
signal input prev_bot;
|
|
104
|
+
signal input prev_new1;
|
|
105
|
+
signal input prev_na;
|
|
106
|
+
signal input prev_upd;
|
|
107
|
+
|
|
108
|
+
signal output st_top;
|
|
109
|
+
signal output st_old0;
|
|
110
|
+
signal output st_bot;
|
|
111
|
+
signal output st_new1;
|
|
112
|
+
signal output st_na;
|
|
113
|
+
signal output st_upd;
|
|
114
|
+
|
|
115
|
+
signal aux1;
|
|
116
|
+
signal aux2;
|
|
117
|
+
|
|
118
|
+
aux1 <== prev_top * levIns;
|
|
119
|
+
aux2 <== aux1*fnc[0]; // prev_top * levIns * fnc[0]
|
|
120
|
+
|
|
121
|
+
// st_top = prev_top*(1-levIns)
|
|
122
|
+
// = + prev_top
|
|
123
|
+
// - prev_top * levIns = aux1
|
|
124
|
+
|
|
125
|
+
st_top <== prev_top - aux1;
|
|
126
|
+
|
|
127
|
+
// st_old0 = prev_top * levIns * is0 * fnc[0]
|
|
128
|
+
// = + prev_top * levIns * is0 * fnc[0] = aux2 * is0
|
|
129
|
+
|
|
130
|
+
st_old0 <== aux2 * is0; // prev_top * levIns * is0 * fnc[0]
|
|
131
|
+
|
|
132
|
+
// st_new1 = prev_top * levIns * (1-is0)*fnc[0] * xor + prev_bot*xor =
|
|
133
|
+
// = + prev_top * levIns * fnc[0] * xor = aux2 * xor
|
|
134
|
+
// - prev_top * levIns * is0 * fnc[0] * xor = st_old0 * xor
|
|
135
|
+
// + prev_bot * xor = prev_bot * xor
|
|
136
|
+
|
|
137
|
+
st_new1 <== (aux2 - st_old0 + prev_bot)*xor;
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
// st_bot = prev_top * levIns * (1-is0)*fnc[0] * (1-xor) + prev_bot*(1-xor);
|
|
141
|
+
// = + prev_top * levIns * fnc[0]
|
|
142
|
+
// - prev_top * levIns * is0 * fnc[0]
|
|
143
|
+
// - prev_top * levIns * fnc[0] * xor
|
|
144
|
+
// + prev_top * levIns * is0 * fnc[0] * xor
|
|
145
|
+
// + prev_bot
|
|
146
|
+
// - prev_bot * xor
|
|
147
|
+
|
|
148
|
+
st_bot <== (1-xor) * (aux2 - st_old0 + prev_bot);
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
// st_upd = prev_top * (1-fnc[0]) *levIns;
|
|
152
|
+
// = + prev_top * levIns
|
|
153
|
+
// - prev_top * levIns * fnc[0]
|
|
154
|
+
|
|
155
|
+
st_upd <== aux1 - aux2;
|
|
156
|
+
|
|
157
|
+
// st_na = prev_new1 + prev_old0 + prev_na + prev_upd;
|
|
158
|
+
// = + prev_new1
|
|
159
|
+
// + prev_old0
|
|
160
|
+
// + prev_na
|
|
161
|
+
// + prev_upd
|
|
162
|
+
|
|
163
|
+
st_na <== prev_new1 + prev_old0 + prev_na + prev_upd;
|
|
164
|
+
|
|
165
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
SMTVerifier is a component to verify inclusion/exclusion of an element in the tree
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
fnc: 0 -> VERIFY INCLUSION
|
|
26
|
+
1 -> VERIFY NOT INCLUSION
|
|
27
|
+
|
|
28
|
+
*/
|
|
29
|
+
pragma circom 2.0.0;
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
include "../gates.circom";
|
|
33
|
+
include "../bitify.circom";
|
|
34
|
+
include "../comparators.circom";
|
|
35
|
+
include "../switcher.circom";
|
|
36
|
+
include "smtlevins.circom";
|
|
37
|
+
include "smtverifierlevel.circom";
|
|
38
|
+
include "smtverifiersm.circom";
|
|
39
|
+
include "smthash_poseidon.circom";
|
|
40
|
+
|
|
41
|
+
template SMTVerifier(nLevels) {
|
|
42
|
+
signal input enabled;
|
|
43
|
+
signal input root;
|
|
44
|
+
signal input siblings[nLevels];
|
|
45
|
+
signal input oldKey;
|
|
46
|
+
signal input oldValue;
|
|
47
|
+
signal input isOld0;
|
|
48
|
+
signal input key;
|
|
49
|
+
signal input value;
|
|
50
|
+
signal input fnc;
|
|
51
|
+
|
|
52
|
+
var i;
|
|
53
|
+
|
|
54
|
+
component hash1Old = SMTHash1();
|
|
55
|
+
hash1Old.key <== oldKey;
|
|
56
|
+
hash1Old.value <== oldValue;
|
|
57
|
+
|
|
58
|
+
component hash1New = SMTHash1();
|
|
59
|
+
hash1New.key <== key;
|
|
60
|
+
hash1New.value <== value;
|
|
61
|
+
|
|
62
|
+
component n2bOld = Num2Bits_strict();
|
|
63
|
+
component n2bNew = Num2Bits_strict();
|
|
64
|
+
|
|
65
|
+
n2bOld.in <== oldKey;
|
|
66
|
+
n2bNew.in <== key;
|
|
67
|
+
|
|
68
|
+
component smtLevIns = SMTLevIns(nLevels);
|
|
69
|
+
for (i=0; i<nLevels; i++) smtLevIns.siblings[i] <== siblings[i];
|
|
70
|
+
smtLevIns.enabled <== enabled;
|
|
71
|
+
|
|
72
|
+
component sm[nLevels];
|
|
73
|
+
for (i=0; i<nLevels; i++) {
|
|
74
|
+
sm[i] = SMTVerifierSM();
|
|
75
|
+
if (i==0) {
|
|
76
|
+
sm[i].prev_top <== enabled;
|
|
77
|
+
sm[i].prev_i0 <== 0;
|
|
78
|
+
sm[i].prev_inew <== 0;
|
|
79
|
+
sm[i].prev_iold <== 0;
|
|
80
|
+
sm[i].prev_na <== 1-enabled;
|
|
81
|
+
} else {
|
|
82
|
+
sm[i].prev_top <== sm[i-1].st_top;
|
|
83
|
+
sm[i].prev_i0 <== sm[i-1].st_i0;
|
|
84
|
+
sm[i].prev_inew <== sm[i-1].st_inew;
|
|
85
|
+
sm[i].prev_iold <== sm[i-1].st_iold;
|
|
86
|
+
sm[i].prev_na <== sm[i-1].st_na;
|
|
87
|
+
}
|
|
88
|
+
sm[i].is0 <== isOld0;
|
|
89
|
+
sm[i].fnc <== fnc;
|
|
90
|
+
sm[i].levIns <== smtLevIns.levIns[i];
|
|
91
|
+
}
|
|
92
|
+
sm[nLevels-1].st_na + sm[nLevels-1].st_iold + sm[nLevels-1].st_inew + sm[nLevels-1].st_i0 === 1;
|
|
93
|
+
|
|
94
|
+
component levels[nLevels];
|
|
95
|
+
for (i=nLevels-1; i != -1; i--) {
|
|
96
|
+
levels[i] = SMTVerifierLevel();
|
|
97
|
+
|
|
98
|
+
levels[i].st_top <== sm[i].st_top;
|
|
99
|
+
levels[i].st_i0 <== sm[i].st_i0;
|
|
100
|
+
levels[i].st_inew <== sm[i].st_inew;
|
|
101
|
+
levels[i].st_iold <== sm[i].st_iold;
|
|
102
|
+
levels[i].st_na <== sm[i].st_na;
|
|
103
|
+
|
|
104
|
+
levels[i].sibling <== siblings[i];
|
|
105
|
+
levels[i].old1leaf <== hash1Old.out;
|
|
106
|
+
levels[i].new1leaf <== hash1New.out;
|
|
107
|
+
|
|
108
|
+
levels[i].lrbit <== n2bNew.out[i];
|
|
109
|
+
if (i==nLevels-1) {
|
|
110
|
+
levels[i].child <== 0;
|
|
111
|
+
} else {
|
|
112
|
+
levels[i].child <== levels[i+1].root;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
// Check that if checking for non inclussuin and isOld0==0 then key!=old
|
|
118
|
+
component areKeyEquals = IsEqual();
|
|
119
|
+
areKeyEquals.in[0] <== oldKey;
|
|
120
|
+
areKeyEquals.in[1] <== key;
|
|
121
|
+
|
|
122
|
+
component keysOk = MultiAND(4);
|
|
123
|
+
keysOk.in[0] <== fnc;
|
|
124
|
+
keysOk.in[1] <== 1-isOld0;
|
|
125
|
+
keysOk.in[2] <== areKeyEquals.out;
|
|
126
|
+
keysOk.in[3] <== enabled;
|
|
127
|
+
|
|
128
|
+
keysOk.out === 0;
|
|
129
|
+
|
|
130
|
+
// Check the root
|
|
131
|
+
component checkRoot = ForceEqualIfEnabled();
|
|
132
|
+
checkRoot.enabled <== enabled;
|
|
133
|
+
checkRoot.in[0] <== levels[0].root;
|
|
134
|
+
checkRoot.in[1] <== root;
|
|
135
|
+
|
|
136
|
+
// levels[0].root === root;
|
|
137
|
+
|
|
138
|
+
}
|