Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sync] upstream inverse acceleration #10

Merged
merged 3 commits into from
Aug 17, 2024
Merged
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
12 changes: 10 additions & 2 deletions benches/bn256_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use halo2curves::bn256::*;
use halo2curves::ff::Field;
use halo2curves::{bn256::*, ff::Field, legendre::Legendre};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

Expand Down Expand Up @@ -40,6 +39,15 @@ pub fn bench_bn256_field(c: &mut Criterion) {
group.bench_function("bn256_fq_square", |bencher| {
bencher.iter(|| black_box(&a).square())
});
group.bench_function("bn256_fq_invert", |bencher| {
bencher.iter(|| black_box(&a).invert())
});
group.bench_function("bn256_fq_legendre", |bencher| {
bencher.iter(|| black_box(&a).legendre())
});
group.bench_function("bn256_fq_jacobi", |bencher| {
bencher.iter(|| black_box(&a).jacobi())
});
}

criterion_group!(benches, bench_bn256_field);
Expand Down
13 changes: 3 additions & 10 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,10 @@ impl ff::Field for Fq {
ff::helpers::sqrt_ratio_generic(num, div)
}

/// Computes the multiplicative inverse of this element,
/// failing if the element is zero.
/// Returns the multiplicative inverse of the
/// element. If it is zero, the method fails.
fn invert(&self) -> CtOption<Self> {
let tmp = self.pow([
0x3c208c16d87cfd45,
0x97816a916871ca8d,
0xb85045b68181585d,
0x30644e72e131a029,
]);

CtOption::new(tmp, !self.ct_eq(&Self::zero()))
self.invert()
}
}

Expand Down
13 changes: 3 additions & 10 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,10 @@ impl ff::Field for Fr {
self.square()
}

/// Computes the multiplicative inverse of this element,
/// failing if the element is zero.
/// Returns the multiplicative inverse of the
/// element. If it is zero, the method fails.
fn invert(&self) -> CtOption<Self> {
let tmp = self.pow([
0x43e1f593efffffff,
0x2833e84879b97091,
0xb85045b68181585d,
0x30644e72e131a029,
]);

CtOption::new(tmp, !self.ct_eq(&Self::zero()))
self.invert()
}

fn sqrt(&self) -> CtOption<Self> {
Expand Down
44 changes: 44 additions & 0 deletions src/derive/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ macro_rules! field_common {
$r2:ident,
$r3:ident
) => {
/// Bernstein-Yang modular multiplicative inverter created for the modulus equal to
/// the characteristic of the field to invert positive integers in the Montgomery form.
const BYINVERTOR: $crate::ff_inverse::BYInverter<6> =
$crate::ff_inverse::BYInverter::<6>::new(&$modulus.0, &$r2.0);

impl $field {
/// Returns zero, the additive identity.
#[inline]
Expand All @@ -37,6 +42,22 @@ macro_rules! field_common {
$r
}

/// Returns the multiplicative inverse of the
/// element. If it is zero, the method fails.
pub fn invert(&self) -> CtOption<Self> {
if let Some(inverse) = BYINVERTOR.invert(&self.0) {
CtOption::new(Self(inverse), Choice::from(1))
} else {
CtOption::new(Self::zero(), Choice::from(0))
}
}

// Returns the Legendre symbol, where the numerator and denominator
// are the element and the characteristic of the field, respectively.
pub fn jacobi(&self) -> i64 {
$crate::ff_jacobi::jacobi::<5>(&self.0, &$modulus.0)
}

fn from_u512(limbs: [u64; 8]) -> $field {
// We reduce an arbitrary 512-bit number by decomposing it into two 256-bit digits
// with the higher bits multiplied by 2^256. Thus, we perform two reductions
Expand Down Expand Up @@ -332,13 +353,36 @@ macro_rules! field_common {
Ok(())
}
}

#[test]
fn test_jacobi() {
use rand::SeedableRng;
use $crate::ff::Field;
use $crate::legendre::Legendre;
let mut rng = rand_xorshift::XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
]);
for _ in 0..100000 {
let e = $field::random(&mut rng);
assert_eq!(
e.legendre(),
match e.jacobi() {
1 => $field::ONE,
-1 => -$field::ONE,
_ => $field::ZERO,
}
);
}
}
};
}

#[macro_export]
macro_rules! field_arithmetic {
($field:ident, $modulus:ident, $inv:ident, $field_type:ident) => {
field_specific!($field, $modulus, $inv, $field_type);

impl $field {
/// Doubles this field element.
#[inline]
Expand Down
Loading
Loading