use alloc::vec::Vec;
use core::iter::Sum;
use core::mem::MaybeUninit;
use core::ops::Mul;
use p3_maybe_rayon::prelude::*;
use crate::field::Field;
use crate::{PackedValue, PrimeCharacteristicRing, PrimeField, PrimeField32};
pub fn cyclic_subgroup_known_order<F: Field>(
generator: F,
order: usize,
) -> impl Iterator<Item = F> + Clone {
generator.powers().take(order)
}
pub fn cyclic_subgroup_coset_known_order<F: Field>(
generator: F,
shift: F,
order: usize,
) -> impl Iterator<Item = F> + Clone {
generator.shifted_powers(shift).take(order)
}
pub fn scale_slice_in_place_single_core<F: Field>(slice: &mut [F], s: F) {
let (packed, sfx) = F::Packing::pack_slice_with_suffix_mut(slice);
let packed_s: F::Packing = s.into();
packed.iter_mut().for_each(|x| *x *= packed_s);
sfx.iter_mut().for_each(|x| *x *= s);
}
#[inline]
pub fn par_scale_slice_in_place<F: Field>(slice: &mut [F], s: F) {
let (packed, sfx) = F::Packing::pack_slice_with_suffix_mut(slice);
let packed_s: F::Packing = s.into();
packed.par_iter_mut().for_each(|x| *x *= packed_s);
sfx.iter_mut().for_each(|x| *x *= s);
}
pub fn add_scaled_slice_in_place<F: Field>(slice: &mut [F], other: &[F], s: F) {
debug_assert_eq!(slice.len(), other.len(), "slices must have equal length");
let (slice_packed, slice_sfx) = F::Packing::pack_slice_with_suffix_mut(slice);
let (other_packed, other_sfx) = F::Packing::pack_slice_with_suffix(other);
let packed_s: F::Packing = s.into();
slice_packed
.iter_mut()
.zip(other_packed)
.for_each(|(x, y)| *x += *y * packed_s);
slice_sfx
.iter_mut()
.zip(other_sfx)
.for_each(|(x, y)| *x += *y * s);
}
pub fn par_add_scaled_slice_in_place<F: Field>(slice: &mut [F], other: &[F], s: F) {
debug_assert_eq!(slice.len(), other.len(), "slices must have equal length");
let (slice_packed, slice_sfx) = F::Packing::pack_slice_with_suffix_mut(slice);
let (other_packed, other_sfx) = F::Packing::pack_slice_with_suffix(other);
let packed_s: F::Packing = s.into();
slice_packed
.par_iter_mut()
.zip(other_packed.par_iter())
.for_each(|(x, y)| *x += *y * packed_s);
slice_sfx
.iter_mut()
.zip(other_sfx)
.for_each(|(x, y)| *x += *y * s);
}
#[inline]
#[must_use]
pub const fn field_to_array<R: PrimeCharacteristicRing, const D: usize>(x: R) -> [R; D] {
let mut arr = [const { MaybeUninit::uninit() }; D];
arr[0] = MaybeUninit::new(x);
let mut i = 1;
while i < D {
arr[i] = MaybeUninit::new(R::ZERO);
i += 1;
}
unsafe { core::mem::transmute_copy::<_, [R; D]>(&arr) }
}
#[inline]
#[must_use]
pub const fn halve_u32<const P: u32>(x: u32) -> u32 {
let shift = (P + 1) >> 1;
let half = x >> 1;
if x & 1 == 0 { half } else { half + shift }
}
#[inline]
#[must_use]
pub const fn halve_u64<const P: u64>(x: u64) -> u64 {
let shift = (P + 1) >> 1;
let half = x >> 1;
if x & 1 == 0 { half } else { half + shift }
}
#[must_use]
pub fn reduce_32<SF: PrimeField32, TF: PrimeField>(vals: &[SF]) -> TF {
let base = TF::from_int(1u64 << 32);
vals.iter().rev().fold(TF::ZERO, |acc, val| {
acc * base + TF::from_int(val.as_canonical_u32())
})
}
#[must_use]
pub fn split_32<SF: PrimeField, TF: PrimeField32>(val: SF, n: usize) -> Vec<TF> {
let mut result: Vec<TF> = val
.as_canonical_biguint()
.to_u64_digits()
.iter()
.take(n)
.map(|d| TF::from_u64(*d))
.collect();
result.resize_with(n, || TF::ZERO);
result
}
#[must_use]
pub fn dot_product<S, LI, RI>(li: LI, ri: RI) -> S
where
LI: Iterator,
RI: Iterator,
LI::Item: Mul<RI::Item>,
S: Sum<<LI::Item as Mul<RI::Item>>::Output>,
{
li.zip(ri).map(|(l, r)| l * r).sum()
}