Skip to content

Commit 76bb949

Browse files
Merge branch 'main' into add_learning_materials
2 parents ae30a43 + 712d894 commit 76bb949

File tree

8 files changed

+74
-21
lines changed

8 files changed

+74
-21
lines changed

benches/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ ark-test-curves = { git = "https://github.com/arkworks-rs/algebra", rev = "ef8f7
99
ark-std = "0.4.0"
1010
rand = "0.8.5"
1111
rand_chacha = "0.3.1"
12-
starknet-curve = { git = "https://github.com/xJonathanLEI/starknet-rs" }
13-
starknet-ff = { git = "https://github.com/xJonathanLEI/starknet-rs" }
14-
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs" }
12+
starknet-curve = { git = "https://github.com/xJonathanLEI/starknet-rs", tag = "starknet-curve/v0.4.2" }
13+
starknet-ff = { git = "https://github.com/xJonathanLEI/starknet-rs", tag = "starknet-ff/v0.3.7" }
14+
starknet-crypto = { git = "https://github.com/xJonathanLEI/starknet-rs", tag = "starknet-crypto/v0.6.2" }
1515
pathfinder-crypto = { git = "https://github.com/eqlabs/pathfinder.git" }
1616

1717
[dependencies.lambdaworks-math]

benches/benches/poseidon.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ fn poseidon_benchmarks(c: &mut Criterion) {
3131
mont_x.reverse();
3232
mont_y.reverse();
3333

34-
let sn_ff_x = starknet_ff::FieldElement::from_mont(mont_x);
35-
let sn_ff_y = starknet_ff::FieldElement::from_mont(mont_y);
34+
let sn_ff_x = starknet_crypto::FieldElement::from_mont(mont_x);
35+
let sn_ff_y = starknet_crypto::FieldElement::from_mont(mont_y);
3636
group.bench_function("starknet-rs", |bench| {
3737
bench.iter(|| black_box(starknet_crypto::poseidon_hash(sn_ff_x, sn_ff_y)))
3838
});

crypto/src/merkle_tree/merkle.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ where
3333
pub fn build(unhashed_leaves: &[B::Data]) -> Self {
3434
let mut hashed_leaves: Vec<B::Node> = B::hash_leaves(unhashed_leaves);
3535

36-
// If there is only one node, handle it specially
37-
if hashed_leaves.len() == 1 {
38-
hashed_leaves.push(hashed_leaves[0].clone());
39-
}
40-
4136
//The leaf must be a power of 2 set
4237
hashed_leaves = complete_until_power_of_two(&mut hashed_leaves);
4338
let leaves_len = hashed_leaves.len();

crypto/src/merkle_tree/utils.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ pub fn parent_index(node_index: usize) -> usize {
2222

2323
// The list of values is completed repeating the last value to a power of two length
2424
pub fn complete_until_power_of_two<T: Clone>(values: &mut Vec<T>) -> Vec<T> {
25-
if values.len() == 1 {
26-
return values.clone(); // Return immediately if there is only one element.
27-
}
2825
while !is_power_of_two(values.len()) {
2926
values.push(values[values.len() - 1].clone());
3027
}
3128
values.to_vec()
3229
}
3330

34-
pub fn is_power_of_two(x: usize) -> bool {
35-
(x != 0) && ((x & (x - 1)) == 0)
31+
// ! NOTE !
32+
// We in this function we say 2^0 = 1 is not a power of two when it is infact true. We
33+
// need this to maintain that the smallest tree at two leaves counts and we could not find
34+
// a name that wasn't overly verbose. In the case of a single value being present in a leaf node
35+
// this indicates we pad to next power of two (i.e. 2). The function is private and is only used
36+
// to ensure the tree has a power of 2 number of leaves.
37+
fn is_power_of_two(x: usize) -> bool {
38+
(x > 1) && ((x & (x - 1)) == 0)
3639
}
3740

3841
// ! CAUTION !
@@ -109,6 +112,19 @@ mod tests {
109112
}
110113
}
111114

115+
#[test]
116+
// expected |2|2|
117+
fn complete_the_length_of_one_field_element_to_be_a_power_of_two() {
118+
let mut values: Vec<FE> = vec![FE::new(2)];
119+
let hashed_leaves = complete_until_power_of_two(&mut values);
120+
121+
let mut expected_leaves = vec![FE::new(2)];
122+
expected_leaves.extend([FE::new(2)]);
123+
assert_eq!(hashed_leaves.len(), 2);
124+
assert_eq!(hashed_leaves[0], expected_leaves[0]);
125+
assert_eq!(hashed_leaves[1], expected_leaves[1]);
126+
}
127+
112128
const ROOT: usize = 0;
113129

114130
#[test]

math/src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum ByteConversionError {
1111
pub enum CreationError {
1212
InvalidHexString,
1313
InvalidDecString,
14+
HexStringIsTooBig,
1415
EmptyString,
1516
}
1617

math/src/field/element.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,14 @@ impl<F: IsPrimeField> FieldElement<F> {
509509
/// Creates a `FieldElement` from a hexstring. It can contain `0x` or not.
510510
/// Returns an `CreationError::InvalidHexString`if the value is not a hexstring.
511511
/// Returns a `CreationError::EmptyString` if the input string is empty.
512+
/// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger
513+
/// than the maximum amount of characters for this element.
512514
pub fn from_hex(hex_string: &str) -> Result<Self, CreationError> {
513515
if hex_string.is_empty() {
514-
return Err(CreationError::EmptyString)?;
516+
return Err(CreationError::EmptyString);
515517
}
516-
517-
Ok(Self {
518-
value: F::from_hex(hex_string)?,
519-
})
518+
let value = F::from_hex(hex_string)?;
519+
Ok(Self { value })
520520
}
521521

522522
#[cfg(feature = "std")]

math/src/field/fields/montgomery_backed_prime_fields.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,22 @@ mod tests_u256_prime_fields {
11491149
assert_eq!(a, b);
11501150
}
11511151

1152+
#[test]
1153+
fn creating_a_field_element_from_hex_too_big_errors() {
1154+
let a = U256FP1Element::from_hex(&"f".repeat(65));
1155+
assert!(a.is_err());
1156+
assert_eq!(
1157+
a.unwrap_err(),
1158+
crate::errors::CreationError::HexStringIsTooBig
1159+
)
1160+
}
1161+
1162+
#[test]
1163+
fn creating_a_field_element_from_hex_works_on_the_size_limit() {
1164+
let a = U256FP1Element::from_hex(&"f".repeat(64));
1165+
assert!(a.is_ok());
1166+
}
1167+
11521168
#[test]
11531169
fn creating_a_field_element_from_hex_works_2() {
11541170
let a = U256F29Element::from_hex_unchecked("aa");

math/src/unsigned_integer/element.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
425425
/// Creates an `UnsignedInteger` from a hexstring. It can contain `0x` or not.
426426
/// Returns an `CreationError::InvalidHexString`if the value is not a hexstring.
427427
/// Returns a `CreationError::EmptyString` if the input string is empty.
428+
/// Returns a `CreationError::HexStringIsTooBig` if the the input hex string is bigger
429+
/// than the maximum amount of characters for this element.
428430
pub fn from_hex(value: &str) -> Result<Self, CreationError> {
429431
let mut string = value;
430432
let mut char_iterator = value.chars();
@@ -440,6 +442,14 @@ impl<const NUM_LIMBS: usize> UnsignedInteger<NUM_LIMBS> {
440442
if !Self::is_hex_string(string) {
441443
return Err(CreationError::InvalidHexString);
442444
}
445+
446+
// Limbs are of 64 bits - 8 bytes
447+
// We have 16 nibbles per bytes
448+
let max_amount_of_hex_chars = NUM_LIMBS * 16;
449+
if string.len() > max_amount_of_hex_chars {
450+
return Err(CreationError::HexStringIsTooBig);
451+
}
452+
443453
Ok(Self::from_hex_unchecked(string))
444454
}
445455

@@ -1001,7 +1011,7 @@ impl<const NUM_LIMBS: usize> Arbitrary for UnsignedInteger<NUM_LIMBS> {
10011011
#[cfg(test)]
10021012
mod tests_u384 {
10031013
use crate::traits::ByteConversion;
1004-
use crate::unsigned_integer::element::{UnsignedInteger, U384};
1014+
use crate::unsigned_integer::element::{UnsignedInteger, U256, U384};
10051015
#[cfg(feature = "proptest")]
10061016
use proptest::prelude::*;
10071017
#[cfg(feature = "proptest")]
@@ -1246,6 +1256,21 @@ mod tests_u384 {
12461256
);
12471257
}
12481258

1259+
#[test]
1260+
fn from_hex_with_overflowing_hexstring_should_error() {
1261+
let u256_from_big_string = U256::from_hex(&"f".repeat(65));
1262+
assert!(u256_from_big_string.is_err());
1263+
assert!(
1264+
u256_from_big_string
1265+
== Err(crate::unsigned_integer::element::CreationError::HexStringIsTooBig)
1266+
);
1267+
}
1268+
1269+
#[test]
1270+
fn from_hex_with_non_overflowing_hexstring_should_work() {
1271+
assert_eq!(U256::from_hex(&"0".repeat(64)).unwrap().limbs, [0, 0, 0, 0])
1272+
}
1273+
12491274
#[test]
12501275
fn construct_new_integer_from_dec_1() {
12511276
let a = U384::from_dec_str("1").unwrap();

0 commit comments

Comments
 (0)