Add 'normalize' to Group trait for converting something into affine.
This commit is contained in:
parent
90656b0bbf
commit
207cf4eba2
|
@ -130,7 +130,7 @@ impl<P: GroupParams> G<P> {
|
|||
}
|
||||
|
||||
impl<P: GroupParams> AffineG<P> {
|
||||
fn to_jacobian(&self) -> G<P> {
|
||||
pub fn to_jacobian(&self) -> G<P> {
|
||||
G {
|
||||
x: self.x,
|
||||
y: self.y,
|
||||
|
|
17
src/lib.rs
17
src/lib.rs
|
@ -73,6 +73,7 @@ pub trait Group:
|
|||
fn one() -> Self;
|
||||
fn random<R: Rng>(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<R: Rng>(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<G1> for G1 {
|
||||
|
@ -119,6 +128,14 @@ impl Group for G2 {
|
|||
fn one() -> Self { G2(groups::G2::one()) }
|
||||
fn random<R: Rng>(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<G2> for G2 {
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue