jubjub: Use readable literals

This commit is contained in:
Jack Grigg 2020-01-14 10:55:00 -05:00
parent 049847f1a8
commit 4c9793d001
2 changed files with 141 additions and 136 deletions

View File

@ -64,10 +64,10 @@ impl ConditionallySelectable for Fr {
/// Constant representing the modulus /// Constant representing the modulus
/// r = 0x0e7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7 /// r = 0x0e7db4ea6533afa906673b0101343b00a6682093ccc81082d0970e5ed6f72cb7
pub const MODULUS: Fr = Fr([ pub const MODULUS: Fr = Fr([
0xd0970e5ed6f72cb7, 0xd097_0e5e_d6f7_2cb7,
0xa6682093ccc81082, 0xa668_2093_ccc8_1082,
0x06673b0101343b00, 0x0667_3b01_0134_3b00,
0x0e7db4ea6533afa9, 0x0e7d_b4ea_6533_afa9,
]); ]);
impl<'a> Neg for &'a Fr { impl<'a> Neg for &'a Fr {
@ -121,30 +121,30 @@ impl_binops_additive!(Fr, Fr);
impl_binops_multiplicative!(Fr, Fr); impl_binops_multiplicative!(Fr, Fr);
/// INV = -(r^{-1} mod 2^64) mod 2^64 /// INV = -(r^{-1} mod 2^64) mod 2^64
const INV: u64 = 0x1ba3a358ef788ef9; const INV: u64 = 0x1ba3_a358_ef78_8ef9;
/// R = 2^256 mod r /// R = 2^256 mod r
const R: Fr = Fr([ const R: Fr = Fr([
0x25f80bb3b99607d9, 0x25f8_0bb3_b996_07d9,
0xf315d62f66b6e750, 0xf315_d62f_66b6_e750,
0x932514eeeb8814f4, 0x9325_14ee_eb88_14f4,
0x09a6fc6f479155c6, 0x09a6_fc6f_4791_55c6,
]); ]);
/// R^2 = 2^512 mod r /// R^2 = 2^512 mod r
const R2: Fr = Fr([ const R2: Fr = Fr([
0x67719aa495e57731, 0x6771_9aa4_95e5_7731,
0x51b0cef09ce3fc26, 0x51b0_cef0_9ce3_fc26,
0x69dab7fac026e9a5, 0x69da_b7fa_c026_e9a5,
0x04f6547b8d127688, 0x04f6_547b_8d12_7688,
]); ]);
/// R^2 = 2^768 mod r /// R^2 = 2^768 mod r
const R3: Fr = Fr([ const R3: Fr = Fr([
0xe0d6c6563d830544, 0xe0d6_c656_3d83_0544,
0x323e3883598d0f85, 0x323e_3883_598d_0f85,
0xf0fea3004c2e2ba8, 0xf0fe_a300_4c2e_2ba8,
0x05874f84946737ec, 0x0587_4f84_9467_37ec,
]); ]);
impl Default for Fr { impl Default for Fr {
@ -296,10 +296,10 @@ impl Fr {
// sqrt can be done with only one exponentiation, // sqrt can be done with only one exponentiation,
// via the computation of self^((r + 1) // 4) (mod r) // via the computation of self^((r + 1) // 4) (mod r)
let sqrt = self.pow_vartime(&[ let sqrt = self.pow_vartime(&[
0xb425c397b5bdcb2e, 0xb425_c397_b5bd_cb2e,
0x299a0824f3320420, 0x299a_0824_f332_0420,
0x4199cec0404d0ec0, 0x4199_cec0_404d_0ec0,
0x039f6d3a994cebea, 0x039f_6d3a_994c_ebea,
]); ]);
CtOption::new( CtOption::new(
@ -768,7 +768,7 @@ fn test_from_u512_r2() {
#[test] #[test]
fn test_from_u512_max() { fn test_from_u512_max() {
let max_u64 = 0xffffffffffffffff; let max_u64 = 0xffff_ffff_ffff_ffff;
assert_eq!( assert_eq!(
R3 - R, R3 - R,
Fr::from_u512([max_u64, max_u64, max_u64, max_u64, max_u64, max_u64, max_u64, max_u64]) Fr::from_u512([max_u64, max_u64, max_u64, max_u64, max_u64, max_u64, max_u64, max_u64])
@ -803,10 +803,10 @@ fn test_from_bytes_wide_negative_one() {
fn test_from_bytes_wide_maximum() { fn test_from_bytes_wide_maximum() {
assert_eq!( assert_eq!(
Fr([ Fr([
0x8b75c9015ae42a22, 0x8b75_c901_5ae4_2a22,
0xe59082e7bf9e38b8, 0xe590_82e7_bf9e_38b8,
0x6440c91261da51b3, 0x6440_c912_61da_51b3,
0xa5e07ffb20991cf 0x0a5e_07ff_b209_91cf,
]), ]),
Fr::from_bytes_wide(&[0xff; 64]) Fr::from_bytes_wide(&[0xff; 64])
); );
@ -822,10 +822,10 @@ fn test_zero() {
#[cfg(test)] #[cfg(test)]
const LARGEST: Fr = Fr([ const LARGEST: Fr = Fr([
0xd0970e5ed6f72cb6, 0xd097_0e5e_d6f7_2cb6,
0xa6682093ccc81082, 0xa668_2093_ccc8_1082,
0x06673b0101343b00, 0x0667_3b01_0134_3b00,
0x0e7db4ea6533afa9, 0x0e7d_b4ea_6533_afa9,
]); ]);
#[test] #[test]
@ -836,10 +836,10 @@ fn test_addition() {
assert_eq!( assert_eq!(
tmp, tmp,
Fr([ Fr([
0xd0970e5ed6f72cb5, 0xd097_0e5e_d6f7_2cb5,
0xa6682093ccc81082, 0xa668_2093_ccc8_1082,
0x06673b0101343b00, 0x0667_3b01_0134_3b00,
0x0e7db4ea6533afa9 0x0e7d_b4ea_6533_afa9
]) ])
); );
@ -956,10 +956,10 @@ fn test_inversion() {
#[test] #[test]
fn test_invert_is_pow() { fn test_invert_is_pow() {
let r_minus_2 = [ let r_minus_2 = [
0xd0970e5ed6f72cb5, 0xd097_0e5e_d6f7_2cb5,
0xa6682093ccc81082, 0xa668_2093_ccc8_1082,
0x06673b0101343b00, 0x0667_3b01_0134_3b00,
0x0e7db4ea6533afa9, 0x0e7d_b4ea_6533_afa9,
]; ];
let mut r1 = R; let mut r1 = R;
@ -984,10 +984,10 @@ fn test_invert_is_pow() {
fn test_sqrt() { fn test_sqrt() {
let mut square = Fr([ let mut square = Fr([
// r - 2 // r - 2
0xd0970e5ed6f72cb5, 0xd097_0e5e_d6f7_2cb5,
0xa6682093ccc81082, 0xa668_2093_ccc8_1082,
0x06673b0101343b00, 0x0667_3b01_0134_3b00,
0x0e7db4ea6533afa9, 0x0e7d_b4ea_6533_afa9,
]); ]);
let mut none_count = 0; let mut none_count = 0;
@ -1009,12 +1009,12 @@ fn test_sqrt() {
fn test_from_raw() { fn test_from_raw() {
assert_eq!( assert_eq!(
Fr::from_raw([ Fr::from_raw([
0x25f80bb3b99607d8, 0x25f8_0bb3_b996_07d8,
0xf315d62f66b6e750, 0xf315_d62f_66b6_e750,
0x932514eeeb8814f4, 0x9325_14ee_eb88_14f4,
0x9a6fc6f479155c6 0x09a6_fc6f_4791_55c6,
]), ]),
Fr::from_raw([0xffffffffffffffff; 4]) Fr::from_raw([0xffff_ffff_ffff_ffff; 4])
); );
assert_eq!(Fr::from_raw(MODULUS.0), Fr::zero()); assert_eq!(Fr::from_raw(MODULUS.0), Fr::zero());

View File

@ -344,18 +344,18 @@ impl_binops_multiplicative_mixed!(ExtendedNielsPoint, Fr, ExtendedPoint);
// `d = -(10240/10241)` // `d = -(10240/10241)`
const EDWARDS_D: Fq = Fq::from_raw([ const EDWARDS_D: Fq = Fq::from_raw([
0x01065fd6d6343eb1, 0x0106_5fd6_d634_3eb1,
0x292d7f6d37579d26, 0x292d_7f6d_3757_9d26,
0xf5fd9207e6bd7fd4, 0xf5fd_9207_e6bd_7fd4,
0x2a9318e74bfa2b48, 0x2a93_18e7_4bfa_2b48,
]); ]);
// `2*d` // `2*d`
const EDWARDS_D2: Fq = Fq::from_raw([ const EDWARDS_D2: Fq = Fq::from_raw([
0x020cbfadac687d62, 0x020c_bfad_ac68_7d62,
0x525afeda6eaf3a4c, 0x525a_feda_6eaf_3a4c,
0xebfb240fcd7affa8, 0xebfb_240f_cd7a_ffa8,
0x552631ce97f45691, 0x5526_31ce_97f4_5691,
]); ]);
impl AffinePoint { impl AffinePoint {
@ -952,16 +952,16 @@ fn test_extended_niels_point_identity() {
fn test_assoc() { fn test_assoc() {
let p = ExtendedPoint::from(AffinePoint { let p = ExtendedPoint::from(AffinePoint {
u: Fq::from_raw([ u: Fq::from_raw([
0x81c571e5d883cfb0, 0x81c5_71e5_d883_cfb0,
0x049f7a686f147029, 0x049f_7a68_6f14_7029,
0xf539c860bc3ea21f, 0xf539_c860_bc3e_a21f,
0x4284715b7ccc8162, 0x4284_715b_7ccc_8162,
]), ]),
v: Fq::from_raw([ v: Fq::from_raw([
0xbf096275684bb8ca, 0xbf09_6275_684b_b8ca,
0xc7ba245890af256d, 0xc7ba_2458_90af_256d,
0x59119f3e86380eb0, 0x5911_9f3e_8638_0eb0,
0x3793de182f9fb1d2, 0x3793_de18_2f9f_b1d2,
]), ]),
}) })
.mul_by_cofactor(); .mul_by_cofactor();
@ -977,16 +977,16 @@ fn test_assoc() {
fn test_batch_normalize() { fn test_batch_normalize() {
let mut p = ExtendedPoint::from(AffinePoint { let mut p = ExtendedPoint::from(AffinePoint {
u: Fq::from_raw([ u: Fq::from_raw([
0x81c571e5d883cfb0, 0x81c5_71e5_d883_cfb0,
0x049f7a686f147029, 0x049f_7a68_6f14_7029,
0xf539c860bc3ea21f, 0xf539_c860_bc3e_a21f,
0x4284715b7ccc8162, 0x4284_715b_7ccc_8162,
]), ]),
v: Fq::from_raw([ v: Fq::from_raw([
0xbf096275684bb8ca, 0xbf09_6275_684b_b8ca,
0xc7ba245890af256d, 0xc7ba_2458_90af_256d,
0x59119f3e86380eb0, 0x5911_9f3e_8638_0eb0,
0x3793de182f9fb1d2, 0x3793_de18_2f9f_b1d2,
]), ]),
}) })
.mul_by_cofactor(); .mul_by_cofactor();
@ -1019,10 +1019,10 @@ fn test_batch_normalize() {
#[cfg(test)] #[cfg(test)]
const FULL_GENERATOR: AffinePoint = AffinePoint::from_raw_unchecked( const FULL_GENERATOR: AffinePoint = AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0xe4b3d35df1a7adfe, 0xe4b3_d35d_f1a7_adfe,
0xcaf55d1b29bf81af, 0xcaf5_5d1b_29bf_81af,
0x8b0f03ddd60a8187, 0x8b0f_03dd_d60a_8187,
0x62edcbb8bf3787c8, 0x62ed_cbb8_bf37_87c8,
]), ]),
Fq::from_raw([0xb, 0x0, 0x0, 0x0]), Fq::from_raw([0xb, 0x0, 0x0, 0x0]),
); );
@ -1031,80 +1031,85 @@ const FULL_GENERATOR: AffinePoint = AffinePoint::from_raw_unchecked(
const EIGHT_TORSION: [AffinePoint; 8] = [ const EIGHT_TORSION: [AffinePoint; 8] = [
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0xd92e6a7927200d43, 0xd92e_6a79_2720_0d43,
0x7aa41ac43dae8582, 0x7aa4_1ac4_3dae_8582,
0xeaaae086a16618d1, 0xeaaa_e086_a166_18d1,
0x71d4df38ba9e7973, 0x71d4_df38_ba9e_7973,
]), ]),
Fq::from_raw([ Fq::from_raw([
0xff0d2068eff496dd, 0xff0d_2068_eff4_96dd,
0x9106ee90f384a4a1, 0x9106_ee90_f384_a4a1,
0x16a13035ad4d7266, 0x16a1_3035_ad4d_7266,
0x4958bdb21966982e, 0x4958_bdb2_1966_982e,
]), ]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0xfffeffff00000001, 0xfffe_ffff_0000_0001,
0x67baa40089fb5bfe, 0x67ba_a400_89fb_5bfe,
0xa5e80b39939ed334, 0xa5e8_0b39_939e_d334,
0x73eda753299d7d47, 0x73ed_a753_299d_7d47,
]), ]),
Fq::from_raw([0x0, 0x0, 0x0, 0x0]), Fq::from_raw([0x0, 0x0, 0x0, 0x0]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0xd92e6a7927200d43, 0xd92e_6a79_2720_0d43,
0x7aa41ac43dae8582, 0x7aa4_1ac4_3dae_8582,
0xeaaae086a16618d1, 0xeaaa_e086_a166_18d1,
0x71d4df38ba9e7973, 0x71d4_df38_ba9e_7973,
]), ]),
Fq::from_raw([ Fq::from_raw([
0xf2df96100b6924, 0x00f2_df96_100b_6924,
0xc2b6b5720c79b75d, 0xc2b6_b572_0c79_b75d,
0x1c98a7d25c54659e, 0x1c98_a7d2_5c54_659e,
0x2a94e9a11036e51a, 0x2a94_e9a1_1036_e51a,
]), ]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([0x0, 0x0, 0x0, 0x0]), Fq::from_raw([0x0, 0x0, 0x0, 0x0]),
Fq::from_raw([ Fq::from_raw([
0xffffffff00000000, 0xffff_ffff_0000_0000,
0x53bda402fffe5bfe, 0x53bd_a402_fffe_5bfe,
0x3339d80809a1d805, 0x3339_d808_09a1_d805,
0x73eda753299d7d48, 0x73ed_a753_299d_7d48,
]), ]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0x26d19585d8dff2be, 0x26d1_9585_d8df_f2be,
0xd919893ec24fd67c, 0xd919_893e_c24f_d67c,
0x488ef781683bbf33, 0x488e_f781_683b_bf33,
0x218c81a6eff03d4, 0x0218_c81a_6eff_03d4,
]), ]),
Fq::from_raw([ Fq::from_raw([
0xf2df96100b6924, 0x00f2_df96_100b_6924,
0xc2b6b5720c79b75d, 0xc2b6_b572_0c79_b75d,
0x1c98a7d25c54659e, 0x1c98_a7d2_5c54_659e,
0x2a94e9a11036e51a, 0x2a94_e9a1_1036_e51a,
]), ]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([0x1000000000000, 0xec03000276030000, 0x8d51ccce760304d0, 0x0]), Fq::from_raw([
0x0001_0000_0000_0000,
0xec03_0002_7603_0000,
0x8d51_ccce_7603_04d0,
0x0,
]),
Fq::from_raw([0x0, 0x0, 0x0, 0x0]), Fq::from_raw([0x0, 0x0, 0x0, 0x0]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
Fq::from_raw([ Fq::from_raw([
0x26d19585d8dff2be, 0x26d1_9585_d8df_f2be,
0xd919893ec24fd67c, 0xd919_893e_c24f_d67c,
0x488ef781683bbf33, 0x488e_f781_683b_bf33,
0x218c81a6eff03d4, 0x0218_c81a_6eff_03d4,
]), ]),
Fq::from_raw([ Fq::from_raw([
0xff0d2068eff496dd, 0xff0d_2068_eff4_96dd,
0x9106ee90f384a4a1, 0x9106_ee90_f384_a4a1,
0x16a13035ad4d7266, 0x16a1_3035_ad4d_7266,
0x4958bdb21966982e, 0x4958_bdb2_1966_982e,
]), ]),
), ),
AffinePoint::from_raw_unchecked( AffinePoint::from_raw_unchecked(
@ -1192,36 +1197,36 @@ fn test_is_identity() {
#[test] #[test]
fn test_mul_consistency() { fn test_mul_consistency() {
let a = Fr([ let a = Fr([
0x21e61211d9934f2e, 0x21e6_1211_d993_4f2e,
0xa52c058a693c3e07, 0xa52c_058a_693c_3e07,
0x9ccb77bfb12d6360, 0x9ccb_77bf_b12d_6360,
0x07df2470ec94398e, 0x07df_2470_ec94_398e,
]); ]);
let b = Fr([ let b = Fr([
0x03336d1cbe19dbe0, 0x0333_6d1c_be19_dbe0,
0x0153618f6156a536, 0x0153_618f_6156_a536,
0x2604c9e1fc3c6b15, 0x2604_c9e1_fc3c_6b15,
0x04ae581ceb028720, 0x04ae_581c_eb02_8720,
]); ]);
let c = Fr([ let c = Fr([
0xd7abf5bb24683f4c, 0xd7ab_f5bb_2468_3f4c,
0x9d7712cc274b7c03, 0x9d77_12cc_274b_7c03,
0x973293db9683789f, 0x9732_93db_9683_789f,
0x0b677e29380a97a7, 0x0b67_7e29_380a_97a7,
]); ]);
assert_eq!(a * b, c); assert_eq!(a * b, c);
let p = ExtendedPoint::from(AffinePoint { let p = ExtendedPoint::from(AffinePoint {
u: Fq::from_raw([ u: Fq::from_raw([
0x81c571e5d883cfb0, 0x81c5_71e5_d883_cfb0,
0x049f7a686f147029, 0x049f_7a68_6f14_7029,
0xf539c860bc3ea21f, 0xf539_c860_bc3e_a21f,
0x4284715b7ccc8162, 0x4284_715b_7ccc_8162,
]), ]),
v: Fq::from_raw([ v: Fq::from_raw([
0xbf096275684bb8ca, 0xbf09_6275_684b_b8ca,
0xc7ba245890af256d, 0xc7ba_2458_90af_256d,
0x59119f3e86380eb0, 0x5911_9f3e_8638_0eb0,
0x3793de182f9fb1d2, 0x3793_de18_2f9f_b1d2,
]), ]),
}) })
.mul_by_cofactor(); .mul_by_cofactor();