From 9228d20862476f3784ba31c71631c899bb7a77a9 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Thu, 30 May 2019 18:14:18 +0300 Subject: [PATCH] prepare for gpu integration --- Cargo.toml | 6 ++++-- src/bls12_381/ec.rs | 16 ++++++++++++++-- src/bn256/ec.rs | 18 ++++++++++++++---- src/lib.rs | 20 ++++++++++++++------ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e7b64b3..22dbc56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,14 @@ description = "Pairing-friendly elliptic curve library" documentation = "https://docs.rs/pairing/" homepage = "https://github.com/matter-labs/pairing" repository = "https://github.com/matter-labs/pairing" +edition = "2018" [dependencies] rand = "0.4" byteorder = "1" -ff_ce = {version = "0.6", features = ["derive"] } -#ff = { git = 'https://github.com/matterinc/ff', features = ["derive"], tag = "0.5"} +#ff_ce = {version = "0.6", features = ["derive"] } +#ff_ce = { git = 'https://github.com/matter-labs/ff', features = ["derive"], branch = "gpu"} +ff_ce = { path = '../ff', features = ["derive", "derive_serde"]} serde = "1.0.80" serde_derive = "1.0.80" serde_json = "1.0.33" diff --git a/src/bls12_381/ec.rs b/src/bls12_381/ec.rs index 5c0545f..d4e1131 100644 --- a/src/bls12_381/ec.rs +++ b/src/bls12_381/ec.rs @@ -626,7 +626,7 @@ pub mod g1 { use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField}; use rand::{Rand, Rng}; use std::fmt; - use {CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; + use crate::{RawEncodable, CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; curve_impl!( "G1", @@ -750,6 +750,18 @@ pub mod g1 { } } + impl RawEncodable for G1Affine { + fn into_raw_uncompressed_le(&self) -> Self::Uncompressed { + let mut res = Self::Uncompressed::empty(); + let mut writer = &mut res.0[..]; + + self.x.into_raw_repr().write_le(&mut writer).unwrap(); + self.y.into_raw_repr().write_le(&mut writer).unwrap(); + + res + } + } + #[derive(Copy, Clone)] pub struct G1Compressed([u8; 48]); @@ -1272,7 +1284,7 @@ pub mod g2 { use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField}; use rand::{Rand, Rng}; use std::fmt; - use {CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; + use crate::{CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; curve_impl!( "G2", diff --git a/src/bn256/ec.rs b/src/bn256/ec.rs index f4a3a63..ab58e29 100644 --- a/src/bn256/ec.rs +++ b/src/bn256/ec.rs @@ -190,9 +190,7 @@ macro_rules! curve_impl { fn into_projective(&self) -> $projective { (*self).into() } - } - // impl Rand for $projective { // fn rand(rng: &mut R) -> Self { // loop { @@ -630,7 +628,7 @@ pub mod g1 { use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField}; use rand::{Rand, Rng}; use std::fmt; - use {CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; + use crate::{RawEncodable, CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; curve_impl!( "G1", @@ -644,6 +642,18 @@ pub mod g1 { G2Affine ); + impl RawEncodable for G1Affine { + fn into_raw_uncompressed_le(&self) -> Self::Uncompressed { + let mut res = Self::Uncompressed::empty(); + let mut writer = &mut res.0[..]; + + self.x.into_raw_repr().write_le(&mut writer).unwrap(); + self.y.into_raw_repr().write_le(&mut writer).unwrap(); + + res + } + } + #[derive(Copy, Clone)] pub struct G1Uncompressed([u8; 64]); @@ -1013,7 +1023,7 @@ pub mod g2 { use ff::{BitIterator, Field, PrimeField, PrimeFieldRepr, SqrtField}; use rand::{Rand, Rng}; use std::fmt; - use {CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; + use crate::{CurveAffine, CurveProjective, EncodedPoint, Engine, GroupDecodingError}; curve_impl!( "G2", diff --git a/src/lib.rs b/src/lib.rs index f704b21..f6f1df6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,11 +22,13 @@ extern crate serde_derive; #[cfg(test)] pub mod tests; -extern crate ff_ce as imported_ff; +pub extern crate ff_ce as ff; -pub mod ff { - pub use imported_ff::*; -} +pub use ff::*; + +// pub mod ff { +// pub use ff::*; +// } pub mod bls12_381; pub mod bn256; @@ -60,7 +62,7 @@ pub trait Engine: ScalarEngine { Pair = Self::G2Affine, PairingResult = Self::Fqk, > - + From; + + From + RawEncodable; /// The projective representation of an element in G2. type G2: CurveProjective< @@ -102,7 +104,7 @@ pub trait Engine: ScalarEngine { >; /// Perform final exponentiation of the result of a miller loop. - fn final_exponentiation(&Self::Fqk) -> Option; + fn final_exponentiation(r: &Self::Fqk) -> Option; /// Performs a complete pairing operation `(p, q)`. fn pairing(p: G1, q: G2) -> Self::Fqk @@ -240,6 +242,12 @@ pub trait CurveAffine: } } +pub trait RawEncodable: CurveAffine { + /// Converts this element into its uncompressed encoding, so long as it's not + /// the point at infinity. Leaves coordinates in Montgommery form + fn into_raw_uncompressed_le(&self) -> Self::Uncompressed; +} + /// An encoded elliptic curve point, which should essentially wrap a `[u8; N]`. pub trait EncodedPoint: Sized + Send + Sync + AsRef<[u8]> + AsMut<[u8]> + Clone + Copy + 'static