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
#![allow(dead_code)]
#![allow(unused_imports)]
#[macro_use]
extern crate lazy_static;
pub use crate::poseidon::{Arity, Poseidon};
use crate::round_constants::generate_constants;
use crate::round_numbers::calc_round_numbers;
pub use bellperson::bls::Fr as Scalar;
use bellperson::bls::FrRepr;
pub use error::Error;
use ff::{Field, PrimeField, ScalarEngine};
use generic_array::GenericArray;
#[cfg(all(feature = "gpu", feature = "opencl"))]
compile_error!("gpu and opencl features are mutually exclusive");
pub mod circuit;
pub mod error;
mod matrix;
mod mds;
pub mod poseidon;
mod poseidon_alt;
mod preprocessing;
mod round_constants;
mod round_numbers;
pub mod hash_type;
#[cfg(any(feature = "gpu", feature = "opencl"))]
pub mod tree_builder;
#[cfg(any(feature = "gpu", feature = "opencl"))]
pub mod column_tree_builder;
#[cfg(feature = "gpu")]
pub mod triton;
#[cfg(any(feature = "gpu", feature = "opencl"))]
pub mod batch_hasher;
#[cfg(feature = "opencl")]
pub mod proteus;
pub(crate) const TEST_SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Strength {
Standard,
Strengthened,
}
pub(crate) const DEFAULT_STRENGTH: Strength = Strength::Standard;
pub trait BatchHasher<A>
where
A: Arity<Scalar>,
{
fn hash(&mut self, preimages: &[GenericArray<Scalar, A>]) -> Result<Vec<Scalar>, Error>;
fn hash_into_slice(
&mut self,
target_slice: &mut [Scalar],
preimages: &[GenericArray<Scalar, A>],
) -> Result<(), Error> {
assert_eq!(target_slice.len(), preimages.len());
Ok(target_slice.copy_from_slice(self.hash(preimages)?.as_slice()))
}
fn max_batch_size(&self) -> usize {
700000
}
}
fn round_numbers_base(arity: usize) -> (usize, usize) {
let t = arity + 1;
calc_round_numbers(t, true)
}
fn round_numbers_strengthened(arity: usize) -> (usize, usize) {
let (full_round, partial_rounds) = round_numbers_base(arity);
let strengthened_partial_rounds = f64::ceil(partial_rounds as f64 * 1.25) as usize;
(full_round, strengthened_partial_rounds)
}
pub fn round_numbers(arity: usize, strength: &Strength) -> (usize, usize) {
match strength {
Strength::Standard => round_numbers_base(arity),
Strength::Strengthened => round_numbers_strengthened(arity),
}
}
pub fn scalar_from_u64<Fr: PrimeField>(i: u64) -> Fr {
Fr::from_repr(<Fr::Repr as From<u64>>::from(i)).unwrap()
}
pub fn scalar_from_u64s(parts: [u64; 4]) -> Scalar {
Scalar::from_repr(FrRepr(parts)).unwrap()
}
const SBOX: u8 = 1;
const FIELD: u8 = 1;
fn round_constants<E: ScalarEngine>(arity: usize, strength: &Strength) -> Vec<E::Fr> {
let t = arity + 1;
let (full_rounds, partial_rounds) = round_numbers(arity, strength);
let r_f = full_rounds as u16;
let r_p = partial_rounds as u16;
let fr_num_bits = E::Fr::NUM_BITS;
let field_size = {
assert!(fr_num_bits <= std::u16::MAX as u32);
fr_num_bits as u16
};
generate_constants::<E>(FIELD, SBOX, field_size, t as u16, r_f, r_p)
}
pub(crate) fn quintic_s_box<E: ScalarEngine>(
l: &mut E::Fr,
pre_add: Option<&E::Fr>,
post_add: Option<&E::Fr>,
) {
if let Some(x) = pre_add {
l.add_assign(x);
}
let mut tmp = l.clone();
tmp.square();
tmp.square();
l.mul_assign(&tmp);
if let Some(x) = post_add {
l.add_assign(x);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_strengthened_round_numbers() {
let cases = [
(1, 69),
(2, 69),
(3, 70),
(4, 70),
(5, 70),
(6, 70),
(7, 72),
(8, 72),
(9, 72),
(10, 72),
(11, 72),
(16, 74),
(24, 74),
(36, 75),
(64, 77),
];
cases.iter().for_each(|(arity, expected_rounds)| {
let (full_rounds, actual_rounds) = round_numbers_strengthened(*arity);
assert_eq!(8, full_rounds);
assert_eq!(
*expected_rounds, actual_rounds,
"wrong number of partial rounds for arity {}",
*arity
);
})
}
}