Skip to content

Commit

Permalink
Use a better algorithm for getting base16 len (#390)
Browse files Browse the repository at this point in the history
This uses binary search on the number of digits instead of a linear search
  • Loading branch information
amanusk authored Dec 14, 2022
1 parent bd9fd23 commit f81bab3
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions contracts/starknet/lib/eip712.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from starkware.cairo.common.cairo_keccak.keccak import (
from starkware.cairo.common.uint256 import (
Uint256,
uint256_eq,
uint256_le,
uint256_mul,
uint256_unsigned_div_rem,
)
Expand Down Expand Up @@ -525,13 +526,44 @@ func _get_base16_len{range_check_ptr}(num: Uint256) -> (res: felt) {
let (is_eq) = uint256_eq(num, Uint256(0, 0));
if (is_eq == 1) {
return (0,);
} else {
// Divide by 16
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16, 0));

}
let (lt) = uint256_le(Uint256(16 ** 32, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 32, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 32,);
}
let (lt) = uint256_le(Uint256(16 ** 16, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 16, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 16,);
}
let (lt) = uint256_le(Uint256(16 ** 8, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 8, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 8,);
}
let (lt) = uint256_le(Uint256(16 ** 4, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 4, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 4,);
}
let (lt) = uint256_le(Uint256(16 ** 2, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 2, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 2,);
}
let (lt) = uint256_le(Uint256(16 ** 1, 0), num);
if (lt == 1) {
let (divided, _) = uint256_unsigned_div_rem(num, Uint256(16 ** 1, 0));
let (res_len) = _get_base16_len(divided);
return (res_len + 1,);
}
return (1,);
}

// Computes `base ** exp` where `base` and `exp` are both `felts` and returns the result as a `Uint256`.
Expand Down

0 comments on commit f81bab3

Please sign in to comment.