diff --git a/Cargo.lock b/Cargo.lock index 58ea1ce9..cb7878d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1659,6 +1659,7 @@ dependencies = [ name = "starknet-curve" version = "0.4.1" dependencies = [ + "bitvec", "starknet-types-core", ] diff --git a/starknet-crypto/src/ecdsa.rs b/starknet-crypto/src/ecdsa.rs index 309627eb..d513a747 100644 --- a/starknet-crypto/src/ecdsa.rs +++ b/starknet-crypto/src/ecdsa.rs @@ -109,9 +109,11 @@ pub fn sign(private_key: &Felt, message: &Felt, k: &Felt) -> Result Result Felt { - let mut result = lhs.to_raw(); - - for (result_i, &b_i) in result.iter_mut().zip(rhs.to_raw().iter()) { - *result_i &= b_i; - } - - Felt::from_raw(result) -} - /// Recovers the public key from a message and (r, s, v) signature parameters /// /// ### Arguments @@ -188,7 +178,7 @@ pub fn recover(message: &Felt, r: &Felt, s: &Felt, v: &Felt) -> Result::into(full_r.y.to_bigint() & Felt::ONE.to_bigint()) != *v { full_r.y = -full_r.y; } let full_rs = mul_by_bits(&full_r, s); @@ -206,11 +196,8 @@ pub fn recover(message: &Felt, r: &Felt, s: &Felt, v: &Felt) -> Result AffinePoint { let x = ProjectivePoint::from(x); - let mut y_bool = [false; 256_usize]; - for (bool_ref, bit) in y_bool.iter_mut().zip(y.to_bits_le().iter().by_vals()) { - *bool_ref = bit; - } - let z = &x * &y_bool; + let y = y.to_bits_be(); + let z = &x * &y; AffinePoint::from(&z) } diff --git a/starknet-crypto/src/pedersen_hash.rs b/starknet-crypto/src/pedersen_hash.rs index 2580cdf0..06c74a5c 100644 --- a/starknet-crypto/src/pedersen_hash.rs +++ b/starknet-crypto/src/pedersen_hash.rs @@ -13,8 +13,8 @@ const SHIFT_POINT: ProjectivePoint = ProjectivePoint::from_affine_point(&curve_p /// * `x`: The x coordinate /// * `y`: The y coordinate pub fn pedersen_hash(x: &Felt, y: &Felt) -> Felt { - let x = x.to_bits_le(); - let y = y.to_bits_le(); + let x = x.to_bits_be(); + let y = y.to_bits_be(); // Preprocessed material is lookup-tables for each chunk of bits let table_size = (1 << CURVE_CONSTS_BITS) - 1; @@ -22,11 +22,7 @@ pub fn pedersen_hash(x: &Felt, y: &Felt) -> Felt { bits.chunks_exact(CURVE_CONSTS_BITS) .enumerate() .for_each(|(i, v)| { - let mut bools_array = [false; CURVE_CONSTS_BITS]; - for (bool_ref, bit) in bools_array.iter_mut().zip(v.iter().by_vals()) { - *bool_ref = bit; - } - let offset = bools_to_usize_le(&bools_array); + let offset = bitslice_to_usize_le(v); if offset > 0 { // Table lookup at 'offset-1' in table for chunk 'i' *acc += &prep[i * table_size + offset - 1]; @@ -49,11 +45,11 @@ pub fn pedersen_hash(x: &Felt, y: &Felt) -> Felt { } #[inline] -fn bools_to_usize_le(bools: &[bool]) -> usize { +fn bitslice_to_usize_le(bits: &BitSlice) -> usize { let mut result: usize = 0; - for (ind, bit) in bools.iter().enumerate() { + for (ind, bit) in bits.iter().enumerate() { if *bit { - result += 1 << ind; + result |= 1 << ind; } } result diff --git a/starknet-curve/Cargo.toml b/starknet-curve/Cargo.toml index b193b321..edb3d8bb 100644 --- a/starknet-curve/Cargo.toml +++ b/starknet-curve/Cargo.toml @@ -14,3 +14,5 @@ keywords = ["ethereum", "starknet", "web3", "no_std"] [dependencies] starknet-types-core = "0.0.9" +bitvec = { version = "1.0.1", default-features = false } + diff --git a/starknet-curve/src/ec_point.rs b/starknet-curve/src/ec_point.rs index a8260440..183fb2ca 100644 --- a/starknet-curve/src/ec_point.rs +++ b/starknet-curve/src/ec_point.rs @@ -1,6 +1,7 @@ use starknet_types_core::felt::Felt; use crate::curve_params::{ALPHA, BETA}; +use bitvec::array::BitArray; use core::ops; @@ -279,11 +280,28 @@ impl ops::AddAssign<&ProjectivePoint> for ProjectivePoint { } } -impl ops::Mul<&[bool]> for &ProjectivePoint { +// impl ops::Mul<&[bool]> for &ProjectivePoint { +// type Output = ProjectivePoint; + +// #[allow(clippy::suspicious_arithmetic_impl)] +// fn mul(self, rhs: &[bool]) -> Self::Output { +// let mut product = ProjectivePoint::identity(); +// for b in rhs.iter().rev() { +// product.double_assign(); +// if *b { +// product += self; +// } +// } + +// product +// } +// } + +impl ops::Mul<&BitArray<[u64; 4]>> for &ProjectivePoint { type Output = ProjectivePoint; #[allow(clippy::suspicious_arithmetic_impl)] - fn mul(self, rhs: &[bool]) -> Self::Output { + fn mul(self, rhs: &BitArray<[u64; 4]>) -> Self::Output { let mut product = ProjectivePoint::identity(); for b in rhs.iter().rev() { product.double_assign();