Skip to content

Commit

Permalink
feat: configurable nightly optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaMasych committed Jan 29, 2025
1 parent 6eb63f0 commit 7e779ed
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 296 deletions.
4 changes: 4 additions & 0 deletions field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ homepage.workspace = true
repository.workspace = true
keywords.workspace = true
categories.workspace = true
build = "build.rs"

[dependencies]
anyhow = { workspace = true }
Expand All @@ -29,3 +30,6 @@ rustdoc-args = ["--html-in-header", ".cargo/katex-header.html"]

[lints]
workspace = true

[build-dependencies]
rustc_version = "0.4"
12 changes: 12 additions & 0 deletions field/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use rustc_version::{Channel, VersionMeta};

fn main() {
let version_meta = VersionMeta::for_command(std::process::Command::new("rustc")).unwrap();

if version_meta.channel == Channel::Nightly {
println!("cargo:rustc-cfg=nightly");
}

// Declare `nightly` as a valid `cfg` condition for rustc to prevent warnings
println!("cargo::rustc-check-cfg=cfg(nightly)")
}
19 changes: 19 additions & 0 deletions field/src/extension/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use num::bigint::BigUint;
use serde::{Deserialize, Serialize};

use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
#[cfg(nightly)]
use crate::ops::Square;
use crate::types::{Field, Sample};

Expand Down Expand Up @@ -177,6 +178,7 @@ impl<F: Extendable<2>> SubAssign for QuadraticExtension<F> {
}
}

#[cfg(nightly)]
impl<F: Extendable<2>> Mul for QuadraticExtension<F> {
type Output = Self;

Expand All @@ -192,13 +194,30 @@ impl<F: Extendable<2>> Mul for QuadraticExtension<F> {
}
}

#[cfg(not(nightly))]
impl<F: Extendable<2>> Mul for QuadraticExtension<F> {
type Output = Self;

#[inline]
fn mul(self, rhs: Self) -> Self {
let Self([a0, a1]) = self;
let Self([b0, b1]) = rhs;

let c0 = a0 * b0 + <Self as OEF<2>>::W * a1 * b1;
let c1 = a0 * b1 + a1 * b0;

Self([c0, c1])
}
}

impl<F: Extendable<2>> MulAssign for QuadraticExtension<F> {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

#[cfg(nightly)]
impl<F: Extendable<2>> Square for QuadraticExtension<F> {
#[inline(always)]
fn square(&self) -> Self {
Expand Down
21 changes: 21 additions & 0 deletions field/src/extension/quartic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use num::traits::Pow;
use serde::{Deserialize, Serialize};

use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
#[cfg(nightly)]
use crate::ops::Square;
use crate::types::{Field, Sample};

Expand Down Expand Up @@ -201,6 +202,7 @@ impl<F: Extendable<4>> SubAssign for QuarticExtension<F> {
}
}

#[cfg(nightly)]
impl<F: Extendable<4>> Mul for QuarticExtension<F> {
type Output = Self;

Expand All @@ -218,13 +220,32 @@ impl<F: Extendable<4>> Mul for QuarticExtension<F> {
}
}

#[cfg(not(nightly))]
impl<F: Extendable<4>> Mul for QuarticExtension<F> {
type Output = Self;

#[inline]
fn mul(self, rhs: Self) -> Self {
let Self([a0, a1, a2, a3]) = self;
let Self([b0, b1, b2, b3]) = rhs;

let c0 = a0 * b0 + <Self as OEF<4>>::W * (a1 * b3 + a2 * b2 + a3 * b1);
let c1 = a0 * b1 + a1 * b0 + <Self as OEF<4>>::W * (a2 * b3 + a3 * b2);
let c2 = a0 * b2 + a1 * b1 + a2 * b0 + <Self as OEF<4>>::W * a3 * b3;
let c3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0;

Self([c0, c1, c2, c3])
}
}

impl<F: Extendable<4>> MulAssign for QuarticExtension<F> {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

#[cfg(nightly)]
impl<F: Extendable<4>> Square for QuarticExtension<F> {
#[inline(always)]
fn square(&self) -> Self {
Expand Down
23 changes: 23 additions & 0 deletions field/src/extension/quintic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use num::traits::Pow;
use serde::{Deserialize, Serialize};

use crate::extension::{Extendable, FieldExtension, Frobenius, OEF};
#[cfg(nightly)]
use crate::ops::Square;
use crate::types::{Field, Sample};

Expand Down Expand Up @@ -210,6 +211,7 @@ impl<F: Extendable<5>> SubAssign for QuinticExtension<F> {
}
}

#[cfg(nightly)]
impl<F: Extendable<5>> Mul for QuinticExtension<F> {
type Output = Self;

Expand All @@ -229,13 +231,34 @@ impl<F: Extendable<5>> Mul for QuinticExtension<F> {
}
}

#[cfg(not(nightly))]
impl<F: Extendable<5>> Mul for QuinticExtension<F> {
type Output = Self;

#[inline]
fn mul(self, rhs: Self) -> Self {
let Self([a0, a1, a2, a3, a4]) = self;
let Self([b0, b1, b2, b3, b4]) = rhs;
let w = <Self as OEF<5>>::W;

let c0 = a0 * b0 + w * (a1 * b4 + a2 * b3 + a3 * b2 + a4 * b1);
let c1 = a0 * b1 + a1 * b0 + w * (a2 * b4 + a3 * b3 + a4 * b2);
let c2 = a0 * b2 + a1 * b1 + a2 * b0 + w * (a3 * b4 + a4 * b3);
let c3 = a0 * b3 + a1 * b2 + a2 * b1 + a3 * b0 + w * a4 * b4;
let c4 = a0 * b4 + a1 * b3 + a2 * b2 + a3 * b1 + a4 * b0;

Self([c0, c1, c2, c3, c4])
}
}

impl<F: Extendable<5>> MulAssign for QuinticExtension<F> {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}

#[cfg(nightly)]
impl<F: Extendable<5>> Square for QuinticExtension<F> {
#[inline(always)]
fn square(&self) -> Self {
Expand Down
Loading

0 comments on commit 7e779ed

Please sign in to comment.