From 313a0f46fb2190a23ce9f79563d6d839de1f967c Mon Sep 17 00:00:00 2001 From: Antoine FONDEUR Date: Tue, 29 Jul 2025 10:48:32 +0200 Subject: [PATCH 1/3] add poseidon-M31 --- plain_implementations/src/fields/m31.rs | 8 + .../src/poseidon2/poseidon2.rs | 206 +++-- .../src/poseidon2/poseidon2_instance_m31.rs | 721 ++++++++++++++++++ 3 files changed, 888 insertions(+), 47 deletions(-) create mode 100644 plain_implementations/src/fields/m31.rs create mode 100644 plain_implementations/src/poseidon2/poseidon2_instance_m31.rs diff --git a/plain_implementations/src/fields/m31.rs b/plain_implementations/src/fields/m31.rs new file mode 100644 index 0000000..01bd307 --- /dev/null +++ b/plain_implementations/src/fields/m31.rs @@ -0,0 +1,8 @@ +use ark_ff::fields::{Fp64, MontBackend, MontConfig}; +use std::convert::TryInto; + +#[derive(MontConfig)] +#[modulus = "2147483647"] // M31 = 2^31 - 1 +#[generator = "7"] +pub struct FqConfig; +pub type FpM31 = Fp64>; diff --git a/plain_implementations/src/poseidon2/poseidon2.rs b/plain_implementations/src/poseidon2/poseidon2.rs index e306e42..b8af472 100644 --- a/plain_implementations/src/poseidon2/poseidon2.rs +++ b/plain_implementations/src/poseidon2/poseidon2.rs @@ -40,7 +40,7 @@ impl Poseidon2 { current_state[0] = self.sbox_p(¤t_state[0]); self.matmul_internal(&mut current_state, &self.params.mat_internal_diag_m_1); } - + for r in p_end..self.params.rounds { current_state = self.add_rc(¤t_state, &self.params.round_constants[r]); current_state = self.sbox(¤t_state); @@ -82,7 +82,7 @@ impl Poseidon2 { } } - fn matmul_m4(&self, input: &mut[F]) { + fn matmul_m4(&self, input: &mut [F]) { let t = self.params.t; let t4 = t / 4; for i in 0..t4 { @@ -116,7 +116,7 @@ impl Poseidon2 { } } - fn matmul_external(&self, input: &mut[F]) { + fn matmul_external(&self, input: &mut [F]) { let t = self.params.t; match t { 2 => { @@ -162,7 +162,7 @@ impl Poseidon2 { } } - fn matmul_internal(&self, input: &mut[F], mat_internal_diag_m_1: &[F]) { + fn matmul_internal(&self, input: &mut [F], mat_internal_diag_m_1: &[F]) { let t = self.params.t; match t { @@ -193,7 +193,7 @@ impl Poseidon2 { input .iter() .skip(1) - .take(t-1) + .take(t - 1) .for_each(|el| sum.add_assign(el)); // Add sum + diag entry * element to each element for i in 0..input.len() { @@ -230,12 +230,10 @@ impl MerkleTreeHash for Poseidon2 { #[cfg(test)] mod poseidon2_tests_goldilocks { use super::*; - use crate::{fields::{goldilocks::FpGoldiLocks, utils::from_hex, utils::random_scalar}}; + use crate::fields::{goldilocks::FpGoldiLocks, utils::from_hex, utils::random_scalar}; use crate::poseidon2::poseidon2_instance_goldilocks::{ - POSEIDON2_GOLDILOCKS_8_PARAMS, - POSEIDON2_GOLDILOCKS_12_PARAMS, - POSEIDON2_GOLDILOCKS_16_PARAMS, - POSEIDON2_GOLDILOCKS_20_PARAMS, + POSEIDON2_GOLDILOCKS_12_PARAMS, POSEIDON2_GOLDILOCKS_16_PARAMS, + POSEIDON2_GOLDILOCKS_20_PARAMS, POSEIDON2_GOLDILOCKS_8_PARAMS, }; use std::convert::TryFrom; @@ -300,10 +298,9 @@ mod poseidon2_tests_goldilocks { #[cfg(test)] mod poseidon2_tests_babybear { use super::*; - use crate::{fields::{babybear::FpBabyBear, utils::from_hex, utils::random_scalar}}; + use crate::fields::{babybear::FpBabyBear, utils::from_hex, utils::random_scalar}; use crate::poseidon2::poseidon2_instance_babybear::{ - POSEIDON2_BABYBEAR_16_PARAMS, - POSEIDON2_BABYBEAR_24_PARAMS, + POSEIDON2_BABYBEAR_16_PARAMS, POSEIDON2_BABYBEAR_24_PARAMS, }; use std::convert::TryFrom; @@ -315,7 +312,7 @@ mod poseidon2_tests_babybear { fn consistent_perm() { let instances = vec![ Poseidon2::new(&POSEIDON2_BABYBEAR_16_PARAMS), - Poseidon2::new(&POSEIDON2_BABYBEAR_24_PARAMS) + Poseidon2::new(&POSEIDON2_BABYBEAR_24_PARAMS), ]; for instance in instances { let t = instance.params.t; @@ -378,11 +375,9 @@ mod poseidon2_tests_babybear { #[cfg(test)] mod poseidon2_tests_bls12 { use super::*; - use crate::{fields::{bls12::FpBLS12, utils::from_hex, utils::random_scalar}}; + use crate::fields::{bls12::FpBLS12, utils::from_hex, utils::random_scalar}; use crate::poseidon2::poseidon2_instance_bls12::{ - POSEIDON2_BLS_2_PARAMS, - POSEIDON2_BLS_3_PARAMS, - POSEIDON2_BLS_4_PARAMS, + POSEIDON2_BLS_2_PARAMS, POSEIDON2_BLS_3_PARAMS, POSEIDON2_BLS_4_PARAMS, POSEIDON2_BLS_8_PARAMS, }; use std::convert::TryFrom; @@ -397,7 +392,7 @@ mod poseidon2_tests_bls12 { Poseidon2::new(&POSEIDON2_BLS_2_PARAMS), Poseidon2::new(&POSEIDON2_BLS_3_PARAMS), Poseidon2::new(&POSEIDON2_BLS_4_PARAMS), - Poseidon2::new(&POSEIDON2_BLS_8_PARAMS) + Poseidon2::new(&POSEIDON2_BLS_8_PARAMS), ]; for instance in instances { let t = instance.params.t; @@ -429,8 +424,14 @@ mod poseidon2_tests_bls12 { input_2.push(Scalar::from(i as u64)); } let perm_2 = poseidon2_2.permutation(&input_2); - assert_eq!(perm_2[0], from_hex("0x73c46dd530e248a87b61d19e67fa1b4ed30fc3d09f16531fe189fb945a15ce4e")); - assert_eq!(perm_2[1], from_hex("0x1f0e305ee21c9366d5793b80251405032a3fee32b9dd0b5f4578262891b043b4")); + assert_eq!( + perm_2[0], + from_hex("0x73c46dd530e248a87b61d19e67fa1b4ed30fc3d09f16531fe189fb945a15ce4e") + ); + assert_eq!( + perm_2[1], + from_hex("0x1f0e305ee21c9366d5793b80251405032a3fee32b9dd0b5f4578262891b043b4") + ); let poseidon2_3 = Poseidon2::new(&POSEIDON2_BLS_3_PARAMS); let mut input_3: Vec = vec![]; @@ -438,9 +439,18 @@ mod poseidon2_tests_bls12 { input_3.push(Scalar::from(i as u64)); } let perm_3 = poseidon2_3.permutation(&input_3); - assert_eq!(perm_3[0], from_hex("0x1b152349b1950b6a8ca75ee4407b6e26ca5cca5650534e56ef3fd45761fbf5f0")); - assert_eq!(perm_3[1], from_hex("0x4c5793c87d51bdc2c08a32108437dc0000bd0275868f09ebc5f36919af5b3891")); - assert_eq!(perm_3[2], from_hex("0x1fc8ed171e67902ca49863159fe5ba6325318843d13976143b8125f08b50dc6b")); + assert_eq!( + perm_3[0], + from_hex("0x1b152349b1950b6a8ca75ee4407b6e26ca5cca5650534e56ef3fd45761fbf5f0") + ); + assert_eq!( + perm_3[1], + from_hex("0x4c5793c87d51bdc2c08a32108437dc0000bd0275868f09ebc5f36919af5b3891") + ); + assert_eq!( + perm_3[2], + from_hex("0x1fc8ed171e67902ca49863159fe5ba6325318843d13976143b8125f08b50dc6b") + ); let poseidon2_4 = Poseidon2::new(&POSEIDON2_BLS_4_PARAMS); let mut input_4: Vec = vec![]; @@ -448,10 +458,22 @@ mod poseidon2_tests_bls12 { input_4.push(Scalar::from(i as u64)); } let perm_4 = poseidon2_4.permutation(&input_4); - assert_eq!(perm_4[0], from_hex("0x28ff6c4edf9768c08ae26290487e93449cc8bc155fc2fad92a344adceb3ada6d")); - assert_eq!(perm_4[1], from_hex("0x0e56f2b6fad25075aa93560185b70e2b180ed7e269159c507c288b6747a0db2d")); - assert_eq!(perm_4[2], from_hex("0x6d8196f28da6006bb89b3df94600acdc03d0ba7c2b0f3f4409a54c1db6bf30d0")); - assert_eq!(perm_4[3], from_hex("0x07cfb49540ee456cce38b8a7d1a930a57ffc6660737f6589ef184c5e15334e36")); + assert_eq!( + perm_4[0], + from_hex("0x28ff6c4edf9768c08ae26290487e93449cc8bc155fc2fad92a344adceb3ada6d") + ); + assert_eq!( + perm_4[1], + from_hex("0x0e56f2b6fad25075aa93560185b70e2b180ed7e269159c507c288b6747a0db2d") + ); + assert_eq!( + perm_4[2], + from_hex("0x6d8196f28da6006bb89b3df94600acdc03d0ba7c2b0f3f4409a54c1db6bf30d0") + ); + assert_eq!( + perm_4[3], + from_hex("0x07cfb49540ee456cce38b8a7d1a930a57ffc6660737f6589ef184c5e15334e36") + ); } } @@ -459,7 +481,10 @@ mod poseidon2_tests_bls12 { #[cfg(test)] mod poseidon2_tests_bn256 { use super::*; - use crate::{fields::{bn256::FpBN256, utils::from_hex, utils::random_scalar}, poseidon2::poseidon2_instance_bn256::POSEIDON2_BN256_PARAMS}; + use crate::{ + fields::{bn256::FpBN256, utils::from_hex, utils::random_scalar}, + poseidon2::poseidon2_instance_bn256::POSEIDON2_BN256_PARAMS, + }; use std::convert::TryFrom; type Scalar = FpBN256; @@ -497,10 +522,18 @@ mod poseidon2_tests_bn256 { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!(perm[0], from_hex("0x0bb61d24daca55eebcb1929a82650f328134334da98ea4f847f760054f4a3033")); - assert_eq!(perm[1], from_hex("0x303b6f7c86d043bfcbcc80214f26a30277a15d3f74ca654992defe7ff8d03570")); - assert_eq!(perm[2], from_hex("0x1ed25194542b12eef8617361c3ba7c52e660b145994427cc86296242cf766ec8")); - + assert_eq!( + perm[0], + from_hex("0x0bb61d24daca55eebcb1929a82650f328134334da98ea4f847f760054f4a3033") + ); + assert_eq!( + perm[1], + from_hex("0x303b6f7c86d043bfcbcc80214f26a30277a15d3f74ca654992defe7ff8d03570") + ); + assert_eq!( + perm[2], + from_hex("0x1ed25194542b12eef8617361c3ba7c52e660b145994427cc86296242cf766ec8") + ); } } @@ -508,11 +541,9 @@ mod poseidon2_tests_bn256 { #[cfg(test)] mod poseidon2_tests_pallas { use super::*; - use crate::{fields::{pallas::FpPallas, utils::from_hex, utils::random_scalar}}; + use crate::fields::{pallas::FpPallas, utils::from_hex, utils::random_scalar}; use crate::poseidon2::poseidon2_instance_pallas::{ - POSEIDON2_PALLAS_3_PARAMS, - POSEIDON2_PALLAS_4_PARAMS, - POSEIDON2_PALLAS_8_PARAMS, + POSEIDON2_PALLAS_3_PARAMS, POSEIDON2_PALLAS_4_PARAMS, POSEIDON2_PALLAS_8_PARAMS, }; use std::convert::TryFrom; @@ -525,7 +556,7 @@ mod poseidon2_tests_pallas { let instances = vec![ Poseidon2::new(&POSEIDON2_PALLAS_3_PARAMS), Poseidon2::new(&POSEIDON2_PALLAS_4_PARAMS), - Poseidon2::new(&POSEIDON2_PALLAS_8_PARAMS) + Poseidon2::new(&POSEIDON2_PALLAS_8_PARAMS), ]; for instance in instances { let t = instance.params.t; @@ -557,10 +588,18 @@ mod poseidon2_tests_pallas { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!(perm[0], from_hex("0x1a9b54c7512a914dd778282c44b3513fea7251420b9d95750baae059b2268d7a")); - assert_eq!(perm[1], from_hex("0x1c48ea0994a7d7984ea338a54dbf0c8681f5af883fe988d59ba3380c9f7901fc")); - assert_eq!(perm[2], from_hex("0x079ddd0a80a3e9414489b526a2770448964766685f4c4842c838f8a23120b401")); - + assert_eq!( + perm[0], + from_hex("0x1a9b54c7512a914dd778282c44b3513fea7251420b9d95750baae059b2268d7a") + ); + assert_eq!( + perm[1], + from_hex("0x1c48ea0994a7d7984ea338a54dbf0c8681f5af883fe988d59ba3380c9f7901fc") + ); + assert_eq!( + perm[2], + from_hex("0x079ddd0a80a3e9414489b526a2770448964766685f4c4842c838f8a23120b401") + ); } } @@ -568,7 +607,10 @@ mod poseidon2_tests_pallas { #[cfg(test)] mod poseidon2_tests_vesta { use super::*; - use crate::{fields::{vesta::FpVesta, utils::from_hex, utils::random_scalar}, poseidon2::poseidon2_instance_vesta::POSEIDON2_VESTA_PARAMS}; + use crate::{ + fields::{utils::from_hex, utils::random_scalar, vesta::FpVesta}, + poseidon2::poseidon2_instance_vesta::POSEIDON2_VESTA_PARAMS, + }; use std::convert::TryFrom; type Scalar = FpVesta; @@ -606,9 +648,79 @@ mod poseidon2_tests_vesta { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!(perm[0], from_hex("0x261ecbdfd62c617b82d297705f18c788fc9831b14a6a2b8f61229bef68ce2792")); - assert_eq!(perm[1], from_hex("0x2c76327e0b7653873263158cf8545c282364b183880fcdea93ca8526d518c66f")); - assert_eq!(perm[2], from_hex("0x262316c0ce5244838c75873299b59d763ae0849d2dd31bdc95caf7db1c2901bf")); + assert_eq!( + perm[0], + from_hex("0x261ecbdfd62c617b82d297705f18c788fc9831b14a6a2b8f61229bef68ce2792") + ); + assert_eq!( + perm[1], + from_hex("0x2c76327e0b7653873263158cf8545c282364b183880fcdea93ca8526d518c66f") + ); + assert_eq!( + perm[2], + from_hex("0x262316c0ce5244838c75873299b59d763ae0849d2dd31bdc95caf7db1c2901bf") + ); + } +} + +#[allow(unused_imports)] +#[cfg(test)] +mod poseidon2_tests_m31 { + use super::*; + use crate::fields::{m31::FpM31, utils::from_hex, utils::random_scalar}; + use crate::poseidon2::poseidon2_instance_m31::POSEIDON2_M31_16_PARAMS; + use std::convert::TryFrom; + type Scalar = FpM31; + + static TESTRUNS: usize = 5; + + #[test] + fn consistent_perm() { + let poseidon2 = Poseidon2::new(&POSEIDON2_M31_16_PARAMS); + let t = poseidon2.params.t; + for _ in 0..TESTRUNS { + let input1: Vec = (0..t).map(|_| random_scalar()).collect(); + + let mut input2: Vec; + loop { + input2 = (0..t).map(|_| random_scalar()).collect(); + if input1 != input2 { + break; + } + } + + let perm1 = poseidon2.permutation(&input1); + let perm2 = poseidon2.permutation(&input1); + let perm3 = poseidon2.permutation(&input2); + assert_eq!(perm1, perm2); + assert_ne!(perm1, perm3); + } } -} \ No newline at end of file + + #[test] + fn kats() { + let poseidon2 = Poseidon2::new(&POSEIDON2_M31_16_PARAMS); + let mut input: Vec = vec![]; + for i in 0..poseidon2.params.t { + input.push(Scalar::from(i as u64)); + } + let perm = poseidon2.permutation(&input); + assert_eq!(perm[0], from_hex("0x505d9689")); + assert_eq!(perm[1], from_hex("0x3b64c904")); + assert_eq!(perm[2], from_hex("0x79e2fd81")); + assert_eq!(perm[3], from_hex("0x4ba8015f")); + assert_eq!(perm[4], from_hex("0x24b6d2f5")); + assert_eq!(perm[5], from_hex("0x23845add")); + assert_eq!(perm[6], from_hex("0x521f4314")); + assert_eq!(perm[7], from_hex("0x69dfb019")); + assert_eq!(perm[8], from_hex("0x2aaae419")); + assert_eq!(perm[9], from_hex("0x6cb4502c")); + assert_eq!(perm[10], from_hex("0x6f7fa65a")); + assert_eq!(perm[11], from_hex("0x75feff24")); + assert_eq!(perm[12], from_hex("0x128d6587")); + assert_eq!(perm[13], from_hex("0x515877e4")); + assert_eq!(perm[14], from_hex("0x037f4dd7")); + assert_eq!(perm[15], from_hex("0x134b427f")); + } +} diff --git a/plain_implementations/src/poseidon2/poseidon2_instance_m31.rs b/plain_implementations/src/poseidon2/poseidon2_instance_m31.rs new file mode 100644 index 0000000..f763834 --- /dev/null +++ b/plain_implementations/src/poseidon2/poseidon2_instance_m31.rs @@ -0,0 +1,721 @@ +use super::poseidon2_params::Poseidon2Params; +use crate::fields::m31::FpM31; +use crate::fields::utils::from_hex; + +use lazy_static::lazy_static; +use std::sync::Arc; + +type Scalar = FpM31; + +lazy_static! { + pub static ref MAT_DIAG16_M_1: Vec = vec![ + from_hex("0x07b80ac4"), + from_hex("0x6bd9cb33"), + from_hex("0x48ee3f9f"), + from_hex("0x4f63dd19"), + from_hex("0x18c546b3"), + from_hex("0x5af89e8b"), + from_hex("0x4ff23de8"), + from_hex("0x4f78aaf6"), + from_hex("0x53bdc6d4"), + from_hex("0x5c59823e"), + from_hex("0x2a471c72"), + from_hex("0x4c975e79"), + from_hex("0x58dc64d4"), + from_hex("0x06e9315d"), + from_hex("0x2cf32286"), + from_hex("0x2fb6755d"), + ]; + pub static ref MAT_INTERNAL16: Vec> = vec![ + vec![ + from_hex("0x07b80ac5"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x6bd9cb34"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x48ee3fa0"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x4f63dd1a"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x18c546b4"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x5af89e8c"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x4ff23de9"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x4f78aaf7"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x53bdc6d5"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x5c59823f"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x2a471c73"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x4c975e7a"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x58dc64d5"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x06e9315e"), + from_hex("0x00000001"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x2cf32287"), + from_hex("0x00000001"), + ], + vec![ + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x00000001"), + from_hex("0x2fb6755e"), + ], + ]; + pub static ref RC16: Vec> = vec![ + vec![ + from_hex("0x768bab52"), + from_hex("0x70e0ab7d"), + from_hex("0x3d266c8a"), + from_hex("0x6da42045"), + from_hex("0x600fef22"), + from_hex("0x41dace6b"), + from_hex("0x64f9bdd4"), + from_hex("0x5d42d4fe"), + from_hex("0x76b1516d"), + from_hex("0x6fc9a717"), + from_hex("0x70ac4fb6"), + from_hex("0x00194ef6"), + from_hex("0x22b644e2"), + from_hex("0x1f7916d5"), + from_hex("0x47581be2"), + from_hex("0x2710a123"), + ], + vec![ + from_hex("0x6284e867"), + from_hex("0x018d3afe"), + from_hex("0x5df99ef3"), + from_hex("0x4c1e467b"), + from_hex("0x566f6abc"), + from_hex("0x2994e427"), + from_hex("0x538a6d42"), + from_hex("0x5d7bf2cf"), + from_hex("0x7fda2dab"), + from_hex("0x0fd854c4"), + from_hex("0x46922fca"), + from_hex("0x3d7763a1"), + from_hex("0x19fd05ca"), + from_hex("0x0a4bbb43"), + from_hex("0x15075851"), + from_hex("0x3d903d76"), + ], + vec![ + from_hex("0x2d290ff7"), + from_hex("0x40809fa0"), + from_hex("0x59dac6ec"), + from_hex("0x127927a2"), + from_hex("0x6bbf0ea0"), + from_hex("0x0294140f"), + from_hex("0x24742976"), + from_hex("0x6e84c081"), + from_hex("0x22484f4a"), + from_hex("0x354cae59"), + from_hex("0x0453ffe1"), + from_hex("0x3f47a3cc"), + from_hex("0x0088204e"), + from_hex("0x6066e109"), + from_hex("0x3b7c4b80"), + from_hex("0x6b55665d"), + ], + vec![ + from_hex("0x3bc4b897"), + from_hex("0x735bf378"), + from_hex("0x508daf42"), + from_hex("0x1884fc2b"), + from_hex("0x7214f24c"), + from_hex("0x7498be0a"), + from_hex("0x1a60e640"), + from_hex("0x3303f928"), + from_hex("0x29b46376"), + from_hex("0x5c96bb68"), + from_hex("0x65d097a5"), + from_hex("0x1d358e9f"), + from_hex("0x4a9a9017"), + from_hex("0x4724cf76"), + from_hex("0x347af70f"), + from_hex("0x1e77e59a"), + ], + vec![ + from_hex("0x7f7ec4bf"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x0421926f"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x5198e669"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x34db3148"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x4368bafd"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x66685c7f"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x78d3249a"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x60187881"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x76dad67a"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x0690b437"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x1ea95311"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x40e5369a"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x38f103fc"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x1d226a21"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + from_hex("0x00000000"), + ], + vec![ + from_hex("0x57090613"), + from_hex("0x1fa42108"), + from_hex("0x17bbef50"), + from_hex("0x1ff7e11c"), + from_hex("0x047b24ca"), + from_hex("0x4e140275"), + from_hex("0x4fa086f5"), + from_hex("0x079b309c"), + from_hex("0x1159bd47"), + from_hex("0x6d37e4e5"), + from_hex("0x075d8dce"), + from_hex("0x12121ca0"), + from_hex("0x7f6a7c40"), + from_hex("0x68e182ba"), + from_hex("0x5493201b"), + from_hex("0x0444a80e"), + ], + vec![ + from_hex("0x0064f4c6"), + from_hex("0x6467abe6"), + from_hex("0x66975762"), + from_hex("0x2af68f9b"), + from_hex("0x345b33be"), + from_hex("0x1b70d47f"), + from_hex("0x053db717"), + from_hex("0x381189cb"), + from_hex("0x43b915f8"), + from_hex("0x20df3694"), + from_hex("0x0f459d26"), + from_hex("0x77a0e97b"), + from_hex("0x2f73e739"), + from_hex("0x1876c2f9"), + from_hex("0x65a0e29a"), + from_hex("0x4cabefbe"), + ], + vec![ + from_hex("0x5abd1268"), + from_hex("0x4d34a760"), + from_hex("0x12771799"), + from_hex("0x69a0c9ac"), + from_hex("0x39091e55"), + from_hex("0x7f611cd0"), + from_hex("0x3af055da"), + from_hex("0x7ac0bbdf"), + from_hex("0x6e0f3a24"), + from_hex("0x41e3b6f7"), + from_hex("0x49b3756d"), + from_hex("0x568bc538"), + from_hex("0x20c079d8"), + from_hex("0x1701c72c"), + from_hex("0x7670dc6c"), + from_hex("0x5a439035"), + ], + vec![ + from_hex("0x7c93e00e"), + from_hex("0x561fbb4d"), + from_hex("0x1178907b"), + from_hex("0x02737406"), + from_hex("0x32fb24f1"), + from_hex("0x6323b60a"), + from_hex("0x6ab12418"), + from_hex("0x42c99cea"), + from_hex("0x155a0b97"), + from_hex("0x53d1c6aa"), + from_hex("0x2bd20347"), + from_hex("0x279b3d73"), + from_hex("0x4f5f3c70"), + from_hex("0x0245af6c"), + from_hex("0x238359d3"), + from_hex("0x49966a59"), + ], + ]; + pub static ref POSEIDON2_M31_16_PARAMS: Arc> = Arc::new( + Poseidon2Params::new(16, 5, 8, 14, &MAT_DIAG16_M_1, &MAT_INTERNAL16, &RC16) + ); +} + From 985dfa5b0c02919daed35e1a56790573b0369e76 Mon Sep 17 00:00:00 2001 From: Antoine FONDEUR Date: Wed, 30 Jul 2025 09:40:04 +0200 Subject: [PATCH 2/3] fixes --- plain_implementations/src/fields/mod.rs | 1 + plain_implementations/src/poseidon2/mod.rs | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/plain_implementations/src/fields/mod.rs b/plain_implementations/src/fields/mod.rs index 12b058e..6ec66c8 100644 --- a/plain_implementations/src/fields/mod.rs +++ b/plain_implementations/src/fields/mod.rs @@ -15,6 +15,7 @@ pub mod babybear; pub mod pallas; pub mod vesta; pub mod utils; +pub mod m31; // sage: // p = 21888242871839275222246405745257275088548364400416034343698204186575808495617 diff --git a/plain_implementations/src/poseidon2/mod.rs b/plain_implementations/src/poseidon2/mod.rs index 902639e..5d4bd1c 100644 --- a/plain_implementations/src/poseidon2/mod.rs +++ b/plain_implementations/src/poseidon2/mod.rs @@ -6,4 +6,5 @@ pub mod poseidon2_instance_babybear; pub mod poseidon2_instance_bls12; pub mod poseidon2_instance_bn256; pub mod poseidon2_instance_pallas; -pub mod poseidon2_instance_vesta; \ No newline at end of file +pub mod poseidon2_instance_vesta; +pub mod poseidon2_instance_m31; From 5f715d0c9b34f427f8b6b05ddbafc82379e73b20 Mon Sep 17 00:00:00 2001 From: Antoine FONDEUR Date: Wed, 30 Jul 2025 09:41:43 +0200 Subject: [PATCH 3/3] remove trace of trunk --- .../src/poseidon2/poseidon2.rs | 142 ++++++------------ 1 file changed, 46 insertions(+), 96 deletions(-) diff --git a/plain_implementations/src/poseidon2/poseidon2.rs b/plain_implementations/src/poseidon2/poseidon2.rs index b8af472..6654d32 100644 --- a/plain_implementations/src/poseidon2/poseidon2.rs +++ b/plain_implementations/src/poseidon2/poseidon2.rs @@ -82,7 +82,7 @@ impl Poseidon2 { } } - fn matmul_m4(&self, input: &mut [F]) { + fn matmul_m4(&self, input: &mut[F]) { let t = self.params.t; let t4 = t / 4; for i in 0..t4 { @@ -116,7 +116,7 @@ impl Poseidon2 { } } - fn matmul_external(&self, input: &mut [F]) { + fn matmul_external(&self, input: &mut[F]) { let t = self.params.t; match t { 2 => { @@ -162,7 +162,7 @@ impl Poseidon2 { } } - fn matmul_internal(&self, input: &mut [F], mat_internal_diag_m_1: &[F]) { + fn matmul_internal(&self, input: &mut[F], mat_internal_diag_m_1: &[F]) { let t = self.params.t; match t { @@ -193,7 +193,7 @@ impl Poseidon2 { input .iter() .skip(1) - .take(t - 1) + .take(t-1) .for_each(|el| sum.add_assign(el)); // Add sum + diag entry * element to each element for i in 0..input.len() { @@ -230,10 +230,12 @@ impl MerkleTreeHash for Poseidon2 { #[cfg(test)] mod poseidon2_tests_goldilocks { use super::*; - use crate::fields::{goldilocks::FpGoldiLocks, utils::from_hex, utils::random_scalar}; + use crate::{fields::{goldilocks::FpGoldiLocks, utils::from_hex, utils::random_scalar}}; use crate::poseidon2::poseidon2_instance_goldilocks::{ - POSEIDON2_GOLDILOCKS_12_PARAMS, POSEIDON2_GOLDILOCKS_16_PARAMS, - POSEIDON2_GOLDILOCKS_20_PARAMS, POSEIDON2_GOLDILOCKS_8_PARAMS, + POSEIDON2_GOLDILOCKS_8_PARAMS, + POSEIDON2_GOLDILOCKS_12_PARAMS, + POSEIDON2_GOLDILOCKS_16_PARAMS, + POSEIDON2_GOLDILOCKS_20_PARAMS, }; use std::convert::TryFrom; @@ -298,9 +300,10 @@ mod poseidon2_tests_goldilocks { #[cfg(test)] mod poseidon2_tests_babybear { use super::*; - use crate::fields::{babybear::FpBabyBear, utils::from_hex, utils::random_scalar}; + use crate::{fields::{babybear::FpBabyBear, utils::from_hex, utils::random_scalar}}; use crate::poseidon2::poseidon2_instance_babybear::{ - POSEIDON2_BABYBEAR_16_PARAMS, POSEIDON2_BABYBEAR_24_PARAMS, + POSEIDON2_BABYBEAR_16_PARAMS, + POSEIDON2_BABYBEAR_24_PARAMS, }; use std::convert::TryFrom; @@ -312,7 +315,7 @@ mod poseidon2_tests_babybear { fn consistent_perm() { let instances = vec![ Poseidon2::new(&POSEIDON2_BABYBEAR_16_PARAMS), - Poseidon2::new(&POSEIDON2_BABYBEAR_24_PARAMS), + Poseidon2::new(&POSEIDON2_BABYBEAR_24_PARAMS) ]; for instance in instances { let t = instance.params.t; @@ -375,9 +378,11 @@ mod poseidon2_tests_babybear { #[cfg(test)] mod poseidon2_tests_bls12 { use super::*; - use crate::fields::{bls12::FpBLS12, utils::from_hex, utils::random_scalar}; + use crate::{fields::{bls12::FpBLS12, utils::from_hex, utils::random_scalar}}; use crate::poseidon2::poseidon2_instance_bls12::{ - POSEIDON2_BLS_2_PARAMS, POSEIDON2_BLS_3_PARAMS, POSEIDON2_BLS_4_PARAMS, + POSEIDON2_BLS_2_PARAMS, + POSEIDON2_BLS_3_PARAMS, + POSEIDON2_BLS_4_PARAMS, POSEIDON2_BLS_8_PARAMS, }; use std::convert::TryFrom; @@ -392,7 +397,7 @@ mod poseidon2_tests_bls12 { Poseidon2::new(&POSEIDON2_BLS_2_PARAMS), Poseidon2::new(&POSEIDON2_BLS_3_PARAMS), Poseidon2::new(&POSEIDON2_BLS_4_PARAMS), - Poseidon2::new(&POSEIDON2_BLS_8_PARAMS), + Poseidon2::new(&POSEIDON2_BLS_8_PARAMS) ]; for instance in instances { let t = instance.params.t; @@ -424,14 +429,8 @@ mod poseidon2_tests_bls12 { input_2.push(Scalar::from(i as u64)); } let perm_2 = poseidon2_2.permutation(&input_2); - assert_eq!( - perm_2[0], - from_hex("0x73c46dd530e248a87b61d19e67fa1b4ed30fc3d09f16531fe189fb945a15ce4e") - ); - assert_eq!( - perm_2[1], - from_hex("0x1f0e305ee21c9366d5793b80251405032a3fee32b9dd0b5f4578262891b043b4") - ); + assert_eq!(perm_2[0], from_hex("0x73c46dd530e248a87b61d19e67fa1b4ed30fc3d09f16531fe189fb945a15ce4e")); + assert_eq!(perm_2[1], from_hex("0x1f0e305ee21c9366d5793b80251405032a3fee32b9dd0b5f4578262891b043b4")); let poseidon2_3 = Poseidon2::new(&POSEIDON2_BLS_3_PARAMS); let mut input_3: Vec = vec![]; @@ -439,18 +438,9 @@ mod poseidon2_tests_bls12 { input_3.push(Scalar::from(i as u64)); } let perm_3 = poseidon2_3.permutation(&input_3); - assert_eq!( - perm_3[0], - from_hex("0x1b152349b1950b6a8ca75ee4407b6e26ca5cca5650534e56ef3fd45761fbf5f0") - ); - assert_eq!( - perm_3[1], - from_hex("0x4c5793c87d51bdc2c08a32108437dc0000bd0275868f09ebc5f36919af5b3891") - ); - assert_eq!( - perm_3[2], - from_hex("0x1fc8ed171e67902ca49863159fe5ba6325318843d13976143b8125f08b50dc6b") - ); + assert_eq!(perm_3[0], from_hex("0x1b152349b1950b6a8ca75ee4407b6e26ca5cca5650534e56ef3fd45761fbf5f0")); + assert_eq!(perm_3[1], from_hex("0x4c5793c87d51bdc2c08a32108437dc0000bd0275868f09ebc5f36919af5b3891")); + assert_eq!(perm_3[2], from_hex("0x1fc8ed171e67902ca49863159fe5ba6325318843d13976143b8125f08b50dc6b")); let poseidon2_4 = Poseidon2::new(&POSEIDON2_BLS_4_PARAMS); let mut input_4: Vec = vec![]; @@ -458,22 +448,10 @@ mod poseidon2_tests_bls12 { input_4.push(Scalar::from(i as u64)); } let perm_4 = poseidon2_4.permutation(&input_4); - assert_eq!( - perm_4[0], - from_hex("0x28ff6c4edf9768c08ae26290487e93449cc8bc155fc2fad92a344adceb3ada6d") - ); - assert_eq!( - perm_4[1], - from_hex("0x0e56f2b6fad25075aa93560185b70e2b180ed7e269159c507c288b6747a0db2d") - ); - assert_eq!( - perm_4[2], - from_hex("0x6d8196f28da6006bb89b3df94600acdc03d0ba7c2b0f3f4409a54c1db6bf30d0") - ); - assert_eq!( - perm_4[3], - from_hex("0x07cfb49540ee456cce38b8a7d1a930a57ffc6660737f6589ef184c5e15334e36") - ); + assert_eq!(perm_4[0], from_hex("0x28ff6c4edf9768c08ae26290487e93449cc8bc155fc2fad92a344adceb3ada6d")); + assert_eq!(perm_4[1], from_hex("0x0e56f2b6fad25075aa93560185b70e2b180ed7e269159c507c288b6747a0db2d")); + assert_eq!(perm_4[2], from_hex("0x6d8196f28da6006bb89b3df94600acdc03d0ba7c2b0f3f4409a54c1db6bf30d0")); + assert_eq!(perm_4[3], from_hex("0x07cfb49540ee456cce38b8a7d1a930a57ffc6660737f6589ef184c5e15334e36")); } } @@ -481,10 +459,7 @@ mod poseidon2_tests_bls12 { #[cfg(test)] mod poseidon2_tests_bn256 { use super::*; - use crate::{ - fields::{bn256::FpBN256, utils::from_hex, utils::random_scalar}, - poseidon2::poseidon2_instance_bn256::POSEIDON2_BN256_PARAMS, - }; + use crate::{fields::{bn256::FpBN256, utils::from_hex, utils::random_scalar}, poseidon2::poseidon2_instance_bn256::POSEIDON2_BN256_PARAMS}; use std::convert::TryFrom; type Scalar = FpBN256; @@ -522,18 +497,10 @@ mod poseidon2_tests_bn256 { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!( - perm[0], - from_hex("0x0bb61d24daca55eebcb1929a82650f328134334da98ea4f847f760054f4a3033") - ); - assert_eq!( - perm[1], - from_hex("0x303b6f7c86d043bfcbcc80214f26a30277a15d3f74ca654992defe7ff8d03570") - ); - assert_eq!( - perm[2], - from_hex("0x1ed25194542b12eef8617361c3ba7c52e660b145994427cc86296242cf766ec8") - ); + assert_eq!(perm[0], from_hex("0x0bb61d24daca55eebcb1929a82650f328134334da98ea4f847f760054f4a3033")); + assert_eq!(perm[1], from_hex("0x303b6f7c86d043bfcbcc80214f26a30277a15d3f74ca654992defe7ff8d03570")); + assert_eq!(perm[2], from_hex("0x1ed25194542b12eef8617361c3ba7c52e660b145994427cc86296242cf766ec8")); + } } @@ -541,9 +508,11 @@ mod poseidon2_tests_bn256 { #[cfg(test)] mod poseidon2_tests_pallas { use super::*; - use crate::fields::{pallas::FpPallas, utils::from_hex, utils::random_scalar}; + use crate::{fields::{pallas::FpPallas, utils::from_hex, utils::random_scalar}}; use crate::poseidon2::poseidon2_instance_pallas::{ - POSEIDON2_PALLAS_3_PARAMS, POSEIDON2_PALLAS_4_PARAMS, POSEIDON2_PALLAS_8_PARAMS, + POSEIDON2_PALLAS_3_PARAMS, + POSEIDON2_PALLAS_4_PARAMS, + POSEIDON2_PALLAS_8_PARAMS, }; use std::convert::TryFrom; @@ -556,7 +525,7 @@ mod poseidon2_tests_pallas { let instances = vec![ Poseidon2::new(&POSEIDON2_PALLAS_3_PARAMS), Poseidon2::new(&POSEIDON2_PALLAS_4_PARAMS), - Poseidon2::new(&POSEIDON2_PALLAS_8_PARAMS), + Poseidon2::new(&POSEIDON2_PALLAS_8_PARAMS) ]; for instance in instances { let t = instance.params.t; @@ -588,18 +557,10 @@ mod poseidon2_tests_pallas { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!( - perm[0], - from_hex("0x1a9b54c7512a914dd778282c44b3513fea7251420b9d95750baae059b2268d7a") - ); - assert_eq!( - perm[1], - from_hex("0x1c48ea0994a7d7984ea338a54dbf0c8681f5af883fe988d59ba3380c9f7901fc") - ); - assert_eq!( - perm[2], - from_hex("0x079ddd0a80a3e9414489b526a2770448964766685f4c4842c838f8a23120b401") - ); + assert_eq!(perm[0], from_hex("0x1a9b54c7512a914dd778282c44b3513fea7251420b9d95750baae059b2268d7a")); + assert_eq!(perm[1], from_hex("0x1c48ea0994a7d7984ea338a54dbf0c8681f5af883fe988d59ba3380c9f7901fc")); + assert_eq!(perm[2], from_hex("0x079ddd0a80a3e9414489b526a2770448964766685f4c4842c838f8a23120b401")); + } } @@ -607,10 +568,7 @@ mod poseidon2_tests_pallas { #[cfg(test)] mod poseidon2_tests_vesta { use super::*; - use crate::{ - fields::{utils::from_hex, utils::random_scalar, vesta::FpVesta}, - poseidon2::poseidon2_instance_vesta::POSEIDON2_VESTA_PARAMS, - }; + use crate::{fields::{vesta::FpVesta, utils::from_hex, utils::random_scalar}, poseidon2::poseidon2_instance_vesta::POSEIDON2_VESTA_PARAMS}; use std::convert::TryFrom; type Scalar = FpVesta; @@ -648,18 +606,10 @@ mod poseidon2_tests_vesta { input.push(Scalar::from(i as u64)); } let perm = poseidon2.permutation(&input); - assert_eq!( - perm[0], - from_hex("0x261ecbdfd62c617b82d297705f18c788fc9831b14a6a2b8f61229bef68ce2792") - ); - assert_eq!( - perm[1], - from_hex("0x2c76327e0b7653873263158cf8545c282364b183880fcdea93ca8526d518c66f") - ); - assert_eq!( - perm[2], - from_hex("0x262316c0ce5244838c75873299b59d763ae0849d2dd31bdc95caf7db1c2901bf") - ); + assert_eq!(perm[0], from_hex("0x261ecbdfd62c617b82d297705f18c788fc9831b14a6a2b8f61229bef68ce2792")); + assert_eq!(perm[1], from_hex("0x2c76327e0b7653873263158cf8545c282364b183880fcdea93ca8526d518c66f")); + assert_eq!(perm[2], from_hex("0x262316c0ce5244838c75873299b59d763ae0849d2dd31bdc95caf7db1c2901bf")); + } }