From 3871781225d2da6d8fc4b7fd0bdbbffc6bbaf262 Mon Sep 17 00:00:00 2001 From: Weikeng Chen Date: Fri, 2 Jul 2021 16:52:06 -0700 Subject: [PATCH] For reference, add a test for non-zero-affine (#69) * add a test for non-zero-affine for reference * add some doc --- Cargo.toml | 2 +- .../short_weierstrass/non_zero_affine.rs | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 15f884a..591cd86 100644 --- a/Cargo.toml +++ b/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] diff --git a/src/groups/curves/short_weierstrass/non_zero_affine.rs b/src/groups/curves/short_weierstrass/non_zero_affine.rs index a598bdd..3881dd5 100644 --- a/src/groups/curves/short_weierstrass/non_zero_affine.rs +++ b/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::::new_ref(); + + let x = FpVar::Var( + AllocatedFp::::new_witness(cs.clone(), || { + Ok(G1Parameters::AFFINE_GENERATOR_COEFFS.0) + }) + .unwrap(), + ); + let y = FpVar::Var( + AllocatedFp::::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::>::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::>::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); + } +}