Browse Source

For reference, add a test for non-zero-affine (#69)

* add a test for non-zero-affine for reference

* add some doc
master
Weikeng Chen 3 years ago
committed by GitHub
parent
commit
3871781225
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 1 deletions
  1. +1
    -1
      Cargo.toml
  2. +82
    -0
      src/groups/curves/short_weierstrass/non_zero_affine.rs

+ 1
- 1
Cargo.toml

@ -30,7 +30,7 @@ num-bigint = {version = "0.4", default-features = false }
num-traits = {version = "0.2", default-features = false }
[dev-dependencies]
ark-test-curves = { version = "^0.3.0", default-features = false, features = ["bls12_381_scalar_field", "mnt4_753_scalar_field"] }
ark-test-curves = { version = "^0.3.0", default-features = false, features = ["bls12_381_scalar_field", "bls12_381_curve", "mnt4_753_scalar_field"] }
ark-poly = { version = "^0.3.0", default-features = false }
[features]

+ 82
- 0
src/groups/curves/short_weierstrass/non_zero_affine.rs

@ -1,4 +1,5 @@
use super::*;
/// An affine representation of a prime order curve point that is guaranteed
/// to *not* be the point at infinity.
#[derive(Derivative)]
@ -161,3 +162,84 @@ where
Ok(Self::new(x, y))
}
}
#[cfg(test)]
mod test {
use crate::alloc::AllocVar;
use crate::fields::fp::{AllocatedFp, FpVar};
use crate::groups::curves::short_weierstrass::non_zero_affine::NonZeroAffineVar;
use crate::groups::curves::short_weierstrass::ProjectiveVar;
use crate::groups::CurveVar;
use crate::R1CSVar;
use ark_ec::SWModelParameters;
use ark_relations::r1cs::ConstraintSystem;
use ark_std::{vec::Vec, One};
use ark_test_curves::bls12_381::{g1::Parameters as G1Parameters, Fq};
#[test]
fn test_non_zero_affine_cost() {
let cs = ConstraintSystem::<Fq>::new_ref();
let x = FpVar::Var(
AllocatedFp::<Fq>::new_witness(cs.clone(), || {
Ok(G1Parameters::AFFINE_GENERATOR_COEFFS.0)
})
.unwrap(),
);
let y = FpVar::Var(
AllocatedFp::<Fq>::new_witness(cs.clone(), || {
Ok(G1Parameters::AFFINE_GENERATOR_COEFFS.1)
})
.unwrap(),
);
// The following code uses `double` and `add` (`add_unchecked`) to compute
// (1 + 2 + ... + 2^9) G
let sum_a = {
let mut a = ProjectiveVar::<G1Parameters, FpVar<Fq>>::new(
x.clone(),
y.clone(),
FpVar::Constant(Fq::one()),
);
let mut double_sequence = Vec::new();
double_sequence.push(a.clone());
for _ in 1..10 {
a = a.double().unwrap();
double_sequence.push(a.clone());
}
let mut sum = double_sequence[0].clone();
for elem in double_sequence.iter().skip(1) {
sum = sum + elem;
}
let sum = sum.value().unwrap();
(sum.x, sum.y)
};
let sum_b = {
let mut a = NonZeroAffineVar::<G1Parameters, FpVar<Fq>>::new(x, y);
let mut double_sequence = Vec::new();
double_sequence.push(a.clone());
for _ in 1..10 {
a = a.double().unwrap();
double_sequence.push(a.clone());
}
let mut sum = double_sequence[0].clone();
for elem in double_sequence.iter().skip(1) {
sum = sum.add_unchecked(&elem).unwrap();
}
(sum.x.value().unwrap(), sum.y.value().unwrap())
};
assert_eq!(sum_a.0, sum_b.0);
assert_eq!(sum_a.1, sum_b.1);
}
}

Loading…
Cancel
Save