From 3057bea73e1426b6f7d35464e3c76506726c66a5 Mon Sep 17 00:00:00 2001 From: Gali Michlevich Date: Tue, 10 Dec 2024 13:16:46 +0200 Subject: [PATCH] Add secure powers generation for simd --- crates/prover/src/core/backend/simd/utils.rs | 37 ++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/crates/prover/src/core/backend/simd/utils.rs b/crates/prover/src/core/backend/simd/utils.rs index d5f53a22b..d9c287c1c 100644 --- a/crates/prover/src/core/backend/simd/utils.rs +++ b/crates/prover/src/core/backend/simd/utils.rs @@ -1,5 +1,10 @@ use std::simd::Swizzle; +use num_traits::One; + +use crate::core::backend::simd::m31::{PackedM31, N_LANES}; +use crate::core::fields::m31::M31; + /// Used with [`Swizzle::concat_swizzle`] to interleave the even values of two vectors. pub struct InterleaveEvens; @@ -51,11 +56,31 @@ impl UnsafeConst { unsafe impl Send for UnsafeConst {} unsafe impl Sync for UnsafeConst {} +// TODO(Gali): Remove #[allow(dead_code)]. +#[allow(dead_code)] +pub fn generate_secure_powers(felt: M31) -> PackedM31 { + let arr: [M31; N_LANES] = (0..N_LANES) + .scan(M31::one(), |acc, _| { + let res = *acc; + *acc *= felt; + Some(res) + }) + .collect::>() + .try_into() + .expect("Failed generating secure powers."); + + PackedM31::from_array(arr) +} + #[cfg(test)] mod tests { use std::simd::{u32x4, Swizzle}; + use num_traits::One; + use super::{InterleaveEvens, InterleaveOdds}; + use crate::core::fields::m31::M31; + use crate::core::fields::FieldExpOps; #[test] fn interleave_evens() { @@ -76,4 +101,16 @@ mod tests { assert_eq!(res, u32x4::from_array([1, 5, 3, 7])); } + + #[test] + fn generate_secure_powers_works() { + let felt = M31(2); + + let powers = super::generate_secure_powers(felt); + let powers = powers.to_array(); + + assert_eq!(powers[0], M31::one()); + assert_eq!(powers[1], felt); + assert_eq!(powers[7], felt.pow(7)); + } }