From 0b33db0bb90591121a395a9943d6a23bdd683a11 Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Wed, 25 Oct 2023 15:45:56 +0200 Subject: [PATCH 1/6] adds bit rotation to math --- src/math/src/lib.cairo | 97 ++++++++++++++++++++++++++++++ src/math/src/tests/math_test.cairo | 48 ++++++++++++++- 2 files changed, 144 insertions(+), 1 deletion(-) diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index ff778d81..b08edd8f 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -111,6 +111,103 @@ impl U256BitShift of BitShift { } } +trait BitRotate { + fn rotl(x: T, n: T) -> T; + fn rotr(x: T, n: T) -> T; +} + +impl U8BitRotate of BitRotate { + fn rotl(x: u8, n: u8) -> u8 { + let word = u8_wide_mul(x, pow(2, n)); + let (quotient, remainder) = DivRem::div_rem(word, 0x100_u16.try_into().unwrap()); + (quotient + remainder).try_into().unwrap() + } + + fn rotr(x: u8, n: u8) -> u8 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 8 - n) + quotient + } +} + +impl U16BitRotate of BitRotate { + fn rotl(x: u16, n: u16) -> u16 { + let word = u16_wide_mul(x, pow(2, n)); + let (quotient, remainder) = DivRem::div_rem(word, 0x10000_u32.try_into().unwrap()); + (quotient + remainder).try_into().unwrap() + } + + fn rotr(x: u16, n: u16) -> u16 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 16 - n) + quotient + } +} + +impl U32BitRotate of BitRotate { + fn rotl(x: u32, n: u32) -> u32 { + let word = u32_wide_mul(x, pow(2, n)); + let (quotient, remainder) = DivRem::div_rem(word, 0x100000000_u64.try_into().unwrap()); + (quotient + remainder).try_into().unwrap() + } + + fn rotr(x: u32, n: u32) -> u32 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 32 - n) + quotient + } +} + +impl U64BitRotate of BitRotate { + fn rotl(x: u64, n: u64) -> u64 { + let word = u64_wide_mul(x, pow(2, n)); + let (quotient, remainder) = DivRem::div_rem( + word, 0x10000000000000000_u128.try_into().unwrap() + ); + (quotient + remainder).try_into().unwrap() + } + + fn rotr(x: u64, n: u64) -> u64 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 64 - n) + quotient + } +} + +impl U128BitRotate of BitRotate { + fn rotl(x: u128, n: u128) -> u128 { + let (high, low) = u128_wide_mul(x, pow(2, n)); + let word = u256 { low, high }; + let (quotient, remainder) = DivRem::div_rem( + word, u256 { low: 0, high: 1 }.try_into().unwrap() + ); + (quotient + remainder).try_into().unwrap() + } + + fn rotr(x: u128, n: u128) -> u128 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 128 - n) + quotient + } +} + +impl U256BitRotate of BitRotate { + fn rotl(x: u256, n: u256) -> u256 { + // TODO(sveamarcus): missing non-zero implementation for u512 + // let word = u256_wide_mul(x, pow(2, n)); + // let (quotient, remainder) = DivRem::div_rem(word, + // u512_as_non_zero(u512{limb0: 0, limb1: 0, limb2: 1, limb3: 0 })); + // (quotient + remainder).try_into().unwrap() + panic_with_felt252('missing impl') + } + + fn rotr(x: u256, n: u256) -> u256 { + let step = pow(2, n); + let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); + remainder * pow(2, 256 - n) + quotient + } +} + mod aliquot_sum; mod armstrong_number; mod collatz_sequence; diff --git a/src/math/src/tests/math_test.cairo b/src/math/src/tests/math_test.cairo index 5262151d..46f136db 100644 --- a/src/math/src/tests/math_test.cairo +++ b/src/math/src/tests/math_test.cairo @@ -1,4 +1,4 @@ -use alexandria_math::{pow, BitShift, count_digits_of_base}; +use alexandria_math::{pow, BitShift, BitRotate, count_digits_of_base}; use integer::{BoundedInt}; // Test power function @@ -170,3 +170,49 @@ fn shl_should_not_overflow() { assert(BitShift::shl(pow::(2, 127), 1) == 0, 'invalid result'); assert(BitShift::shl(pow::(2, 255), 1) == 0, 'invalid result'); } + +#[test] +#[available_gas(2000000)] +fn test_rotl_min() { + assert(BitRotate::rotl(pow::(2, 7) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotl(pow::(2, 15) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotl(pow::(2, 31) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotl(pow::(2, 63) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotl(pow::(2, 127) + 1, 1) == 3, 'invalid result'); +// TODO(sveamarcus): missing implementation +// assert(BitRotate::rotl(pow::(2, 255) + 1, 1) == 3, 'invalid result'); +} + +#[test] +#[available_gas(2000000)] +fn test_rotl_max() { + assert(BitRotate::rotl(0b101, 7) == pow::(2, 7) + 0b10, 'invalid result'); + assert(BitRotate::rotl(0b101, 15) == pow::(2, 15) + 0b10, 'invalid result'); + assert(BitRotate::rotl(0b101, 31) == pow::(2, 31) + 0b10, 'invalid result'); + assert(BitRotate::rotl(0b101, 63) == pow::(2, 63) + 0b10, 'invalid result'); + assert(BitRotate::rotl(0b101, 127) == pow::(2, 127) + 0b10, 'invalid result'); +// TODO(sveamarcus): missing implementation +// assert(BitRotate::rotl(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); +} + +#[test] +#[available_gas(4000000)] +fn test_rotr_min() { + assert(BitRotate::rotr(pow::(2, 7) + 1, 1) == 0b11 * pow(2, 6), 'invalid result'); + assert(BitRotate::rotr(pow::(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result'); + assert(BitRotate::rotr(pow::(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result'); + assert(BitRotate::rotr(pow::(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result'); + assert(BitRotate::rotr(pow::(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result'); + assert(BitRotate::rotr(pow::(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result'); +} + +#[test] +#[available_gas(2000000)] +fn test_rotr_max() { + assert(BitRotate::rotr(0b101_u8, 7) == 0b1010, 'invalid result'); + assert(BitRotate::rotr(0b101_u16, 15) == 0b1010, 'invalid result'); + assert(BitRotate::rotr(0b101_u32, 31) == 0b1010, 'invalid result'); + assert(BitRotate::rotr(0b101_u64, 63) == 0b1010, 'invalid result'); + assert(BitRotate::rotr(0b101_u128, 127) == 0b1010, 'invalid result'); + assert(BitRotate::rotr(0b101_u256, 255) == 0b1010, 'invalid result'); +} From 98d35376b5bc98a7956db326a957cea2a30dbbda Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Wed, 25 Oct 2023 16:01:22 +0200 Subject: [PATCH 2/6] adds documentation --- src/math/src/lib.cairo | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index b08edd8f..41f0bf94 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -111,8 +111,21 @@ impl U256BitShift of BitShift { } } +/// Rotate the bits of an unsigned integer of type T trait BitRotate { + /// Take the bits of an unsigned integer and rotate in the left direction + /// # Arguments + /// * `x` - rotate its bit representation in the leftward direction + /// * `n` - number of steps to rotate + /// # Returns + /// * `T` - the result of rotating the bits of number `x` left, `n` number of steps fn rotl(x: T, n: T) -> T; + /// Take the bits of an unsigned integer and rotate in the right direction + /// # Arguments + /// * `x` - rotate its bit representation in the rightward direction + /// * `n` - number of steps to rotate + /// # Returns + /// * `T` - the result of rotating the bits of number `x` right, `n` number of steps fn rotr(x: T, n: T) -> T; } From 4467d17bd05fc6fbbdb3f7588b1b50e218e08f75 Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Fri, 3 Nov 2023 15:04:22 +0100 Subject: [PATCH 3/6] rename rotl to rotate_left and rotr to rotate_right to better align with rust --- src/math/src/lib.cairo | 28 ++++++++--------- src/math/src/tests/math_test.cairo | 48 +++++++++++++++--------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index 41f0bf94..06519912 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -119,24 +119,24 @@ trait BitRotate { /// * `n` - number of steps to rotate /// # Returns /// * `T` - the result of rotating the bits of number `x` left, `n` number of steps - fn rotl(x: T, n: T) -> T; + fn rotate_left(x: T, n: T) -> T; /// Take the bits of an unsigned integer and rotate in the right direction /// # Arguments /// * `x` - rotate its bit representation in the rightward direction /// * `n` - number of steps to rotate /// # Returns /// * `T` - the result of rotating the bits of number `x` right, `n` number of steps - fn rotr(x: T, n: T) -> T; + fn rotate_right(x: T, n: T) -> T; } impl U8BitRotate of BitRotate { - fn rotl(x: u8, n: u8) -> u8 { + fn rotate_left(x: u8, n: u8) -> u8 { let word = u8_wide_mul(x, pow(2, n)); let (quotient, remainder) = DivRem::div_rem(word, 0x100_u16.try_into().unwrap()); (quotient + remainder).try_into().unwrap() } - fn rotr(x: u8, n: u8) -> u8 { + fn rotate_right(x: u8, n: u8) -> u8 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 8 - n) + quotient @@ -144,13 +144,13 @@ impl U8BitRotate of BitRotate { } impl U16BitRotate of BitRotate { - fn rotl(x: u16, n: u16) -> u16 { + fn rotate_left(x: u16, n: u16) -> u16 { let word = u16_wide_mul(x, pow(2, n)); let (quotient, remainder) = DivRem::div_rem(word, 0x10000_u32.try_into().unwrap()); (quotient + remainder).try_into().unwrap() } - fn rotr(x: u16, n: u16) -> u16 { + fn rotate_right(x: u16, n: u16) -> u16 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 16 - n) + quotient @@ -158,13 +158,13 @@ impl U16BitRotate of BitRotate { } impl U32BitRotate of BitRotate { - fn rotl(x: u32, n: u32) -> u32 { + fn rotate_left(x: u32, n: u32) -> u32 { let word = u32_wide_mul(x, pow(2, n)); let (quotient, remainder) = DivRem::div_rem(word, 0x100000000_u64.try_into().unwrap()); (quotient + remainder).try_into().unwrap() } - fn rotr(x: u32, n: u32) -> u32 { + fn rotate_right(x: u32, n: u32) -> u32 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 32 - n) + quotient @@ -172,7 +172,7 @@ impl U32BitRotate of BitRotate { } impl U64BitRotate of BitRotate { - fn rotl(x: u64, n: u64) -> u64 { + fn rotate_left(x: u64, n: u64) -> u64 { let word = u64_wide_mul(x, pow(2, n)); let (quotient, remainder) = DivRem::div_rem( word, 0x10000000000000000_u128.try_into().unwrap() @@ -180,7 +180,7 @@ impl U64BitRotate of BitRotate { (quotient + remainder).try_into().unwrap() } - fn rotr(x: u64, n: u64) -> u64 { + fn rotate_right(x: u64, n: u64) -> u64 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 64 - n) + quotient @@ -188,7 +188,7 @@ impl U64BitRotate of BitRotate { } impl U128BitRotate of BitRotate { - fn rotl(x: u128, n: u128) -> u128 { + fn rotate_left(x: u128, n: u128) -> u128 { let (high, low) = u128_wide_mul(x, pow(2, n)); let word = u256 { low, high }; let (quotient, remainder) = DivRem::div_rem( @@ -197,7 +197,7 @@ impl U128BitRotate of BitRotate { (quotient + remainder).try_into().unwrap() } - fn rotr(x: u128, n: u128) -> u128 { + fn rotate_right(x: u128, n: u128) -> u128 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 128 - n) + quotient @@ -205,7 +205,7 @@ impl U128BitRotate of BitRotate { } impl U256BitRotate of BitRotate { - fn rotl(x: u256, n: u256) -> u256 { + fn rotate_left(x: u256, n: u256) -> u256 { // TODO(sveamarcus): missing non-zero implementation for u512 // let word = u256_wide_mul(x, pow(2, n)); // let (quotient, remainder) = DivRem::div_rem(word, @@ -214,7 +214,7 @@ impl U256BitRotate of BitRotate { panic_with_felt252('missing impl') } - fn rotr(x: u256, n: u256) -> u256 { + fn rotate_right(x: u256, n: u256) -> u256 { let step = pow(2, n); let (quotient, remainder) = DivRem::div_rem(x, step.try_into().unwrap()); remainder * pow(2, 256 - n) + quotient diff --git a/src/math/src/tests/math_test.cairo b/src/math/src/tests/math_test.cairo index 46f136db..93795c85 100644 --- a/src/math/src/tests/math_test.cairo +++ b/src/math/src/tests/math_test.cairo @@ -174,45 +174,45 @@ fn shl_should_not_overflow() { #[test] #[available_gas(2000000)] fn test_rotl_min() { - assert(BitRotate::rotl(pow::(2, 7) + 1, 1) == 3, 'invalid result'); - assert(BitRotate::rotl(pow::(2, 15) + 1, 1) == 3, 'invalid result'); - assert(BitRotate::rotl(pow::(2, 31) + 1, 1) == 3, 'invalid result'); - assert(BitRotate::rotl(pow::(2, 63) + 1, 1) == 3, 'invalid result'); - assert(BitRotate::rotl(pow::(2, 127) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 7) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 15) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 31) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 63) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 127) + 1, 1) == 3, 'invalid result'); // TODO(sveamarcus): missing implementation -// assert(BitRotate::rotl(pow::(2, 255) + 1, 1) == 3, 'invalid result'); +// assert(BitRotate::rotate_left(pow::(2, 255) + 1, 1) == 3, 'invalid result'); } #[test] #[available_gas(2000000)] fn test_rotl_max() { - assert(BitRotate::rotl(0b101, 7) == pow::(2, 7) + 0b10, 'invalid result'); - assert(BitRotate::rotl(0b101, 15) == pow::(2, 15) + 0b10, 'invalid result'); - assert(BitRotate::rotl(0b101, 31) == pow::(2, 31) + 0b10, 'invalid result'); - assert(BitRotate::rotl(0b101, 63) == pow::(2, 63) + 0b10, 'invalid result'); - assert(BitRotate::rotl(0b101, 127) == pow::(2, 127) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 7) == pow::(2, 7) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 15) == pow::(2, 15) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 31) == pow::(2, 31) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 63) == pow::(2, 63) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 127) == pow::(2, 127) + 0b10, 'invalid result'); // TODO(sveamarcus): missing implementation -// assert(BitRotate::rotl(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); +// assert(BitRotate::rotate_left(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); } #[test] #[available_gas(4000000)] fn test_rotr_min() { - assert(BitRotate::rotr(pow::(2, 7) + 1, 1) == 0b11 * pow(2, 6), 'invalid result'); - assert(BitRotate::rotr(pow::(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result'); - assert(BitRotate::rotr(pow::(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result'); - assert(BitRotate::rotr(pow::(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result'); - assert(BitRotate::rotr(pow::(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result'); - assert(BitRotate::rotr(pow::(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 7) + 1, 1) == 0b11 * pow(2, 6), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result'); + assert(BitRotate::rotate_right(pow::(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result'); } #[test] #[available_gas(2000000)] fn test_rotr_max() { - assert(BitRotate::rotr(0b101_u8, 7) == 0b1010, 'invalid result'); - assert(BitRotate::rotr(0b101_u16, 15) == 0b1010, 'invalid result'); - assert(BitRotate::rotr(0b101_u32, 31) == 0b1010, 'invalid result'); - assert(BitRotate::rotr(0b101_u64, 63) == 0b1010, 'invalid result'); - assert(BitRotate::rotr(0b101_u128, 127) == 0b1010, 'invalid result'); - assert(BitRotate::rotr(0b101_u256, 255) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u8, 7) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u16, 15) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u32, 31) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u64, 63) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u128, 127) == 0b1010, 'invalid result'); + assert(BitRotate::rotate_right(0b101_u256, 255) == 0b1010, 'invalid result'); } From 06c106b4dd2c4995fc8bcd4c6d261ac6e2963f66 Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Fri, 3 Nov 2023 15:07:20 +0100 Subject: [PATCH 4/6] scarb fmt --- src/math/src/tests/math_test.cairo | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/math/src/tests/math_test.cairo b/src/math/src/tests/math_test.cairo index 93795c85..8fc0d8f3 100644 --- a/src/math/src/tests/math_test.cairo +++ b/src/math/src/tests/math_test.cairo @@ -199,11 +199,21 @@ fn test_rotl_max() { #[available_gas(4000000)] fn test_rotr_min() { assert(BitRotate::rotate_right(pow::(2, 7) + 1, 1) == 0b11 * pow(2, 6), 'invalid result'); - assert(BitRotate::rotate_right(pow::(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result'); - assert(BitRotate::rotate_right(pow::(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result'); - assert(BitRotate::rotate_right(pow::(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result'); - assert(BitRotate::rotate_right(pow::(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result'); - assert(BitRotate::rotate_right(pow::(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result'); + assert( + BitRotate::rotate_right(pow::(2, 15) + 1, 1) == 0b11 * pow(2, 14), 'invalid result' + ); + assert( + BitRotate::rotate_right(pow::(2, 31) + 1, 1) == 0b11 * pow(2, 30), 'invalid result' + ); + assert( + BitRotate::rotate_right(pow::(2, 63) + 1, 1) == 0b11 * pow(2, 62), 'invalid result' + ); + assert( + BitRotate::rotate_right(pow::(2, 127) + 1, 1) == 0b11 * pow(2, 126), 'invalid result' + ); + assert( + BitRotate::rotate_right(pow::(2, 255) + 1, 1) == 0b11 * pow(2, 254), 'invalid result' + ); } #[test] From 4805e6b5c253553bfc4a14d822bbc0ece3fccb58 Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Thu, 9 Nov 2023 16:12:52 +0100 Subject: [PATCH 5/6] merge main --- .all-contributorsrc | 18 ++ README.md | 2 + Scarb.toml | 3 + src/ascii/Scarb.toml | 3 + src/ascii/src/integer.cairo | 12 +- src/ascii/src/lib.cairo | 2 +- src/ascii/src/tests/test_ascii_integer.cairo | 4 +- src/bytes/Scarb.toml | 3 + src/bytes/src/bytes.cairo | 22 +-- src/bytes/src/lib.cairo | 6 +- src/bytes/src/tests/test_bytes.cairo | 182 ++++++++++-------- src/bytes/src/utils.cairo | 10 +- src/data_structures/Scarb.toml | 3 + src/data_structures/src/array_ext.cairo | 3 - src/data_structures/src/bit_array.cairo | 3 +- src/data_structures/src/byte_array_ext.cairo | 3 +- .../src/byte_array_reader.cairo | 7 +- src/data_structures/src/lib.cairo | 2 +- src/data_structures/src/queue.cairo | 3 - src/data_structures/src/stack.cairo | 6 - .../src/tests/array_ext_test.cairo | 2 - .../src/tests/bit_array_test.cairo | 4 +- .../src/tests/byte_array_reader_test.cairo | 7 +- .../src/tests/queue_test.cairo | 4 - .../src/tests/stack_test.cairo | 5 - src/data_structures/src/tests/vec_test.cairo | 5 - src/data_structures/src/vec.cairo | 6 - src/encoding/Scarb.toml | 3 + src/encoding/src/base64.cairo | 6 +- src/encoding/src/reversible.cairo | 1 - src/encoding/src/tests/base64_test.cairo | 2 - src/encoding/src/tests/reversible_test.cairo | 2 - src/linalg/Scarb.toml | 3 + src/linalg/src/dot.cairo | 2 - src/linalg/src/tests/dot_test.cairo | 1 - src/math/Scarb.toml | 3 + src/math/src/collatz_sequence.cairo | 1 - src/math/src/ed25519.cairo | 9 +- src/math/src/fast_power.cairo | 3 - src/math/src/gcd_of_n_numbers.cairo | 2 - src/math/src/karatsuba.cairo | 3 - src/math/src/lib.cairo | 39 ++-- src/math/src/mod_arithmetics.cairo | 2 - src/math/src/perfect_number.cairo | 1 - src/math/src/sha256.cairo | 5 - src/math/src/sha512.cairo | 4 - src/math/src/tests.cairo | 2 +- .../src/tests/collatz_sequence_test.cairo | 4 - src/math/src/tests/ed25519_test.cairo | 5 - .../src/tests/gcd_of_n_numbers_test.cairo | 1 - src/math/src/tests/perfect_number_test.cairo | 4 - src/math/src/tests/sha256_test.cairo | 5 +- src/math/src/tests/sha512_test.cairo | 1 - src/math/src/tests/test_keccak256.cairo | 1 - .../src/tests/zellers_congruence_test.cairo | 2 - src/merkle_tree/Scarb.toml | 4 +- src/merkle_tree/src/merkle_tree.cairo | 4 +- src/merkle_tree/src/storage_proof.cairo | 2 +- src/numeric/Scarb.toml | 3 + src/numeric/src/cumsum.cairo | 2 - src/numeric/src/diff.cairo | 3 - src/numeric/src/interpolate.cairo | 3 - src/numeric/src/lib.cairo | 2 +- src/numeric/src/tests/cumsum_test.cairo | 1 - src/numeric/src/tests/diff_test.cairo | 1 - src/numeric/src/tests/interpolate_test.cairo | 1 - .../src/tests/trapezoidal_rule_test.cairo | 1 - src/numeric/src/trapezoidal_rule.cairo | 3 - src/searching/Scarb.toml | 3 + src/searching/src/dijkstra.cairo | 16 +- .../src/tests/binary_search_test.cairo | 2 - src/searching/src/tests/dijkstra_test.cairo | 8 +- src/sorting/Scarb.toml | 3 + src/sorting/src/bubble_sort.cairo | 1 - src/sorting/src/lib.cairo | 5 +- src/sorting/src/merge_sort.cairo | 1 - src/sorting/src/tests/bubble_sort_test.cairo | 2 - src/sorting/src/tests/merge_sort_test.cairo | 2 - src/storage/Scarb.toml | 3 + src/storage/src/list.cairo | 7 +- src/storage/src/tests/list_test.cairo | 7 - 81 files changed, 224 insertions(+), 313 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 6225c234..9a5d6a90 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -243,6 +243,24 @@ "contributions": [ "code" ] + }, + { + "login": "akhercha", + "name": "akhercha", + "avatar_url": "https://avatars.githubusercontent.com/u/22559023?v=4", + "profile": "https://github.com/akhercha", + "contributions": [ + "code" + ] + }, + { + "login": "azurwastaken", + "name": "azurwastaken", + "avatar_url": "https://avatars.githubusercontent.com/u/30268138?v=4", + "profile": "https://github.com/azurwastaken", + "contributions": [ + "code" + ] } ], "contributorsPerLine": 7, diff --git a/README.md b/README.md index 3133dd66..b758963e 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d maciektr
maciektr

💻 lambda-0x
lambda-0x

💻 Elias Tazartes
Elias Tazartes

💻 + akhercha
akhercha

💻 + azurwastaken
azurwastaken

💻 diff --git a/Scarb.toml b/Scarb.toml index f56e6f40..ce9fa1ba 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -20,5 +20,8 @@ homepage = "https://github.com/keep-starknet-strange/alexandria/" [workspace.dependencies] starknet = "=2.3.1" +[workspace.tool.fmt] +sort-module-level-items = true + [scripts] all = "scarb build && scarb test" diff --git a/src/ascii/Scarb.toml b/src/ascii/Scarb.toml index 5aff6a86..59b8dcc2 100644 --- a/src/ascii/Scarb.toml +++ b/src/ascii/Scarb.toml @@ -4,5 +4,8 @@ version = "0.1.0" description = "utilities for working with ascii values" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/ascii" +[tool] +fmt.workspace = true + [dependencies] alexandria_data_structures = { path = "../data_structures" } \ No newline at end of file diff --git a/src/ascii/src/integer.cairo b/src/ascii/src/integer.cairo index 42c85760..cdf45afb 100644 --- a/src/ascii/src/integer.cairo +++ b/src/ascii/src/integer.cairo @@ -1,8 +1,4 @@ -use option::OptionTrait; use alexandria_data_structures::array_ext::ArrayTraitExt; -use array::{ArrayTrait, SpanTrait}; -use traits::{Into, TryInto, DivRem}; -use zeroable::Zeroable; trait ToAsciiTrait { fn to_ascii(self: T) -> U; @@ -33,7 +29,7 @@ impl ToAsciiArrayTraitImpl< } fn to_inverse_ascii_array(self: T) -> Array { - let mut new_arr = ArrayTrait::new(); + let mut new_arr = array![]; if self <= 9.try_into().unwrap() { new_arr.append(self.into() + 48); return new_arr; @@ -101,7 +97,7 @@ impl BigIntegerToAsciiTraitImpl< +Copy, > of ToAsciiTrait> { fn to_ascii(self: T) -> Array { - let mut data = ArrayTrait::new(); + let mut data = array![]; if self <= 9.try_into().unwrap() { data.append(self.into() + 48); return data; @@ -151,7 +147,7 @@ impl U256ToAsciiArrayTraitImpl of ToAsciiArrayTrait { } fn to_inverse_ascii_array(self: u256) -> Array { - let mut new_arr = ArrayTrait::new(); + let mut new_arr = array![]; if self <= 9 { new_arr.append(self.try_into().expect('number overflow felt252') + 48); return new_arr; @@ -173,7 +169,7 @@ impl U256ToAsciiArrayTraitImpl of ToAsciiArrayTrait { impl U256ToAsciiTraitImpl of ToAsciiTrait> { fn to_ascii(self: u256) -> Array { - let mut data = ArrayTrait::new(); + let mut data = array![]; if self <= 9 { data.append(self.try_into().expect('number overflow felt252') + 48); return data; diff --git a/src/ascii/src/lib.cairo b/src/ascii/src/lib.cairo index 53d0feea..c7547a90 100644 --- a/src/ascii/src/lib.cairo +++ b/src/ascii/src/lib.cairo @@ -1,5 +1,5 @@ mod integer; -use integer::{ToAsciiTrait, ToAsciiArrayTrait}; #[cfg(test)] mod tests; +use integer::{ToAsciiTrait, ToAsciiArrayTrait}; diff --git a/src/ascii/src/tests/test_ascii_integer.cairo b/src/ascii/src/tests/test_ascii_integer.cairo index 362b8f80..417c0a1a 100644 --- a/src/ascii/src/tests/test_ascii_integer.cairo +++ b/src/ascii/src/tests/test_ascii_integer.cairo @@ -1,7 +1,5 @@ -use array::ArrayTrait; -use traits::{Into, TryInto}; -use integer::BoundedInt; use alexandria_ascii::ToAsciiTrait; +use integer::BoundedInt; #[test] #[available_gas(2000000000)] diff --git a/src/bytes/Scarb.toml b/src/bytes/Scarb.toml index 6d97669b..3059a36e 100644 --- a/src/bytes/Scarb.toml +++ b/src/bytes/Scarb.toml @@ -4,6 +4,9 @@ version = "0.1.0" description = "An implementation similar to Solidity bytes written in Cairo 1" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/bytes" +[tool] +fmt.workspace = true + [dependencies] alexandria_math = { path = "../math" } alexandria_data_structures = { path = "../data_structures" } \ No newline at end of file diff --git a/src/bytes/src/bytes.cairo b/src/bytes/src/bytes.cairo index 96ca61f8..7be3de46 100644 --- a/src/bytes/src/bytes.cairo +++ b/src/bytes/src/bytes.cairo @@ -1,14 +1,8 @@ -use clone::Clone; -use array::ArrayTrait; -use traits::Into; -use traits::TryInto; -use traits::DivRem; -use option::OptionTrait; -use starknet::{ContractAddress, Felt252TryIntoContractAddress}; -use alexandria_math::sha256::sha256; use alexandria_bytes::utils::{ u128_join, read_sub_u128, u128_split, u128_array_slice, keccak_u128s_be, u8_array_to_u256 }; +use alexandria_math::sha256::sha256; +use starknet::ContractAddress; /// Bytes is a dynamic array of u128, where each element contains 16 bytes. const BYTES_PER_ELEMENT: usize = 16; @@ -120,11 +114,11 @@ impl BytesImpl of BytesTrait { #[inline(always)] fn new_empty() -> Bytes { - Bytes { size: 0_usize, data: ArrayTrait::::new() } + Bytes { size: 0_usize, data: array![] } } fn zero(size: usize) -> Bytes { - let mut data = ArrayTrait::::new(); + let mut data = array![]; let (data_index, mut data_len) = DivRem::div_rem( size, BYTES_PER_ELEMENT.try_into().expect('Division by 0') ); @@ -222,7 +216,7 @@ impl BytesImpl of BytesTrait { self: @Bytes, offset: usize, array_length: usize, element_size: usize ) -> (usize, Array) { assert(offset + array_length * element_size <= self.size(), 'out of bound'); - let mut array = ArrayTrait::::new(); + let mut array = array![]; if array_length == 0 { return (offset, array); @@ -311,7 +305,7 @@ impl BytesImpl of BytesTrait { /// read a u256 array from Bytes fn read_u256_array(self: @Bytes, offset: usize, array_length: usize) -> (usize, Array) { assert(offset + array_length * 32 <= self.size(), 'out of bound'); - let mut array = ArrayTrait::::new(); + let mut array = array![]; if array_length == 0 { return (offset, array); @@ -340,7 +334,7 @@ impl BytesImpl of BytesTrait { return (offset, BytesTrait::new_empty()); } - let mut array = ArrayTrait::::new(); + let mut array = array![]; // read full array element for sub_bytes let mut offset = offset; @@ -519,7 +513,7 @@ impl BytesImpl of BytesTrait { /// sha256 hash fn sha256(self: @Bytes) -> u256 { - let mut hash_data: Array = ArrayTrait::new(); + let mut hash_data: Array = array![]; let mut i: usize = 0; let mut offset: usize = 0; loop { diff --git a/src/bytes/src/lib.cairo b/src/bytes/src/lib.cairo index 5ca66b7c..be3854a4 100644 --- a/src/bytes/src/lib.cairo +++ b/src/bytes/src/lib.cairo @@ -1,7 +1,7 @@ mod bytes; -mod utils; - -use bytes::{Bytes, BytesTrait}; #[cfg(test)] mod tests; +mod utils; + +use bytes::{Bytes, BytesTrait}; diff --git a/src/bytes/src/tests/test_bytes.cairo b/src/bytes/src/tests/test_bytes.cairo index af7b70b0..84296f3b 100644 --- a/src/bytes/src/tests/test_bytes.cairo +++ b/src/bytes/src/tests/test_bytes.cairo @@ -1,8 +1,5 @@ -use traits::Into; -use array::ArrayTrait; -use starknet::{ContractAddress, ContractAddressIntoFelt252, contract_address_const}; use alexandria_bytes::{Bytes, BytesTrait}; -use debug::PrintTrait; +use starknet::ContractAddress; #[test] #[available_gas(20000000)] @@ -81,10 +78,11 @@ fn test_bytes_update() { #[test] #[available_gas(20000000)] fn test_bytes_read_u128_packed() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -113,10 +111,11 @@ fn test_bytes_read_u128_packed() { #[available_gas(20000000)] #[should_panic(expected: ('out of bound',))] fn test_bytes_read_u128_packed_out_of_bound() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -127,10 +126,11 @@ fn test_bytes_read_u128_packed_out_of_bound() { #[available_gas(20000000)] #[should_panic(expected: ('too large',))] fn test_bytes_read_u128_packed_too_large() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -140,10 +140,11 @@ fn test_bytes_read_u128_packed_too_large() { #[test] #[available_gas(20000000)] fn test_bytes_read_u128_array_packed() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -165,10 +166,11 @@ fn test_bytes_read_u128_array_packed() { #[available_gas(20000000)] #[should_panic(expected: ('out of bound',))] fn test_bytes_read_u128_array_packed_out_of_bound() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -179,10 +181,11 @@ fn test_bytes_read_u128_array_packed_out_of_bound() { #[available_gas(20000000)] #[should_panic(expected: ('too large',))] fn test_bytes_read_u128_array_packed_too_large() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -192,10 +195,11 @@ fn test_bytes_read_u128_array_packed_too_large() { #[test] #[available_gas(20000000)] fn test_bytes_read_felt252_packed() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -208,10 +212,11 @@ fn test_bytes_read_felt252_packed() { #[available_gas(20000000)] #[should_panic(expected: ('out of bound',))] fn test_bytes_read_felt252_packed_out_of_bound() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -222,10 +227,11 @@ fn test_bytes_read_felt252_packed_out_of_bound() { #[available_gas(20000000)] #[should_panic(expected: ('too large',))] fn test_bytes_read_felt252_packed_too_large() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -235,10 +241,11 @@ fn test_bytes_read_felt252_packed_too_large() { #[test] #[available_gas(20000000)] fn test_bytes_read_u8() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -250,10 +257,11 @@ fn test_bytes_read_u8() { #[test] #[available_gas(20000000)] fn test_bytes_read_u16() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -265,10 +273,11 @@ fn test_bytes_read_u16() { #[test] #[available_gas(20000000)] fn test_bytes_read_u32() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -280,10 +289,11 @@ fn test_bytes_read_u32() { #[test] #[available_gas(20000000)] fn test_bytes_read_usize() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -295,10 +305,11 @@ fn test_bytes_read_usize() { #[test] #[available_gas(20000000)] fn test_bytes_read_u64() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -310,10 +321,11 @@ fn test_bytes_read_u64() { #[test] #[available_gas(20000000)] fn test_bytes_read_u128() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -325,10 +337,11 @@ fn test_bytes_read_u128() { #[test] #[available_gas(20000000)] fn test_bytes_read_u256() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x01020304050607080910000000000000 + ]; let bytes = BytesTrait::new(42, array); @@ -341,13 +354,14 @@ fn test_bytes_read_u256() { #[test] #[available_gas(20000000)] fn test_bytes_read_u256_array() { - let mut array = ArrayTrait::::new(); - array.append(0x01020304050607080910111213141516); - array.append(0x16151413121110090807060504030201); - array.append(0x16151413121110090807060504030201); - array.append(0x01020304050607080910111213141516); - array.append(0x01020304050607080910111213141516); - array.append(0x16151413121110090000000000000000); + let mut array = array![ + 0x01020304050607080910111213141516, + 0x16151413121110090807060504030201, + 0x16151413121110090807060504030201, + 0x01020304050607080910111213141516, + 0x01020304050607080910111213141516, + 0x16151413121110090000000000000000 + ]; let bytes = BytesTrait::new(88, array); @@ -364,7 +378,7 @@ fn test_bytes_read_u256_array() { #[test] #[available_gas(20000000)] fn test_bytes_read_address() { - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0x01020304050607080910111213140154); array.append(0x01855d7796176b05d160196ff92381eb); array.append(0x7910f5446c2e0e04e13db2194a4f0000); @@ -380,7 +394,7 @@ fn test_bytes_read_address() { #[test] #[available_gas(20000000)] fn test_bytes_read_bytes() { - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0x01020304050607080910111213140154); array.append(0x01855d7796176b05d160196ff92381eb); array.append(0x7910f5446c2e0e04e13db2194a4f0000); @@ -499,9 +513,9 @@ fn test_bytes_append() { bytes = Bytes { size: size, data: data }; // append_address - let address = contract_address_const::< - 0x015401855d7796176b05d160196ff92381eb7910f5446c2e0e04e13db2194a4f - >(); + let address = 0x015401855d7796176b05d160196ff92381eb7910f5446c2e0e04e13db2194a4f + .try_into() + .unwrap(); bytes.append_address(address); let Bytes{size, mut data } = bytes; assert(size == 117, 'append_address_size'); @@ -605,7 +619,7 @@ fn test_bytes_keccak() { assert(res == hash, 'bytes_keccak_0'); // u256{low: 1, high: 0} - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0); array.append(1); let bytes: Bytes = BytesTrait::new(32, array); @@ -614,7 +628,7 @@ fn test_bytes_keccak() { assert(res == hash, 'bytes_keccak_1'); // test_bytes_append bytes - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0x10111213141516171810111213141516); array.append(0x17180101020102030400000001000003); array.append(0x04050607080000000000000010111213); @@ -642,7 +656,7 @@ fn test_bytes_sha256() { // u256{low: 1, high: 0} // 0x0000000000000000000000000000000000000000000000000000000000000001 - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0); array.append(1); let bytes: Bytes = BytesTrait::new(32, array); @@ -651,7 +665,7 @@ fn test_bytes_sha256() { assert(res == hash, 'bytes_sha256_1'); // test_bytes_append bytes - let mut array = ArrayTrait::::new(); + let mut array = array![]; array.append(0x10111213141516171810111213141516); array.append(0x17180101020102030400000001000003); array.append(0x04050607080000000000000010111213); diff --git a/src/bytes/src/utils.cairo b/src/bytes/src/utils.cairo index e514288d..b61329f3 100644 --- a/src/bytes/src/utils.cairo +++ b/src/bytes/src/utils.cairo @@ -1,12 +1,12 @@ -use keccak::{u128_to_u64, u128_split as u128_split_to_u64, cairo_keccak}; -use integer::u128_byte_reverse; use alexandria_data_structures::array_ext::ArrayTraitExt; +use integer::u128_byte_reverse; +use keccak::{u128_to_u64, u128_split as u128_split_to_u64, cairo_keccak}; /// Computes the keccak256 of multiple uint128 values. /// The values are interpreted as big-endian. /// https://github.com/starkware-libs/cairo/blob/main/corelib/src/keccak.cairo fn keccak_u128s_be(mut input: Span, n_bytes: usize) -> u256 { - let mut keccak_input: Array:: = ArrayTrait::new(); + let mut keccak_input: Array:: = array![]; let mut size = n_bytes; loop { match input.pop_front() { @@ -119,7 +119,7 @@ fn u8_array_to_u256(arr: Span) -> u256 { } fn u64_array_slice(src: @Array, mut begin: usize, end: usize) -> Array { - let mut slice = ArrayTrait::new(); + let mut slice = array![]; let len = begin + end; loop { if begin >= len { @@ -142,7 +142,7 @@ fn u64_array_slice(src: @Array, mut begin: usize, end: usize) -> Array /// # Returns /// * `Array` - The slice of the array. fn u128_array_slice(src: @Array, mut begin: usize, end: usize) -> Array { - let mut slice = ArrayTrait::new(); + let mut slice = array![]; let len = begin + end; loop { if begin >= len { diff --git a/src/data_structures/Scarb.toml b/src/data_structures/Scarb.toml index a164c240..99d0ae57 100644 --- a/src/data_structures/Scarb.toml +++ b/src/data_structures/Scarb.toml @@ -4,5 +4,8 @@ version = "0.1.0" description = "A set of Cairo data structure libraries and algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/data_structures" +[tool] +fmt.workspace = true + [dependencies] alexandria_encoding = { path = "../encoding" } \ No newline at end of file diff --git a/src/data_structures/src/array_ext.cairo b/src/data_structures/src/array_ext.cairo index 5d535bb3..e83887c0 100644 --- a/src/data_structures/src/array_ext.cairo +++ b/src/data_structures/src/array_ext.cairo @@ -1,6 +1,3 @@ -use array::{ArrayTrait, SpanTrait}; -use option::{Option, OptionTrait}; - trait ArrayTraitExt { fn append_all(ref self: Array, ref arr: Array); fn pop_front_n(ref self: Array, n: usize); diff --git a/src/data_structures/src/bit_array.cairo b/src/data_structures/src/bit_array.cairo index 518a22ab..fe23eb17 100644 --- a/src/data_structures/src/bit_array.cairo +++ b/src/data_structures/src/bit_array.cairo @@ -1,12 +1,11 @@ use array::{serialize_array_helper, deserialize_array_helper}; +use byte_array::BYTES_IN_BYTES31_MINUS_ONE; use bytes_31::{ one_shift_left_bytes_u128, one_shift_left_bytes_felt252, bytes31, BYTES_IN_U128, BYTES_IN_BYTES31, }; -use byte_array::BYTES_IN_BYTES31_MINUS_ONE; use integer::u512; use serde::into_felt252_based::SerdeImpl; -use traits::DivRem; const SELECT_BIT: u128 = 0b10; diff --git a/src/data_structures/src/byte_array_ext.cairo b/src/data_structures/src/byte_array_ext.cairo index 465f23bc..e361a6d2 100644 --- a/src/data_structures/src/byte_array_ext.cairo +++ b/src/data_structures/src/byte_array_ext.cairo @@ -4,8 +4,7 @@ use array::{serialize_array_helper, deserialize_array_helper}; use byte_array::ByteArray; use bytes_31::{one_shift_left_bytes_felt252, one_shift_left_bytes_u128, BYTES_IN_BYTES31}; use integer::u512; -use traits::DivRem; -use core::serde::into_felt252_based::SerdeImpl; +use serde::into_felt252_based::SerdeImpl; trait ByteArrayTraitExt { /// Appends an unsigned 16 bit integer encoded in big endian diff --git a/src/data_structures/src/byte_array_reader.cairo b/src/data_structures/src/byte_array_reader.cairo index a97d0d5a..c3e3707e 100644 --- a/src/data_structures/src/byte_array_reader.cairo +++ b/src/data_structures/src/byte_array_reader.cairo @@ -1,8 +1,7 @@ -use integer::u512; -use bytes_31::one_shift_left_bytes_felt252; -use byte_array::ByteArray; use alexandria_data_structures::byte_array_ext::ByteArrayTraitExt; -use core::clone::Clone; +use byte_array::ByteArray; +use bytes_31::one_shift_left_bytes_felt252; +use integer::u512; /// A read-only snapshot of an underlying ByteArray that maintains /// sequential access to the integers encoded in the array diff --git a/src/data_structures/src/lib.cairo b/src/data_structures/src/lib.cairo index f7b8d55c..03633c44 100644 --- a/src/data_structures/src/lib.cairo +++ b/src/data_structures/src/lib.cairo @@ -4,7 +4,7 @@ mod byte_array_ext; mod byte_array_reader; mod queue; mod stack; -mod vec; #[cfg(test)] mod tests; +mod vec; diff --git a/src/data_structures/src/queue.cairo b/src/data_structures/src/queue.cairo index 7cfdd0c9..0c432c40 100644 --- a/src/data_structures/src/queue.cairo +++ b/src/data_structures/src/queue.cairo @@ -1,6 +1,3 @@ -// Core lib imports -use array::ArrayTrait; - const ZERO_USIZE: usize = 0; struct Queue { diff --git a/src/data_structures/src/stack.cairo b/src/data_structures/src/stack.cairo index 0389112b..13502cee 100644 --- a/src/data_structures/src/stack.cairo +++ b/src/data_structures/src/stack.cairo @@ -13,12 +13,6 @@ //! let item = stack.pop(); //! ``` -// Core lib imports -use dict::Felt252DictTrait; -use option::OptionTrait; -use traits::Into; -use nullable::NullableTrait; - trait StackTrait { /// Creates a new Stack instance. fn new() -> S; diff --git a/src/data_structures/src/tests/array_ext_test.cairo b/src/data_structures/src/tests/array_ext_test.cairo index 63c2178b..b00e4925 100644 --- a/src/data_structures/src/tests/array_ext_test.cairo +++ b/src/data_structures/src/tests/array_ext_test.cairo @@ -1,5 +1,3 @@ -use core::option::OptionTrait; -use array::{ArrayTrait, SpanTrait}; use alexandria_data_structures::array_ext::{ArrayTraitExt, SpanTraitExt}; // dedup diff --git a/src/data_structures/src/tests/bit_array_test.cairo b/src/data_structures/src/tests/bit_array_test.cairo index 34f25e3e..58914ca1 100644 --- a/src/data_structures/src/tests/bit_array_test.cairo +++ b/src/data_structures/src/tests/bit_array_test.cairo @@ -1,7 +1,7 @@ use alexandria_data_structures::bit_array::{BitArray, BitArrayTrait, shift_bit}; -use integer::u512; -use integer::BoundedInt; use bytes_31::one_shift_left_bytes_felt252; +use integer::BoundedInt; +use integer::u512; #[test] #[available_gas(30000000)] diff --git a/src/data_structures/src/tests/byte_array_reader_test.cairo b/src/data_structures/src/tests/byte_array_reader_test.cairo index 3fffb9e6..8d74bc0b 100644 --- a/src/data_structures/src/tests/byte_array_reader_test.cairo +++ b/src/data_structures/src/tests/byte_array_reader_test.cairo @@ -1,7 +1,8 @@ -use integer::u512; -use alexandria_data_structures::byte_array_ext::ByteArrayTraitExt; -use alexandria_data_structures::byte_array_reader::ByteArrayReaderTrait; use alexandria_data_structures::tests::byte_array_ext_test::test_byte_array_64; +use alexandria_data_structures::{ + byte_array_ext::ByteArrayTraitExt, byte_array_reader::ByteArrayReaderTrait +}; +use integer::u512; #[test] #[available_gas(10000000)] diff --git a/src/data_structures/src/tests/queue_test.cairo b/src/data_structures/src/tests/queue_test.cairo index e3f1846c..24962615 100644 --- a/src/data_structures/src/tests/queue_test.cairo +++ b/src/data_structures/src/tests/queue_test.cairo @@ -1,7 +1,3 @@ -use array::ArrayTrait; -use box::BoxTrait; -use option::OptionTrait; - use alexandria_data_structures::queue::{Queue, QueueTrait}; #[test] diff --git a/src/data_structures/src/tests/stack_test.cairo b/src/data_structures/src/tests/stack_test.cairo index 980f4d0e..6447ee50 100644 --- a/src/data_structures/src/tests/stack_test.cairo +++ b/src/data_structures/src/tests/stack_test.cairo @@ -1,8 +1,3 @@ -// Core lib imports -use traits::{Into, TryInto}; -use option::OptionTrait; -use integer::Felt252IntoU256; - // Internal imports use alexandria_data_structures::stack::{StackTrait, Felt252Stack, NullableStack}; diff --git a/src/data_structures/src/tests/vec_test.cairo b/src/data_structures/src/tests/vec_test.cairo index bf571263..caa8a7ce 100644 --- a/src/data_structures/src/tests/vec_test.cairo +++ b/src/data_structures/src/tests/vec_test.cairo @@ -1,8 +1,3 @@ -// Core lib imports -use traits::{Index, Into, TryInto}; -use option::OptionTrait; -use result::ResultTrait; - // Internal imports use alexandria_data_structures::vec::{Felt252Vec, NullableVec, VecTrait}; diff --git a/src/data_structures/src/vec.cairo b/src/data_structures/src/vec.cairo index 1ad1dac7..eed2c782 100644 --- a/src/data_structures/src/vec.cairo +++ b/src/data_structures/src/vec.cairo @@ -12,12 +12,6 @@ //! ... //! ``` -// Core lib imports -use core::nullable::NullableTrait; -use dict::Felt252DictTrait; -use option::OptionTrait; -use traits::{Index, Into}; - trait VecTrait { /// Creates a new V instance. /// Returns diff --git a/src/encoding/Scarb.toml b/src/encoding/Scarb.toml index 3a2350d5..2dcd0e80 100644 --- a/src/encoding/Scarb.toml +++ b/src/encoding/Scarb.toml @@ -4,5 +4,8 @@ version = "0.1.0" description = "Encoding utilities" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/encoding" +[tool] +fmt.workspace = true + [dependencies] alexandria_math = { path = "../math" } diff --git a/src/encoding/src/base64.cairo b/src/encoding/src/base64.cairo index 3db80f78..ff3a48d9 100644 --- a/src/encoding/src/base64.cairo +++ b/src/encoding/src/base64.cairo @@ -1,9 +1,5 @@ -use array::ArrayTrait; -use integer::BoundedInt; -use option::OptionTrait; -use traits::{Into, TryInto}; - use alexandria_math::BitShift; +use integer::BoundedInt; const U6_MAX: u128 = 0x3F; diff --git a/src/encoding/src/reversible.cairo b/src/encoding/src/reversible.cairo index f1fde240..6a254178 100644 --- a/src/encoding/src/reversible.cairo +++ b/src/encoding/src/reversible.cairo @@ -1,5 +1,4 @@ use integer::u512; -use traits::DivRem; const SELECT_BYTE: u16 = 0x100; const SELECT_BIT: u8 = 0b10; diff --git a/src/encoding/src/tests/base64_test.cairo b/src/encoding/src/tests/base64_test.cairo index 4ecafc01..27602833 100644 --- a/src/encoding/src/tests/base64_test.cairo +++ b/src/encoding/src/tests/base64_test.cairo @@ -1,5 +1,3 @@ -use array::ArrayTrait; -use debug::PrintTrait; use alexandria_encoding::base64::{Base64Encoder, Base64Decoder, Base64UrlEncoder, Base64UrlDecoder}; #[test] diff --git a/src/encoding/src/tests/reversible_test.cairo b/src/encoding/src/tests/reversible_test.cairo index 2d1690ed..637f4f30 100644 --- a/src/encoding/src/tests/reversible_test.cairo +++ b/src/encoding/src/tests/reversible_test.cairo @@ -1,8 +1,6 @@ use alexandria_encoding::reversible::{ReversibleBits, ReversibleBytes}; use integer::u512; -use debug::PrintTrait; - #[test] #[available_gas(1000000)] fn test_reverse_bytes_u8() { diff --git a/src/linalg/Scarb.toml b/src/linalg/Scarb.toml index 5ea79308..c342a2a8 100644 --- a/src/linalg/Scarb.toml +++ b/src/linalg/Scarb.toml @@ -3,3 +3,6 @@ name = "alexandria_linalg" version = "0.1.0" description = "A set of linear algebra libraries and algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/linalg" + +[tool] +fmt.workspace = true diff --git a/src/linalg/src/dot.cairo b/src/linalg/src/dot.cairo index 6ae5353b..2cb207f0 100644 --- a/src/linalg/src/dot.cairo +++ b/src/linalg/src/dot.cairo @@ -1,6 +1,4 @@ -use core::option::OptionTrait; //! Dot product of two arrays -use array::SpanTrait; /// Compute the dot product for 2 given arrays. /// # Arguments diff --git a/src/linalg/src/tests/dot_test.cairo b/src/linalg/src/tests/dot_test.cairo index b71ddd70..bb70ef46 100644 --- a/src/linalg/src/tests/dot_test.cairo +++ b/src/linalg/src/tests/dot_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_linalg::dot::dot; #[test] diff --git a/src/math/Scarb.toml b/src/math/Scarb.toml index 94a810c4..24e03a68 100644 --- a/src/math/Scarb.toml +++ b/src/math/Scarb.toml @@ -4,6 +4,9 @@ version = "0.2.0" description = "A set of math libraries and algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/math" +[tool] +fmt.workspace = true + [dependencies] # dependency due to ArrayTraitExt::concat in ed25519.cairo alexandria_data_structures = { path = "../data_structures" } diff --git a/src/math/src/collatz_sequence.cairo b/src/math/src/collatz_sequence.cairo index 35a640f5..659a202b 100644 --- a/src/math/src/collatz_sequence.cairo +++ b/src/math/src/collatz_sequence.cairo @@ -1,5 +1,4 @@ //! # Collatz Sequence -use array::ArrayTrait; /// Generates the Collatz sequence for a given number. /// # Arguments diff --git a/src/math/src/ed25519.cairo b/src/math/src/ed25519.cairo index e43bcb31..cb19ebd1 100644 --- a/src/math/src/ed25519.cairo +++ b/src/math/src/ed25519.cairo @@ -1,12 +1,9 @@ -use array::{ArrayTrait, SpanTrait}; -use traits::{Into, TryInto}; -use option::OptionTrait; -use integer::u512; -use alexandria_math::sha512::{sha512, SHA512_LEN}; +use alexandria_data_structures::array_ext::ArrayTraitExt; use alexandria_math::mod_arithmetics::{ add_mod, sub_mod, mult_mod, div_mod, pow_mod, add_inverse_mod }; -use alexandria_data_structures::array_ext::ArrayTraitExt; +use alexandria_math::sha512::{sha512, SHA512_LEN}; +use integer::u512; // As per RFC-8032: https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.7 // Variable namings in this function refer to naming in the RFC diff --git a/src/math/src/fast_power.cairo b/src/math/src/fast_power.cairo index 4d5df34f..23949be0 100644 --- a/src/math/src/fast_power.cairo +++ b/src/math/src/fast_power.cairo @@ -1,7 +1,4 @@ //! # Fast power algorithm -use array::ArrayTrait; -use option::OptionTrait; -use traits::{Into, TryInto}; // Calculate the ( base ^ power ) mod modulus // using the fast powering algorithm # Arguments diff --git a/src/math/src/gcd_of_n_numbers.cairo b/src/math/src/gcd_of_n_numbers.cairo index 81390272..d1ef7fe5 100644 --- a/src/math/src/gcd_of_n_numbers.cairo +++ b/src/math/src/gcd_of_n_numbers.cairo @@ -1,6 +1,4 @@ //! # GCD for N numbers -use array::SpanTrait; -use option::OptionTrait; // Calculate the greatest common dividor for n numbers // # Arguments diff --git a/src/math/src/karatsuba.cairo b/src/math/src/karatsuba.cairo index b8ef5f8f..d3e347ce 100644 --- a/src/math/src/karatsuba.cairo +++ b/src/math/src/karatsuba.cairo @@ -1,8 +1,5 @@ //! # Karatsuba Multiplication. use cmp::max; -use traits::Into; - -// Internal imports. use super::{pow, count_digits_of_base}; /// Algorithm to multiply two numbers in O(n^1.6) running time diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index 06519912..cbceb32b 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -1,10 +1,25 @@ -use option::OptionTrait; -use traits::Into; +mod aliquot_sum; +mod armstrong_number; +mod collatz_sequence; +mod ed25519; +mod extended_euclidean_algorithm; +mod fast_power; +mod fibonacci; +mod gcd_of_n_numbers; +mod karatsuba; +mod keccak256; +mod mod_arithmetics; +mod perfect_number; +mod sha256; +mod sha512; + +#[cfg(test)] +mod tests; +mod zellers_congruence; use integer::{ u8_wide_mul, u16_wide_mul, u32_wide_mul, u64_wide_mul, u128_wide_mul, u256_overflow_mul, BoundedInt }; -use debug::PrintTrait; /// Raise a number to a power. /// O(log n) time complexity. @@ -221,21 +236,3 @@ impl U256BitRotate of BitRotate { } } -mod aliquot_sum; -mod armstrong_number; -mod collatz_sequence; -mod ed25519; -mod extended_euclidean_algorithm; -mod fast_power; -mod fibonacci; -mod gcd_of_n_numbers; -mod karatsuba; -mod mod_arithmetics; -mod perfect_number; -mod sha256; -mod sha512; -mod zellers_congruence; -mod keccak256; - -#[cfg(test)] -mod tests; diff --git a/src/math/src/mod_arithmetics.cairo b/src/math/src/mod_arithmetics.cairo index d9b472bb..280603da 100644 --- a/src/math/src/mod_arithmetics.cairo +++ b/src/math/src/mod_arithmetics.cairo @@ -1,5 +1,3 @@ -use option::OptionTrait; -use traits::{Into, TryInto}; use integer::u512; /// Function that performs modular addition. diff --git a/src/math/src/perfect_number.cairo b/src/math/src/perfect_number.cairo index 910e6a64..6df69a5e 100644 --- a/src/math/src/perfect_number.cairo +++ b/src/math/src/perfect_number.cairo @@ -1,5 +1,4 @@ //! # Perfect Number. -use array::ArrayTrait; /// Algorithm to determine if a number is a perfect number /// # Arguments diff --git a/src/math/src/sha256.cairo b/src/math/src/sha256.cairo index a6e2c13b..a0d7a8a9 100644 --- a/src/math/src/sha256.cairo +++ b/src/math/src/sha256.cairo @@ -1,9 +1,4 @@ -use core::traits::TryInto; -use array::{ArrayTrait, SpanTrait}; -use clone::Clone; use integer::{u32_wrapping_add, BoundedInt}; -use option::OptionTrait; -use traits::Into; fn ch(x: u32, y: u32, z: u32) -> u32 { (x & y) ^ ((x ^ BoundedInt::::max().into()) & z) diff --git a/src/math/src/sha512.cairo b/src/math/src/sha512.cairo index 06f348f6..5e1550e6 100644 --- a/src/math/src/sha512.cairo +++ b/src/math/src/sha512.cairo @@ -1,8 +1,4 @@ use integer::{u64_wrapping_add, bitwise, BoundedInt}; -use traits::{Into, TryInto}; -use option::OptionTrait; -use array::{ArrayTrait, SpanTrait}; - use super::BitShift; // Variable naming is compliant to RFC-6234 (https://datatracker.ietf.org/doc/html/rfc6234) diff --git a/src/math/src/tests.cairo b/src/math/src/tests.cairo index 46ba9084..a0032bc8 100644 --- a/src/math/src/tests.cairo +++ b/src/math/src/tests.cairo @@ -12,5 +12,5 @@ mod mod_arithmetics_test; mod perfect_number_test; mod sha256_test; mod sha512_test; -mod zellers_congruence_test; mod test_keccak256; +mod zellers_congruence_test; diff --git a/src/math/src/tests/collatz_sequence_test.cairo b/src/math/src/tests/collatz_sequence_test.cairo index cabb0e28..0884ae1b 100644 --- a/src/math/src/tests/collatz_sequence_test.cairo +++ b/src/math/src/tests/collatz_sequence_test.cairo @@ -1,7 +1,3 @@ -// Core library imports. -use option::OptionTrait; -use array::ArrayTrait; - use alexandria_math::collatz_sequence::sequence; #[test] diff --git a/src/math/src/tests/ed25519_test.cairo b/src/math/src/tests/ed25519_test.cairo index 4b09e1bd..190a3f6c 100644 --- a/src/math/src/tests/ed25519_test.cairo +++ b/src/math/src/tests/ed25519_test.cairo @@ -1,9 +1,4 @@ -use core::option::OptionTrait; -use core::array::SpanTrait; use alexandria_math::ed25519::{p, Point, verify_signature, SpanU8TryIntoPoint}; -use array::ArrayTrait; -use debug::PrintTrait; -use traits::TryInto; fn gen_msg() -> Span { // Hello World! diff --git a/src/math/src/tests/gcd_of_n_numbers_test.cairo b/src/math/src/tests/gcd_of_n_numbers_test.cairo index 4d78c471..97f0ce46 100644 --- a/src/math/src/tests/gcd_of_n_numbers_test.cairo +++ b/src/math/src/tests/gcd_of_n_numbers_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_math::gcd_of_n_numbers::gcd; #[test] diff --git a/src/math/src/tests/perfect_number_test.cairo b/src/math/src/tests/perfect_number_test.cairo index f9c39086..c00cb08c 100644 --- a/src/math/src/tests/perfect_number_test.cairo +++ b/src/math/src/tests/perfect_number_test.cairo @@ -1,7 +1,3 @@ -// Core library imports. -use option::OptionTrait; -use array::ArrayTrait; - use alexandria_math::perfect_number::{is_perfect_number, perfect_numbers}; // is_perfect_number diff --git a/src/math/src/tests/sha256_test.cairo b/src/math/src/tests/sha256_test.cairo index 48237a5f..3f528977 100644 --- a/src/math/src/tests/sha256_test.cairo +++ b/src/math/src/tests/sha256_test.cairo @@ -1,5 +1,4 @@ use alexandria_math::sha256; -use array::ArrayTrait; #[test] #[available_gas(2000000000)] @@ -90,7 +89,7 @@ fn sha256_lorem_ipsum_test() { // Lorem ipsum, or lsipsum as it is sometimes known, is dummy text used in laying out print, graphic or web designs. // The passage is attributed to an unknown typesetter in the 15th century who is thought to have scrambled parts of // Cicero's De Finibus Bonorum et Malorum for use in a type specimen book. It usually begins with - let mut input = ArrayTrait::::new(); + let mut input = array![]; input.append('L'); input.append('o'); input.append('r'); @@ -450,8 +449,6 @@ fn sha256_lorem_ipsum_test() { assert(*result[30] == 0xEA, 'invalid result'); assert(*result[31] == 0x44, 'invalid result'); } - -use debug::PrintTrait; #[test] #[available_gas(10_000_000_000)] fn sha256_url() { diff --git a/src/math/src/tests/sha512_test.cairo b/src/math/src/tests/sha512_test.cairo index f0fa0dc5..edf1537b 100644 --- a/src/math/src/tests/sha512_test.cairo +++ b/src/math/src/tests/sha512_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_math::sha512::{WordOperations, sha512, Word64, Word64WordOperations}; fn get_lorem_ipsum() -> Array { diff --git a/src/math/src/tests/test_keccak256.cairo b/src/math/src/tests/test_keccak256.cairo index 5929d38e..5c839d7f 100644 --- a/src/math/src/tests/test_keccak256.cairo +++ b/src/math/src/tests/test_keccak256.cairo @@ -1,5 +1,4 @@ use alexandria_math::keccak256::keccak256; -use debug::PrintTrait; #[test] #[available_gas(2000000)] diff --git a/src/math/src/tests/zellers_congruence_test.cairo b/src/math/src/tests/zellers_congruence_test.cairo index 8cf225bf..e5953812 100644 --- a/src/math/src/tests/zellers_congruence_test.cairo +++ b/src/math/src/tests/zellers_congruence_test.cairo @@ -1,7 +1,5 @@ -use option::OptionTrait; use alexandria_math::zellers_congruence::day_of_week; - // Define a test case function to avoid code duplication. fn test_case(day: u128, month: u128, year: u128, expected: u128, error_expected: bool) { let day = day_of_week(day, month, year); diff --git a/src/merkle_tree/Scarb.toml b/src/merkle_tree/Scarb.toml index 566a04ca..dc148cf9 100644 --- a/src/merkle_tree/Scarb.toml +++ b/src/merkle_tree/Scarb.toml @@ -4,5 +4,5 @@ version = "0.1.0" description = "Merkle tree related stuff" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/src/merkle_tree" -[dependencies] -starknet.workspace = true +[tool] +fmt.workspace = true diff --git a/src/merkle_tree/src/merkle_tree.cairo b/src/merkle_tree/src/merkle_tree.cairo index 83fbd6cd..ebfb03e0 100644 --- a/src/merkle_tree/src/merkle_tree.cairo +++ b/src/merkle_tree/src/merkle_tree.cairo @@ -40,8 +40,8 @@ struct Hasher {} /// Hasher impls. mod pedersen { - use pedersen::PedersenTrait; use hash::HashStateTrait; + use pedersen::PedersenTrait; use super::{Hasher, HasherTrait}; impl PedersenHasherImpl of HasherTrait { @@ -57,8 +57,8 @@ mod pedersen { } mod poseidon { - use poseidon::PoseidonTrait; use hash::HashStateTrait; + use poseidon::PoseidonTrait; use super::{Hasher, HasherTrait}; impl PoseidonHasherImpl of HasherTrait { diff --git a/src/merkle_tree/src/storage_proof.cairo b/src/merkle_tree/src/storage_proof.cairo index dd935146..388ba70c 100644 --- a/src/merkle_tree/src/storage_proof.cairo +++ b/src/merkle_tree/src/storage_proof.cairo @@ -1,6 +1,6 @@ +use hash::HashStateTrait; use pedersen::PedersenTrait; use poseidon::PoseidonTrait; -use hash::HashStateTrait; #[derive(Drop)] struct BinaryNode { diff --git a/src/numeric/Scarb.toml b/src/numeric/Scarb.toml index 01c2fc42..61defe63 100644 --- a/src/numeric/Scarb.toml +++ b/src/numeric/Scarb.toml @@ -3,3 +3,6 @@ name = "alexandria_numeric" version = "0.1.0" description = "A set of numerical analysis libraries and algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/main/src/numeric" + +[tool] +fmt.workspace = true diff --git a/src/numeric/src/cumsum.cairo b/src/numeric/src/cumsum.cairo index ddb92a7a..6732e0c8 100644 --- a/src/numeric/src/cumsum.cairo +++ b/src/numeric/src/cumsum.cairo @@ -1,6 +1,4 @@ //! The cumulative sum of the elements. -use array::{SpanTrait, ArrayTrait}; -use option::OptionTrait; /// Compute the cumulative sum of a sequence. /// # Arguments diff --git a/src/numeric/src/diff.cairo b/src/numeric/src/diff.cairo index 76251801..2aa68312 100644 --- a/src/numeric/src/diff.cairo +++ b/src/numeric/src/diff.cairo @@ -1,7 +1,4 @@ -use core::option::OptionTrait; //! The discrete difference of the elements. -use array::{ArrayTrait, SpanTrait}; -use zeroable::Zeroable; /// Compute the discrete difference of a sorted sequence. /// # Arguments diff --git a/src/numeric/src/interpolate.cairo b/src/numeric/src/interpolate.cairo index 123346a0..e0ebae95 100644 --- a/src/numeric/src/interpolate.cairo +++ b/src/numeric/src/interpolate.cairo @@ -1,7 +1,4 @@ //! One-dimensional linear interpolation for monotonically increasing sample points. -use array::SpanTrait; -use traits::Into; -use zeroable::Zeroable; #[derive(Serde, Copy, Drop, PartialEq)] enum Interpolation { diff --git a/src/numeric/src/lib.cairo b/src/numeric/src/lib.cairo index 4355bb2e..777ad2f4 100644 --- a/src/numeric/src/lib.cairo +++ b/src/numeric/src/lib.cairo @@ -1,7 +1,7 @@ mod cumsum; mod diff; mod interpolate; -mod trapezoidal_rule; #[cfg(test)] mod tests; +mod trapezoidal_rule; diff --git a/src/numeric/src/tests/cumsum_test.cairo b/src/numeric/src/tests/cumsum_test.cairo index de4c51af..2f0315a0 100644 --- a/src/numeric/src/tests/cumsum_test.cairo +++ b/src/numeric/src/tests/cumsum_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_numeric::cumsum::cumsum; #[test] diff --git a/src/numeric/src/tests/diff_test.cairo b/src/numeric/src/tests/diff_test.cairo index c2df180d..9e8a86de 100644 --- a/src/numeric/src/tests/diff_test.cairo +++ b/src/numeric/src/tests/diff_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_numeric::diff::diff; #[test] diff --git a/src/numeric/src/tests/interpolate_test.cairo b/src/numeric/src/tests/interpolate_test.cairo index ccd93a99..9d701146 100644 --- a/src/numeric/src/tests/interpolate_test.cairo +++ b/src/numeric/src/tests/interpolate_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_numeric::interpolate::{interpolate, Interpolation, Extrapolation}; #[test] diff --git a/src/numeric/src/tests/trapezoidal_rule_test.cairo b/src/numeric/src/tests/trapezoidal_rule_test.cairo index 438dc247..8d30d530 100644 --- a/src/numeric/src/tests/trapezoidal_rule_test.cairo +++ b/src/numeric/src/tests/trapezoidal_rule_test.cairo @@ -1,4 +1,3 @@ -use array::ArrayTrait; use alexandria_numeric::trapezoidal_rule::trapezoidal_rule; #[test] diff --git a/src/numeric/src/trapezoidal_rule.cairo b/src/numeric/src/trapezoidal_rule.cairo index 33cdcbdb..7d370a25 100644 --- a/src/numeric/src/trapezoidal_rule.cairo +++ b/src/numeric/src/trapezoidal_rule.cairo @@ -1,7 +1,4 @@ //! Integrate using the composite trapezoidal rule -use array::SpanTrait; -use zeroable::Zeroable; -use traits::Into; /// Integrate y(x). /// # Arguments diff --git a/src/searching/Scarb.toml b/src/searching/Scarb.toml index bfa40754..d786b5c5 100644 --- a/src/searching/Scarb.toml +++ b/src/searching/Scarb.toml @@ -4,6 +4,9 @@ version = "0.1.0" description = "A set of searching algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/src/searching" +[tool] +fmt.workspace = true + [dependencies] # dependency due to VecTrait usage in dijkstra.cairo alexandria_data_structures = { path = "../data_structures" } diff --git a/src/searching/src/dijkstra.cairo b/src/searching/src/dijkstra.cairo index 036f0a00..9cd56bd5 100644 --- a/src/searching/src/dijkstra.cairo +++ b/src/searching/src/dijkstra.cairo @@ -1,11 +1,5 @@ -//! Dijkstra algorithm using priority queue -use option::OptionTrait; -use box::BoxTrait; -use array::{Array, ArrayTrait, SpanTrait}; -use traits::{Into, Index}; -use dict::Felt252DictTrait; use nullable::FromNullableResult; - +//! Dijkstra algorithm using priority queue #[derive(Copy, Drop)] struct Node { @@ -39,12 +33,12 @@ impl DestructGraph, +Felt252DictValue> of Destruct> { impl GraphImpl of GraphTrait { fn new() -> Graph>> { - Graph { nodes: ArrayTrait::new(), adj_nodes: Default::default() } + Graph { nodes: array![], adj_nodes: Default::default() } } fn add_edge(ref self: Graph>>, source: u32, dest: u32, weight: u128) { let adj_nodes = self.adj_nodes.get(source.into()); - let mut nodes: Array = ArrayTrait::new(); + let mut nodes: Array = array![]; let mut is_null: bool = false; let node = Node { source, dest, weight }; let mut span = match match_nullable(adj_nodes) { @@ -82,8 +76,8 @@ impl GraphImpl of GraphTrait { } fn dijkstra(ref self: Graph>>, source: u32) -> Felt252Dict { - let mut priority_queue = ArrayTrait::new(); - let mut visited_node = ArrayTrait::new(); + let mut priority_queue = array![]; + let mut visited_node = array![]; let mut dist: Felt252Dict = Default::default(); let node_size = self.nodes.len(); let nodes = self.nodes.span(); diff --git a/src/searching/src/tests/binary_search_test.cairo b/src/searching/src/tests/binary_search_test.cairo index 5e662e94..92a392cf 100644 --- a/src/searching/src/tests/binary_search_test.cairo +++ b/src/searching/src/tests/binary_search_test.cairo @@ -1,6 +1,4 @@ use alexandria_searching::binary_search::binary_search; -use array::ArrayTrait; -use option::OptionTrait; #[test] #[available_gas(2000000)] diff --git a/src/searching/src/tests/dijkstra_test.cairo b/src/searching/src/tests/dijkstra_test.cairo index 32918eba..12fe2264 100644 --- a/src/searching/src/tests/dijkstra_test.cairo +++ b/src/searching/src/tests/dijkstra_test.cairo @@ -1,11 +1,5 @@ -use debug::PrintTrait; -use box::BoxTrait; -use traits::Into; -use option::OptionTrait; -use array::{Array, ArrayTrait, SpanTrait}; -use dict::Felt252DictTrait; -use nullable::FromNullableResult; use alexandria_searching::dijkstra::{Graph, Node, GraphTrait}; +use nullable::FromNullableResult; #[test] diff --git a/src/sorting/Scarb.toml b/src/sorting/Scarb.toml index b24f9b5d..c539c184 100644 --- a/src/sorting/Scarb.toml +++ b/src/sorting/Scarb.toml @@ -3,3 +3,6 @@ name = "alexandria_sorting" version = "0.1.0" description = "A set of sorting algorithms" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/src/sorting" + +[tool] +fmt.workspace = true diff --git a/src/sorting/src/bubble_sort.cairo b/src/sorting/src/bubble_sort.cairo index 304c10dc..0913eff1 100644 --- a/src/sorting/src/bubble_sort.cairo +++ b/src/sorting/src/bubble_sort.cairo @@ -1,5 +1,4 @@ //! Bubble sort algorithm -use array::ArrayTrait; // Bubble sort /// # Arguments diff --git a/src/sorting/src/lib.cairo b/src/sorting/src/lib.cairo index 8b0042c6..c231aa33 100644 --- a/src/sorting/src/lib.cairo +++ b/src/sorting/src/lib.cairo @@ -1,12 +1,9 @@ -mod merge_sort; mod bubble_sort; +mod merge_sort; #[cfg(test)] mod tests; -use array::SpanTrait; -use option::OptionTrait; - // Check if two arrays are equal. /// * `a` - The first array. /// * `b` - The second array. diff --git a/src/sorting/src/merge_sort.cairo b/src/sorting/src/merge_sort.cairo index a86ebbbd..973b38f9 100644 --- a/src/sorting/src/merge_sort.cairo +++ b/src/sorting/src/merge_sort.cairo @@ -1,5 +1,4 @@ //! Merge Sort -use array::ArrayTrait; // Merge Sort /// # Arguments diff --git a/src/sorting/src/tests/bubble_sort_test.cairo b/src/sorting/src/tests/bubble_sort_test.cairo index 3d60b603..0f2331ba 100644 --- a/src/sorting/src/tests/bubble_sort_test.cairo +++ b/src/sorting/src/tests/bubble_sort_test.cairo @@ -1,5 +1,3 @@ -use array::ArrayTrait; - use alexandria_sorting::{is_equal, bubble_sort}; #[test] diff --git a/src/sorting/src/tests/merge_sort_test.cairo b/src/sorting/src/tests/merge_sort_test.cairo index 186bd4f8..9cc337bc 100644 --- a/src/sorting/src/tests/merge_sort_test.cairo +++ b/src/sorting/src/tests/merge_sort_test.cairo @@ -1,5 +1,3 @@ -use array::ArrayTrait; - use alexandria_sorting::{is_equal, merge_sort::merge}; #[test] diff --git a/src/storage/Scarb.toml b/src/storage/Scarb.toml index d9a2d730..cd2759fb 100644 --- a/src/storage/Scarb.toml +++ b/src/storage/Scarb.toml @@ -4,5 +4,8 @@ version = "0.2.0" description = "Starknet storage utilities" homepage = "https://github.com/keep-starknet-strange/alexandria/tree/src/storage" +[tool] +fmt.workspace = true + [dependencies] starknet.workspace = true diff --git a/src/storage/src/list.cairo b/src/storage/src/list.cairo index 84e2f356..4379c899 100644 --- a/src/storage/src/list.cairo +++ b/src/storage/src/list.cairo @@ -1,13 +1,10 @@ -use array::ArrayTrait; use integer::U32DivRem; -use option::OptionTrait; use poseidon::poseidon_hash_span; -use starknet::{storage_read_syscall, storage_write_syscall, SyscallResult, SyscallResultTrait}; use starknet::storage_access::{ Store, StorageBaseAddress, storage_address_to_felt252, storage_address_from_base, - storage_address_from_base_and_offset, storage_base_address_from_felt252 + storage_base_address_from_felt252 }; -use traits::{Default, DivRem, IndexView, Into, TryInto}; +use starknet::{storage_read_syscall, storage_write_syscall, SyscallResult, SyscallResultTrait}; const POW2_8: u32 = 256; // 2^8 diff --git a/src/storage/src/tests/list_test.cairo b/src/storage/src/tests/list_test.cairo index 8c51995e..efc7595b 100644 --- a/src/storage/src/tests/list_test.cairo +++ b/src/storage/src/tests/list_test.cairo @@ -1,4 +1,3 @@ -use option::OptionTrait; use starknet::ContractAddress; #[starknet::interface] @@ -24,7 +23,6 @@ trait IAListHolder { #[starknet::contract] mod AListHolder { use alexandria_storage::list::{List, ListTrait}; - use option::OptionTrait; use starknet::ContractAddress; #[storage] @@ -103,12 +101,7 @@ mod AListHolder { #[cfg(test)] mod tests { - use array::{ArrayTrait, SpanTrait}; - use debug::PrintTrait; - use option::OptionTrait; use starknet::{ClassHash, ContractAddress, deploy_syscall, SyscallResultTrait}; - use traits::{Default, Into, TryInto}; - use super::{AListHolder, IAListHolderDispatcher, IAListHolderDispatcherTrait}; fn deploy_mock() -> IAListHolderDispatcher { From 74be1ac188859436a74cad9831d4e073a91aa36f Mon Sep 17 00:00:00 2001 From: Marcus Liotta Date: Thu, 9 Nov 2023 16:29:55 +0100 Subject: [PATCH 6/6] adds alternative implementation for rotate_left on u256 --- src/math/src/lib.cairo | 8 ++------ src/math/src/tests/math_test.cairo | 10 ++++------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/math/src/lib.cairo b/src/math/src/lib.cairo index cbceb32b..b29d91e1 100644 --- a/src/math/src/lib.cairo +++ b/src/math/src/lib.cairo @@ -221,12 +221,8 @@ impl U128BitRotate of BitRotate { impl U256BitRotate of BitRotate { fn rotate_left(x: u256, n: u256) -> u256 { - // TODO(sveamarcus): missing non-zero implementation for u512 - // let word = u256_wide_mul(x, pow(2, n)); - // let (quotient, remainder) = DivRem::div_rem(word, - // u512_as_non_zero(u512{limb0: 0, limb1: 0, limb2: 1, limb3: 0 })); - // (quotient + remainder).try_into().unwrap() - panic_with_felt252('missing impl') + // Alternative solution since we cannot divide u512 yet + BitShift::shl(x, n) + BitShift::shr(x, 256 - n) } fn rotate_right(x: u256, n: u256) -> u256 { diff --git a/src/math/src/tests/math_test.cairo b/src/math/src/tests/math_test.cairo index 95aca929..d90bef4d 100644 --- a/src/math/src/tests/math_test.cairo +++ b/src/math/src/tests/math_test.cairo @@ -170,27 +170,25 @@ fn shl_should_not_overflow() { } #[test] -#[available_gas(2000000)] +#[available_gas(3000000)] fn test_rotl_min() { assert(BitRotate::rotate_left(pow::(2, 7) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 15) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 31) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 63) + 1, 1) == 3, 'invalid result'); assert(BitRotate::rotate_left(pow::(2, 127) + 1, 1) == 3, 'invalid result'); -// TODO(sveamarcus): missing implementation -// assert(BitRotate::rotate_left(pow::(2, 255) + 1, 1) == 3, 'invalid result'); + assert(BitRotate::rotate_left(pow::(2, 255) + 1, 1) == 3, 'invalid result'); } #[test] -#[available_gas(2000000)] +#[available_gas(3000000)] fn test_rotl_max() { assert(BitRotate::rotate_left(0b101, 7) == pow::(2, 7) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 15) == pow::(2, 15) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 31) == pow::(2, 31) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 63) == pow::(2, 63) + 0b10, 'invalid result'); assert(BitRotate::rotate_left(0b101, 127) == pow::(2, 127) + 0b10, 'invalid result'); -// TODO(sveamarcus): missing implementation -// assert(BitRotate::rotate_left(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); + assert(BitRotate::rotate_left(0b101, 255) == pow::(2, 255) + 0b10, 'invalid result'); } #[test]