-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathposeidon.circom
More file actions
279 lines (232 loc) · 7.61 KB
/
Copy pathposeidon.circom
File metadata and controls
279 lines (232 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Poseidon adapted for ECDH symmetric encryption
Largely copied from @kohweijie's https://github.com/iden3/circomlib/pull/60
Except PoseidonEncrypt(), a wrapper around PoseidonDecrypt()
Not audited, no guarantees
*/
pragma circom 2.0.3;
include "poseidon_constants.circom";
include "calculateTotal.circom";
include "../../node_modules/circomlib/circuits/comparators.circom";
template Sigma() {
signal input in;
signal output out;
signal in2;
signal in4;
in2 <== in*in;
in4 <== in2*in2;
out <== in4*in;
}
template Ark(t, C, r) {
signal input in[t];
signal output out[t];
for (var i=0; i<t; i++) {
out[i] <== in[i] + C[i + r];
}
}
template Mix(t, M) {
signal input in[t];
signal output out[t];
var lc;
for (var i=0; i<t; i++) {
lc = 0;
for (var j=0; j<t; j++) {
lc += M[i][j]*in[j];
}
out[i] <== lc;
}
}
template Poseidon(nInputs) {
signal input inputs[nInputs];
signal output out;
component strategy = PoseidonPerm(nInputs + 1);
strategy.inputs[0] <== 0;
for (var i = 0; i < nInputs; i ++) {
strategy.inputs[i + 1] <== inputs[i];
}
out <== strategy.out[0];
}
template PoseidonPerm(t) {
// Using recommended parameters from whitepaper https://eprint.iacr.org/2019/458.pdf (table 2, table 8)
// Generated by https://extgit.iaik.tugraz.at/krypto/hadeshash/-/blob/master/code/calc_round_numbers.py
// And rounded up to nearest integer that divides by t
var N_ROUNDS_P[16] = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
var nRoundsF = 8;
var nRoundsP = N_ROUNDS_P[t - 2];
var C[t*(nRoundsF + nRoundsP)] = POSEIDON_C(t);
var M[t][t] = POSEIDON_M(t);
signal input inputs[t];
signal output out[t];
component ark[nRoundsF + nRoundsP];
component sigmaF[nRoundsF][t];
component sigmaP[nRoundsP];
component mix[nRoundsF + nRoundsP];
var k;
for (var i=0; i<nRoundsF + nRoundsP; i++) {
ark[i] = Ark(t, C, t*i);
for (var j=0; j<t; j++) {
if (i==0) {
ark[i].in[j] <== inputs[j];
} else {
ark[i].in[j] <== mix[i-1].out[j];
}
}
if (i < nRoundsF/2 || i >= nRoundsP + nRoundsF/2) {
k = i < nRoundsF/2 ? i : i - nRoundsP;
mix[i] = Mix(t, M);
for (var j=0; j<t; j++) {
sigmaF[k][j] = Sigma();
sigmaF[k][j].in <== ark[i].out[j];
mix[i].in[j] <== sigmaF[k][j].out;
}
} else {
k = i - nRoundsF/2;
mix[i] = Mix(t, M);
sigmaP[k] = Sigma();
sigmaP[k].in <== ark[i].out[0];
mix[i].in[0] <== sigmaP[k].out;
for (var j=1; j<t; j++) {
mix[i].in[j] <== ark[i].out[j];
}
}
}
// Output the final state.
for (var i = 0; i < t; i ++) {
out[i] <== mix[nRoundsF + nRoundsP -1].out[i];
}
}
// Modified:
// Checks ciphertext is correctly generated using input values, returns 1 if true
// Note to self: message length (l) is always fixed in my use case
template PoseidonEncryptCheck(l) {
var decryptedLength = l;
while (decryptedLength % 3 != 0) {
decryptedLength += 1;
}
// e.g. l = 4; decryptedLength = 6
// public inputs
signal input nonce;
signal input ciphertext[decryptedLength+1];
// private inputs, where hash(input) are public
signal input message[l];
signal input key[2];
component pd = PoseidonDecrypt(l);
pd.nonce <== nonce;
pd.key[0] <== key[0];
pd.key[1] <== key[1];
for (var i = 0; i < decryptedLength + 1 ; i++) {
pd.ciphertext[i] <== ciphertext[i];
}
component calcTotal = CalculateTotal(l);
component eqs[l];
for (var i = 0; i < l; i++) {
eqs[i] = IsEqual();
eqs[i].in[0] <== message[i];
eqs[i].in[1] <== pd.decrypted[i];
calcTotal.in[i] <== eqs[i].out;
}
component o = IsEqual();
o.in[0] <== calcTotal.out;
o.in[1] <== l;
// Returns 1 or 0
signal output out;
out <== o.out;
}
template PoseidonDecrypt(l) {
// TODO refactor this into a function()
var decryptedLength = l;
while (decryptedLength % 3 != 0) {
decryptedLength += 1;
}
// e.g. if l == 4, decryptedLength == 6
signal input ciphertext[decryptedLength + 1];
signal input nonce;
signal input key[2];
signal output decrypted[decryptedLength];
component iterations = PoseidonDecryptIterations(l);
iterations.nonce <== nonce;
iterations.key[0] <== key[0];
iterations.key[1] <== key[1];
for (var i = 0; i < decryptedLength + 1; i ++) {
iterations.ciphertext[i] <== ciphertext[i];
}
// Check the last ciphertext element
iterations.decryptedLast === ciphertext[decryptedLength];
for (var i = 0; i < decryptedLength; i ++) {
decrypted[i] <== iterations.decrypted[i];
}
// If length > 3, check if the last (3 - (l mod 3)) elements of the message
// are 0
if (l % 3 > 0) {
if (l % 3 == 2) {
decrypted[decryptedLength - 1] === 0;
} else if (l % 3 == 2) {
decrypted[decryptedLength - 1] === 0;
decrypted[decryptedLength - 2] === 0;
}
}
}
// Decrypt a ciphertext without checking if the last ciphertext element or
// whether the last 3 - (l mod 3) elements are 0. This is useful in
// applications where you do not want an invalid decryption to prevent the
// generation of a proof.
template PoseidonDecryptWithoutCheck(l) {
var decryptedLength = l;
while (decryptedLength % 3 != 0) {
decryptedLength += 1;
}
// e.g. if l == 4, decryptedLength == 6
signal input ciphertext[decryptedLength + 1];
signal input nonce;
signal input key[2];
signal output decrypted[decryptedLength];
component iterations = PoseidonDecryptIterations(l);
iterations.nonce <== nonce;
iterations.key[0] <== key[0];
iterations.key[1] <== key[1];
for (var i = 0; i < decryptedLength + 1; i ++) {
iterations.ciphertext[i] <== ciphertext[i];
}
for (var i = 0; i < decryptedLength; i ++) {
decrypted[i] <== iterations.decrypted[i];
}
}
template PoseidonDecryptIterations(l) {
var decryptedLength = l;
while (decryptedLength % 3 != 0) {
decryptedLength += 1;
}
// e.g. if l == 4, decryptedLength == 6
signal input ciphertext[decryptedLength + 1];
signal input nonce;
signal input key[2];
signal output decrypted[decryptedLength];
signal output decryptedLast;
var two128 = 2 ** 128;
// The nonce must be less than 2 ^ 128
component lt = LessThan(252);
lt.in[0] <== nonce;
lt.in[1] <== two128;
lt.out === 1;
var n = (decryptedLength + 1) \ 3;
component strategies[n + 1];
// Iterate Poseidon on the initial state
strategies[0] = PoseidonPerm(4);
strategies[0].inputs[0] <== 0;
strategies[0].inputs[1] <== key[0];
strategies[0].inputs[2] <== key[1];
strategies[0].inputs[3] <== nonce + (l * two128);
for (var i = 0; i < n; i ++) {
// Release three elements of the message
for (var j = 0; j < 3; j ++) {
decrypted[i * 3 + j] <== ciphertext[i * 3 + j] - strategies[i].out[j + 1];
}
// Iterate Poseidon on the state
strategies[i + 1] = PoseidonPerm(4);
strategies[i + 1].inputs[0] <== strategies[i].out[0];
for (var j = 0; j < 3; j ++) {
strategies[i + 1].inputs[j + 1] <== ciphertext[i * 3 + j];
}
}
decryptedLast <== strategies[n].out[1];
}