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
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use crate::{Element, Expression, Field, GadgetBuilder, MdsMatrix, MonomialPermutation, MultiPermutation, Permutation};
const DEFAULT_SECURITY_BITS: usize = 128;
const SECURITY_MARGIN: usize = 2;
const MINIMUM_ROUNDS: usize = 10;
pub struct Rescue<F: Field> {
width: usize,
alpha: Element<F>,
num_rounds: usize,
mds_matrix: MdsMatrix<F>,
}
impl<F: Field> Rescue<F> {
fn pi_1(&self, builder: &mut GadgetBuilder<F>, x: &Expression<F>) -> Expression<F> {
MonomialPermutation::new(self.alpha.clone()).permute(builder, x)
}
fn pi_2(&self, builder: &mut GadgetBuilder<F>, x: &Expression<F>) -> Expression<F> {
MonomialPermutation::new(self.alpha.clone()).inverse(builder, x)
}
}
impl<F: Field> MultiPermutation<F> for Rescue<F> {
fn width(&self) -> usize {
self.width
}
fn permute(&self, builder: &mut GadgetBuilder<F>, inputs: &[Expression<F>])
-> Vec<Expression<F>> {
let mut current = inputs.to_vec();
for _round in 0..self.num_rounds {
current = current.iter().map(|exp| self.pi_1(builder, exp)).collect();
current = &self.mds_matrix * current.as_slice();
current = current.iter().map(|exp| self.pi_2(builder, exp)).collect();
current = &self.mds_matrix * current.as_slice();
}
current
}
fn inverse(&self, _builder: &mut GadgetBuilder<F>, _outputs: &[Expression<F>])
-> Vec<Expression<F>> {
unimplemented!("TODO: implement inverse Rescue")
}
}
pub struct RescueBuilder<F: Field> {
width: usize,
alpha: Option<Element<F>>,
num_rounds: Option<usize>,
security_bits: Option<usize>,
mds_matrix: Option<MdsMatrix<F>>,
}
impl<F: Field> RescueBuilder<F> {
pub fn new(width: usize) -> Self {
RescueBuilder {
width,
alpha: None,
num_rounds: None,
security_bits: None,
mds_matrix: None,
}
}
pub fn alpha(&mut self, alpha: Element<F>) -> &mut Self {
self.alpha = Some(alpha);
self
}
pub fn num_rounds(&mut self, num_rounds: usize) -> &mut Self {
self.num_rounds = Some(num_rounds);
self
}
pub fn security_bits(&mut self, security_bits: usize) -> &mut Self {
self.security_bits = Some(security_bits);
self
}
pub fn mds_matrix(&mut self, mds_matrix: MdsMatrix<F>) -> &mut Self {
self.mds_matrix = Some(mds_matrix);
self
}
pub fn build(&self) -> Rescue<F> {
let width = self.width;
let alpha = self.alpha.clone().unwrap_or_else(Self::smallest_alpha);
let mds_matrix = self.mds_matrix.clone().expect("MDS matrix required for now");
if self.num_rounds.is_some() && self.security_bits.is_some() {
panic!("Cannot specify both the number of rounds and the desired security level");
}
let num_rounds = self.num_rounds.unwrap_or_else(
|| Self::secure_num_rounds(
self.security_bits.unwrap_or(DEFAULT_SECURITY_BITS),
width));
Rescue { width, alpha, num_rounds, mds_matrix }
}
fn smallest_alpha() -> Element<F> {
let largest_element = Element::<F>::largest_element();
let mut alpha = Element::<F>::from(3u8);
while !largest_element.gcd(&alpha).is_one() {
alpha += Element::one();
while !alpha.is_prime() {
alpha += Element::one();
}
}
alpha
}
fn secure_num_rounds(security_bits: usize, width: usize) -> usize {
let attackable_rounds = integer_division_ceil(security_bits, 4 * width);
(attackable_rounds * SECURITY_MARGIN).max(MINIMUM_ROUNDS)
}
}
fn integer_division_ceil(n: usize, m: usize) -> usize {
(n + m - 1) / m
}
#[cfg(test)]
mod tests {
use crate::MdsMatrix;
use crate::rescue::RescueBuilder;
use crate::test_util::F11;
#[test]
fn rescue_permutation_f11() {
let mds_matrix = MdsMatrix::<F11>::new(vec![
vec![2u8.into(), 3u8.into(), 1u8.into(), 1u8.into()],
vec![1u8.into(), 2u8.into(), 3u8.into(), 1u8.into()],
vec![1u8.into(), 1u8.into(), 2u8.into(), 3u8.into()],
vec![3u8.into(), 1u8.into(), 1u8.into(), 2u8.into()],
]);
let _rescue = RescueBuilder::new(2).security_bits(128).mds_matrix(mds_matrix).build();
}
}