Skip to content

Commit

Permalink
add fps_{int,log,exp,pow}
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtkana committed Sep 28, 2023
1 parent f38ed28 commit a201a8b
Show file tree
Hide file tree
Showing 3 changed files with 501 additions and 95 deletions.
16 changes: 9 additions & 7 deletions libs/fp2/src/fourier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ type F3 = Fp<P3>;
/// let c = fps_mul(&a, &b);
/// assert_eq!(c, vec![fp!(4), fp!(13), fp!(28), fp!(27), fp!(18)]);
/// ```
pub fn fps_mul<const P: u64>(a: &[Fp<P>], b: &[Fp<P>]) -> Vec<Fp<P>>
pub fn fps_mul<const P: u64>(a: impl AsRef<[Fp<P>]>, b: impl AsRef<[Fp<P>]>) -> Vec<Fp<P>>
where
(): PrimitiveRoot<P>,
{
let a = a.as_ref();
let b = b.as_ref();
if a.is_empty() || b.is_empty() {
return vec![];
}
Expand All @@ -48,16 +50,16 @@ where
/// Multiplies two polynomials.
pub fn any_mod_fps_mul<const P: u64>(a: &[Fp<P>], b: &[Fp<P>]) -> Vec<Fp<P>> {
let v1 = fps_mul(
&a.iter().map(|&x| F1::new(x.value())).collect::<Vec<_>>(),
&b.iter().map(|&x| F1::new(x.value())).collect::<Vec<_>>(),
a.iter().map(|&x| F1::new(x.value())).collect::<Vec<_>>(),
b.iter().map(|&x| F1::new(x.value())).collect::<Vec<_>>(),
);
let v2 = fps_mul(
&a.iter().map(|&x| F2::new(x.value())).collect::<Vec<_>>(),
&b.iter().map(|&x| F2::new(x.value())).collect::<Vec<_>>(),
a.iter().map(|&x| F2::new(x.value())).collect::<Vec<_>>(),
b.iter().map(|&x| F2::new(x.value())).collect::<Vec<_>>(),
);
let v3 = fps_mul(
&a.iter().map(|&x| F3::new(x.value())).collect::<Vec<_>>(),
&b.iter().map(|&x| F3::new(x.value())).collect::<Vec<_>>(),
a.iter().map(|&x| F3::new(x.value())).collect::<Vec<_>>(),
b.iter().map(|&x| F3::new(x.value())).collect::<Vec<_>>(),
);
v1.into_iter()
.zip(v2)
Expand Down
42 changes: 27 additions & 15 deletions libs/fp2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! ```
//! use fp2::fp;
//! use fp2::Fp;
//! type F = Fp<998244353>;
//! let a = F::new(3);
//! let b = F::new(4);
//! assert_eq!(a + b, F::new(7));
//! assert_eq!(a - b, F::new(998244352));
//! assert_eq!(a * b, F::new(12));
//! assert_eq!(a / b * b, F::new(3));
//! assert_eq!(a.pow(3), F::new(27));
//! type Fp = Fp<998244353>;
//! let a = Fp::new(3);
//! let b = Fp::new(4);
//! assert_eq!(a + b, Fp::new(7));
//! assert_eq!(a - b, Fp::new(998244352));
//! assert_eq!(a * b, Fp::new(12));
//! assert_eq!(a / b * b, Fp::new(3));
//! assert_eq!(a.pow(3), Fp::new(27));
//! ```
//!
//! ## Factorials
Expand Down Expand Up @@ -91,19 +91,18 @@ impl PrimitiveRoot<924844033> for () {
const VALUE: Fp<924844033> = Fp::new(5);
}

/// A value in $\mathbb{F}_p$.
/// A value in $\mathbb{Fp}_p$.
/// # Requirements
/// - $P$ is odd and prime ($P \gt 2^{31}$)
/// # Invariants
/// - $0 \le \text{value} < P$
/// # Examples
/// ```
/// use fp2::Fp;
/// type F = Fp<998244353>;
/// assert_eq!(F::new(3) + F::new(4), F::new(7));
/// assert_eq!(F::new(3) - F::new(4), F::new(998244352));
/// assert_eq!(F::new(3) * F::new(4), F::new(12));
/// assert_eq!(F::new(3) / F::new(4) * F::new(4), F::new(3));
/// type Fp = fp2::Fp<998244353>;
/// assert_eq!(Fp::new(3) + Fp::new(4), Fp::new(7));
/// assert_eq!(Fp::new(3) - Fp::new(4), Fp::new(998244352));
/// assert_eq!(Fp::new(3) * Fp::new(4), Fp::new(12));
/// assert_eq!(Fp::new(3) / Fp::new(4) * Fp::new(4), Fp::new(3));
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Fp<const P: u64> {
Expand Down Expand Up @@ -164,6 +163,19 @@ impl<const P: u64> Fp<P> {
}
result
}

/// Returns $(-1)^{\text{pow}}$.
///
/// # Examples
/// ```
/// use fp2::Fp;
/// type Fp = Fp<998244353>;
/// assert_eq!(Fp::sign(0), Fp::new(1));
/// assert_eq!(Fp::sign(1), Fp::new(-1));
/// assert_eq!(Fp::sign(2), Fp::new(1));
/// assert_eq!(Fp::sign(3), Fp::new(-1));
/// ```
pub fn sign(pow: usize) -> Self { Self::new(if pow % 2 == 0 { 1 } else { P - 1 }) }
}
impl<const P: u64> std::fmt::Debug for Fp<P> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down
Loading

0 comments on commit a201a8b

Please sign in to comment.