forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbn254.nr
249 lines (195 loc) · 5.44 KB
/
bn254.nr
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
// Instantiations of Poseidon constants, permutations and sponge for prime field of the same order as BN254
mod perm;
mod consts;
use crate::hash::poseidon::PoseidonConfig;
use crate::hash::poseidon::apply_matrix;
// Optimised permutation for this particular field; uses hardcoded rf and rp values,
// which should agree with those in pos_conf.
fn permute<M,N,O>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O])
-> [Field; O] {
let PoseidonConfig {t, rf: config_rf, rp: config_rp, alpha, ark, mds} = pos_conf;
let rf = 8;
let rp = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68][state.len() - 2];
assert(t == state.len());
assert(rf == config_rf as Field);
assert(rp == config_rp as Field);
let mut count = 0;
// First half of full rounds
for _r in 0..rf/2 {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants
for i in 0..state.len() {
state[i] = state[i].pow_32(alpha);
}
state = apply_matrix(mds, state); // Apply MDS matrix
count = count + t;
}
// Partial rounds
for _r in 0..rp {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants
state[0] = state[0].pow_32(alpha);
state = apply_matrix(mds, state); // Apply MDS matrix
count = count + t;
}
// Second half of full rounds
for _r in 0..rf/2 {
for i in 0..state.len() {
state[i] = state[i] + ark[count + i];
} // Shift by round constants
for i in 0..state.len() {
state[i] = state[i].pow_32(alpha);
}
state = apply_matrix(mds, state); // Apply MDS matrix
count = count + t;
}
state
}
// Corresponding absorption.
fn absorb<M,N,O,P>(
pos_conf: PoseidonConfig<M, N>,
mut state: [Field; O], // Initial state; usually [0; O]
rate: comptime Field, // Rate
capacity: comptime Field, // Capacity; usually 1
msg: [Field; P] // Arbitrary length message
) -> [Field; O] {
assert(pos_conf.t == rate + capacity);
let mut i = 0;
for k in 0..msg.len() {
// Add current block to state
state[capacity + i] += msg[k];
i = i+1;
// Enough to absorb
if i == rate {
state = permute(pos_conf, state);
i = 0;
}
}
// If we have one more block to permute
if i != 0 {
state = permute(pos_conf, state);
}
state
}
// Variable-length Poseidon-128 sponge as suggested in second bullet point of §3 of https://eprint.iacr.org/2019/458.pdf
fn sponge<N>(msg: [Field; N]) -> Field {
absorb(consts::x5_5_config(), [0;5], 4, 1, msg)[1]
}
// Various instances of the Poseidon hash function
// Consistent with Circom's implementation
fn hash_1(input: [Field; 1]) -> Field {
let mut state = [0; 2];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_2(state)[0]
}
fn hash_2(input: [Field; 2]) -> Field {
let mut state = [0; 3];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_3(state)[0]
}
fn hash_3(input: [Field; 3]) -> Field {
let mut state = [0; 4];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_4(state)[0]
}
fn hash_4(input: [Field; 4]) -> Field {
let mut state = [0; 5];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_5(state)[0]
}
fn hash_5(input: [Field; 5]) -> Field {
let mut state = [0; 6];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_6(state)[0]
}
fn hash_6(input: [Field; 6]) -> Field {
let mut state = [0; 7];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_7(state)[0]
}
fn hash_7(input: [Field; 7]) -> Field {
let mut state = [0; 8];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_8(state)[0]
}
fn hash_8(input: [Field; 8]) -> Field {
let mut state = [0; 9];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_9(state)[0]
}
fn hash_9(input: [Field; 9]) -> Field {
let mut state = [0; 10];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_10(state)[0]
}
fn hash_10(input: [Field; 10]) -> Field {
let mut state = [0; 11];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_11(state)[0]
}
fn hash_11(input: [Field; 11]) -> Field {
let mut state = [0; 12];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_12(state)[0]
}
fn hash_12(input: [Field; 12]) -> Field {
let mut state = [0; 13];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_13(state)[0]
}
fn hash_13(input: [Field; 13]) -> Field {
let mut state = [0; 14];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_14(state)[0]
}
fn hash_14(input: [Field; 14]) -> Field {
let mut state = [0; 15];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_15(state)[0]
}
fn hash_15(input: [Field; 15]) -> Field {
let mut state = [0; 16];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_16(state)[0]
}
fn hash_16(input: [Field; 16]) -> Field {
let mut state = [0; 17];
for i in 0..input.len() {
state[i+1] = input[i];
}
perm::x5_17(state)[0]
}