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

Add multivariate polynomial support #261

Closed
wants to merge 10 commits into from
Closed
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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --all --all-features -- --skip dpc --skip integration_test
args: "--all \
--all-features \
--exclude cp-benches \
--exclude algebra-benches \
--exclude ff-fft-benches \
-- --skip dpc --skip integration_test"

check_no_std:
name: Check no_std
Expand Down
2 changes: 2 additions & 0 deletions algebra-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub extern crate alloc;
pub use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
collections::btree_map::BTreeMap,
format,
string::String,
vec,
Expand All @@ -40,6 +41,7 @@ pub use alloc::{
pub use std::{
borrow::{Cow, ToOwned},
boxed::Box,
collections::btree_map::BTreeMap,
format,
string::String,
vec,
Expand Down
58 changes: 54 additions & 4 deletions algebra-core/src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use flags::*;
#[doc(hidden)]
pub use algebra_core_derive::*;

use crate::{Cow, ToOwned, Vec};
use crate::{BTreeMap, Cow, ToOwned, Vec};
use core::convert::TryFrom;

/// Serializer in little endian format allowing to encode flags.
Expand Down Expand Up @@ -61,9 +61,9 @@ pub trait CanonicalSerialize {
self.serialize(writer)
}

/// Serializes `self` into `writer` without compression, and without performing
/// validity checks. Should be used *only* when there is no danger of
/// adversarial manipulation of the output.
/// Serializes `self` into `writer` without compression, and without
/// performing validity checks. Should be used *only* when there is no
/// danger of adversarial manipulation of the output.
#[inline]
fn serialize_unchecked<W: Write>(&self, writer: W) -> Result<(), SerializationError> {
self.serialize_uncompressed(writer)
Expand Down Expand Up @@ -793,6 +793,44 @@ impl CanonicalDeserialize for bool {
}
}

impl<K, V> CanonicalSerialize for BTreeMap<K, V>
where
K: CanonicalSerialize,
V: CanonicalSerialize,
{
fn serialize<W: Write>(&self, mut writer: W) -> Result<(), SerializationError> {
let len = self.len() as u64;
len.serialize(&mut writer)?;
for (k, v) in self.iter() {
k.serialize(&mut writer)?;
v.serialize(&mut writer)?;
}
Ok(())
}

fn serialized_size(&self) -> usize {
8 + self
.iter()
.map(|(k, v)| k.serialized_size() + v.serialized_size())
.sum::<usize>()
}
}

impl<K, V> CanonicalDeserialize for BTreeMap<K, V>
where
K: Ord + CanonicalDeserialize,
V: CanonicalDeserialize,
{
fn deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
let len = u64::deserialize(&mut reader)?;
let mut map = BTreeMap::new();
for _ in 0..len {
map.insert(K::deserialize(&mut reader)?, V::deserialize(&mut reader)?);
}
Ok(map)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -859,6 +897,18 @@ mod test {
test_serialize(false);
}

#[test]
fn test_btreemap() {
let mut map = BTreeMap::new();
map.insert(0u64, true);
map.insert(5u64, false);
test_serialize(map);
let mut map = BTreeMap::new();
map.insert(10u64, vec![1u8, 2u8, 3u8]);
map.insert(50u64, vec![4u8, 5u8, 6u8]);
test_serialize(map);
}

#[test]
fn test_phantomdata() {
test_serialize(core::marker::PhantomData::<u64>);
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/merkle_tree/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
// proof.
let leaf_bits = leaf.to_bytes()?;
let leaf_hash = CRHGadget::evaluate(parameters, &leaf_bits)?;
let cs = leaf_hash.cs().or(root.cs()).unwrap();
let cs = leaf_hash.cs().or(root.cs());

// Check if leaf is one of the bottom-most siblings.
let leaf_is_left = Boolean::new_witness(r1cs_core::ns!(cs, "leaf_is_left"), || {
Expand Down
2 changes: 1 addition & 1 deletion crypto-primitives/src/prf/blake2s/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<ConstraintF: PrimeField> AllocVar<[u8; 32], ConstraintF> for OutputVar<Cons
impl<F: PrimeField> R1CSVar<F> for OutputVar<F> {
type Value = [u8; 32];

fn cs(&self) -> Option<ConstraintSystemRef<F>> {
fn cs(&self) -> ConstraintSystemRef<F> {
self.0.cs()
}

Expand Down
1 change: 1 addition & 0 deletions ff-fft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ edition = "2018"
algebra-core = { path = "../algebra-core", default-features = false }
rand = { version = "0.7", default-features = false }
rayon = { version = "1", optional = true }
derivative = { version = "2", default-features = false }

[dev-dependencies]
algebra = { path = "../algebra", default-features = false, features = [ "bls12_381", "mnt6_753", "mnt4_753" ] }
Expand Down
11 changes: 6 additions & 5 deletions ff-fft/src/domain/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//! depending on the number of coefficients and the two-adicity of the prime.

pub use crate::domain::utils::Elements;
use crate::domain::{
DomainCoeff, EvaluationDomain, MixedRadixEvaluationDomain, Radix2EvaluationDomain,
use crate::{
domain::{DomainCoeff, EvaluationDomain, MixedRadixEvaluationDomain, Radix2EvaluationDomain},
Vec,
};
use crate::Vec;
use algebra_core::{FftField, FftParameters};

/// Defines a domain over which finite field (I)FFTs can be performed.
Expand Down Expand Up @@ -116,7 +116,7 @@ impl<F: FftField> EvaluationDomain<F> for GeneralEvaluationDomain<F> {
}

#[inline]
fn vanishing_polynomial(&self) -> crate::SparsePolynomial<F> {
fn vanishing_polynomial(&self) -> crate::univariate::SparsePolynomial<F> {
match self {
GeneralEvaluationDomain::Radix2(domain) => domain.vanishing_polynomial(),
GeneralEvaluationDomain::MixedRadix(domain) => domain.vanishing_polynomial(),
Expand Down Expand Up @@ -148,7 +148,8 @@ impl<F: FftField> EvaluationDomain<F> for GeneralEvaluationDomain<F> {

/// A generalized version of an iterator over the elements of a domain.
pub enum GeneralElements<F: FftField> {
/// A basic iterator over the elements of a domain (currently, the only one in use).
/// A basic iterator over the elements of a domain (currently, the only one
/// in use).
BasicElements(Elements<F>),
}

Expand Down
17 changes: 9 additions & 8 deletions ff-fft/src/domain/mixed_radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
//! to obtain a subgroup generated by `F::LARGE_SUBGROUP_ROOT_OF_UNITY`.

pub use crate::domain::utils::Elements;
use crate::domain::{
utils::{best_fft, bitreverse},
DomainCoeff, EvaluationDomain,
use crate::{
domain::{
utils::{best_fft, bitreverse},
DomainCoeff, EvaluationDomain,
},
Vec,
};
use crate::Vec;
use algebra_core::{fields::utils::k_adicity, FftField, FftParameters};
use core::cmp::min;
use core::convert::TryFrom;
Expand Down Expand Up @@ -192,9 +194,9 @@ impl<F: FftField> EvaluationDomain<F> for MixedRadixEvaluationDomain<F> {
}
}

fn vanishing_polynomial(&self) -> crate::SparsePolynomial<F> {
fn vanishing_polynomial(&self) -> crate::univariate::SparsePolynomial<F> {
let coeffs = vec![(0, -F::one()), (self.size(), F::one())];
crate::SparsePolynomial::from_coefficients_vec(coeffs)
crate::univariate::SparsePolynomial::from_coefficients_vec(coeffs)
}

/// This evaluates the vanishing polynomial for this domain at tau.
Expand Down Expand Up @@ -448,8 +450,7 @@ mod tests {
#[cfg(feature = "parallel")]
fn parallel_fft_consistency() {
use super::serial_mixed_radix_fft;
use crate::domain::utils::parallel_fft;
use crate::Vec;
use crate::{domain::utils::parallel_fft, Vec};
use algebra::mnt6_753::MNT6_753;
use algebra_core::{test_rng, PairingEngine, UniformRand};
use core::cmp::min;
Expand Down
2 changes: 1 addition & 1 deletion ff-fft/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub trait EvaluationDomain<F: FftField>:
fn evaluate_all_lagrange_coefficients(&self, tau: F) -> Vec<F>;

/// Return the sparse vanishing polynomial.
fn vanishing_polynomial(&self) -> crate::SparsePolynomial<F>;
fn vanishing_polynomial(&self) -> crate::univariate::SparsePolynomial<F>;

/// This evaluates the vanishing polynomial for this domain at tau.
fn evaluate_vanishing_polynomial(&self, tau: F) -> F;
Expand Down
17 changes: 9 additions & 8 deletions ff-fft/src/domain/radix2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
//! FFTs of size at most `2^F::TWO_ADICITY`.

pub use crate::domain::utils::Elements;
use crate::domain::{
utils::{best_fft, bitreverse},
DomainCoeff, EvaluationDomain,
use crate::{
domain::{
utils::{best_fft, bitreverse},
DomainCoeff, EvaluationDomain,
},
Vec,
};
use crate::Vec;
use algebra_core::{FftField, FftParameters};
use core::convert::TryFrom;
use core::fmt;
Expand Down Expand Up @@ -159,9 +161,9 @@ impl<F: FftField> EvaluationDomain<F> for Radix2EvaluationDomain<F> {
}
}

fn vanishing_polynomial(&self) -> crate::SparsePolynomial<F> {
fn vanishing_polynomial(&self) -> crate::univariate::SparsePolynomial<F> {
let coeffs = vec![(0, -F::one()), (self.size(), F::one())];
crate::SparsePolynomial::from_coefficients_vec(coeffs)
crate::univariate::SparsePolynomial::from_coefficients_vec(coeffs)
}

/// This evaluates the vanishing polynomial for this domain at tau.
Expand Down Expand Up @@ -277,8 +279,7 @@ mod tests {
#[cfg(feature = "parallel")]
fn parallel_fft_consistency() {
use super::serial_radix2_fft;
use crate::domain::utils::parallel_fft;
use crate::Vec;
use crate::{domain::utils::parallel_fft, Vec};
use algebra::bls12_381::Bls12_381;
use algebra_core::{test_rng, PairingEngine, UniformRand};
use core::cmp::min;
Expand Down
4 changes: 3 additions & 1 deletion ff-fft/src/evaluations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! A polynomial represented in evaluations form.

use crate::{DensePolynomial, EvaluationDomain, GeneralEvaluationDomain, Vec};
use crate::{
univariate::DensePolynomial, EvaluationDomain, GeneralEvaluationDomain, UVPolynomial, Vec,
};
use algebra_core::FftField;
use core::ops::{Add, AddAssign, Div, DivAssign, Index, Mul, MulAssign, Sub, SubAssign};

Expand Down
6 changes: 4 additions & 2 deletions ff-fft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#![deny(unreachable_pub, unused_extern_crates, trivial_numeric_casts)]
#![forbid(unsafe_code)]

#[macro_use]
extern crate derivative;

#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;
Expand Down Expand Up @@ -92,15 +95,14 @@ macro_rules! cfg_chunks_mut {
}

pub mod domain;

pub mod evaluations;
pub mod polynomial;

pub use domain::{
EvaluationDomain, GeneralEvaluationDomain, MixedRadixEvaluationDomain, Radix2EvaluationDomain,
};
pub use evaluations::Evaluations;
pub use polynomial::{DenseOrSparsePolynomial, DensePolynomial, SparsePolynomial};
pub use polynomial::{multivariate, univariate, MVPolynomial, Polynomial, UVPolynomial};

#[cfg(test)]
mod test;
Loading