Skip to content

Commit

Permalink
pairing: Remove BLS12-381 implementation
Browse files Browse the repository at this point in the history
It is replaced by the bls12_381 crate.
  • Loading branch information
str4d committed Aug 25, 2020
1 parent 200901d commit 459fa4a
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 113 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ byteorder = "1"
subtle = "2.2.1"

[dev-dependencies]
bls12_381 = { version = "0.1", path = "../bls12_381" }
hex-literal = "0.2"
rand = "0.7"
rand_xorshift = "0.2"
Expand Down
6 changes: 3 additions & 3 deletions src/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ fn parallel_fft<S: PrimeField, T: Group<S>>(
#[cfg(feature = "pairing")]
#[test]
fn polynomial_arith() {
use pairing::bls12_381::Fr;
use bls12_381::Scalar as Fr;
use rand_core::RngCore;

fn test_mul<S: PrimeField, R: RngCore>(rng: &mut R) {
Expand Down Expand Up @@ -422,7 +422,7 @@ fn polynomial_arith() {
#[cfg(feature = "pairing")]
#[test]
fn fft_composition() {
use pairing::bls12_381::Fr;
use bls12_381::Scalar as Fr;
use rand_core::RngCore;

fn test_comp<S: PrimeField, R: RngCore>(rng: &mut R) {
Expand Down Expand Up @@ -460,7 +460,7 @@ fn fft_composition() {
#[cfg(feature = "pairing")]
#[test]
fn parallel_fft_consistency() {
use pairing::bls12_381::Fr;
use bls12_381::Scalar as Fr;
use rand_core::RngCore;
use std::cmp::min;

Expand Down
18 changes: 9 additions & 9 deletions src/gadgets/blake2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ pub fn blake2s<Scalar: PrimeField, CS: ConstraintSystem<Scalar>>(
#[cfg(test)]
mod test {
use blake2s_simd::Params as Blake2sParams;
use bls12_381::Scalar;
use hex_literal::hex;
use pairing::bls12_381::Fr;
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;

Expand All @@ -420,7 +420,7 @@ mod test {

#[test]
fn test_blank_hash() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let input_bits = vec![];
let out = blake2s(&mut cs, &input_bits, b"12345678").unwrap();
assert!(cs.is_satisfied());
Expand All @@ -443,7 +443,7 @@ mod test {

#[test]
fn test_blake2s_constraints() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let input_bits: Vec<_> = (0..512)
.map(|i| {
AllocatedBit::alloc(cs.namespace(|| format!("input bit {}", i)), Some(true))
Expand All @@ -461,7 +461,7 @@ mod test {
// Test that 512 fixed leading bits (constants)
// doesn't result in more constraints.

let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
Expand All @@ -481,7 +481,7 @@ mod test {

#[test]
fn test_blake2s_constant_constraints() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
Expand Down Expand Up @@ -512,7 +512,7 @@ mod test {

let hash_result = h.finalize();

let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut input_bits = vec![];

Expand Down Expand Up @@ -559,7 +559,7 @@ mod test {
let data: Vec<u8> = hex!("be9f9c485e670acce8b1516a378176161b20583637b6f1c536fbc1158a0a3296831df2920e57a442d5738f4be4dd6be89dd7913fc8b4d1c0a815646a4d674b77f7caf313bd880bf759fcac27037c48c2b2a20acd2fd5248e3be426c84a341c0a3c63eaf36e0d537d10b8db5c6e4c801832c41eb1a3ed602177acded8b4b803bd34339d99a18b71df399641cc8dfae2ad193fcd74b5913e704551777160d14c78f2e8d5c32716a8599c1080cb89a40ccd6ba596694a8b4a065d9f2d0667ef423ed2e418093caff884540858b4f4b62acd47edcea880523e1b1cda8eb225c128c2e9e83f14f6e7448c5733a195cac7d79a53dde5083172462c45b2f799e42af1c9").to_vec();
assert_eq!(data.len(), 256);

let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut input_bits = vec![];

Expand Down Expand Up @@ -596,7 +596,7 @@ mod test {
let data: Vec<u8> = hex!("5dcfe8bab4c758d2eb1ddb7ef337583e0df3e2c358e1755b7cd303a658de9a1227eed1d1114179a5c3c38d692ff2cf2d4e5c92a9516de750106774bbf9f7d063f707f4c9b6a02c0a77e4feb99e036c3ccaee7d1a31cb144093aa074bc9da608f8ff30b39c3c60e4a243cc0bbd406d1262a7d6607b31c60275c6bcc8b0ac49a06a4b629a98693c5f7640f3bca45e4977cfabc5b17f52838af3433b1fd407dbbdc131e8e4bd58bcee85bbab4b57b656c6a2ec6cf852525bc8423675e2bf29159139cd5df99db94719f3f7167230e0d5bd76f6d7891b656732cef9c3c0d48a5fa3d7a879988157b39015a85451b25af0301ca5e759ac35fea79dca38c673ec6db9f3885d9103e2dcb3304bd3d59b0b1d01babc97ef8a74d91b6ab6bf50f29eb5adf7250a28fd85db37bff0133193635da69caeefc72979cf3bef1d2896d847eea7e8a81e0927893dbd010feb6fb845d0399007d9a148a0596d86cd8f4192631f975c560f4de8da5f712c161342063af3c11029d93d6df7ff46db48343499de9ec4786cac059c4025ef418c9fe40132428ff8b91259d71d1709ff066add84ae944b45a817f60b4c1bf719e39ae23e9b413469db2310793e9137cf38741e5dd2a3c138a566dbde1950c00071b20ac457b46ba9b0a7ebdddcc212bd228d2a4c4146a970e54158477247c27871af1564b176576e9fd43bf63740bf77434bc4ea3b1a4b430e1a11714bf43160145578a575c3f78ddeaa48de97f73460f26f8df2b5d63e31800100d16bc27160fea5ced5a977ef541cfe8dadc7b3991ed1c0d4f16a3076bbfed96ba3e155113e794987af8abb133f06feefabc2ac32eb4d4d4ba1541ca08b9e518d2e74b7f946b0cbd2663d58c689359b9a565821acc619011233d1011963fa302cde34fc9c5ba2e03eeb2512f547391e940d56218e22ae325f2dfa38d4bae35744ee707aa5dc9c17674025d15390a08f5c452343546ef6da0f7").to_vec();
assert_eq!(data.len(), 700);

let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut input_bits = vec![];

Expand Down Expand Up @@ -651,7 +651,7 @@ mod test {

let hash_result = h.finalize();

let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut input_bits = vec![];

Expand Down
62 changes: 31 additions & 31 deletions src/gadgets/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,19 +749,19 @@ mod test {
use super::{field_into_allocated_bits_le, u64_into_boolean_vec_le, AllocatedBit, Boolean};
use crate::gadgets::test::*;
use crate::ConstraintSystem;
use bls12_381::Scalar;
use ff::{Field, PrimeField};
use pairing::bls12_381::Fr;

#[test]
fn test_allocated_bit() {
let mut cs = TestConstraintSystem::new();

AllocatedBit::alloc(&mut cs, Some(true)).unwrap();
assert!(cs.get("boolean") == Fr::one());
assert!(cs.get("boolean") == Scalar::one());
assert!(cs.is_satisfied());
cs.set("boolean", Fr::zero());
cs.set("boolean", Scalar::zero());
assert!(cs.is_satisfied());
cs.set("boolean", Fr::from_str("2").unwrap());
cs.set("boolean", Scalar::from_str("2").unwrap());
assert!(!cs.is_satisfied());
assert!(cs.which_is_unsatisfied() == Some("boolean constraint"));
}
Expand All @@ -770,7 +770,7 @@ mod test {
fn test_xor() {
for a_val in [false, true].iter() {
for b_val in [false, true].iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let a = AllocatedBit::alloc(cs.namespace(|| "a"), Some(*a_val)).unwrap();
let b = AllocatedBit::alloc(cs.namespace(|| "b"), Some(*b_val)).unwrap();
let c = AllocatedBit::xor(&mut cs, &a, &b).unwrap();
Expand Down Expand Up @@ -806,7 +806,7 @@ mod test {
fn test_and() {
for a_val in [false, true].iter() {
for b_val in [false, true].iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let a = AllocatedBit::alloc(cs.namespace(|| "a"), Some(*a_val)).unwrap();
let b = AllocatedBit::alloc(cs.namespace(|| "b"), Some(*b_val)).unwrap();
let c = AllocatedBit::and(&mut cs, &a, &b).unwrap();
Expand Down Expand Up @@ -842,7 +842,7 @@ mod test {
fn test_and_not() {
for a_val in [false, true].iter() {
for b_val in [false, true].iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let a = AllocatedBit::alloc(cs.namespace(|| "a"), Some(*a_val)).unwrap();
let b = AllocatedBit::alloc(cs.namespace(|| "b"), Some(*b_val)).unwrap();
let c = AllocatedBit::and_not(&mut cs, &a, &b).unwrap();
Expand Down Expand Up @@ -878,7 +878,7 @@ mod test {
fn test_nor() {
for a_val in [false, true].iter() {
for b_val in [false, true].iter() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let a = AllocatedBit::alloc(cs.namespace(|| "a"), Some(*a_val)).unwrap();
let b = AllocatedBit::alloc(cs.namespace(|| "b"), Some(*b_val)).unwrap();
let c = AllocatedBit::nor(&mut cs, &a, &b).unwrap();
Expand Down Expand Up @@ -917,7 +917,7 @@ mod test {
for a_neg in [false, true].iter().cloned() {
for b_neg in [false, true].iter().cloned() {
{
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut a = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "a"), Some(a_bool)).unwrap(),
Expand All @@ -938,7 +938,7 @@ mod test {
assert_eq!(cs.is_satisfied(), (a_bool ^ a_neg) == (b_bool ^ b_neg));
}
{
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut a = Boolean::Constant(a_bool);
let mut b = Boolean::from(
Expand All @@ -957,7 +957,7 @@ mod test {
assert_eq!(cs.is_satisfied(), (a_bool ^ a_neg) == (b_bool ^ b_neg));
}
{
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut a = Boolean::from(
AllocatedBit::alloc(cs.namespace(|| "a"), Some(a_bool)).unwrap(),
Expand All @@ -976,7 +976,7 @@ mod test {
assert_eq!(cs.is_satisfied(), (a_bool ^ a_neg) == (b_bool ^ b_neg));
}
{
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut a = Boolean::Constant(a_bool);
let mut b = Boolean::Constant(b_bool);
Expand Down Expand Up @@ -1005,7 +1005,7 @@ mod test {

#[test]
fn test_boolean_negation() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let mut b = Boolean::from(AllocatedBit::alloc(&mut cs, Some(true)).unwrap());

Expand Down Expand Up @@ -1097,7 +1097,7 @@ mod test {

for first_operand in variants.iter().cloned() {
for second_operand in variants.iter().cloned() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let a;
let b;
Expand Down Expand Up @@ -1306,7 +1306,7 @@ mod test {

for first_operand in variants.iter().cloned() {
for second_operand in variants.iter().cloned() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let a;
let b;
Expand Down Expand Up @@ -1527,7 +1527,7 @@ mod test {

#[test]
fn test_u64_into_boolean_vec_le() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let bits = u64_into_boolean_vec_le(&mut cs, Some(17234652694787248421)).unwrap();

Expand All @@ -1548,9 +1548,9 @@ mod test {

#[test]
fn test_field_into_allocated_bits_le() {
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let r = Fr::from_str(
let r = Scalar::from_str(
"9147677615426976802526883532204139322118074541891858454835346926874644257775",
)
.unwrap();
Expand Down Expand Up @@ -1643,16 +1643,16 @@ mod test {
} else {
assert_eq!(cs.get("ch"), {
if expected {
Fr::one()
Scalar::one()
} else {
Fr::zero()
Scalar::zero()
}
});
cs.set("ch", {
if expected {
Fr::zero()
Scalar::zero()
} else {
Fr::one()
Scalar::one()
}
});
assert_eq!(cs.which_is_unsatisfied().unwrap(), "ch computation");
Expand Down Expand Up @@ -1735,16 +1735,16 @@ mod test {
} else {
assert_eq!(cs.get("maj"), {
if expected {
Fr::one()
Scalar::one()
} else {
Fr::zero()
Scalar::zero()
}
});
cs.set("maj", {
if expected {
Fr::zero()
Scalar::zero()
} else {
Fr::one()
Scalar::one()
}
});
assert_eq!(cs.which_is_unsatisfied().unwrap(), "maj computation");
Expand All @@ -1757,7 +1757,7 @@ mod test {
#[test]
fn test_alloc_conditionally() {
{
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let b = AllocatedBit::alloc(&mut cs, Some(false)).unwrap();

let value = None;
Expand All @@ -1773,7 +1773,7 @@ mod test {

{
// since value is true, b must be false, so it should succeed
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let value = Some(true);
let b = AllocatedBit::alloc(&mut cs, Some(false)).unwrap();
Expand All @@ -1790,7 +1790,7 @@ mod test {

{
// since value is true, b must be false, so it should fail
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();

let value = Some(true);
let b = AllocatedBit::alloc(&mut cs, Some(true)).unwrap();
Expand All @@ -1805,15 +1805,15 @@ mod test {

let value = Some(false);
//check with false bit
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let b1 = AllocatedBit::alloc(&mut cs, Some(false)).unwrap();
AllocatedBit::alloc_conditionally(cs.namespace(|| "alloc_conditionally"), value, &b1)
.unwrap();

assert!(cs.is_satisfied());

//check with true bit
let mut cs = TestConstraintSystem::<Fr>::new();
let mut cs = TestConstraintSystem::<Scalar>::new();
let b2 = AllocatedBit::alloc(&mut cs, Some(true)).unwrap();
AllocatedBit::alloc_conditionally(cs.namespace(|| "alloc_conditionally"), value, &b2)
.unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/gadgets/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ mod test {
use crate::gadgets::boolean::{AllocatedBit, Boolean};
use crate::gadgets::test::*;

use bls12_381::Scalar;
use ff::Field;
use pairing::bls12_381::Fr;
use rand_core::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use std::ops::{AddAssign, Neg};
Expand All @@ -218,8 +218,8 @@ mod test {

let bits = vec![a, b, c];

let points: Vec<(Fr, Fr)> = (0..8)
.map(|_| (Fr::random(&mut rng), Fr::random(&mut rng)))
let points: Vec<(Scalar, Scalar)> = (0..8)
.map(|_| (Scalar::random(&mut rng), Scalar::random(&mut rng)))
.collect();

let res = lookup3_xy(&mut cs, &bits, &points).unwrap();
Expand Down Expand Up @@ -263,8 +263,8 @@ mod test {

let bits = vec![a, b, c];

let points: Vec<(Fr, Fr)> = (0..4)
.map(|_| (Fr::random(&mut rng), Fr::random(&mut rng)))
let points: Vec<(Scalar, Scalar)> = (0..4)
.map(|_| (Scalar::random(&mut rng), Scalar::random(&mut rng)))
.collect();

let res = lookup3_xy_with_conditional_negation(&mut cs, &bits, &points).unwrap();
Expand Down Expand Up @@ -297,15 +297,15 @@ mod test {

let window_size = 4;

let mut assignment = vec![Fr::zero(); 1 << window_size];
let mut assignment = vec![Scalar::zero(); 1 << window_size];
let constants: Vec<_> = (0..(1 << window_size))
.map(|_| Fr::random(&mut rng))
.map(|_| Scalar::random(&mut rng))
.collect();

synth(window_size, &constants, &mut assignment);

for b in 0..(1 << window_size) {
let mut acc = Fr::zero();
let mut acc = Scalar::zero();

for j in 0..(1 << window_size) {
if j & b == j {
Expand Down
Loading

0 comments on commit 459fa4a

Please sign in to comment.