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
use crate::spec::MDSMatrix;
use halo2curves::FieldExt;
use std::marker::PhantomData;
pub(super) struct Grain<F: FieldExt, const T: usize, const RATE: usize> {
bit_sequence: Vec<bool>,
_field: PhantomData<F>,
}
impl<F: FieldExt, const T: usize, const RATE: usize> Grain<F, T, RATE> {
pub(crate) fn generate(r_f: usize, r_p: usize) -> (Vec<[F; T]>, MDSMatrix<F, T, RATE>) {
debug_assert!(T > 1 && T == RATE + 1);
const FIELD_TYPE: u8 = 1u8;
const SBOX_TYPE: u8 = 0;
let field_size = F::NUM_BITS;
let n_bytes = F::Repr::default().as_ref().len();
assert_eq!((field_size as f32 / 8.0).ceil() as usize, n_bytes);
assert_eq!(r_f % 2, 0);
let mut bit_sequence: Vec<bool> = Vec::new();
append_bits(&mut bit_sequence, 2, FIELD_TYPE);
append_bits(&mut bit_sequence, 4, SBOX_TYPE);
append_bits(&mut bit_sequence, 12, field_size);
append_bits(&mut bit_sequence, 12, T as u32);
append_bits(&mut bit_sequence, 10, r_f as u16);
append_bits(&mut bit_sequence, 10, r_p as u16);
append_bits(&mut bit_sequence, 30, 0b111111111111111111111111111111u128);
debug_assert_eq!(bit_sequence.len(), 80);
let mut grain: Grain<F, T, RATE> = Grain {
bit_sequence,
_field: PhantomData,
};
for _ in 0..160 {
grain.new_bit();
}
assert_eq!(grain.bit_sequence.len(), 80);
let number_of_rounds = r_p as usize + r_f as usize;
let constants = (0..number_of_rounds)
.map(|_| {
let mut round_constants = [F::zero(); T];
for c in round_constants.iter_mut() {
*c = grain.next_field_element();
}
round_constants
})
.collect::<Vec<[F; T]>>();
let (mut xs, mut ys) = ([F::zero(); T], [F::zero(); T]);
for x in xs.iter_mut() {
*x = grain.next_field_element_without_rejection();
}
for y in ys.iter_mut() {
*y = grain.next_field_element_without_rejection();
}
(constants, MDSMatrix::cauchy(&xs, &ys))
}
pub(super) fn next_field_element(&mut self) -> F {
loop {
let mut bytes = F::Repr::default();
let view = bytes.as_mut();
for (i, bit) in self.take(F::NUM_BITS as usize).enumerate() {
let i = F::NUM_BITS as usize - 1 - i;
view[i / 8] |= if bit { 1 << (i % 8) } else { 0 };
}
if let Some(f) = F::from_repr_vartime(bytes) {
break f;
}
}
}
pub(super) fn next_field_element_without_rejection(&mut self) -> F {
let mut bytes = [0u8; 64];
let view = bytes.as_mut();
for (i, bit) in self.take(F::NUM_BITS as usize).enumerate() {
let i = F::NUM_BITS as usize - 1 - i;
view[i / 8] |= if bit { 1 << (i % 8) } else { 0 };
}
F::from_bytes_wide(&bytes)
}
fn new_bit(&mut self) -> bool {
let new_bit = vec![62, 51, 38, 23, 13usize]
.iter()
.fold(self.bit_sequence[0], |acc, pos| {
acc ^ self.bit_sequence[*pos]
});
assert_eq!(self.bit_sequence.len(), 80);
self.bit_sequence.remove(0);
self.bit_sequence.push(new_bit);
new_bit
}
}
impl<F: FieldExt, const T: usize, const RATE: usize> Iterator for Grain<F, T, RATE> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
while !self.new_bit() {
self.new_bit();
}
Some(self.new_bit())
}
}
fn append_bits<T: Into<u128>>(vec: &mut Vec<bool>, n: usize, from: T) {
let val = from.into() as u128;
for i in (0..n).rev() {
vec.push((val >> i) & 1 != 0);
}
}