Skip to content

Commit

Permalink
Add secure powers generation for simd
Browse files Browse the repository at this point in the history
  • Loading branch information
Gali-StarkWare committed Dec 10, 2024
1 parent 060f0e4 commit 3057bea
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions crates/prover/src/core/backend/simd/utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -51,11 +56,31 @@ impl<T> UnsafeConst<T> {
unsafe impl<T> Send for UnsafeConst<T> {}
unsafe impl<T> Sync for UnsafeConst<T> {}

// 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::<Vec<M31>>()
.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() {
Expand All @@ -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));
}
}

0 comments on commit 3057bea

Please sign in to comment.