diff --git a/src/arith.rs b/src/arith.rs index 2d816b7..7c09437 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -20,6 +20,12 @@ impl From<[u64; 4]> for U256 { } } +impl From for U256 { + fn from(d: u64) -> Self { + U256::from([d, 0, 0, 0]) + } +} + /// 512-bit, stack allocated biginteger for use in extension /// field serialization and scalar interpretation. #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -74,6 +80,22 @@ impl U512 { U512(res) } + pub fn from_slice(s: &[u8]) -> Result { + if s.len() != 64 { + return Err(Error::InvalidLength { + expected: 32, + actual: s.len(), + }); + } + + let mut n = [0; 4]; + for (l, i) in (0..4).rev().zip((0..4).map(|i| i * 16)) { + n[l] = BigEndian::read_u128(&s[i..]); + } + + Ok(U512(n)) + } + /// Get a random U512 pub fn random(rng: &mut R) -> U512 { U512(rng.gen()) @@ -423,7 +445,6 @@ fn div2(a: &mut [u128; 2]) { /// Multiply by two #[inline] -#[inline] fn mul2(a: &mut [u128; 2]) { let tmp = a[0] >> 127; a[0] <<= 1; diff --git a/src/fields/fq2.rs b/src/fields/fq2.rs index 8f64016..df96d40 100644 --- a/src/fields/fq2.rs +++ b/src/fields/fq2.rs @@ -92,6 +92,14 @@ impl Fq2 { } } } + + pub fn real(&self) -> &Fq { + &self.c0 + } + + pub fn imaginary(&self) -> &Fq { + &self.c1 + } } impl FieldElement for Fq2 { diff --git a/src/groups/mod.rs b/src/groups/mod.rs index fc247a8..9d911c4 100644 --- a/src/groups/mod.rs +++ b/src/groups/mod.rs @@ -689,13 +689,13 @@ fn test_miller_loop() { assert_eq!(gt, Fq12::new(Fq6::new( - Fq2::new(Fq::from_str("14551901853310307118181117653102171756020286507151693083446930124375536995872").unwrap(), Fq::from_str("9312135802322424742640599513015426415694425842442244572104764725304978020017").unwrap()), - Fq2::new(Fq::from_str("2008578374540014049115224515107136454624926345291695498760935593377832328658").unwrap(), Fq::from_str("19401931167387470703307774451905975977586101231060812348184567722817888018105").unwrap()), + Fq2::new(Fq::from_str("14551901853310307118181117653102171756020286507151693083446930124375536995872").unwrap(), Fq::from_str("9312135802322424742640599513015426415694425842442244572104764725304978020017").unwrap()), + Fq2::new(Fq::from_str("2008578374540014049115224515107136454624926345291695498760935593377832328658").unwrap(), Fq::from_str("19401931167387470703307774451905975977586101231060812348184567722817888018105").unwrap()), Fq2::new(Fq::from_str("15835061253582829097893482726334173316772697321004871665993836763948321578465").unwrap(), Fq::from_str("2434436628082562384254182545550914004674636606111293955202388712261962820365").unwrap()) ), Fq6::new( - Fq2::new(Fq::from_str("2874440054453559166574356420729655370224872280550180463983603224123901706537").unwrap(), Fq::from_str("21199736323249863378180814900160978651989782296293186487853700340281870105680").unwrap()), - Fq2::new(Fq::from_str("19165582755854282767090326095669835261356341739532443976394958023142879015770").unwrap(), Fq::from_str("1381947898997178910398427566832118260186305708991760706544743699683050330259").unwrap()), + Fq2::new(Fq::from_str("2874440054453559166574356420729655370224872280550180463983603224123901706537").unwrap(), Fq::from_str("21199736323249863378180814900160978651989782296293186487853700340281870105680").unwrap()), + Fq2::new(Fq::from_str("19165582755854282767090326095669835261356341739532443976394958023142879015770").unwrap(), Fq::from_str("1381947898997178910398427566832118260186305708991760706544743699683050330259").unwrap()), Fq2::new(Fq::from_str("282285618133171001983721596014922591835675934808772882476123488581876545578").unwrap(), Fq::from_str("9533292755262567365755835323107174518472361243562718718917822947506880920117").unwrap()) ) ) @@ -1017,15 +1017,15 @@ fn predefined_pair() { let g2 = AffineG2::new( Fq2::new( Fq::from_str("10857046999023057135944570762232829481370756359578518086990519993285655852781") - .expect("a-coeff of g2 x generator is of the right order"), + .expect("a-coeff of g2 x generator is of the right order"), Fq::from_str("11559732032986387107991004021392285783925812861821192530917403151452391805634") - .expect("b-coeff of g2 x generator is of the right order"), + .expect("b-coeff of g2 x generator is of the right order"), ), Fq2::new( Fq::from_str("8495653923123431417604973247489272438418190587263600148770280649306958101930") - .expect("a-coeff of g2 y generator is of the right order"), + .expect("a-coeff of g2 y generator is of the right order"), Fq::from_str("4082367875863433681332203403145435568316851327593401208105741076214120093531") - .expect("b-coeff of g2 y generator is of the right order"), + .expect("b-coeff of g2 y generator is of the right order"), ), ).expect("Point(11559732032986387107991004021392285783925812861821192530917403151452391805634 * i + 10857046999023057135944570762232829481370756359578518086990519993285655852781, 4082367875863433681332203403145435568316851327593401208105741076214120093531 * i + 8495653923123431417604973247489272438418190587263600148770280649306958101930) is a valid generator for G2") .to_jacobian(); diff --git a/src/lib.rs b/src/lib.rs index 8ce0d4f..e64e391 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ mod fields; mod groups; use fields::FieldElement; -use groups::GroupElement; +use groups::{GroupElement, G1Params, G2Params, GroupParams}; use std::ops::{Add, Mul, Neg, Sub}; use rand::Rng; @@ -146,6 +146,15 @@ impl Fq { a.to_big_endian(slice) .map_err(|_| FieldError::InvalidSliceLength) } + pub fn from_u256(u256: arith::U256) -> Result { + Ok(Fq(fields::Fq::new(u256).ok_or(FieldError::NotMember)?)) + } + pub fn into_u256(self) -> arith::U256 { + (self.0).into() + } + pub fn modulus() -> arith::U256 { + fields::Fq::modulus() + } } impl Add for Fq { @@ -180,6 +189,8 @@ impl Mul for Fq { } } +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[repr(C)] pub struct Fq2(fields::Fq2); impl Fq2 { @@ -187,6 +198,10 @@ impl Fq2 { Fq2(fields::Fq2::one()) } + pub fn i() -> Fq2 { + Fq2::new(Fq::zero(), Fq::one()) + } + pub fn zero() -> Fq2 { Fq2(fields::Fq2::zero()) } @@ -199,6 +214,51 @@ impl Fq2 { pub fn is_zero(&self) -> bool { self.0.is_zero() } + + pub fn pow(&self, exp: arith::U256) -> Self { + Fq2(self.0.pow(exp)) + } + + pub fn real(&self) -> Fq { + Fq(*self.0.real()) + } + + pub fn imaginary(&self) -> Fq { + Fq(*self.0.imaginary()) + } +} + + +impl Add for Fq2 { + type Output = Self; + + fn add(self, other: Self) -> Self { + Fq2(self.0 + other.0) + } +} + +impl Sub for Fq2 { + type Output = Self; + + fn sub(self, other: Self) -> Self { + Fq2(self.0 - other.0) + } +} + +impl Neg for Fq2 { + type Output = Self; + + fn neg(self) -> Self { + Fq2(-self.0) + } +} + +impl Mul for Fq2 { + type Output = Self; + + fn mul(self, other: Self) -> Self { + Fq2(self.0 * other.0) + } } pub trait Group @@ -253,6 +313,10 @@ impl G1 { pub fn set_z(&mut self, z: Fq) { *self.0.z_mut() = z.0 } + + pub fn b() -> Fq { + Fq(G1Params::coeff_b()) + } } impl Group for G1 { @@ -380,6 +444,10 @@ impl G2 { pub fn set_z(&mut self, z: Fq2) { *self.0.z_mut() = z.0 } + + pub fn b() -> Fq2 { + Fq2(G2Params::coeff_b()) + } } impl Group for G2 {