Skip to content

Commit

Permalink
Sync with PSE upstream (#9)
Browse files Browse the repository at this point in the history
* Add field conversion to/from `[u64;4]` (privacy-scaling-explorations#80)

* feat: add field conversion to/from `[u64;4]`

* Added conversion tests
* Added `montgomery_reduce_short` for no-asm
* For bn256, uses assembly conversion when asm feature is on

* fix: remove conflict for asm

* chore: bump rust-toolchain to 1.67.0

* Compute Legendre symbol for `hash_to_curve` (privacy-scaling-explorations#77)

* Add `Legendre` trait and macro

 - Add Legendre macro with norm and legendre symbol computation
 - Add macro for automatic implementation in prime fields

* Add legendre macro call for prime fields

* Remove unused imports

* Remove leftover

* Add `is_quadratic_non_residue` for hash_to_curve

* Add `legendre` function

* Compute modulus separately

* Substitute division for shift

* Update modulus computation

* Add quadratic residue check func

* Add quadratic residue tests

* Add hash_to_curve bench

* Implement Legendre trait for all curves

* Move misplaced comment

* Add all curves to hash bench

* fix: add suggestion for legendre_exp

* fix: imports after rebase

* Add simplified SWU method (privacy-scaling-explorations#81)

* Fix broken link

* Add simple SWU algorithm

* Add simplified SWU hash_to_curve for secp256r1

* add: sswu z reference

* update MAP_ID identifier

Co-authored-by: Han <[email protected]>

---------

Co-authored-by: Han <[email protected]>

* Bring back curve algorithms for `a = 0` (privacy-scaling-explorations#82)

* refactor: bring back curve algorithms for `a = 0`

* fix: clippy warning

* fix: Improve serialization for prime fields (privacy-scaling-explorations#85)

* fix: Improve serialization for prime fields

Summary: 256-bit field serialization is currently 4x u64, ie. the native format. This implements the standard of byte-serialization (corresponding to the PrimeField::{to,from}_repr), and an hex-encoded variant of
that for (de)serializers that are human-readable (concretely, json).

- Added a new macro `serialize_deserialize_32_byte_primefield!` for custom serialization and deserialization of 32-byte prime field in different struct (Fq, Fp, Fr) across the secp256r, bn256, and derive libraries.
- Implemented the new macro for serialization and deserialization in various structs, replacing the previous `serde::{Deserialize, Serialize}` direct use.
- Enhanced error checking in the custom serialization methods to ensure valid field elements.
- Updated the test function in the tests/field.rs file to include JSON serialization and deserialization tests for object integrity checking.

* fixup! fix: Improve serialization for prime fields

---------

Co-authored-by: Carlos Pérez <[email protected]>

* refactor: (De)Serialization of points using `GroupEncoding` (privacy-scaling-explorations#88)

* refactor: implement (De)Serialization of points using the `GroupEncoding` trait

- Updated curve point (de)serialization logic from the internal representation to the
  representation offered by the implementation of the `GroupEncoding` trait.

* fix: add explicit json serde tests

* Insert MSM and FFT code and their benchmarks. (privacy-scaling-explorations#86)

* Insert MSM and FFT code and their benchmarks.

Resolves taikoxyz/zkevm-circuits#150.

* feedback

* Add instructions

* feeback

* Implement feedback:  Actually supply the correct arguments to `best_multiexp`.

Split into `singlecore` and `multicore` benchmarks so Criterion's result
caching and comparison over multiple runs makes sense.

Rewrite point and scalar generation.

* Use slicing and parallelism to to decrease running time.

Laptop measurements:
k=22: 109 sec
k=16:   1 sec

* Refactor msm

* Refactor fft

* Update module comments

* Fix formatting

* Implement suggestion for fixing CI

* Re-export also mod `pairing` and remove flag `reexport` to alwasy re-export (privacy-scaling-explorations#93)

fix: re-export also mod `pairing` and remove flag `reexport` to alwasy re-export

* fix regression in privacy-scaling-explorations#93 reexport field benches aren't run (privacy-scaling-explorations#94)

fix regression in privacy-scaling-explorations#93, field benches aren't run

* Fast modular inverse - 9.4x acceleration (privacy-scaling-explorations#83)

* Bernstein yang modular multiplicative inverter (#2)

* rename similar to privacy-scaling-explorations#95

---------

Co-authored-by: Aleksei Vambol <[email protected]>

* Fast isSquare / Legendre symbol / Jacobi symbol - 16.8x acceleration (privacy-scaling-explorations#95)

* Derivatives of the Pornin's method (taikoxyz#3)

* renaming file

* make cargo fmt happy

* clarifications from privacy-scaling-explorations#95 (comment) [skip ci]

* Formatting and slightly changing a comment

---------

Co-authored-by: Aleksei Vambol <[email protected]>

* chore: delete bernsteinyang module (replaced by ff_inverse)

* Bump version to 0.4.1

---------

Co-authored-by: David Nevado <[email protected]>
Co-authored-by: Han <[email protected]>
Co-authored-by: François Garillot <[email protected]>
Co-authored-by: Carlos Pérez <[email protected]>
Co-authored-by: einar-taiko <[email protected]>
Co-authored-by: Mamy Ratsimbazafy <[email protected]>
Co-authored-by: Aleksei Vambol <[email protected]>
  • Loading branch information
8 people authored Nov 13, 2023
1 parent e185711 commit cf6dfa4
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 cf6dfa4

Please sign in to comment.