Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: bitshift takes usize as shift type #1001

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/evm/src/instructions/comparison_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
if i > 31 {
return self.stack.push(0);
}
let i: usize = i.try_into().unwrap(); // Safe because i <= 31

// Right shift value by offset bits and then take the least significant byte.
let result = x.shr((31 - i) * 8) & 0xFF;
Expand All @@ -150,7 +151,7 @@ pub impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
if shift > 255 {
return self.stack.push(0);
}

let shift: usize = shift.try_into().unwrap(); // Safe because shift <= 255
let result = val.wrapping_shl(shift);
self.stack.push(result)
}
Expand All @@ -163,6 +164,11 @@ pub impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
let shift = *popped[0];
let value = *popped[1];

// if shift is bigger than 255 return 0
if shift > 255 {
return self.stack.push(0);
}
let shift: usize = shift.try_into().unwrap(); // Safe because shift <= 255
let result = value.wrapping_shr(shift);
self.stack.push(result)
}
Expand All @@ -187,6 +193,7 @@ pub impl ComparisonAndBitwiseOperations of ComparisonAndBitwiseOperationsTrait {
if (shift >= 256) {
self.stack.push(sign)
} else {
let shift: usize = shift.try_into().unwrap(); // Safe because shift <= 256
// XORing with sign before and after the shift propagates the sign bit of the operation
let result = (sign ^ value.value).shr(shift) ^ sign;
self.stack.push(result)
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/src/memory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl MemoryImpl of MemoryTrait {

// First erase byte value at offset, then set the new value using bitwise ops
let word: u128 = self.items.get(chunk_index.into());
let new_word = (word & ~mask) | (value.into().shl(right_offset.into() * 8));
let new_word = (word & ~mask) | (value.into().shl(right_offset * 8));
self.items.insert(chunk_index.into(), new_word);
}

Expand Down
261 changes: 261 additions & 0 deletions crates/utils/src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,267 @@ pub const POW_2: [
0x80000000000000000000000000000000
];

pub const POW_2_256: [
u256
; 256] = [
0x1,
0x2,
0x4,
0x8,
0x10,
0x20,
0x40,
0x80,
0x100,
0x200,
0x400,
0x800,
0x1000,
0x2000,
0x4000,
0x8000,
0x10000,
0x20000,
0x40000,
0x80000,
0x100000,
0x200000,
0x400000,
0x800000,
0x1000000,
0x2000000,
0x4000000,
0x8000000,
0x10000000,
0x20000000,
0x40000000,
0x80000000,
0x100000000,
0x200000000,
0x400000000,
0x800000000,
0x1000000000,
0x2000000000,
0x4000000000,
0x8000000000,
0x10000000000,
0x20000000000,
0x40000000000,
0x80000000000,
0x100000000000,
0x200000000000,
0x400000000000,
0x800000000000,
0x1000000000000,
0x2000000000000,
0x4000000000000,
0x8000000000000,
0x10000000000000,
0x20000000000000,
0x40000000000000,
0x80000000000000,
0x100000000000000,
0x200000000000000,
0x400000000000000,
0x800000000000000,
0x1000000000000000,
0x2000000000000000,
0x4000000000000000,
0x8000000000000000,
0x10000000000000000,
0x20000000000000000,
0x40000000000000000,
0x80000000000000000,
0x100000000000000000,
0x200000000000000000,
0x400000000000000000,
0x800000000000000000,
0x1000000000000000000,
0x2000000000000000000,
0x4000000000000000000,
0x8000000000000000000,
0x10000000000000000000,
0x20000000000000000000,
0x40000000000000000000,
0x80000000000000000000,
0x100000000000000000000,
0x200000000000000000000,
0x400000000000000000000,
0x800000000000000000000,
0x1000000000000000000000,
0x2000000000000000000000,
0x4000000000000000000000,
0x8000000000000000000000,
0x10000000000000000000000,
0x20000000000000000000000,
0x40000000000000000000000,
0x80000000000000000000000,
0x100000000000000000000000,
0x200000000000000000000000,
0x400000000000000000000000,
0x800000000000000000000000,
0x1000000000000000000000000,
0x2000000000000000000000000,
0x4000000000000000000000000,
0x8000000000000000000000000,
0x10000000000000000000000000,
0x20000000000000000000000000,
0x40000000000000000000000000,
0x80000000000000000000000000,
0x100000000000000000000000000,
0x200000000000000000000000000,
0x400000000000000000000000000,
0x800000000000000000000000000,
0x1000000000000000000000000000,
0x2000000000000000000000000000,
0x4000000000000000000000000000,
0x8000000000000000000000000000,
0x10000000000000000000000000000,
0x20000000000000000000000000000,
0x40000000000000000000000000000,
0x80000000000000000000000000000,
0x100000000000000000000000000000,
0x200000000000000000000000000000,
0x400000000000000000000000000000,
0x800000000000000000000000000000,
0x1000000000000000000000000000000,
0x2000000000000000000000000000000,
0x4000000000000000000000000000000,
0x8000000000000000000000000000000,
0x10000000000000000000000000000000,
0x20000000000000000000000000000000,
0x40000000000000000000000000000000,
0x80000000000000000000000000000000,
0x100000000000000000000000000000000,
0x200000000000000000000000000000000,
0x400000000000000000000000000000000,
0x800000000000000000000000000000000,
0x1000000000000000000000000000000000,
0x2000000000000000000000000000000000,
0x4000000000000000000000000000000000,
0x8000000000000000000000000000000000,
0x10000000000000000000000000000000000,
0x20000000000000000000000000000000000,
0x40000000000000000000000000000000000,
0x80000000000000000000000000000000000,
0x100000000000000000000000000000000000,
0x200000000000000000000000000000000000,
0x400000000000000000000000000000000000,
0x800000000000000000000000000000000000,
0x1000000000000000000000000000000000000,
0x2000000000000000000000000000000000000,
0x4000000000000000000000000000000000000,
0x8000000000000000000000000000000000000,
0x10000000000000000000000000000000000000,
0x20000000000000000000000000000000000000,
0x40000000000000000000000000000000000000,
0x80000000000000000000000000000000000000,
0x100000000000000000000000000000000000000,
0x200000000000000000000000000000000000000,
0x400000000000000000000000000000000000000,
0x800000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000000000000000,
0x10000000000000000000000000000000000000000000000000000000000000,
0x20000000000000000000000000000000000000000000000000000000000000,
0x40000000000000000000000000000000000000000000000000000000000000,
0x80000000000000000000000000000000000000000000000000000000000000,
0x100000000000000000000000000000000000000000000000000000000000000,
0x200000000000000000000000000000000000000000000000000000000000000,
0x400000000000000000000000000000000000000000000000000000000000000,
0x800000000000000000000000000000000000000000000000000000000000000,
0x1000000000000000000000000000000000000000000000000000000000000000,
0x2000000000000000000000000000000000000000000000000000000000000000,
0x4000000000000000000000000000000000000000000000000000000000000000,
0x8000000000000000000000000000000000000000000000000000000000000000,
];

pub const POW_2_0: u128 = 0x1;
pub const POW_2_8: u128 = 0x100;
pub const POW_2_16: u128 = 0x10000;
Expand Down
2 changes: 1 addition & 1 deletion crates/utils/src/crypto/blake2_compress.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn rotate_right(value: u64, n: u32) -> u64 {
let bits = BitSize::<u64>::bits(); // The number of bits in a u64
let n = n % bits; // Ensure n is less than 64

let res = value.wrapping_shr(n.into()) | value.wrapping_shl((bits - n).into());
let res = value.wrapping_shr(n) | value.wrapping_shl((bits - n));
res
}
}
Expand Down
Loading
Loading