Skip to content

Commit

Permalink
clean a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucqs committed Feb 7, 2024
1 parent 30e9dc0 commit 8aaa963
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 96 deletions.
67 changes: 0 additions & 67 deletions crates/starknet-types-core/src/curve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> = 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<bool, SignatureVerificationError> {
// 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)
// }
34 changes: 5 additions & 29 deletions crates/starknet-types-core/src/felt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,6 @@ pub struct Felt(pub(crate) FieldElement<Stark252PrimeField>);
pub struct NonZeroFelt(FieldElement<Stark252PrimeField>);

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.
Expand All @@ -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)
}
}
Expand All @@ -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::<Stark252PrimeField>::from_hex_unchecked("0"));
Expand All @@ -120,6 +102,8 @@ impl Felt {
pub const MAX: Self = Self(FieldElement::<Stark252PrimeField>::const_from_raw(
UnsignedInteger::from_limbs([544, 0, 0, 32]),
));

/// 2 ** 251
pub const ELEMENT_UPPER_BOUND: Felt = Felt::from_raw_const([
576459263475450960,
18446744073709255680,
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 8aaa963

Please sign in to comment.