Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/cargo/hashbrown-0.14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
weikengchen authored Aug 4, 2023
2 parents 406833b + 1e8257f commit 7665291
Show file tree
Hide file tree
Showing 18 changed files with 159 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ members = [
"test-templates",
]

resolver = "2"

[profile.release]
opt-level = 3
lto = "thin"
Expand Down
2 changes: 1 addition & 1 deletion bench-templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/algebra/"
keywords = ["cryptography", "finite-fields", "elliptic-curves", "pairing"]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
2 changes: 1 addition & 1 deletion ec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-ec/"
keywords = ["cryptography", "elliptic-curves", "pairing"]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "doc", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
2 changes: 1 addition & 1 deletion ec/src/scalar_mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub mod wnaf;
pub mod fixed_base;
pub mod variable_base;

use crate::PrimeGroup;
use crate::short_weierstrass::{Affine, Projective, SWCurveConfig};
use crate::PrimeGroup;
use ark_ff::{AdditiveGroup, Zero};
use ark_std::{
ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign},
Expand Down
2 changes: 1 addition & 1 deletion ff-asm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-ff-asm/"
keywords = ["cryptography", "finite-fields", "assembly" ]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.60"

Expand Down
2 changes: 1 addition & 1 deletion ff-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-ff-asm/"
keywords = ["cryptography", "finite-fields", "assembly" ]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.60"

Expand Down
2 changes: 1 addition & 1 deletion ff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-ff/"
keywords = ["cryptography", "finite-fields" ]
categories = ["cryptography"]
include = ["Cargo.toml", "build.rs", "src", "doc", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
28 changes: 28 additions & 0 deletions ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ pub trait Field:
+ From<u32>
+ From<u16>
+ From<u8>
+ From<i128>
+ From<i64>
+ From<i32>
+ From<i16>
+ From<i8>
+ From<bool>
+ Product<Self>
{
Expand Down Expand Up @@ -467,6 +472,29 @@ mod no_std_tests {
}
}

#[test]
pub fn test_from_ints() {
let felt2 = Fr::one() + Fr::one();
let felt16 = felt2 * felt2 * felt2 * felt2;

assert_eq!(Fr::from(1u8), Fr::one());
assert_eq!(Fr::from(1u16), Fr::one());
assert_eq!(Fr::from(1u32), Fr::one());
assert_eq!(Fr::from(1u64), Fr::one());
assert_eq!(Fr::from(1u128), Fr::one());
assert_eq!(Fr::from(-1i8), -Fr::one());
assert_eq!(Fr::from(-1i64), -Fr::one());

assert_eq!(Fr::from(0), Fr::zero());

assert_eq!(Fr::from(-16i32), -felt16);
assert_eq!(Fr::from(16u32), felt16);
assert_eq!(Fr::from(16i64), felt16);

assert_eq!(Fr::from(-2i128), -felt2);
assert_eq!(Fr::from(2u16), felt2);
}

#[test]
fn test_from_into_biguint() {
let mut rng = ark_std::test_rng();
Expand Down
2 changes: 1 addition & 1 deletion poly/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-poly/"
keywords = ["cryptography", "finite-fields", "fft", "polynomials"]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
10 changes: 5 additions & 5 deletions poly/benches/dense_uv_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ fn bench_div_by_vanishing_poly<F: FftField>(b: &mut Bencher, degree: &usize) {

fn poly_benches<F: FftField>(c: &mut Criterion, name: &'static str) {
if ENABLE_ADD_BENCH {
let cur_name = format!("{:?} - add_polynomial", name.clone());
let cur_name = format!("{:?} - add_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_add::<F>);
}
if ENABLE_ADD_ASSIGN_BENCH {
let cur_name = format!("{:?} - add_assign_polynomial", name.clone());
let cur_name = format!("{:?} - add_assign_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_add_assign::<F>);
}
if ENABLE_EVALUATE_BENCH {
let cur_name = format!("{:?} - evaluate_polynomial", name.clone());
let cur_name = format!("{:?} - evaluate_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_evaluate::<F>);
}
if ENABLE_SPARSE_EVALUATE_BENCH {
let cur_name = format!("{:?} - evaluate_sparse_polynomial", name.clone());
let cur_name = format!("{:?} - evaluate_sparse_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_sparse_poly_evaluate::<F>);
}
if ENABLE_DIV_BY_VANISHING_POLY_BENCH {
let cur_name = format!("{:?} - evaluate_div_by_vanishing_poly", name.clone());
let cur_name = format!("{:?} - evaluate_div_by_vanishing_poly", name);
setup_bench::<F>(c, &cur_name, bench_div_by_vanishing_poly::<F>);
}
}
Expand Down
23 changes: 23 additions & 0 deletions poly/src/domain/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ use ark_std::{
/// Defines a domain over which finite field (I)FFTs can be performed.
/// Generally tries to build a radix-2 domain and falls back to a mixed-radix
/// domain if the radix-2 multiplicative subgroup is too small.
///
/// # Examples
///
/// ```
/// use ark_poly::{GeneralEvaluationDomain, EvaluationDomain};
/// use ark_poly::{univariate::DensePolynomial, Polynomial, DenseUVPolynomial};
/// use ark_ff::FftField;
///
/// // The field we are using is FFT-friendly, with 2-adicity of 32.
/// // We can efficiently evaluate polynomials over this field on up to 2^32 points.
/// use ark_test_curves::bls12_381::Fr;
///
/// let small_domain = GeneralEvaluationDomain::<Fr>::new(4).unwrap();
/// let evals = vec![Fr::from(1u8), Fr::from(2u8), Fr::from(3u8), Fr::from(4u8)];
/// // From a vector of evaluations, we can recover the polynomial.
/// let coeffs = small_domain.ifft(&evals);
/// let poly = DensePolynomial::from_coefficients_vec(coeffs.clone());
/// assert_eq!(poly.degree(), 3);
///
/// // We could also evaluate this polynomial at a large number of points efficiently, e.g. for Reed-Solomon encoding.
/// let large_domain = GeneralEvaluationDomain::<Fr>::new(1<<10).unwrap();
/// let new_evals = large_domain.fft(&coeffs);
/// ```
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
pub enum GeneralEvaluationDomain<F: FftField> {
/// Radix-2 domain
Expand Down
37 changes: 36 additions & 1 deletion poly/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use ark_ff::FftField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{fmt, hash, rand::Rng, vec::Vec};
use ark_std::{fmt, hash, rand::Rng, vec::Vec, Zero};

#[cfg(feature = "parallel")]
use rayon::prelude::*;
Expand Down Expand Up @@ -234,6 +234,41 @@ pub trait EvaluationDomain<F: FftField>:
tau.pow([self.size() as u64]) - self.coset_offset_pow_size()
}

/// Return the filter polynomial of `self` with respect to the subdomain `subdomain`.
/// Assumes that `subdomain` is contained within `self`.
///
/// # Panics
///
/// Panics if `subdomain` is not contained within `self`.
fn filter_polynomial(&self, subdomain: &Self) -> crate::univariate::DensePolynomial<F> {
use crate::univariate::DenseOrSparsePolynomial;
let self_vanishing_poly = DenseOrSparsePolynomial::from(
&self.vanishing_polynomial()
* (subdomain.size_as_field_element()
* subdomain.coset_offset().pow([subdomain.size() as u64])),
);
let subdomain_vanishing_poly = DenseOrSparsePolynomial::from(
&subdomain.vanishing_polynomial() * self.size_as_field_element(),
);
let (quotient, remainder) = self_vanishing_poly
.divide_with_q_and_r(&subdomain_vanishing_poly)
.unwrap();
assert!(remainder.is_zero());
quotient
}

/// This evaluates at `tau` the filter polynomial for `self` with respect
/// to the subdomain `subdomain`.
fn evaluate_filter_polynomial(&self, subdomain: &Self, tau: F) -> F {
let v_subdomain_of_tau = subdomain.evaluate_vanishing_polynomial(tau);
if v_subdomain_of_tau.is_zero() {
F::one()
} else {
subdomain.size_as_field_element() * self.evaluate_vanishing_polynomial(tau)
/ (self.size_as_field_element() * v_subdomain_of_tau)
}
}

/// Returns the `i`-th element of the domain.
fn element(&self, i: usize) -> F {
let mut result = self.group_gen().pow([i as u64]);
Expand Down
47 changes: 46 additions & 1 deletion poly/src/domain/radix2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
EvaluationDomain, Radix2EvaluationDomain,
};
use ark_ff::{FftField, Field, One, UniformRand, Zero};
use ark_std::{rand::Rng, test_rng};
use ark_std::{collections::BTreeSet, rand::Rng, test_rng};
use ark_test_curves::bls12_381::Fr;

#[test]
Expand Down Expand Up @@ -220,6 +220,51 @@ mod tests {
}
}

#[test]
fn filter_polynomial_test() {
for log_domain_size in 1..=4 {
let domain_size = 1 << log_domain_size;
let domain = Radix2EvaluationDomain::<Fr>::new(domain_size).unwrap();
for log_subdomain_size in 1..=log_domain_size {
let subdomain_size = 1 << log_subdomain_size;
let subdomain = Radix2EvaluationDomain::<Fr>::new(subdomain_size).unwrap();

// Obtain all possible offsets of `subdomain` within `domain`.
let mut possible_offsets = vec![Fr::one()];
let domain_generator = domain.group_gen();

let mut offset = domain_generator;
let subdomain_generator = subdomain.group_gen();
while offset != subdomain_generator {
possible_offsets.push(offset);
offset *= domain_generator;
}

assert_eq!(possible_offsets.len(), domain_size / subdomain_size);

// Get all possible cosets of `subdomain` within `domain`.
let cosets = possible_offsets
.iter()
.map(|offset| subdomain.get_coset(*offset).unwrap());

for coset in cosets {
let coset_elements = coset.elements().collect::<BTreeSet<_>>();
let filter_poly = domain.filter_polynomial(&coset);
assert_eq!(filter_poly.degree(), domain_size - subdomain_size);
for element in domain.elements() {
let evaluation = domain.evaluate_filter_polynomial(&coset, element);
assert_eq!(evaluation, filter_poly.evaluate(&element));
if coset_elements.contains(&element) {
assert_eq!(evaluation, Fr::one())
} else {
assert_eq!(evaluation, Fr::zero())
}
}
}
}
}
}

#[test]
fn size_of_elements() {
for coeffs in 1..10 {
Expand Down
2 changes: 1 addition & 1 deletion serialize-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/algebra/"
keywords = ["cryptography", "finite-fields", "elliptic-curves", "serialization"]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.60"

Expand Down
2 changes: 1 addition & 1 deletion serialize/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-serialize/"
keywords = ["cryptography", "serialization" ]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
2 changes: 1 addition & 1 deletion test-curves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-test-curves/"
keywords = ["cryptography", "serialization" ]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down
9 changes: 8 additions & 1 deletion test-curves/src/bls12_381/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ pub mod g2;
#[cfg(feature = "bls12_381_curve")]
pub mod g2_swu_iso;
#[cfg(feature = "bls12_381_curve")]
pub use {fq::*, fq12::*, fq2::*, fq6::*, g1::*, g1_swu_iso::*, g2::*, g2_swu_iso::*};
pub use {
fq::*,
fq12::*,
fq2::*,
fq6::*,
g1::{G1Affine, G1Projective},
g2::{G2Affine, G2Projective},
};

#[cfg(test)]
mod tests;
Expand Down
2 changes: 1 addition & 1 deletion test-templates/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ documentation = "https://docs.rs/ark-curve-tests/"
keywords = ["cryptography", "finite-fields", "elliptic-curves" ]
categories = ["cryptography"]
include = ["Cargo.toml", "src", "README.md", "LICENSE-APACHE", "LICENSE-MIT"]
license = "MIT/Apache-2.0"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63"

Expand Down

0 comments on commit 7665291

Please sign in to comment.