Skip to content

Commit

Permalink
[RFC] Convert identity functions in Field, Group, and {Projective,Aff…
Browse files Browse the repository at this point in the history
…ine}Curve traits with One/Zero traits from num_traits.

- contributes to arkworks-rs#50,
- depends on arkworks-rs#53 and builds on it,
- due to coherence & requirements of `num_traits::{Zero, One}` to implement `std::ops::Add<Self, ..>` and (resp.) `std::ops::Mul<Self, ..>`, I've had to replace the afferent `impl<'a, P: ..> (Add|Mul)<&'a Self> for Group(Affine|Projective)<P>` by direct implementations on `Self`,
- I did not have to fight the borrow checker for this conversion => I think this hints arithmetic operations are called in contexts where the operand is owned,
- hence should this end up on a merge track, we may want to open an issue to convert the `impl<'a, P:..> (Neg|Sub|..)<&'a Self> for ..<P>` trait usage to direct `impl<P:..> (Neg|Sub|..)<Self> for ..<P>`
- the `impl AddAssign for GroupAffine<P>` in curves/models/short_weierstrass_jacobian.rs is provided to fit trait bounds, and without any guarantee of suitability for any particular purpose
- and that, even though I don't think it's used.
  • Loading branch information
huitseeker committed Jan 15, 2020
1 parent 93d23eb commit a85611e
Show file tree
Hide file tree
Showing 76 changed files with 970 additions and 638 deletions.
1 change: 1 addition & 0 deletions algebra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ edition = "2018"
byteorder = { version = "1" }
rand = { version = "0.7" }
derivative = { version = "1" }
num-traits = { version = "0.2.11"}

colored = { version = "1", optional = true }
rayon = { version = "1", optional = true }
Expand Down
36 changes: 23 additions & 13 deletions algebra/src/curves/bls12_377/g1.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::field_new;
use crate::{
biginteger::{BigInteger256, BigInteger384},
curves::models::{ModelParameters, SWModelParameters},
fields::{
bls12_377::{Fq, Fr},
Field,
},
field_new,
fields::bls12_377::{Fq, Fr},
};
use num_traits::Zero;

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct Bls12_377G1Parameters;
Expand All @@ -21,26 +19,32 @@ impl SWModelParameters for Bls12_377G1Parameters {
const COEFF_A: Fq = field_new!(Fq, BigInteger384([0x0, 0x0, 0x0, 0x0, 0x0, 0x0]));

/// COEFF_B = 1
const COEFF_B: Fq = field_new!(Fq, BigInteger384([
const COEFF_B: Fq = field_new!(
Fq,
BigInteger384([
0x2cdffffffffff68,
0x51409f837fffffb1,
0x9f7db3a98a7d3ff2,
0x7b4e97b76e7c6305,
0x4cf495bf803c84e8,
0x8d6661e2fdf49a,
]));
])
);

/// COFACTOR = (x - 1)^2 / 3 = 30631250834960419227450344600217059328
const COFACTOR: &'static [u64] = &[0x0, 0x170b5d4430000000];

/// COFACTOR_INV = COFACTOR^{-1} mod r
/// = 5285428838741532253824584287042945485047145357130994810877
const COFACTOR_INV: Fr = field_new!(Fr, BigInteger256([
const COFACTOR_INV: Fr = field_new!(
Fr,
BigInteger256([
2013239619100046060,
4201184776506987597,
2526766393982337036,
1114629510922847535,
]));
])
);

/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
Expand All @@ -54,22 +58,28 @@ impl SWModelParameters for Bls12_377G1Parameters {

/// G1_GENERATOR_X =
/// 81937999373150964239938255573465948239988671502647976594219695644855304257327692006745978603320413799295628339695
pub const G1_GENERATOR_X: Fq = field_new!(Fq, BigInteger384([
pub const G1_GENERATOR_X: Fq = field_new!(
Fq,
BigInteger384([
0x260f33b9772451f4,
0xc54dd773169d5658,
0x5c1551c469a510dd,
0x761662e4425e1698,
0xc97d78cc6f065272,
0xa41206b361fd4d,
]));
])
);

/// G1_GENERATOR_Y =
/// 241266749859715473739788878240585681733927191168601896383759122102112907357779751001206799952863815012735208165030
pub const G1_GENERATOR_Y: Fq = field_new!(Fq, BigInteger384([
pub const G1_GENERATOR_Y: Fq = field_new!(
Fq,
BigInteger384([
0x8193961fb8cb81f3,
0x638d4c5f44adb8,
0xfafaf3dad4daf54a,
0xc27849e2d655cd18,
0x2ec3ddb401d52814,
0x7da93326303c71,
]));
])
);
56 changes: 37 additions & 19 deletions algebra/src/curves/bls12_377/g2.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::field_new;
use super::g1::Bls12_377G1Parameters;
use crate::{
biginteger::{BigInteger256, BigInteger384},
curves::models::{ModelParameters, SWModelParameters},
fields::{
bls12_377::{Fq, Fq2, Fr},
Field,
},
field_new,
fields::bls12_377::{Fq, Fq2, Fr},
};
use num_traits::Zero;

#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct Bls12_377G2Parameters;
Expand All @@ -19,7 +17,8 @@ impl ModelParameters for Bls12_377G2Parameters {

impl SWModelParameters for Bls12_377G2Parameters {
/// COEFF_A = [0, 0]
const COEFF_A: Fq2 = field_new!(Fq2,
const COEFF_A: Fq2 = field_new!(
Fq2,
Bls12_377G1Parameters::COEFF_A,
Bls12_377G1Parameters::COEFF_A,
);
Expand All @@ -30,16 +29,20 @@ impl SWModelParameters for Bls12_377G2Parameters {
// In our case, i = u (App A.3, T_6).
/// COEFF_B = [0,
/// 155198655607781456406391640216936120121836107652948796323930557600032281009004493664981332883744016074664192874906]
const COEFF_B: Fq2 = field_new!(Fq2,
const COEFF_B: Fq2 = field_new!(
Fq2,
field_new!(Fq, BigInteger384([0, 0, 0, 0, 0, 0])),
field_new!(Fq, BigInteger384([
field_new!(
Fq,
BigInteger384([
9255502405446297221,
10229180150694123945,
9215585410771530959,
13357015519562362907,
5437107869987383107,
16259554076827459,
])),
])
),
);

/// COFACTOR =
Expand All @@ -57,12 +60,15 @@ impl SWModelParameters for Bls12_377G2Parameters {

/// COFACTOR_INV = COFACTOR^{-1} mod r
/// = 6764900296503390671038341982857278410319949526107311149686707033187604810669
const COFACTOR_INV: Fr = field_new!(Fr, BigInteger256([
const COFACTOR_INV: Fr = field_new!(
Fr,
BigInteger256([
15499857013495546999,
4613531467548868169,
14546778081091178013,
549402535258503313,
]));
])
);

/// AFFINE_GENERATOR_COEFFS = (G2_GENERATOR_X, G2_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
Expand All @@ -79,44 +85,56 @@ pub const G2_GENERATOR_Y: Fq2 = field_new!(Fq2, G2_GENERATOR_Y_C0, G2_GENERATOR_

/// G2_GENERATOR_X_C0 =
/// 233578398248691099356572568220835526895379068987715365179118596935057653620464273615301663571204657964920925606294
pub const G2_GENERATOR_X_C0: Fq = field_new!(Fq, BigInteger384([
pub const G2_GENERATOR_X_C0: Fq = field_new!(
Fq,
BigInteger384([
0x68904082f268725b,
0x668f2ea74f45328b,
0xebca7a65802be84f,
0x1e1850f4c1ada3e6,
0x830dc22d588ef1e9,
0x1862a81767c0982,
]));
])
);

/// G2_GENERATOR_X_C1 =
/// 140913150380207355837477652521042157274541796891053068589147167627541651775299824604154852141315666357241556069118
pub const G2_GENERATOR_X_C1: Fq = field_new!(Fq, BigInteger384([
pub const G2_GENERATOR_X_C1: Fq = field_new!(
Fq,
BigInteger384([
0x5f02a915c91c7f39,
0xf8c553ba388da2a7,
0xd51a416dbd198850,
0xe943c6f38ae3073a,
0xffe24aa8259a4981,
0x11853391e73dfdd,
]));
])
);

/// G2_GENERATOR_Y_C0 =
/// 63160294768292073209381361943935198908131692476676907196754037919244929611450776219210369229519898517858833747423
pub const G2_GENERATOR_Y_C0: Fq = field_new!(Fq, BigInteger384([
pub const G2_GENERATOR_Y_C0: Fq = field_new!(
Fq,
BigInteger384([
0xd5b19b897881430f,
0x5be9118a5b371ed,
0x6063f91f86c131ee,
0x3244a61be8f4ec19,
0xa02e425b9f9a3a12,
0x18af8c04f3360d2,
]));
])
);

/// G2_GENERATOR_Y_C1 =
/// 149157405641012693445398062341192467754805999074082136895788947234480009303640899064710353187729182149407503257491
pub const G2_GENERATOR_Y_C1: Fq = field_new!(Fq, BigInteger384([
pub const G2_GENERATOR_Y_C1: Fq = field_new!(
Fq,
BigInteger384([
0x57601ac71a5b96f5,
0xe99acc1714f2440e,
0x2339612f10118ea9,
0x8321e68a3b1cd722,
0x2b543b050cc74917,
0x590182b396c112,
]));
])
);
1 change: 1 addition & 0 deletions algebra/src/curves/bls12_377/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
},
groups::tests::group_test,
};
use num_traits::{One, Zero};
use std::ops::{AddAssign, MulAssign};

#[test]
Expand Down
36 changes: 23 additions & 13 deletions algebra/src/curves/bls12_381/g1.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::field_new;
use crate::{
biginteger::{BigInteger256, BigInteger384},
curves::{
Expand All @@ -7,11 +6,10 @@ use crate::{
models::{ModelParameters, SWModelParameters},
PairingCurve, PairingEngine,
},
fields::{
bls12_381::{Fq, Fq12, Fr},
Field,
},
field_new,
fields::bls12_381::{Fq, Fq12, Fr},
};
use num_traits::Zero;

pub type G1Affine = Bls12G1Affine<Bls12_381Parameters>;
pub type G1Projective = Bls12G1Projective<Bls12_381Parameters>;
Expand Down Expand Up @@ -44,26 +42,32 @@ impl SWModelParameters for Bls12_381G1Parameters {
const COEFF_A: Fq = field_new!(Fq, BigInteger384([0x0, 0x0, 0x0, 0x0, 0x0, 0x0]));

/// COEFF_B = 4
const COEFF_B: Fq = field_new!(Fq, BigInteger384([
const COEFF_B: Fq = field_new!(
Fq,
BigInteger384([
0xaa270000000cfff3,
0x53cc0032fc34000a,
0x478fe97a6b0a807f,
0xb1d37ebee6ba24d7,
0x8ec9733bbf78ab2f,
0x9d645513d83de7e,
]));
])
);

/// COFACTOR = (x - 1)^2 / 3 = 76329603384216526031706109802092473003
const COFACTOR: &'static [u64] = &[0x8c00aaab0000aaab, 0x396c8c005555e156];

/// COFACTOR_INV = COFACTOR^{-1} mod r
/// = 52435875175126190458656871551744051925719901746859129887267498875565241663483
const COFACTOR_INV: Fr = field_new!(Fr, BigInteger256([
const COFACTOR_INV: Fr = field_new!(
Fr,
BigInteger256([
288839107172787499,
1152722415086798946,
2612889808468387987,
5124657601728438008,
]));
])
);

/// AFFINE_GENERATOR_COEFFS = (G1_GENERATOR_X, G1_GENERATOR_Y)
const AFFINE_GENERATOR_COEFFS: (Self::BaseField, Self::BaseField) =
Expand All @@ -77,22 +81,28 @@ impl SWModelParameters for Bls12_381G1Parameters {

/// G1_GENERATOR_X =
/// 3685416753713387016781088315183077757961620795782546409894578378688607592378376318836054947676345821548104185464507
pub const G1_GENERATOR_X: Fq = field_new!(Fq, BigInteger384([
pub const G1_GENERATOR_X: Fq = field_new!(
Fq,
BigInteger384([
0x5cb38790fd530c16,
0x7817fc679976fff5,
0x154f95c7143ba1c1,
0xf0ae6acdf3d0e747,
0xedce6ecc21dbf440,
0x120177419e0bfb75,
]));
])
);

/// G1_GENERATOR_Y =
/// 1339506544944476473020471379941921221584933875938349620426543736416511423956333506472724655353366534992391756441569
pub const G1_GENERATOR_Y: Fq = field_new!(Fq, BigInteger384([
pub const G1_GENERATOR_Y: Fq = field_new!(
Fq,
BigInteger384([
0xbaac93d50ce72271,
0x8c22631a7918fd8e,
0xdd595f13570725ce,
0x51ac582950405194,
0xe1c8c3fad0059c0,
0xbbc3efc5008a26a,
]));
])
);
Loading

0 comments on commit a85611e

Please sign in to comment.