Skip to content

Commit

Permalink
refactor: Improve input validation in bootstrap.sh and refactor bit_t…
Browse files Browse the repository at this point in the history
…raits.rs (#9406)

- Improved the input validation logic for the 'clean' command in
`bootstrap.sh` to accept various forms of confirmation such as 'y', 'Y',
'yes', and 'YES'.
- Refactored `bit_traits.rs` to reduce code duplication and improve
efficiency:
  - Utilized `leading_zeros()` to optimize the `get_msb` function.
- Implemented a generalized `BitsQueryable` trait for numeric types,
reducing the need for multiple implementations.
- Maintained individual implementations for `FieldElement` and
`MemoryAddress` to prevent type-related issues.

---------

Co-authored-by: Facundo <[email protected]>
  • Loading branch information
cypherpepe and fcarreiro authored Nov 7, 2024
1 parent f6f0be8 commit 65b1cd2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
30 changes: 11 additions & 19 deletions avm-transpiler/src/bit_traits.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use acvm::{acir::brillig::MemoryAddress, AcirField, FieldElement};

fn get_msb(n: u128) -> usize {
let mut n = n;
let mut msb = 0;
while n > 0 {
n >>= 1;
msb += 1;
if n == 0 {
0
} else {
128 - n.leading_zeros() as usize
}
msb
}

pub trait BitsQueryable {
Expand Down Expand Up @@ -66,18 +64,12 @@ impl BitsQueryable for MemoryAddress {
}

pub fn bits_needed_for<T: BitsQueryable>(val: &T) -> usize {
let num_bits = val.num_bits();
if num_bits <= 8 {
8
} else if num_bits <= 16 {
16
} else if num_bits <= 32 {
32
} else if num_bits <= 64 {
64
} else if num_bits <= 128 {
128
} else {
254
match val.num_bits() {
0..=8 => 8,
9..=16 => 16,
17..=32 => 32,
33..=64 => 64,
65..=128 => 128,
_ => 254,
}
}
2 changes: 1 addition & 1 deletion bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if [ "$CMD" = "clean" ]; then
echo "WARNING: This will erase *all* untracked files, including hooks and submodules."
echo -n "Continue? [y/n] "
read user_input
if [ "$user_input" != "y" ] && [ "$user_input" != "yes" ] && [ "$user_input" != "Y" ] && [ "$user_input" != "YES" ]; then
if [[ ! "$user_input" =~ ^[yY](es)?$ ]]; then
echo "Exiting without cleaning"
exit 1
fi
Expand Down

0 comments on commit 65b1cd2

Please sign in to comment.