From: Matt Corallo Date: Tue, 7 May 2024 19:38:17 +0000 (+0000) Subject: Rename `IntModP`/`IntModN` to `CurveField`/`ScalarField` X-Git-Tag: v0.5.4~6 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=f3e1fae9c268b7c9490d4cf80d362a99e5c6b7d7;p=dnssec-prover Rename `IntModP`/`IntModN` to `CurveField`/`ScalarField` which improves readability greatly --- diff --git a/src/crypto/ec.rs b/src/crypto/ec.rs index 7562deb..0908e33 100644 --- a/src/crypto/ec.rs +++ b/src/crypto/ec.rs @@ -63,29 +63,29 @@ impl + Clone + Eq> IntMod for U384Mod { pub(super) trait Curve : Copy { type Int: Int; - // With const generics, both IntModP and IntModN can be replaced with a single IntMod. - type IntModP: IntMod; - type IntModN: IntMod; + // With const generics, both CurveField and ScalarField can be replaced with a single IntMod. + type CurveField: IntMod; + type ScalarField: IntMod; - type P: PrimeModulus; - type N: PrimeModulus; + type CurveModulus: PrimeModulus; + type ScalarModulus: PrimeModulus; // Curve parameters y^2 = x^3 + ax + b - const A: Self::IntModP; - const B: Self::IntModP; + const A: Self::CurveField; + const B: Self::CurveField; const G: Point; } #[derive(Clone, PartialEq, Eq)] pub(super) struct Point { - x: C::IntModP, - y: C::IntModP, - z: C::IntModP, + x: C::CurveField, + y: C::CurveField, + z: C::CurveField, } impl Point { - fn on_curve(x: &C::IntModP, y: &C::IntModP) -> Result<(), ()> { + fn on_curve(x: &C::CurveField, y: &C::CurveField) -> Result<(), ()> { let x_2 = x.square(); let x_3 = x_2.mul(&x); let v = x_3.add(&C::A.mul(&x)).add(&C::B); @@ -99,8 +99,8 @@ impl Point { } #[cfg(debug_assertions)] - fn on_curve_z(x: &C::IntModP, y: &C::IntModP, z: &C::IntModP) -> Result<(), ()> { - let m = C::IntModP::from_modinv_of(z.clone().into_i())?; + fn on_curve_z(x: &C::CurveField, y: &C::CurveField, z: &C::CurveField) -> Result<(), ()> { + let m = C::CurveField::from_modinv_of(z.clone().into_i())?; let m_2 = m.square(); let m_3 = m_2.mul(&m); let x_norm = x.mul(&m_2); @@ -109,25 +109,25 @@ impl Point { } #[cfg(test)] - fn normalize_x(&self) -> Result { - let m = C::IntModP::from_modinv_of(self.z.clone().into_i())?; + fn normalize_x(&self) -> Result { + let m = C::CurveField::from_modinv_of(self.z.clone().into_i())?; Ok(self.x.mul(&m.square())) } fn from_xy(x: C::Int, y: C::Int) -> Result { - let x = C::IntModP::from_i(x); - let y = C::IntModP::from_i(y); + let x = C::CurveField::from_i(x); + let y = C::CurveField::from_i(y); Self::on_curve(&x, &y)?; - Ok(Point { x, y, z: C::IntModP::ONE }) + Ok(Point { x, y, z: C::CurveField::ONE }) } - pub(super) const fn from_xy_assuming_on_curve(x: C::IntModP, y: C::IntModP) -> Self { - Point { x, y, z: C::IntModP::ONE } + pub(super) const fn from_xy_assuming_on_curve(x: C::CurveField, y: C::CurveField) -> Self { + Point { x, y, z: C::CurveField::ONE } } /// Checks that `expected_x` is equal to our X affine coordinate (without modular inversion). - fn eq_x(&self, expected_x: &C::IntModN) -> Result<(), ()> { - debug_assert!(expected_x.clone().into_i() < C::P::PRIME, "N is < P"); + fn eq_x(&self, expected_x: &C::ScalarField) -> Result<(), ()> { + debug_assert!(expected_x.clone().into_i() < C::CurveModulus::PRIME, "N is < P"); // If x is between N and P the below calculations will fail and we'll spuriously reject a // signature and the wycheproof tests will fail. We should in theory accept such @@ -136,18 +136,18 @@ impl Point { #[allow(unused_mut, unused_assignments)] let mut slow_check = None; #[cfg(test)] { - slow_check = Some(C::IntModN::from_i(self.normalize_x()?.into_i()) == *expected_x); + slow_check = Some(C::ScalarField::from_i(self.normalize_x()?.into_i()) == *expected_x); } - let e: C::IntModP = C::IntModP::from_i(expected_x.clone().into_i()); - if self.z == C::IntModP::ZERO { return Err(()); } + let e: C::CurveField = C::CurveField::from_i(expected_x.clone().into_i()); + if self.z == C::CurveField::ZERO { return Err(()); } let ezz = e.mul(&self.z).mul(&self.z); if self.x == ezz || slow_check == Some(true) { Ok(()) } else { Err(()) } } fn double(&self) -> Result { - if self.y == C::IntModP::ZERO { return Err(()); } - if self.z == C::IntModP::ZERO { return Err(()); } + if self.y == C::CurveField::ZERO { return Err(()); } + if self.z == C::CurveField::ZERO { return Err(()); } let s = self.x.times_four().mul(&self.y.square()); let z_2 = self.z.square(); @@ -191,7 +191,7 @@ impl Point { /// Calculates i * I + j * J #[allow(non_snake_case)] -fn add_two_mul(i: C::IntModN, I: &Point, j: C::IntModN, J: &Point) -> Result, ()> { +fn add_two_mul(i: C::ScalarField, I: &Point, j: C::ScalarField, J: &Point) -> Result, ()> { let i = i.into_i(); let j = j.into_i(); @@ -273,14 +273,14 @@ pub(super) fn validate_ecdsa(pk: &[u8], sig: &[u8], hash_input: &[u8]) // perfectly safe to do so, the wycheproof tests expect such signatures to be rejected, so we // do so here. let r_u256 = C::Int::from_be_bytes(r_bytes)?; - if r_u256 > C::N::PRIME { return Err(()); } + if r_u256 > C::ScalarModulus::PRIME { return Err(()); } let s_u256 = C::Int::from_be_bytes(s_bytes)?; - if s_u256 > C::N::PRIME { return Err(()); } + if s_u256 > C::ScalarModulus::PRIME { return Err(()); } - let r = C::IntModN::from_i(r_u256); - let s_inv = C::IntModN::from_modinv_of(s_u256)?; + let r = C::ScalarField::from_i(r_u256); + let s_inv = C::ScalarField::from_modinv_of(s_u256)?; - let z = C::IntModN::from_i(C::Int::from_be_bytes(hash_input)?); + let z = C::ScalarField::from_i(C::Int::from_be_bytes(hash_input)?); let u_a = z.mul(&s_inv); let u_b = r.mul(&s_inv); diff --git a/src/crypto/secp256r1.rs b/src/crypto/secp256r1.rs index 41d4851..88db883 100644 --- a/src/crypto/secp256r1.rs +++ b/src/crypto/secp256r1.rs @@ -29,11 +29,11 @@ struct P256(); impl ec::Curve for P256 { type Int = U256; - type IntModP = U256Mod

; - type IntModN = U256Mod; + type CurveField = U256Mod

; + type ScalarField = U256Mod; - type P = P; - type N = N; + type CurveModulus = P; + type ScalarModulus = N; const A: U256Mod

= U256Mod::from_u256_panicking(U256::from_32_be_bytes_panicking(&hex_lit::hex!( "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc"))); diff --git a/src/crypto/secp384r1.rs b/src/crypto/secp384r1.rs index 9c99ad3..b7bc159 100644 --- a/src/crypto/secp384r1.rs +++ b/src/crypto/secp384r1.rs @@ -29,11 +29,11 @@ struct P384(); impl ec::Curve for P384 { type Int = U384; - type IntModP = U384Mod

; - type IntModN = U384Mod; + type CurveField = U384Mod

; + type ScalarField = U384Mod; - type P = P; - type N = N; + type CurveModulus = P; + type ScalarModulus = N; const A: U384Mod

= U384Mod::from_u384_panicking(U384::from_48_be_bytes_panicking(&hex_lit::hex!( "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeffffffff0000000000000000fffffffc")));