1#![cfg_attr(not(feature = "std"), no_std)]
2use ark_bn254::Fr;
3use ark_ff::fields::Field;
4use ark_std::str::FromStr;
5use ark_std::vec;
6use ark_std::vec::Vec;
7use ark_std::Zero;
8use core::ops::{AddAssign, MulAssign};
9
10mod constants;
11
12#[derive(Debug)]
13pub struct Constants {
14 pub c: Vec<Vec<Fr>>,
15 pub m: Vec<Vec<Vec<Fr>>>,
16 pub n_rounds_f: usize,
17 pub n_rounds_p: Vec<usize>,
18}
19
20pub fn load_constants() -> Constants {
21 let (c_str, m_str) = constants::constants();
22 let mut c: Vec<Vec<Fr>> = Vec::new();
23 for i in 0..c_str.len() {
24 let mut cci: Vec<Fr> = Vec::new();
25 for j in 0..c_str[i].len() {
26 let b: Fr = Fr::from_str(c_str[i][j]).unwrap();
27 cci.push(b);
28 }
29 c.push(cci);
30 }
31 let mut m: Vec<Vec<Vec<Fr>>> = Vec::new();
32 for i in 0..m_str.len() {
33 let mut mi: Vec<Vec<Fr>> = Vec::new();
34 for j in 0..m_str[i].len() {
35 let mut mij: Vec<Fr> = Vec::new();
36 for k in 0..m_str[i][j].len() {
37 let b: Fr = Fr::from_str(m_str[i][j][k]).unwrap();
38 mij.push(b);
39 }
40 mi.push(mij);
41 }
42 m.push(mi);
43 }
44 Constants {
45 c,
46 m,
47 n_rounds_f: 8,
48 n_rounds_p: vec![
49 56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68,
50 ],
51 }
52}
53
54pub struct Poseidon {
55 constants: Constants,
56}
57impl Poseidon {
58 pub fn new() -> Poseidon {
59 Poseidon {
60 constants: load_constants(),
61 }
62 }
63 pub fn ark(&self, state: &mut Vec<Fr>, c: &[Fr], it: usize) {
64 for i in 0..state.len() {
65 state[i].add_assign(&c[it + i]);
66 }
67 }
68
69 pub fn sbox(&self, n_rounds_f: usize, n_rounds_p: usize, state: &mut Vec<Fr>, i: usize) {
70 if i < n_rounds_f / 2 || i >= n_rounds_f / 2 + n_rounds_p {
71 for j in 0..state.len() {
72 let aux = state[j];
73 state[j] = state[j].square();
74 state[j] = state[j].square();
75 state[j].mul_assign(&aux);
76 }
77 } else {
78 let aux = state[0];
79 state[0] = state[0].square();
80 state[0] = state[0].square();
81 state[0].mul_assign(&aux);
82 }
83 }
84
85 pub fn mix(&self, state: &Vec<Fr>, m: &[Vec<Fr>]) -> Vec<Fr> {
86 let mut new_state: Vec<Fr> = Vec::new();
87 for i in 0..state.len() {
88 new_state.push(Fr::zero());
89 for j in 0..state.len() {
90 let mut mij = m[i][j];
91 mij.mul_assign(&state[j]);
92 new_state[i].add_assign(&mij);
93 }
94 }
95 new_state.clone()
96 }
97
98 pub fn hash(&self, inp: Vec<Fr>) -> Result<Fr, &'static str> {
99 let t = inp.len() + 1;
100 if inp.is_empty() || inp.len() > self.constants.n_rounds_p.len() {
101 return Err("Wrong inputs length");
102 }
103 let n_rounds_f = self.constants.n_rounds_f.clone();
104 let n_rounds_p = self.constants.n_rounds_p[t - 2].clone();
105
106 let mut state = vec![Fr::zero(); t];
107 state[1..].clone_from_slice(&inp);
108
109 for i in 0..(n_rounds_f + n_rounds_p) {
110 self.ark(&mut state, &self.constants.c[t - 2], i * t);
111 self.sbox(n_rounds_f, n_rounds_p, &mut state, i);
112 state = self.mix(&state, &self.constants.m[t - 2]);
113 }
114
115 Ok(state[0])
116 }
117}
118
119#[cfg(test)]
120mod tests {
121 use super::*;
122
123 #[test]
124 fn test_load_constants() {
125 let cons = load_constants();
126 assert_eq!(
127 cons.c[0][0].to_string(),
128 "4417881134626180770308697923359573201005643519861877412381846989312604493735"
129 );
130 assert_eq!(
131 cons.c[cons.c.len() - 1][0].to_string(),
132 "21579410516734741630578831791708254656585702717204712919233299001262271512412"
133 );
134 assert_eq!(
135 cons.m[0][0][0].to_string(),
136 "2910766817845651019878574839501801340070030115151021261302834310722729507541"
137 );
138 assert_eq!(
139 cons.m[cons.m.len() - 1][0][0].to_string(),
140 "11497693837059016825308731789443585196852778517742143582474723527597064448312"
141 );
142 }
143
144 #[test]
145 fn test_hash() {
146 let b0: Fr = Fr::from_str("0").unwrap();
147 let b1: Fr = Fr::from_str("1").unwrap();
148 let b2: Fr = Fr::from_str("2").unwrap();
149 let b3: Fr = Fr::from_str("3").unwrap();
150 let b4: Fr = Fr::from_str("4").unwrap();
151 let b5: Fr = Fr::from_str("5").unwrap();
152 let b6: Fr = Fr::from_str("6").unwrap();
153 let b7: Fr = Fr::from_str("7").unwrap();
154 let b8: Fr = Fr::from_str("8").unwrap();
155 let b9: Fr = Fr::from_str("9").unwrap();
156 let b10: Fr = Fr::from_str("10").unwrap();
157 let b11: Fr = Fr::from_str("11").unwrap();
158 let b12: Fr = Fr::from_str("12").unwrap();
159 let b13: Fr = Fr::from_str("13").unwrap();
160 let b14: Fr = Fr::from_str("14").unwrap();
161 let b15: Fr = Fr::from_str("15").unwrap();
162 let b16: Fr = Fr::from_str("16").unwrap();
163
164 let poseidon = Poseidon::new();
165
166 let big_arr: Vec<Fr> = vec![b1];
167 let h = poseidon.hash(big_arr.clone()).unwrap();
168 assert_eq!(
169 h.to_string(),
170 "18586133768512220936620570745912940619677854269274689475585506675881198879027"
171 );
172
173 let big_arr: Vec<Fr> = vec![b1, b2];
174 let h = poseidon.hash(big_arr.clone()).unwrap();
175 assert_eq!(
176 h.to_string(),
177 "7853200120776062878684798364095072458815029376092732009249414926327459813530"
178 );
179
180 let big_arr: Vec<Fr> = vec![b1, b2, b0, b0, b0];
181 let h = poseidon.hash(big_arr.clone()).unwrap();
182 assert_eq!(
183 h.to_string(),
184 "1018317224307729531995786483840663576608797660851238720571059489595066344487"
185 );
186
187 let big_arr: Vec<Fr> = vec![b1, b2, b0, b0, b0, b0];
188 let h = poseidon.hash(big_arr.clone()).unwrap();
189 assert_eq!(
190 h.to_string(),
191 "15336558801450556532856248569924170992202208561737609669134139141992924267169"
192 );
193
194 let big_arr: Vec<Fr> = vec![b3, b4, b0, b0, b0];
195 let h = poseidon.hash(big_arr.clone()).unwrap();
196 assert_eq!(
197 h.to_string(),
198 "5811595552068139067952687508729883632420015185677766880877743348592482390548"
199 );
200
201 let big_arr: Vec<Fr> = vec![b3, b4, b0, b0, b0, b0];
202 let h = poseidon.hash(big_arr).unwrap();
203 assert_eq!(
204 h.to_string(),
205 "12263118664590987767234828103155242843640892839966517009184493198782366909018"
206 );
207
208 let big_arr: Vec<Fr> = vec![b1, b2, b3, b4, b5, b6];
209 let h = poseidon.hash(big_arr).unwrap();
210 assert_eq!(
211 h.to_string(),
212 "20400040500897583745843009878988256314335038853985262692600694741116813247201"
213 );
214
215 let big_arr: Vec<Fr> = vec![b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14];
216 let h = poseidon.hash(big_arr).unwrap();
217 assert_eq!(
218 h.to_string(),
219 "8354478399926161176778659061636406690034081872658507739535256090879947077494"
220 );
221
222 let big_arr: Vec<Fr> = vec![b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b0, b0, b0, b0];
223 let h = poseidon.hash(big_arr).unwrap();
224 assert_eq!(
225 h.to_string(),
226 "5540388656744764564518487011617040650780060800286365721923524861648744699539"
227 );
228
229 let big_arr: Vec<Fr> = vec![
230 b1, b2, b3, b4, b5, b6, b7, b8, b9, b0, b0, b0, b0, b0, b0, b0,
231 ];
232 let h = poseidon.hash(big_arr).unwrap();
233 assert_eq!(
234 h.to_string(),
235 "11882816200654282475720830292386643970958445617880627439994635298904836126497"
236 );
237
238 let big_arr: Vec<Fr> = vec![
239 b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16,
240 ];
241 let h = poseidon.hash(big_arr).unwrap();
242 assert_eq!(
243 h.to_string(),
244 "9989051620750914585850546081941653841776809718687451684622678807385399211877"
245 );
246 }
247 #[test]
248 fn test_wrong_inputs() {
249 let b0: Fr = Fr::from_str("0").unwrap();
250 let b1: Fr = Fr::from_str("1").unwrap();
251 let b2: Fr = Fr::from_str("2").unwrap();
252
253 let poseidon = Poseidon::new();
254
255 let big_arr: Vec<Fr> = vec![
256 b1, b2, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0, b0,
257 ];
258 poseidon.hash(big_arr).expect_err("Wrong inputs length");
259 }
260}