diff --git a/crates/starknet-types-core/src/curve/mod.rs b/crates/starknet-types-core/src/curve/mod.rs index e58b3e3..7f315a8 100644 --- a/crates/starknet-types-core/src/curve/mod.rs +++ b/crates/starknet-types-core/src/curve/mod.rs @@ -2,73 +2,6 @@ mod affine_point; mod curve_errors; mod projective_point; -// use core::any::Any; - -// use lambdaworks_math::elliptic_curve::short_weierstrass::curves::stark_curve::StarkCurve; -// use lambdaworks_math::elliptic_curve::traits::IsEllipticCurve; - -// use crate::felt::Felt; -// use crate::felt::NonZeroFelt; - pub use self::affine_point::*; pub use self::curve_errors::*; pub use self::projective_point::*; - -// pub enum SignatureVerificationError { -// InvalidPublicKey, -// InvalidMessage, -// InvalidR, -// InvalidS, -// } -// const EC_ORDER: NonZeroFelt = unsafe { -// NonZeroFelt::from_raw_const([ -// 369010039416812937, -// 9, -// 1143265896874747514, -// 8939893405601011193, -// ]) -// }; - -// #[inline(always)] -// fn mul_by_bits(x: &AffinePoint, y: &Felt) -> AffinePoint { -// let x = ProjectivePoint::from_affine(x.x(), x.y()).unwrap(); -// let y: Vec = y.to_bits_le().into_iter().collect(); -// let z = &x * &y; -// z.to_affine() -// } -// pub fn verify_signature( -// public_key: &Felt, -// msg: &Felt, -// r: &Felt, -// s: &Felt, -// ) -> Result { -// if msg >= &Felt::ELEMENT_UPPER_BOUND { -// return Err(SignatureVerificationError::InvalidMessage); -// } -// if r == &Felt::ZERO || r >= &Felt::ELEMENT_UPPER_BOUND { -// return Err(SignatureVerificationError::InvalidR); -// } -// if s == &Felt::ZERO || s >= &Felt::ELEMENT_UPPER_BOUND { -// return Err(SignatureVerificationError::InvalidS); -// } - -// let full_public_key = match AffinePoint::from_x(*public_key) { -// Some(value) => value, -// None => return Err(SignatureVerificationError::InvalidPublicKey), -// }; - -// let w = s -// .mod_inverse(&EC_ORDER) -// .ok_or(SignatureVerificationError::InvalidS)?; -// if w == Felt::ZERO || w >= Felt::ELEMENT_UPPER_BOUND { -// return Err(SignatureVerificationError::InvalidS); -// } - -// let zw = msg.mul_mod(&w, &EC_ORDER); -// let zw_g = StarkCurve::generator().mul_by_bits(&zw); - -// let rw = r.mul_mod_floor(&w, &EC_ORDER); -// let rw_q = full_public_key.mul_by_bits(&rw); - -// Ok((&zw_g + &rw_q).x == *r || (&zw_g - &rw_q).x == *r) -// } diff --git a/crates/starknet-types-core/src/felt/mod.rs b/crates/starknet-types-core/src/felt/mod.rs index d82aa1e..a50a23a 100644 --- a/crates/starknet-types-core/src/felt/mod.rs +++ b/crates/starknet-types-core/src/felt/mod.rs @@ -53,21 +53,6 @@ pub struct Felt(pub(crate) FieldElement); pub struct NonZeroFelt(FieldElement); impl NonZeroFelt { - // /// Create a [NonZeroFelt] as a constant. If the value is zero will panic. - // pub const unsafe fn from_felt_const(felt: Felt) -> Self { - // let value = felt.0.representative().limbs; - // let mut i = 0; - // let mut zeros_nb = 0; - // while i < value.len() { - // if value[i] == 0 { - // zeros_nb += 1; - // } - // i += 1; - // } - // assert!(zeros_nb < value.len(), "Felt is zero"); - // Self(felt.0) - // } - /// Create a [NonZeroFelt] as a constant. /// # Safety /// If the value is zero will panic. @@ -89,7 +74,7 @@ impl NonZeroFelt { /// this can lead to undefined behaviour and big security issue. /// You should always use the [TryFrom] implementation #[cfg(feature = "unsafe-non-zero")] - pub fn from_felt_unchecked(value: Felt) -> Self { + pub const fn from_felt_unchecked(value: Felt) -> Self { Self(value.0) } } @@ -100,9 +85,6 @@ pub struct FeltIsZeroError; #[derive(Debug)] pub struct FromStrError; -#[derive(Debug)] -pub struct FromBytesError; - impl Felt { /// [Felt] constant that's equal to 0. pub const ZERO: Self = Self(FieldElement::::from_hex_unchecked("0")); @@ -120,6 +102,8 @@ impl Felt { pub const MAX: Self = Self(FieldElement::::const_from_raw( UnsignedInteger::from_limbs([544, 0, 0, 32]), )); + + /// 2 ** 251 pub const ELEMENT_UPPER_BOUND: Felt = Felt::from_raw_const([ 576459263475450960, 18446744073709255680, @@ -299,7 +283,6 @@ impl Felt { } /// Helper to produce a hexadecimal formatted string of 66 chars. - /// Equivalent to calling `format!("{self:#066x}")`. #[cfg(feature = "alloc")] pub fn to_fixed_hex_string(&self) -> alloc::string::String { let hex_str = alloc::format!("{self:#x}"); @@ -447,6 +430,8 @@ impl Felt { } } + /// Returns the internal representation of a felt and reverses it to match + /// starknet-rs mont representation pub fn to_raw_reversed(&self) -> [u64; 4] { let mut res = self.0.to_raw().limbs; res.reverse(); @@ -1089,15 +1074,6 @@ mod errors { "Failed to create Felt from string".fmt(f) } } - - #[cfg(feature = "std")] - impl std::error::Error for FromBytesError {} - - impl fmt::Display for FromBytesError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - "Failed to create Felt from bytes".fmt(f) - } - } } #[cfg(test)]