Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Mar 8, 2024
1 parent a628baf commit 56ca893
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 8 additions & 21 deletions starknet-crypto/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ pub fn sign(private_key: &Felt, message: &Felt, k: &Felt) -> Result<ExtendedSign
return Err(SignError::InvalidK);
}

let v = felt_bit_and(&full_r.y, &Felt::ONE);

Ok(ExtendedSignature { r, s, v })
Ok(ExtendedSignature {
r,
s,
v: (full_r.y.to_bigint() & Felt::ONE.to_bigint()).into(),
})
}

/// Verifies if a signature is valid over a message hash given a public key. Returns an error
Expand Down Expand Up @@ -153,18 +155,6 @@ pub fn verify(public_key: &Felt, message: &Felt, r: &Felt, s: &Felt) -> Result<b
Ok((&zw_g + &rw_q).x == *r || (&zw_g - &rw_q).x == *r)
}

// Temporary function
// TODO: remove it once `BitAnd` is implemented for `Felt`
fn felt_bit_and(lhs: &Felt, rhs: &Felt) -> 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
Expand All @@ -188,7 +178,7 @@ pub fn recover(message: &Felt, r: &Felt, s: &Felt, v: &Felt) -> Result<Felt, Rec
}

let mut full_r = AffinePoint::from_x(*r).ok_or(RecoverError::InvalidR)?;
if felt_bit_and(&full_r.y, &Felt::ONE) != *v {
if Into::<Felt>::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);
Expand All @@ -206,11 +196,8 @@ pub fn recover(message: &Felt, r: &Felt, s: &Felt, v: &Felt) -> Result<Felt, Rec
#[inline(always)]
fn mul_by_bits(x: &AffinePoint, y: &Felt) -> 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)
}

Expand Down
16 changes: 6 additions & 10 deletions starknet-crypto/src/pedersen_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ 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;
let add_points = |acc: &mut ProjectivePoint, bits: &BitSlice<u64>, prep: &[AffinePoint]| {
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];
Expand All @@ -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<u64>) -> 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
Expand Down
2 changes: 2 additions & 0 deletions starknet-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

22 changes: 20 additions & 2 deletions starknet-curve/src/ec_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use starknet_types_core::felt::Felt;

use crate::curve_params::{ALPHA, BETA};
use bitvec::array::BitArray;

use core::ops;

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 56ca893

Please sign in to comment.