Refactor algebra API, split into algebra and algebra-core. (#100)

This commit is contained in:
Pratyush Mishra
2020-02-26 21:42:04 -08:00
committed by GitHub
parent d4896ade47
commit 8bf042a029
68 changed files with 417 additions and 572 deletions

View File

@@ -22,24 +22,30 @@ edition = "2018"
################################# Dependencies ################################
[dependencies]
algebra = { path = "../algebra", default-features = false }
algebra-core = { path = "../algebra-core", default-features = false }
bench-utils = { path = "../bench-utils" }
blake2 = { version = "0.7", default-features = false }
derivative = { version = "1.0", features = ["use_core"] }
digest = "0.7"
ff-fft = { path = "../ff-fft", default-features = false }
gm17 = { path = "../gm17", optional = true, default-features = false }
groth16 = { path = "../groth16", optional = true, default-features = false }
r1cs-core = { path = "../r1cs-core", optional = true, default-features = false }
r1cs-std = { path = "../r1cs-std", optional = true, default-features = false }
rand = { version = "0.7", default-features = false }
rayon = { version = "1.0", optional = true }
derivative = { version = "1.0", features = ["use_core"] }
[features]
default = ["parallel"]
default = ["std"]
r1cs = ["r1cs-core", "r1cs-std"]
std = ["r1cs", "algebra/std", "r1cs-core/std", "r1cs-std/std", "gm17/std", "groth16/std", "ff-fft/std"]
parallel = ["std", "rayon"]
std = ["r1cs", "algebra-core/std", "r1cs-core/std", "r1cs-std/std"]
parallel = ["std", "rayon", "gm17/parallel", "groth16/parallel", "ff-fft/parallel"]
[dev-dependencies]
algebra = { path = "../algebra", default-features = false, features = [ "jubjub", "bls12_377" ] }
r1cs-std = { path = "../r1cs-std", default-features = false, features = [ "jubjub", "bls12_377" ] }
rand_xorshift = { version = "0.2" }

View File

@@ -5,7 +5,7 @@ use crate::{
prf::blake2s::constraints::{blake2s_gadget, Blake2sOutputGadget},
CommitmentGadget,
};
use algebra::{Field, PrimeField};
use algebra_core::{Field, PrimeField};
use r1cs_std::prelude::*;
use core::borrow::Borrow;
@@ -117,7 +117,7 @@ mod test {
},
*,
};
use algebra::{fields::bls12_381::Fr, test_rng};
use algebra::{jubjub::Fq as Fr, test_rng};
use r1cs_core::ConstraintSystem;
use r1cs_std::{prelude::*, test_constraint_system::TestConstraintSystem};
use rand::Rng;

View File

@@ -1,5 +1,5 @@
use crate::CommitmentScheme;
use algebra::Field;
use algebra_core::Field;
use core::fmt::Debug;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;

View File

@@ -1,4 +1,4 @@
use algebra::{Field, PrimeField};
use algebra_core::{Field, PrimeField};
use crate::commitment::{
injective_map::{InjectiveMap, PedersenCommCompressor},
@@ -12,7 +12,7 @@ use crate::commitment::{
};
pub use crate::crh::injective_map::constraints::InjectiveMapGadget;
use algebra::groups::Group;
use algebra_core::groups::Group;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::{groups::GroupGadget, uint8::UInt8};

View File

@@ -7,7 +7,7 @@ use super::{
CommitmentScheme,
};
pub use crate::crh::injective_map::InjectiveMap;
use algebra::groups::Group;
use algebra_core::groups::Group;
#[cfg(feature = "r1cs")]
pub mod constraints;

View File

@@ -1,8 +1,8 @@
use algebra::UniformRand;
use algebra_core::UniformRand;
use core::{fmt::Debug, hash::Hash};
use rand::Rng;
use algebra::bytes::ToBytes;
use algebra_core::bytes::ToBytes;
pub mod blake2s;
pub mod injective_map;

View File

@@ -2,11 +2,13 @@ use crate::{
commitment::pedersen::{PedersenCommitment, PedersenParameters, PedersenRandomness},
crh::pedersen::PedersenWindow,
};
use algebra::{to_bytes, Group, ToBytes};
use algebra_core::{
fields::{Field, PrimeField},
to_bytes, Group, ToBytes,
};
use r1cs_core::{ConstraintSystem, SynthesisError};
use crate::commitment::CommitmentGadget;
use algebra::fields::{Field, PrimeField};
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_std::prelude::*;
@@ -171,6 +173,11 @@ where
#[cfg(test)]
mod test {
use algebra::{
jubjub::{Fq, Fr, JubJubProjective as JubJub},
test_rng, ProjectiveCurve, UniformRand,
};
use crate::{
commitment::{
pedersen::{
@@ -180,14 +187,9 @@ mod test {
},
crh::pedersen::PedersenWindow,
};
use algebra::{
curves::{jubjub::JubJubProjective as JubJub, ProjectiveCurve},
fields::jubjub::{fq::Fq, fr::Fr},
test_rng, UniformRand,
};
use r1cs_core::ConstraintSystem;
use r1cs_std::{
groups::jubjub::JubJubGadget, prelude::*, test_constraint_system::TestConstraintSystem,
jubjub::JubJubGadget, prelude::*, test_constraint_system::TestConstraintSystem,
};
#[test]

View File

@@ -1,14 +1,15 @@
use crate::{Error, Vec};
use algebra::{
bytes::ToBytes, groups::Group, BitIterator, Field, FpParameters, PrimeField, ToConstraintField,
UniformRand,
use algebra_core::{
bytes::ToBytes,
groups::Group,
io::{Result as IoResult, Write},
BitIterator, Field, FpParameters, PrimeField, ToConstraintField, UniformRand,
};
use core::marker::PhantomData;
use rand::Rng;
use super::CommitmentScheme;
use algebra::io::{Result as IoResult, Write};
pub use crate::crh::pedersen::PedersenWindow;
use crate::crh::{

View File

@@ -1,16 +1,14 @@
use algebra::Field;
use core::hash::Hash;
use core::{borrow::Borrow, hash::Hash, marker::PhantomData};
use crate::crh::{
bowe_hopwood::{BoweHopwoodPedersenCRH, BoweHopwoodPedersenParameters, CHUNK_SIZE},
pedersen::PedersenWindow,
FixedLengthCRHGadget,
};
use algebra::groups::Group;
use algebra_core::{groups::Group, Field};
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::{alloc::AllocGadget, groups::GroupGadget, uint8::UInt8};
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_std::bits::boolean::Boolean;
#[derive(Derivative)]
@@ -125,38 +123,39 @@ impl<G: Group, W: PedersenWindow, ConstraintF: Field, GG: GroupGadget<G, Constra
#[cfg(test)]
mod test {
use rand::Rng;
use crate::crh::{
bowe_hopwood::{constraints::BoweHopwoodPedersenCRHGadget, BoweHopwoodPedersenCRH},
pedersen::PedersenWindow,
FixedLengthCRH, FixedLengthCRHGadget,
};
use algebra::{
curves::edwards_sw6::EdwardsProjective as Edwards, fields::sw6::fr::Fr, test_rng,
ProjectiveCurve,
jubjub::{Fq as Fr, JubJubProjective as JubJub},
test_rng, ProjectiveCurve,
};
use r1cs_core::ConstraintSystem;
use r1cs_std::{
alloc::AllocGadget, groups::curves::twisted_edwards::edwards_sw6::EdwardsSWGadget,
test_constraint_system::TestConstraintSystem, uint8::UInt8,
alloc::AllocGadget, jubjub::JubJubGadget, test_constraint_system::TestConstraintSystem,
uint8::UInt8,
};
use rand::Rng;
type TestCRH = BoweHopwoodPedersenCRH<Edwards, Window>;
type TestCRHGadget = BoweHopwoodPedersenCRHGadget<Edwards, Fr, EdwardsSWGadget>;
type TestCRH = BoweHopwoodPedersenCRH<JubJub, Window>;
type TestCRHGadget = BoweHopwoodPedersenCRHGadget<JubJub, Fr, JubJubGadget>;
#[derive(Clone, PartialEq, Eq, Hash)]
pub(super) struct Window;
impl PedersenWindow for Window {
const WINDOW_SIZE: usize = 90;
const WINDOW_SIZE: usize = 63;
const NUM_WINDOWS: usize = 8;
}
fn generate_input<CS: ConstraintSystem<Fr>, R: Rng>(
mut cs: CS,
rng: &mut R,
) -> ([u8; 270], Vec<UInt8>) {
let mut input = [1u8; 270];
) -> ([u8; 189], Vec<UInt8>) {
let mut input = [1u8; 189];
rng.fill_bytes(&mut input);
let mut input_bytes = vec![];

View File

@@ -9,7 +9,7 @@ use rayon::prelude::*;
use super::pedersen::{bytes_to_bits, PedersenCRH, PedersenWindow};
use crate::crh::FixedLengthCRH;
use algebra::{biginteger::BigInteger, fields::PrimeField, groups::Group};
use algebra_core::{biginteger::BigInteger, fields::PrimeField, groups::Group};
use ff_fft::cfg_chunks;
#[cfg(feature = "r1cs")]
@@ -173,22 +173,22 @@ mod test {
crh::{bowe_hopwood::BoweHopwoodPedersenCRH, pedersen::PedersenWindow},
FixedLengthCRH,
};
use algebra::{curves::edwards_sw6::EdwardsProjective, test_rng};
use algebra::{jubjub::JubJubProjective, test_rng};
#[test]
fn test_simple_bh() {
#[derive(Clone)]
struct TestWindow {}
impl PedersenWindow for TestWindow {
const WINDOW_SIZE: usize = 90;
const WINDOW_SIZE: usize = 63;
const NUM_WINDOWS: usize = 8;
}
let rng = &mut test_rng();
let params =
<BoweHopwoodPedersenCRH<EdwardsProjective, TestWindow> as FixedLengthCRH>::setup(rng)
<BoweHopwoodPedersenCRH<JubJubProjective, TestWindow> as FixedLengthCRH>::setup(rng)
.unwrap();
<BoweHopwoodPedersenCRH<EdwardsProjective, TestWindow> as FixedLengthCRH>::evaluate(
<BoweHopwoodPedersenCRH<JubJubProjective, TestWindow> as FixedLengthCRH>::evaluate(
&params,
&[1, 2, 3],
)

View File

@@ -1,4 +1,4 @@
use algebra::Field;
use algebra_core::Field;
use core::fmt::Debug;
use crate::crh::FixedLengthCRH;

View File

@@ -9,7 +9,7 @@ use crate::crh::{
FixedLengthCRHGadget,
};
use algebra::{
use algebra_core::{
curves::{
models::{ModelParameters, TEModelParameters},
twisted_edwards_extended::{GroupAffine as TEAffine, GroupProjective as TEProjective},

View File

@@ -1,5 +1,5 @@
use crate::{CryptoError, Error};
use algebra::bytes::ToBytes;
use algebra_core::bytes::ToBytes;
use core::{fmt::Debug, hash::Hash, marker::PhantomData};
use rand::Rng;
@@ -7,7 +7,7 @@ use super::{
pedersen::{PedersenCRH, PedersenParameters, PedersenWindow},
FixedLengthCRH,
};
use algebra::{
use algebra_core::{
curves::{
models::{ModelParameters, TEModelParameters},
twisted_edwards_extended::{GroupAffine as TEAffine, GroupProjective as TEProjective},

View File

@@ -1,4 +1,4 @@
use algebra::bytes::ToBytes;
use algebra_core::bytes::ToBytes;
use core::hash::Hash;
use rand::Rng;

View File

@@ -2,7 +2,7 @@ use crate::crh::{
pedersen::{PedersenCRH, PedersenParameters, PedersenWindow},
FixedLengthCRHGadget,
};
use algebra::{Field, Group};
use algebra_core::{Field, Group};
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;
@@ -119,14 +119,12 @@ mod test {
FixedLengthCRH, FixedLengthCRHGadget,
};
use algebra::{
curves::{jubjub::JubJubProjective as JubJub, ProjectiveCurve},
fields::bls12_381::fr::Fr,
test_rng,
jubjub::{Fq as Fr, JubJubProjective as JubJub},
test_rng, ProjectiveCurve,
};
use r1cs_core::ConstraintSystem;
use r1cs_std::{
groups::curves::twisted_edwards::jubjub::JubJubGadget, prelude::*,
test_constraint_system::TestConstraintSystem,
jubjub::JubJubGadget, prelude::*, test_constraint_system::TestConstraintSystem,
};
use rand::Rng;

View File

@@ -8,7 +8,7 @@ use rand::Rng;
use rayon::prelude::*;
use crate::crh::FixedLengthCRH;
use algebra::{groups::Group, Field, ToConstraintField};
use algebra_core::{groups::Group, Field, ToConstraintField};
use ff_fft::cfg_chunks;
#[cfg(feature = "r1cs")]

View File

@@ -42,7 +42,7 @@ pub use self::{
pub type Error = Box<dyn std::error::Error>;
#[cfg(not(feature = "std"))]
pub type Error = Box<dyn algebra::Error>;
pub type Error = Box<dyn algebra_core::Error>;
#[derive(Debug)]
pub enum CryptoError {
@@ -69,4 +69,4 @@ impl std::error::Error for CryptoError {
}
#[cfg(not(feature = "std"))]
impl algebra::Error for CryptoError {}
impl algebra_core::Error for CryptoError {}

View File

@@ -1,4 +1,4 @@
use algebra::Field;
use algebra_core::Field;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::{boolean::AllocatedBit, prelude::*};
@@ -185,16 +185,13 @@ mod test {
},
merkle_tree::*,
};
use algebra::{curves::jubjub::JubJubAffine as JubJub, fields::jubjub::fq::Fq};
use algebra::jubjub::{Fq, JubJubAffine as JubJub};
use r1cs_core::ConstraintSystem;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use super::*;
use r1cs_std::{
groups::curves::twisted_edwards::jubjub::JubJubGadget,
test_constraint_system::TestConstraintSystem,
};
use r1cs_std::{jubjub::JubJubGadget, test_constraint_system::TestConstraintSystem};
#[derive(Clone)]
pub(super) struct Window4x256;

View File

@@ -1,5 +1,5 @@
use crate::{crh::FixedLengthCRH, Error, Vec};
use algebra::{bytes::ToBytes, io::Cursor};
use algebra_core::{bytes::ToBytes, io::Cursor};
use core::fmt;
#[cfg(feature = "r1cs")]
@@ -264,12 +264,12 @@ impl std::error::Error for MerkleTreeError {
}
#[cfg(not(feature = "std"))]
impl algebra::Error for MerkleTreeError {}
impl algebra_core::Error for MerkleTreeError {}
/// Returns the log2 value of the given number.
#[inline]
fn log2(number: usize) -> usize {
algebra::log2(number) as usize
algebra_core::log2(number) as usize
}
/// Returns the height of the tree, given the size of the tree.
@@ -371,7 +371,7 @@ mod test {
crh::{pedersen::*, *},
merkle_tree::*,
};
use algebra::{curves::jubjub::JubJubAffine as JubJub, Zero};
use algebra::{jubjub::JubJubAffine as JubJub, Zero};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

View File

@@ -1,4 +1,4 @@
use algebra::Field;
use algebra_core::Field;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;

View File

@@ -1,5 +1,5 @@
use crate::nizk::{gm17::Gm17, NIZKVerifierGadget};
use algebra::{AffineCurve, Field, PairingEngine, ToConstraintField};
use algebra_core::{AffineCurve, Field, PairingEngine, ToConstraintField};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;
@@ -404,12 +404,11 @@ mod test {
use super::*;
use algebra::{
curves::bls12_377::Bls12_377,
fields::bls12_377::{Fq, Fr},
bls12_377::{Bls12_377, Fq, Fr},
test_rng, BitIterator, PrimeField,
};
use r1cs_std::{
boolean::Boolean, pairing::bls12_377::PairingGadget as Bls12_377PairingGadget,
bls12_377::PairingGadget as Bls12_377PairingGadget, boolean::Boolean,
test_constraint_system::TestConstraintSystem,
};
use rand::Rng;

View File

@@ -1,5 +1,5 @@
use crate::Error;
use algebra::PairingEngine;
use algebra_core::PairingEngine;
use gm17::{
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
Parameters, PreparedVerifyingKey, Proof, VerifyingKey,
@@ -7,7 +7,7 @@ use gm17::{
use r1cs_core::ConstraintSynthesizer;
use rand::Rng;
use algebra::ToConstraintField;
use algebra_core::ToConstraintField;
use core::marker::PhantomData;
use super::NIZK;

View File

@@ -1,5 +1,5 @@
use crate::nizk::{groth16::Groth16, NIZKVerifierGadget};
use algebra::{AffineCurve, Field, PairingEngine, ToConstraintField};
use algebra_core::{AffineCurve, Field, PairingEngine, ToConstraintField};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;
@@ -351,12 +351,11 @@ mod test {
use super::*;
use algebra::{
curves::bls12_377::Bls12_377,
fields::bls12_377::{Fq, Fr},
bls12_377::{Bls12_377, Fq, Fr},
test_rng, BitIterator, PrimeField,
};
use r1cs_std::{
boolean::Boolean, pairing::bls12_377::PairingGadget as Bls12_377PairingGadget,
bls12_377::PairingGadget as Bls12_377PairingGadget, boolean::Boolean,
test_constraint_system::TestConstraintSystem,
};
use rand::Rng;

View File

@@ -1,5 +1,5 @@
use crate::Error;
use algebra::PairingEngine;
use algebra_core::PairingEngine;
use groth16::{
create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
Parameters, PreparedVerifyingKey, Proof, VerifyingKey,
@@ -7,7 +7,7 @@ use groth16::{
use r1cs_core::ConstraintSynthesizer;
use rand::Rng;
use algebra::ToConstraintField;
use algebra_core::ToConstraintField;
use core::marker::PhantomData;
use super::NIZK;

View File

@@ -1,4 +1,4 @@
use algebra::bytes::ToBytes;
use algebra_core::bytes::ToBytes;
use rand::Rng;
#[cfg(feature = "gm17")]
@@ -59,7 +59,10 @@ mod test {
#[test]
fn test_gm17() {
use crate::nizk::{gm17::Gm17, NIZK};
use algebra::{curves::bls12_381::Bls12_381, fields::bls12_381::Fr, One};
use algebra::{
bls12_377::{Bls12_377, Fr},
One,
};
use r1cs_core::{ConstraintSynthesizer, ConstraintSystem, SynthesisError};
#[derive(Copy, Clone)]
@@ -104,13 +107,13 @@ mod test {
let rng = &mut test_rng();
let parameters = Gm17::<Bls12_381, R1CSCircuit, [Fr]>::setup(circuit, rng).unwrap();
let parameters = Gm17::<Bls12_377, R1CSCircuit, [Fr]>::setup(circuit, rng).unwrap();
let proof =
Gm17::<Bls12_381, R1CSCircuit, [Fr]>::prove(&parameters.0, circuit, rng).unwrap();
Gm17::<Bls12_377, R1CSCircuit, [Fr]>::prove(&parameters.0, circuit, rng).unwrap();
let result =
Gm17::<Bls12_381, R1CSCircuit, [Fr]>::verify(&parameters.1, &[Fr::one(), sum], &proof)
Gm17::<Bls12_377, R1CSCircuit, [Fr]>::verify(&parameters.1, &[Fr::one(), sum], &proof)
.unwrap();
assert!(result);
}

View File

@@ -1,4 +1,4 @@
use algebra::PrimeField;
use algebra_core::PrimeField;
use r1cs_core::{ConstraintSystem, SynthesisError};
use crate::prf::PRFGadget;
@@ -499,7 +499,7 @@ impl<ConstraintF: PrimeField> PRFGadget<Blake2s, ConstraintF> for Blake2sGadget
#[cfg(test)]
mod test {
use algebra::fields::bls12_377::fr::Fr;
use algebra::jubjub::Fq as Fr;
use digest::{FixedOutput, Input};
use rand::{Rng, SeedableRng};
use rand_xorshift::XorShiftRng;

View File

@@ -1,4 +1,4 @@
use algebra::Field;
use algebra_core::Field;
use core::fmt::Debug;
use crate::prf::PRF;

View File

@@ -1,4 +1,4 @@
use algebra::bytes::{FromBytes, ToBytes};
use algebra_core::bytes::{FromBytes, ToBytes};
use core::{fmt::Debug, hash::Hash};
use crate::CryptoError;

View File

@@ -1,4 +1,4 @@
use algebra::Field;
use algebra_core::Field;
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;

View File

@@ -1,5 +1,5 @@
use crate::Error;
use algebra::bytes::ToBytes;
use algebra_core::bytes::ToBytes;
use core::hash::Hash;
use rand::Rng;
@@ -54,8 +54,7 @@ pub trait SignatureScheme {
mod test {
use crate::{signature::schnorr::SchnorrSignature, SignatureScheme};
use algebra::{
curves::edwards_sw6::EdwardsAffine as Edwards, groups::Group, test_rng, to_bytes, ToBytes,
UniformRand,
groups::Group, jubjub::JubJubAffine as JubJub, test_rng, to_bytes, ToBytes, UniformRand,
};
use blake2::Blake2s;
@@ -90,13 +89,13 @@ mod test {
fn schnorr_signature_test() {
let message = "Hi, I am a Schnorr signature!";
let rng = &mut test_rng();
sign_and_verify::<SchnorrSignature<Edwards, Blake2s>>(message.as_bytes());
failed_verification::<SchnorrSignature<Edwards, Blake2s>>(
sign_and_verify::<SchnorrSignature<JubJub, Blake2s>>(message.as_bytes());
failed_verification::<SchnorrSignature<JubJub, Blake2s>>(
message.as_bytes(),
"Bad message".as_bytes(),
);
let random_scalar = to_bytes!(<Edwards as Group>::ScalarField::rand(rng)).unwrap();
randomize_and_verify::<SchnorrSignature<Edwards, Blake2s>>(
let random_scalar = to_bytes!(<JubJub as Group>::ScalarField::rand(rng)).unwrap();
randomize_and_verify::<SchnorrSignature<JubJub, Blake2s>>(
message.as_bytes(),
&random_scalar.as_slice(),
);

View File

@@ -1,4 +1,4 @@
use algebra::{groups::Group, Field};
use algebra_core::{groups::Group, Field};
use r1cs_core::{ConstraintSystem, SynthesisError};
use r1cs_std::prelude::*;

View File

@@ -1,5 +1,5 @@
use crate::{Error, SignatureScheme, Vec};
use algebra::{
use algebra_core::{
bytes::ToBytes,
fields::{Field, PrimeField},
groups::Group,