Skip to content

Commit

Permalink
Refactor flip_endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
fmkra committed Dec 22, 2023
1 parent 33c02e0 commit 6a39b8d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/common/flip_endianness.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,58 @@ impl FlipEndiannessU32 of FlipEndiannessTrait<u32> {

impl FlipEndiannessU256 of FlipEndiannessTrait<u256> {
fn flip_endianness(self: u256) -> u256 {
let mut data = ArrayTrait::<u32>::new();
data.append_big_endian(self);
data.span().from_span_le()
(self % 0x100) * 0x100000000000000000000000000000000000000000000000000000000000000
+ (self / 0x100 % 0x100) * 0x1000000000000000000000000000000000000000000000000000000000000
+ (self / 0x10000 % 0x100) * 0x10000000000000000000000000000000000000000000000000000000000
+ (self / 0x1000000 % 0x100) * 0x100000000000000000000000000000000000000000000000000000000
+ (self / 0x100000000 % 0x100) * 0x1000000000000000000000000000000000000000000000000000000
+ (self / 0x10000000000 % 0x100) * 0x10000000000000000000000000000000000000000000000000000
+ (self / 0x1000000000000 % 0x100) * 0x100000000000000000000000000000000000000000000000000
+ (self / 0x100000000000000 % 0x100) * 0x1000000000000000000000000000000000000000000000000
+ (self / 0x10000000000000000 % 0x100) * 0x10000000000000000000000000000000000000000000000
+ (self / 0x1000000000000000000 % 0x100) * 0x100000000000000000000000000000000000000000000
+ (self / 0x100000000000000000000 % 0x100) * 0x1000000000000000000000000000000000000000000
+ (self / 0x10000000000000000000000 % 0x100) * 0x10000000000000000000000000000000000000000
+ (self / 0x1000000000000000000000000 % 0x100) * 0x100000000000000000000000000000000000000
+ (self / 0x100000000000000000000000000 % 0x100) * 0x1000000000000000000000000000000000000
+ (self / 0x10000000000000000000000000000 % 0x100) * 0x10000000000000000000000000000000000
+ (self / 0x1000000000000000000000000000000 % 0x100) * 0x100000000000000000000000000000000
+ (self / 0x100000000000000000000000000000000 % 0x100) * 0x1000000000000000000000000000000
+ (self / 0x10000000000000000000000000000000000 % 0x100) * 0x10000000000000000000000000000
+ (self / 0x1000000000000000000000000000000000000 % 0x100) * 0x100000000000000000000000000
+ (self / 0x100000000000000000000000000000000000000 % 0x100) * 0x1000000000000000000000000
+ (self / 0x10000000000000000000000000000000000000000 % 0x100) * 0x10000000000000000000000
+ (self / 0x1000000000000000000000000000000000000000000 % 0x100) * 0x100000000000000000000
+ (self / 0x100000000000000000000000000000000000000000000 % 0x100) * 0x1000000000000000000
+ (self / 0x10000000000000000000000000000000000000000000000 % 0x100) * 0x10000000000000000
+ (self / 0x1000000000000000000000000000000000000000000000000 % 0x100) * 0x100000000000000
+ (self / 0x100000000000000000000000000000000000000000000000000 % 0x100) * 0x1000000000000
+ (self / 0x10000000000000000000000000000000000000000000000000000 % 0x100) * 0x10000000000
+ (self / 0x1000000000000000000000000000000000000000000000000000000 % 0x100) * 0x100000000
+ (self / 0x100000000000000000000000000000000000000000000000000000000 % 0x100) * 0x1000000
+ (self / 0x10000000000000000000000000000000000000000000000000000000000 % 0x100) * 0x10000
+ (self / 0x1000000000000000000000000000000000000000000000000000000000000 % 0x100) * 0x100
+ (self / 0x100000000000000000000000000000000000000000000000000000000000000 % 0x100)
}
}

impl FlipEndiannessU128 of FlipEndiannessTrait<u128> {
fn flip_endianness(self: u128) -> u128 {
let mut data = ArrayTrait::<u32>::new();
data.append_big_endian(self);
data.span().from_span_le()
(self % 0x100) * 0x1000000000000000000000000000000
+ (self / 0x100 % 0x100) * 0x10000000000000000000000000000
+ (self / 0x10000 % 0x100) * 0x100000000000000000000000000
+ (self / 0x1000000 % 0x100) * 0x1000000000000000000000000
+ (self / 0x100000000 % 0x100) * 0x10000000000000000000000
+ (self / 0x10000000000 % 0x100) * 0x100000000000000000000
+ (self / 0x1000000000000 % 0x100) * 0x1000000000000000000
+ (self / 0x100000000000000 % 0x100) * 0x10000000000000000
+ (self / 0x10000000000000000 % 0x100) * 0x100000000000000
+ (self / 0x1000000000000000000 % 0x100) * 0x1000000000000
+ (self / 0x100000000000000000000 % 0x100) * 0x10000000000
+ (self / 0x10000000000000000000000 % 0x100) * 0x100000000
+ (self / 0x1000000000000000000000000 % 0x100) * 0x1000000
+ (self / 0x100000000000000000000000000 % 0x100) * 0x10000
+ (self / 0x10000000000000000000000000000 % 0x100) * 0x100
+ (self / 0x1000000000000000000000000000000 % 0x100)
}
}
16 changes: 16 additions & 0 deletions src/common/tests/test_flip_endianness.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,69 @@ use cairo_verifier::common::flip_endianness::FlipEndiannessTrait;
fn test_flip_endianness_0() {
let le_val: u128 = 0x10000000000000000000009123092121;
assert(le_val.flip_endianness() == 0x21210923910000000000000000000010, 'Invalid value');
assert(le_val == 0x10000000000000000000009123092121, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_1() {
let le_val: u128 = 0x9123092121;
assert(le_val.flip_endianness().flip_endianness() == le_val, 'Invalid value');
assert(le_val == 0x9123092121, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_2() {
let le_val: u128 = 0x98071087308702187408710847087;
assert(le_val.flip_endianness().flip_endianness() == le_val, 'Invalid value');
assert(le_val == 0x98071087308702187408710847087, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_3() {
let le_val: u128 = 0xABCDEF0123456789ABCDEF0123456789;
assert(le_val.flip_endianness() == 0x8967452301EFCDAB8967452301EFCDAB, 'Invalid value');
assert(le_val == 0xABCDEF0123456789ABCDEF0123456789, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_4() {
let le_val: u128 = 0x00000000000000000000ABCDEF012345;
assert(le_val.flip_endianness() == 0x452301EFCDAB00000000000000000000, 'Invalid value');
assert(le_val == 0x00000000000000000000ABCDEF012345, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_5() {
let le_val: u128 = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
assert(le_val.flip_endianness() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'Invalid value');
assert(le_val == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_6() {
let le_val: u128 = 0x00000000000000000000000000000000;
assert(le_val.flip_endianness() == 0x00000000000000000000000000000000, 'Invalid value');
assert(le_val == 0x00000000000000000000000000000000, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_7() {
let le_val: u128 = 0x1234567890ABCDEF1234567890ABCDEF;
assert(le_val.flip_endianness().flip_endianness() == le_val, 'Invalid value');
assert(le_val == 0x1234567890ABCDEF1234567890ABCDEF, 'Original value has mutated');
}

#[test]
#[available_gas(9999999999)]
fn test_flip_endianness_256() {
let le_val: u256 = 0xb8c727aa44f32839b875a9323a8f204a330c70f29ca91581be9207a703f70d74;
let be_val: u256 = 0x740df703a70792be8115a99cf2700c334a208f3a32a975b83928f344aa27c7b8;
assert(le_val.flip_endianness() == be_val, 'Invalid value');
}

0 comments on commit 6a39b8d

Please sign in to comment.