Skip to content

Commit

Permalink
[fix] used div_rem when using / and % in same value (keep-starknet-st…
Browse files Browse the repository at this point in the history
…range#219)

<!-- enter the gh issue after hash -->

- [x] issue keep-starknet-strange#206
- [x] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [x] code change includes tests

<!-- PR description below -->
This PR will add the usage of div_rem when using / and % in the same
value.

*Pending:* in utils.cairo file, the ``` fast_power ``` function is
giving problems for using div_rem.

Co-authored-by: Brandon Roberts <brandonjroberts22@gmail.com>
  • Loading branch information
2 people authored and j1mbo64 committed Sep 18, 2024
1 parent 569d3db commit 304e614
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
5 changes: 3 additions & 2 deletions packages/engine/src/signature/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ pub fn check_signature_encoding<T, +Drop<T>>(
let s_value = u256_from_byte_array_with_offset(sig_bytes, s_offset, 32);
let mut half_order = Secp256Trait::<Secp256k1Point>::get_curve_size();

let carry = half_order.high % 2;
let (half_order_high_upper, half_order_high_lower) = DivRem::div_rem(half_order.high, 2);
let carry = half_order_high_lower;
half_order.low = (half_order.low / 2) + (carry * (constants::MAX_U128 / 2 + 1));
half_order.high /= 2;
half_order.high = half_order_high_upper;

if s_value > half_order {
return Result::Err('sig not canonical high S value');
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ pub fn felt252_to_byte_array(value: felt252) -> ByteArray {
let mut byte_array = "";
let mut valueU256: u256 = value.into();
while valueU256 != 0 {
byte_array.append_byte((valueU256 % byte_shift).try_into().unwrap());
valueU256 /= byte_shift;
let (value_upper, value_lower) = DivRem::div_rem(valueU256, byte_shift);
byte_array.append_byte(value_lower.try_into().unwrap());
valueU256 = value_upper;
};
byte_array.rev()
}
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/bytecode.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ pub fn bytecode_to_hex(bytecode: @ByteArray) -> ByteArray {
return "0x00";
}
while i != bytecode_len {
let upper_half_byte = bytecode[i] / half_byte_shift;
let lower_half_byte = bytecode[i] % half_byte_shift;
let (upper_half_byte, lower_half_byte) = DivRem::div_rem(bytecode[i], half_byte_shift);
let upper_half: u8 = if upper_half_byte < 10 {
upper_half_byte + zero
} else {
Expand Down
3 changes: 1 addition & 2 deletions packages/utils/src/hex.cairo
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub fn int_to_hex(value: u8) -> felt252 {
let half_byte_shift = 16;
let byte_shift = 256;
let upper_half_value = value / half_byte_shift;
let lower_half_value = value % half_byte_shift;

let (upper_half_value, lower_half_value) = DivRem::div_rem(value, half_byte_shift);
let upper_half: u8 = if upper_half_value < 10 {
upper_half_value + '0'
} else {
Expand Down

0 comments on commit 304e614

Please sign in to comment.