Skip to content

Commit

Permalink
Sync with PSE upstream (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang authored Nov 13, 2023
2 parents e185711 + e4e6205 commit f11edd0
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 18 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "halo2curves"
version = "0.4.0"
version = "0.4.1"
authors = ["Privacy Scaling Explorations team"]
license = "MIT/Apache-2.0"
edition = "2021"
Expand Down Expand Up @@ -36,15 +36,14 @@ blake2b_simd = "1"
maybe-rayon = { version = "0.1.0", default-features = false }

[features]
default = ["reexport", "bits", "multicore", "bn256-table", "derive_serde"]
default = ["bits", "multicore", "bn256-table", "derive_serde"]
multicore = ["maybe-rayon/threads"]
asm = []
bits = ["ff/bits"]
bn256-table = []
derive_serde = ["serde/derive", "serde_arrays", "hex"]
prefetch = []
print-trace = ["ark-std/print-trace"]
reexport = []

[profile.bench]
opt-level = 3
Expand All @@ -62,7 +61,6 @@ harness = false
[[bench]]
name = "bn256_field"
harness = false
required-features = ["reexport"]

[[bench]]
name = "group"
Expand Down
9 changes: 7 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 @@ -43,6 +42,12 @@ pub fn bench_bn256_field(c: &mut Criterion) {
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
32 changes: 30 additions & 2 deletions src/derive/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ macro_rules! field_common {
) => {
/// 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::bernsteinyang::BYInverter<6> =
$crate::bernsteinyang::BYInverter::<6>::new(&$modulus.0, &$r2.0);
const BYINVERTOR: $crate::ff_inverse::BYInverter<6> =
$crate::ff_inverse::BYInverter::<6>::new(&$modulus.0, &$r2.0);

impl $field {
/// Returns zero, the additive identity.
Expand All @@ -52,6 +52,12 @@ macro_rules! field_common {
}
}

// 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 @@ -353,6 +359,28 @@ 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,
}
);
}
}
};
}

Expand Down
File renamed without changes.
Loading

0 comments on commit f11edd0

Please sign in to comment.