Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrliu committed Mar 30, 2023
1 parent 09b5f71 commit f646116
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const TWO_INV: Fq = Fq::from_raw([
0x183227397098d014,
]);

// TODO: Can we simply put 0 here::
const ROOT_OF_UNITY: Fq = Fq::zero();

// Unused constant for base field
Expand Down
3 changes: 1 addition & 2 deletions src/derive/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ macro_rules! new_curve_impl {
unimplemented!();
}

// TODO: Fix now that we have (a)?
fn is_on_curve(&self) -> Choice {
// Check (Y/Z)^2 = (X/Z)^3 + a(X/Z) + b
// <=> Z Y^2 - X^3 - a(X Z^2) = Z^3 b
Expand Down Expand Up @@ -1022,7 +1021,7 @@ macro_rules! new_curve_impl {
let x3 = x3 - t0;
let t0 = t3 * t1;
let z3 = t5 * z3;
let z3 = z3 + t0;
let z3 = z3 + t0;

$name {
x: x3,
Expand Down
15 changes: 12 additions & 3 deletions src/secp256r1/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,18 @@ const SECP_GENERATOR_Y: Fp = Fp::from_raw([
0x4FE342E2FE1A7F9B,
]);

const SECP_A: Fp = Fp::from_raw([0xFFFFFFFFFFFFFFFC, 0x00000000FFFFFFFF, 0x0000000000000000, 0xFFFFFFFF00000001]);
const SECP_B: Fp = Fp::from_raw([0x3BCE3C3E27D2604B, 0x651D06B0CC53B0F6, 0xB3EBBD55769886BC, 0x5AC635D8AA3A93E7]);
const SECP_A: Fp = Fp::from_raw([
0xFFFFFFFFFFFFFFFC,
0x00000000FFFFFFFF,
0x0000000000000000,
0xFFFFFFFF00000001,
]);
const SECP_B: Fp = Fp::from_raw([
0x3BCE3C3E27D2604B,
0x651D06B0CC53B0F6,
0xB3EBBD55769886BC,
0x5AC635D8AA3A93E7,
]);

use crate::{
batch_add, impl_add_binop_specify_output, impl_binops_additive,
Expand Down Expand Up @@ -75,7 +85,6 @@ impl CurveAffineExt for Secp256r1Affine {
}
}

// TODO: uncomment this
#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256r1>();
Expand Down
34 changes: 20 additions & 14 deletions src/secp256r1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,30 @@ const INV: u64 = 0x1;

/// R = 2^256 mod p
/// 0xfffffffeffffffffffffffffffffffff000000000000000000000001
const R : Fp = Fp([0x0000000000000001, 0xffffffff00000000, 0xffffffffffffffff, 0xfffffffe]);
const R: Fp = Fp([
0x0000000000000001,
0xffffffff00000000,
0xffffffffffffffff,
0xfffffffe,
]);

/// R^2 = 2^512 mod p
/// 0x4fffffffdfffffffffffffffefffffffbffffffff0000000000000003
const R2: Fp = Fp([0x0000000000000003, 0xfffffffbffffffff, 0xfffffffffffffffe, 0x4fffffffd]);
const R2: Fp = Fp([
0x0000000000000003,
0xfffffffbffffffff,
0xfffffffffffffffe,
0x4fffffffd,
]);

/// R^3 = 2^768 mod p
/// 0x180000000100000005fffffffcffffffedfffffff7fffffffd0000000a
const R3: Fp = Fp([0xfffffffd0000000a, 0xffffffedfffffff7, 0x00000005fffffffc, 0x1800000001]);
const R3: Fp = Fp([
0xfffffffd0000000a,
0xffffffedfffffff7,
0x00000005fffffffc,
0x1800000001,
]);

/// 1 / 2 mod p
/// 0x7fffffff80000000800000000000000000000000800000000000000000000000
Expand All @@ -74,11 +89,6 @@ const TWO_INV: Fp = Fp::from_raw([
0x7fffffff80000000,
]);

// TODO
/// 4d6ea8928adb86cf
/// 62388a8e0ef62331
/// 2e68c59bdef3e53f
/// d964598eb819acce
const ZETA: Fp = Fp::from_raw([
0xd964598eb819acce,
0x2e68c59bdef3e53f,
Expand All @@ -91,26 +101,22 @@ const ZETA: Fp = Fp::from_raw([
/// `0x0000000000000000000000000000000000000000000000000000000000000024`.
const DELTA: Fp = Fp::from_raw([0x24, 0, 0, 0]);

// TODO
/// Implementations of this trait MUST ensure that this is the generator used to derive Self::ROOT_OF_UNITY.
/// Derived from:
/// ```ignore
/// Zp(Zp(mul_generator)^t) where t = (modulus - 1 )/ 2
/// 115792089237316195423570985008687907853269984665640564039457584007908834671662
/// ```
/// 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffe
///
/// `0xffffffff00000001000000000000000000000000fffffffffffffffffffffffe`
const ROOT_OF_UNITY: Fp = Fp::from_raw([
0xfffffffffffffffe,
0x00000000ffffffff,
0x0000000000000000,
0xffffffff00000001,
]);

// TODO
/// Inverse of [`ROOT_OF_UNITY`].
/// 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffe
/// 0x
/// `0xffffffff00000001000000000000000000000000fffffffffffffffffffffffe`
const ROOT_OF_UNITY_INV: Fp = Fp::from_raw([
0xfffffffffffffffe,
0x00000000ffffffff,
Expand Down
38 changes: 10 additions & 28 deletions src/secp256r1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ const INV: u64 = 0xccd1c8aaee00bc4f;

/// R = 2^256 mod q
/// 0xffffffff00000000000000004319055258e8617b0c46353d039cdaaf
const R: Fq = Fq([0x0c46353d039cdaaf, 0x4319055258e8617b, 0x0000000000000000, 0xffffffff]);
const R: Fq = Fq([
0x0c46353d039cdaaf,
0x4319055258e8617b,
0x0000000000000000,
0xffffffff,
]);

/// R^2 = 2^512 mod q
/// 0x66e12d94f3d956202845b2392b6bec594699799c49bd6fa683244c95be79eea2
Expand All @@ -76,34 +81,17 @@ const R3: Fq = Fq([
/// It's derived with SageMath with: `GF(MODULUS).primitive_element()`.
const GENERATOR: Fq = Fq::from_raw([0x07, 0x00, 0x00, 0x00]);

// TODO
/// GENERATOR^t where t * 2^s + 1 = r with t odd. In other words, this is a 2^s root of unity.
/// `0xc1dc060e7a91986df9879a3fbc483a898bdeab680756045992f4b5402b052f2`
/// `0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632550`
///
/// ffffffff00000000
/// ffffffffffffffff
/// bce6faada7179e84
/// f3b9cac2fc632550
///
/// ffc97f062a770992
/// ba807ace842a3dfc
/// 1546cad004378daf
/// 0592d7fbb41e6602
/// `ffc97f062a770992ba807ace842a3dfc1546cad004378daf0592d7fbb41e6602`
const ROOT_OF_UNITY: Fq = Fq::from_raw([
0x0592d7fbb41e6602,
0x1546cad004378daf,
0xba807ace842a3dfc,
0xffc97f062a770992,
]);

// TODO
/// 1 / ROOT_OF_UNITY mod q
///
/// a0a66a5562d46f2a
/// c645fa0458131cae
/// e3ac117c794c4137
/// 379c7f0657c73764
/// `a0a66a5562d46f2ac645fa0458131caee3ac117c794c4137379c7f0657c73764`
const ROOT_OF_UNITY_INV: Fq = Fq::from_raw([
0x379c7f0657c73764,
0xe3ac117c794c4137,
Expand All @@ -119,11 +107,6 @@ const TWO_INV: Fq = Fq::from_raw([
0x7fffffff80000000,
]);

/// TODO: Fix
/// 52891d43d946a035
/// 4e786d0777fd6aef
/// 9405335ce9c83e1d
/// 7cbf87ff12884e21
const ZETA: Fq = Fq::from_raw([
0x7cbf87ff12884e21,
0x9405335ce9c83e1d,
Expand All @@ -133,9 +116,8 @@ const ZETA: Fq = Fq::from_raw([

/// Generator of the t-order multiplicative subgroup.
/// Computed by exponentiating Self::MULTIPLICATIVE_GENERATOR by 2^s, where s is Self::S.
/// `0x0000000000000000000cbc21fe4561c8d63b78e780e1341e199417c8c0bb7601`
/// 0x1e39a5057d81
const DELTA: Fq = Fq::from_raw([ 0x1e39a5057d81, 0, 0, 0 ]);
/// `0x1e39a5057d81`
const DELTA: Fq = Fq::from_raw([0x1e39a5057d81, 0, 0, 0]);

use crate::{
field_arithmetic, field_common, field_specific, impl_add_binop_specify_output,
Expand Down

0 comments on commit f646116

Please sign in to comment.