From 207cf4eba262e606413f86ea75479752f21577af Mon Sep 17 00:00:00 2001 From: Sean Bowe Date: Fri, 14 Oct 2016 12:24:16 -0600 Subject: [PATCH] Add 'normalize' to Group trait for converting something into affine. --- src/groups/mod.rs | 2 +- src/lib.rs | 17 +++++++++++++++++ tests/serialization.rs | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/groups/mod.rs b/src/groups/mod.rs index 4afb8fd..60338c0 100644 --- a/src/groups/mod.rs +++ b/src/groups/mod.rs @@ -130,7 +130,7 @@ impl G

{ } impl AffineG

{ - fn to_jacobian(&self) -> G

{ + pub fn to_jacobian(&self) -> G

{ G { x: self.x, y: self.y, diff --git a/src/lib.rs b/src/lib.rs index 420a6fd..2212da7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,6 +73,7 @@ pub trait Group: fn one() -> Self; fn random(rng: &mut R) -> Self; fn is_zero(&self) -> bool; + fn normalize(&mut self); } #[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)] @@ -84,6 +85,14 @@ impl Group for G1 { fn one() -> Self { G1(groups::G1::one()) } fn random(rng: &mut R) -> Self { G1(groups::G1::random(rng)) } fn is_zero(&self) -> bool { self.0.is_zero() } + fn normalize(&mut self) { + let new = match self.0.to_affine() { + Some(a) => a, + None => return + }; + + self.0 = new.to_jacobian(); + } } impl Add for G1 { @@ -119,6 +128,14 @@ impl Group for G2 { fn one() -> Self { G2(groups::G2::one()) } fn random(rng: &mut R) -> Self { G2(groups::G2::random(rng)) } fn is_zero(&self) -> bool { self.0.is_zero() } + fn normalize(&mut self) { + let new = match self.0.to_affine() { + Some(a) => a, + None => return + }; + + self.0 = new.to_jacobian(); + } } impl Add for G2 { diff --git a/tests/serialization.rs b/tests/serialization.rs index 9377312..88c928e 100644 --- a/tests/serialization.rs +++ b/tests/serialization.rs @@ -36,6 +36,10 @@ fn group_serialization_and_deserialization() { assert!(reserialize(a) == a); assert!(reserialize(reserialize(a)) == a); + let mut c = a; + c.normalize(); + + assert!(a == c); } let mut a = G2::one(); @@ -44,6 +48,11 @@ fn group_serialization_and_deserialization() { assert!(reserialize(a) == a); assert!(reserialize(reserialize(a)) == a); + let mut c = a; + c.normalize(); + + assert!(a == c); + } }