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
#![allow(dead_code)]
use crate::{FieldParameters, PrimeField};
use snarkvm_utilities::{vec::Vec, FromBits};
use anyhow::{bail, Result};
pub struct PoseidonGrainLFSR {
pub field_size_in_bits: u64,
pub state: [bool; 80],
pub head: usize,
}
impl PoseidonGrainLFSR {
pub fn new(
is_sbox_an_inverse: bool,
field_size_in_bits: u64,
state_len: u64,
num_full_rounds: u64,
num_partial_rounds: u64,
) -> Self {
let mut state = [false; 80];
state[1] = true;
if is_sbox_an_inverse {
state[5] = true;
} else {
state[5] = false;
}
{
let mut cur = field_size_in_bits;
for i in (6..=17).rev() {
state[i] = cur & 1 == 1;
cur >>= 1;
}
}
{
let mut cur = state_len;
for i in (18..=29).rev() {
state[i] = cur & 1 == 1;
cur >>= 1;
}
}
{
let mut cur = num_full_rounds;
for i in (30..=39).rev() {
state[i] = cur & 1 == 1;
cur >>= 1;
}
}
{
let mut cur = num_partial_rounds;
for i in (40..=49).rev() {
state[i] = cur & 1 == 1;
cur >>= 1;
}
}
#[allow(clippy::needless_range_loop)]
for i in 50..=79 {
state[i] = true;
}
let mut res = Self { field_size_in_bits, state, head: 0 };
for _ in 0..160 {
res.next_bit();
}
res
}
pub fn get_field_elements_rejection_sampling<F: PrimeField>(&mut self, num_elements: usize) -> Result<Vec<F>> {
if self.field_size_in_bits != F::Parameters::MODULUS_BITS as u64 {
bail!("The number of bits in the field must match the modulus");
}
let mut output = Vec::with_capacity(num_elements);
let mut bits = Vec::with_capacity(self.field_size_in_bits as usize);
for _ in 0..num_elements {
loop {
bits.extend(self.get_bits(self.field_size_in_bits as usize));
bits.reverse();
let bigint = F::BigInteger::from_bits_le(&bits)?;
bits.clear();
if let Some(element) = F::from_bigint(bigint) {
output.push(element);
break;
}
}
}
Ok(output)
}
pub fn get_field_elements_mod_p<F: PrimeField>(&mut self, num_elems: usize) -> Result<Vec<F>> {
if self.field_size_in_bits != F::Parameters::MODULUS_BITS as u64 {
bail!("The number of bits in the field must match the modulus");
}
let mut output = Vec::with_capacity(num_elems);
for _ in 0..num_elems {
let mut bits = self.get_bits(self.field_size_in_bits as usize).collect::<Vec<_>>();
bits.reverse();
let bytes = bits
.chunks(8)
.map(|chunk| {
let mut sum = chunk[0] as u8;
let mut cur = 1;
for i in chunk.iter().skip(1) {
cur *= 2;
sum += cur * (*i as u8);
}
sum
})
.rev()
.collect::<Vec<u8>>();
output.push(F::from_bytes_be_mod_order(&bytes));
}
Ok(output)
}
}
impl PoseidonGrainLFSR {
#[inline]
fn get_bits(&mut self, num_bits: usize) -> LFSRIter<'_> {
LFSRIter { lfsr: self, num_bits, current_bit: 0 }
}
#[inline]
fn next_bit(&mut self) -> bool {
let next_bit = self.state[(self.head + 62) % 80]
^ self.state[(self.head + 51) % 80]
^ self.state[(self.head + 38) % 80]
^ self.state[(self.head + 23) % 80]
^ self.state[(self.head + 13) % 80]
^ self.state[self.head];
self.state[self.head] = next_bit;
self.head += 1;
self.head %= 80;
next_bit
}
}
pub struct LFSRIter<'a> {
lfsr: &'a mut PoseidonGrainLFSR,
num_bits: usize,
current_bit: usize,
}
impl<'a> Iterator for LFSRIter<'a> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
if self.current_bit < self.num_bits {
let mut new_bit = self.lfsr.next_bit();
while !new_bit {
let _ = self.lfsr.next_bit();
new_bit = self.lfsr.next_bit();
}
self.current_bit += 1;
Some(self.lfsr.next_bit())
} else {
None
}
}
}