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
use crate::{Spec, State};
use halo2curves::FieldExt;
#[derive(Debug, Clone)]
pub struct Poseidon<F: FieldExt, const T: usize, const RATE: usize> {
state: State<F, T>,
spec: Spec<F, T, RATE>,
absorbing: Vec<F>,
}
impl<F: FieldExt, const T: usize, const RATE: usize> Poseidon<F, T, RATE> {
pub fn new(r_f: usize, r_p: usize) -> Self {
Self {
spec: Spec::new(r_f, r_p),
state: State::default(),
absorbing: Vec::new(),
}
}
pub fn update(&mut self, elements: &[F]) {
let mut input_elements = self.absorbing.clone();
input_elements.extend_from_slice(elements);
for chunk in input_elements.chunks(RATE) {
if chunk.len() < RATE {
self.absorbing = chunk.to_vec();
} else {
for (input_element, state) in chunk.iter().zip(self.state.0.iter_mut().skip(1)) {
state.add_assign(input_element);
}
self.spec.permute(&mut self.state);
self.absorbing.clear();
}
}
}
pub fn squeeze(&mut self) -> F {
let mut last_chunk = self.absorbing.clone();
{
debug_assert!(last_chunk.len() < RATE);
}
last_chunk.push(F::one());
for (input_element, state) in last_chunk.iter().zip(self.state.0.iter_mut().skip(1)) {
state.add_assign(input_element);
}
self.spec.permute(&mut self.state);
self.absorbing.clear();
self.state.result()
}
}
#[test]
fn test_padding() {
use group::ff::Field;
use halo2curves::bn256::Fr;
const R_F: usize = 8;
const R_P: usize = 57;
const T: usize = 5;
const RATE: usize = 4;
use rand_core::OsRng;
{
let mut poseidon = Poseidon::<Fr, T, RATE>::new(R_F, R_P);
let number_of_permutation = 5;
let number_of_inputs = RATE * number_of_permutation - 1;
let inputs = (0..number_of_inputs)
.map(|_| Fr::random(OsRng))
.collect::<Vec<Fr>>();
poseidon.update(&inputs[..]);
let result_0 = poseidon.squeeze();
let spec = poseidon.spec.clone();
let mut inputs = inputs.clone();
inputs.push(Fr::one());
assert!(inputs.len() % RATE == 0);
let mut state = State::<Fr, T>::default();
for chunk in inputs.chunks(RATE) {
let mut inputs = vec![Fr::zero()];
inputs.extend_from_slice(chunk);
state.add_constants(&inputs.try_into().unwrap());
spec.permute(&mut state)
}
let result_1 = state.result();
assert_eq!(result_0, result_1);
}
{
let mut poseidon = Poseidon::<Fr, T, RATE>::new(R_F, R_P);
let number_of_permutation = 5;
let number_of_inputs = RATE * number_of_permutation;
let inputs = (0..number_of_inputs)
.map(|_| Fr::random(OsRng))
.collect::<Vec<Fr>>();
poseidon.update(&inputs[..]);
let result_0 = poseidon.squeeze();
let spec = poseidon.spec.clone();
let mut inputs = inputs.clone();
let mut extra_padding = vec![Fr::zero(); RATE];
extra_padding[0] = Fr::one();
inputs.extend(extra_padding);
assert!(inputs.len() % RATE == 0);
let mut state = State::<Fr, T>::default();
for chunk in inputs.chunks(RATE) {
let mut inputs = vec![Fr::zero()];
inputs.extend_from_slice(chunk);
state.add_constants(&inputs.try_into().unwrap());
spec.permute(&mut state)
}
let result_1 = state.result();
assert_eq!(result_0, result_1);
}
fn run<const T: usize, const RATE: usize>() {
for number_of_iters in 1..25 {
let mut poseidon = Poseidon::<Fr, T, RATE>::new(R_F, R_P);
let mut inputs = vec![];
for number_of_inputs in 0..=number_of_iters {
let chunk = (0..number_of_inputs)
.map(|_| Fr::random(OsRng))
.collect::<Vec<Fr>>();
poseidon.update(&chunk[..]);
inputs.extend(chunk);
}
let result_0 = poseidon.squeeze();
inputs.push(Fr::one());
let offset = inputs.len() % RATE;
if offset != 0 {
inputs.extend(vec![Fr::zero(); RATE - offset]);
}
let spec = poseidon.spec.clone();
let mut state = State::<Fr, T>::default();
for chunk in inputs.chunks(RATE) {
let mut round_inputs = vec![Fr::zero()];
round_inputs.extend_from_slice(chunk);
state.add_constants(&round_inputs.try_into().unwrap());
spec.permute(&mut state)
}
let result_1 = state.result();
assert_eq!(result_0, result_1);
}
}
run::<3, 2>();
run::<4, 3>();
run::<5, 4>();
run::<6, 5>();
run::<7, 6>();
run::<8, 7>();
run::<9, 8>();
run::<10, 9>();
}