Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/halo2_proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ tracing = "0.1"
[target.'cfg(all(target_arch = "aarch64", target_vendor = "apple"))'.dependencies]
pasta_curves = { workspace = true, features = ["default", "aarch64-asm"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
pasta_curves = { workspace = true, features = ["default", "x86_64-asm"] }

[dev-dependencies]
assert_matches = "1.5"
criterion = "0.3"
Expand Down
3 changes: 3 additions & 0 deletions crates/pasta_curves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ repr-c = []
serde = ["hex", "serde_crate"]
sqrt-table = ["alloc", "dep:once_cell"]
uninline-portable = []
# Inline-assembly Montgomery multiplication using BMI2/ADX (x86_64 only;
# requires an ADX-capable CPU: Intel Broadwell+ or AMD Zen+).
x86_64-asm = []

[[bench]]
name = "fp"
Expand Down
7 changes: 7 additions & 0 deletions crates/pasta_curves/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ fn mul_by_inverse_power_of_two(
))]
mod aarch64_asm;

// The x86_64 Montgomery assembly relies on inline `asm!`, which is
// contained within a private module whose public interface consists only of
// safe wrappers.
#[allow(unsafe_code)]
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
mod x86_64_asm;

pub use fp::*;
pub use fq::*;

Expand Down
68 changes: 52 additions & 16 deletions crates/pasta_curves/src/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,18 @@ impl Fp {
Fp(super::aarch64_asm::mul(&self.0, &rhs.0, &MODULUS.0, INV))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fp(super::x86_64_asm::mul(&self.0, &rhs.0, &MODULUS.0, INV))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
self.mul(rhs)
Expand All @@ -422,10 +430,18 @@ impl Fp {
Fp(super::aarch64_asm::square(&self.0, &MODULUS.0, INV))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fp(super::x86_64_asm::square(&self.0, &MODULUS.0, INV))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
self.square()
Expand All @@ -450,10 +466,20 @@ impl Fp {
))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fp(super::x86_64_asm::sqr_n_mul(
&self.0, n as usize, &by.0, &MODULUS.0, INV,
))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
// Leave the accumulator unreduced between squarings. The closing
Expand Down Expand Up @@ -486,10 +512,20 @@ impl Fp {
}
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fp(super::x86_64_asm::sqr_n(
&self.0, n as usize, &MODULUS.0, INV,
))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
Fp(portable::canonicalize(
Expand Down
68 changes: 52 additions & 16 deletions crates/pasta_curves/src/fields/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,18 @@ impl Fq {
Fq(super::aarch64_asm::mul(&self.0, &rhs.0, &MODULUS.0, INV))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fq(super::x86_64_asm::mul(&self.0, &rhs.0, &MODULUS.0, INV))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
self.mul(rhs)
Expand All @@ -422,10 +430,18 @@ impl Fq {
Fq(super::aarch64_asm::square(&self.0, &MODULUS.0, INV))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fq(super::x86_64_asm::square(&self.0, &MODULUS.0, INV))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
self.square()
Expand All @@ -450,10 +466,20 @@ impl Fq {
))
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fq(super::x86_64_asm::sqr_n_mul(
&self.0, n as usize, &by.0, &MODULUS.0, INV,
))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
// Leave the accumulator unreduced between squarings. The closing
Expand Down Expand Up @@ -486,10 +512,20 @@ impl Fq {
}
}

#[cfg(not(all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
#[cfg(all(feature = "x86_64-asm", target_arch = "x86_64"))]
{
Fq(super::x86_64_asm::sqr_n(
&self.0, n as usize, &MODULUS.0, INV,
))
}

#[cfg(not(any(
all(
feature = "aarch64-asm",
target_arch = "aarch64",
target_vendor = "apple"
),
all(feature = "x86_64-asm", target_arch = "x86_64")
)))]
{
Fq(portable::canonicalize(
Expand Down
5 changes: 5 additions & 0 deletions crates/pasta_curves/src/fields/portable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use crate::arithmetic::{adc, mac, sbb};

#[cfg(target_arch = "x86_64")]
#[cfg_attr(feature = "x86_64-asm", allow(dead_code))]
const PASTA_MODULUS_TOP_SHIFT: u32 = 62;
#[cfg(target_arch = "x86_64")]
#[cfg_attr(feature = "x86_64-asm", allow(dead_code))]
const PASTA_MODULUS_TOP_OVERFLOW_SHIFT: u32 = u64::BITS - PASTA_MODULUS_TOP_SHIFT;

/// Squares a canonical element, returning the canonical square.
Expand Down Expand Up @@ -162,6 +164,7 @@ pub(super) const fn montgomery_reduce_low_lazy(

/// Adds `k * 2^62` to `value` and `carry`, returning the low and high limbs.
#[cfg(target_arch = "x86_64")]
#[cfg_attr(feature = "x86_64-asm", allow(dead_code))]
#[inline(always)]
const fn mac_pasta_modulus_top(value: u64, k: u64, carry: u64) -> (u64, u64) {
let (low, carry) = adc(value, k << PASTA_MODULUS_TOP_SHIFT, carry);
Expand All @@ -170,6 +173,7 @@ const fn mac_pasta_modulus_top(value: u64, k: u64, carry: u64) -> (u64, u64) {

/// Squares and lazily reduces while interleaving independent upper-half work.
#[cfg(target_arch = "x86_64")]
#[cfg_attr(feature = "x86_64-asm", allow(dead_code))]
#[inline(always)]
const fn square_reduce_lazy_pasta(value: &[u64; 4], modulus: &[u64; 4], inv: u64) -> [u64; 4] {
debug_assert!(modulus[2] == 0);
Expand Down Expand Up @@ -296,6 +300,7 @@ pub(super) const fn canonicalize(value: &[u64; 4], modulus: &[u64; 4]) -> [u64;
),
allow(dead_code)
)]
#[cfg_attr(all(feature = "x86_64-asm", target_arch = "x86_64"), allow(dead_code))]
pub(super) fn sqr_n_lazy(value: &[u64; 4], n: u32, modulus: &[u64; 4], inv: u64) -> [u64; 4] {
let mut acc = *value;
for _ in 0..n {
Expand Down
Loading