From 5df8d83c19f3fb97ae3fdefa5bad5279babe795e Mon Sep 17 00:00:00 2001 From: NikVolf Date: Mon, 27 Mar 2017 18:05:51 +0300 Subject: [PATCH] missing bindings for api --- src/lib.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5cbadd9..984c133 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,6 +120,23 @@ impl Mul for Fq { fn mul(self, other: Fq) -> Fq { Fq(self.0 * other.0) } } +pub struct Fq2(fields::Fq2); + +impl Fq2 { + pub fn one() -> Fq2 { + Fq2(fields::Fq2::one()) + } + + pub fn zero() -> Fq2 { + Fq2(fields::Fq2::zero()) + } + + /// Initalizes new F_q2(a + bi, a is real coeff, b is imaginary) + pub fn new(a: Fq, b: Fq) -> Fq2 { + Fq2(fields::Fq2::new(a.0, b.0)) + } +} + pub trait Group: rustc_serialize::Encodable + rustc_serialize::Decodable + @@ -256,6 +273,36 @@ impl From for G1 { #[repr(C)] pub struct G2(groups::G2); +impl G2 { + pub fn new(x: Fq2, y: Fq2, z: Fq2) -> Self { + G2(groups::G2::new(x.0, y.0, z.0)) + } + + pub fn x(&self) -> Fq2 { + Fq2(self.0.x().clone()) + } + + pub fn set_x(&mut self, x: Fq2) { + *self.0.x_mut() = x.0 + } + + pub fn y(&self) -> Fq2 { + Fq2(self.0.y().clone()) + } + + pub fn set_y(&mut self, y: Fq2) { + *self.0.y_mut() = y.0 + } + + pub fn z(&self) -> Fq2 { + Fq2(self.0.z().clone()) + } + + pub fn set_z(&mut self, z: Fq2) { + *self.0.z_mut() = z.0 + } +} + impl Group for G2 { fn zero() -> Self { G2(groups::G2::zero()) } fn one() -> Self { G2(groups::G2::one()) } @@ -313,4 +360,40 @@ impl Mul for Gt { pub fn pairing(p: G1, q: G2) -> Gt { Gt(groups::pairing(&p.0, &q.0)) +} + +#[derive(Copy, Clone, PartialEq, Eq, RustcDecodable, RustcEncodable)] +#[repr(C)] +pub struct AffineG2(groups::AffineG2); + +impl AffineG2 { + pub fn new(x: Fq2, y: Fq2) -> Result { + Ok(AffineG2(groups::AffineG2::new(x.0, y.0)?)) + } + + pub fn x(&self) -> Fq2 { + Fq2(self.0.x().clone()) + } + + pub fn set_x(&mut self, x: Fq2) { + *self.0.x_mut() = x.0 + } + + pub fn y(&self) -> Fq2 { + Fq2(self.0.y().clone()) + } + + pub fn set_y(&mut self, y: Fq2) { + *self.0.y_mut() = y.0 + } + + pub fn from_jacobian(g2: G2) -> Option { + g2.0.to_affine().map(|x| AffineG2(x)) + } +} + +impl From for G2 { + fn from(affine: AffineG2) -> Self { + G2(affine.0.to_jacobian()) + } } \ No newline at end of file