Skip to content

Commit

Permalink
misc: use From (not as) for lossless conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-ferdinand committed Feb 10, 2024
1 parent 60289eb commit 4e8b28b
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 55 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ twenty-first = { git = "https://github.com/Neptune-Crypto/twenty-first", rev = "
unicode-width = "0.1"

[workspace.lints.clippy]
cast_lossless = "warn"
used_underscore_binding = "warn"

[workspace.dependencies.cargo-husky]
Expand Down
10 changes: 5 additions & 5 deletions triton-tui/src/components/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ impl<'a> Memory<'a> {
return;
};

let modulus = BFieldElement::P as i128;
let modulus = i128::from(BFieldElement::P);
if address < -modulus || modulus <= address {
self.user_address = None;
return;
}
let address = (address + modulus) % modulus;
let address = BFieldElement::from(address as u64);
let address = ((address + modulus) % modulus) as u64;
let address = BFieldElement::from(address);
self.user_address = Some(address);
}

Expand Down Expand Up @@ -170,7 +170,7 @@ impl<'a> Memory<'a> {
}

fn first_address_in_block(block: u32) -> BFieldElement {
((block as u64) << 32).into()
(u64::from(block) << 32).into()
}

fn distribute_area_for_widgets(&self, area: Rect) -> WidgetAreas {
Expand All @@ -182,7 +182,7 @@ impl<'a> Memory<'a> {
let block = Self::memory_widget_block();
let draw_area = render_info.areas.memory;

let num_lines = block.inner(draw_area).height as u64;
let num_lines = u64::from(block.inner(draw_area).height);
let address_range_start = self.requested_address() - BFieldElement::from(num_lines / 2);
let address_range_end = address_range_start + BFieldElement::from(num_lines);

Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ impl<H: AlgebraicHasher> Fri<H> {

// Skip rounds for which Merkle tree verification cost exceeds arithmetic cost,
// because more than half the codeword's locations are queried.
let num_rounds_checking_all_locations = self.num_collinearity_checks.ilog2() as u64;
let num_rounds_checking_all_locations = u64::from(self.num_collinearity_checks.ilog2());
let num_rounds_checking_most_locations = num_rounds_checking_all_locations + 1;

max_num_rounds.saturating_sub(num_rounds_checking_most_locations) as usize
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ mod tests {
fn flag_set(self) -> u32 {
let instruction = self.replace_default_argument_if_illegal();
InstructionBucket::iter()
.map(|bucket| bucket.contains(instruction) as u32)
.map(|bucket| u32::from(bucket.contains(instruction)))
.enumerate()
.map(|(bucket_index, contains_self)| contains_self << bucket_index)
.fold(0, |acc, bit_flag| acc | bit_flag)
Expand Down
8 changes: 4 additions & 4 deletions triton-vm/src/op_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ impl OpStack {
Ok(element)
}

pub(crate) fn assert_is_u32(&self, stack_element: OpStackElement) -> Result<()> {
pub(crate) fn is_u32(&self, stack_element: OpStackElement) -> Result<()> {
let element = self[stack_element];
match element.value() <= u32::MAX as u64 {
true => Ok(()),
false => Err(FailedU32Conversion(element)),
match u32::try_from(element.value()) {
Ok(_) => Ok(()),
Err(_) => Err(FailedU32Conversion(element)),
}
}

Expand Down
9 changes: 3 additions & 6 deletions triton-vm/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,11 @@ fn field_element(s_orig: &str) -> ParseResult<BFieldElement> {
let (s, negative) = opt(token0("-"))(s_orig)?;
let (s, n) = digit1(s)?;

let mut n: i128 = match n.parse() {
Ok(n) => n,
Err(_err) => {
return context("out-of-bounds constant", fail)(s);
}
let Ok(mut n): Result<i128, _> = n.parse() else {
return context("out-of-bounds constant", fail)(s);
};

let quotient = BFieldElement::P as i128;
let quotient = i128::from(BFieldElement::P);
if n >= quotient {
return context("out-of-bounds constant", fail)(s_orig);
}
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/stark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2291,7 +2291,7 @@ pub(crate) mod tests {
#[proptest]
fn negative_log_2_floor(
#[strategy(arb())]
#[filter(#st0.value() > u32::MAX as u64)]
#[filter(#st0.value() > u64::from(u32::MAX))]
st0: BFieldElement,
) {
let program = triton_program!(push {st0} log_2_floor halt);
Expand Down
8 changes: 4 additions & 4 deletions triton-vm/src/table/cascade_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl CascadeTable {
let to_look_up_hi = ((to_look_up >> 8) & 0xff) as u8;

let mut row = cascade_table.row_mut(row_idx);
row[LookInLo.base_table_index()] = BFieldElement::new(to_look_up_lo as u64);
row[LookInHi.base_table_index()] = BFieldElement::new(to_look_up_hi as u64);
row[LookInLo.base_table_index()] = BFieldElement::new(u64::from(to_look_up_lo));
row[LookInHi.base_table_index()] = BFieldElement::new(u64::from(to_look_up_hi));
row[LookOutLo.base_table_index()] = Self::lookup_8_bit_limb(to_look_up_lo);
row[LookOutHi.base_table_index()] = Self::lookup_8_bit_limb(to_look_up_hi);
row[LookupMultiplicity.base_table_index()] = BFieldElement::new(multiplicity);
Expand Down Expand Up @@ -114,8 +114,8 @@ impl CascadeTable {
}

fn lookup_8_bit_limb(to_look_up: u8) -> BFieldElement {
let looked_up = tip5::LOOKUP_TABLE[to_look_up as usize] as u64;
BFieldElement::new(looked_up)
let looked_up = tip5::LOOKUP_TABLE[to_look_up as usize];
BFieldElement::new(u64::from(looked_up))
}

pub fn lookup_16_bit_limb(to_look_up: u16) -> BFieldElement {
Expand Down
2 changes: 1 addition & 1 deletion triton-vm/src/table/challenges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Challenges {
challenges[StandardOutputIndeterminate.index()],
);
let lookup_terminal = EvalArg::compute_terminal(
&tip5::LOOKUP_TABLE.map(|i| BFieldElement::new(i as u64)),
&tip5::LOOKUP_TABLE.map(u64::from).map(BFieldElement::new),
EvalArg::default_initial(),
challenges[LookupTablePublicIndeterminate.index()],
);
Expand Down
21 changes: 12 additions & 9 deletions triton-vm/src/table/lookup_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,32 @@ impl LookupTable {
lookup_table: &mut ArrayViewMut2<BFieldElement>,
aet: &AlgebraicExecutionTrace,
) {
assert!(lookup_table.nrows() >= 1 << 8);
const LOOKUP_TABLE_LEN: usize = tip5::LOOKUP_TABLE.len();
assert!(lookup_table.nrows() >= LOOKUP_TABLE_LEN);

// Lookup Table input
let lookup_input = Array1::from_iter((0_u64..1 << 8).map(BFieldElement::new));
let lookup_input =
Array1::from_iter((0..LOOKUP_TABLE_LEN).map(|i| BFieldElement::new(i as u64)));
let lookup_input_column =
lookup_table.slice_mut(s![..1_usize << 8, LookIn.base_table_index()]);
lookup_table.slice_mut(s![..LOOKUP_TABLE_LEN, LookIn.base_table_index()]);
lookup_input.move_into(lookup_input_column);

// Lookup Table output
let lookup_output = Array1::from_iter(
(0..1 << 8).map(|i| BFieldElement::new(tip5::LOOKUP_TABLE[i] as u64)),
);
let lookup_output =
Array1::from_iter(tip5::LOOKUP_TABLE.map(u64::from).map(BFieldElement::new));
let lookup_output_column =
lookup_table.slice_mut(s![..1_usize << 8, LookOut.base_table_index()]);
lookup_table.slice_mut(s![..LOOKUP_TABLE_LEN, LookOut.base_table_index()]);
lookup_output.move_into(lookup_output_column);

// Lookup Table multiplicities
let lookup_multiplicities = Array1::from_iter(
aet.lookup_table_lookup_multiplicities
.map(BFieldElement::new),
);
let lookup_multiplicities_column =
lookup_table.slice_mut(s![..1_usize << 8, LookupMultiplicity.base_table_index()]);
let lookup_multiplicities_column = lookup_table.slice_mut(s![
..LOOKUP_TABLE_LEN,
LookupMultiplicity.base_table_index()
]);
lookup_multiplicities.move_into(lookup_multiplicities_column);
}

Expand Down
46 changes: 23 additions & 23 deletions triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ impl VMState {
if self.secret_digests.is_empty() {
return Err(EmptySecretDigestInput);
}
self.op_stack.assert_is_u32(ST5)?;
self.op_stack.is_u32(ST5)?;

let known_digest = self.op_stack.pop_multiple()?;
let node_index = self.op_stack.pop_u32()?;
Expand Down Expand Up @@ -580,8 +580,8 @@ impl VMState {
}

fn lt(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.assert_is_u32(ST1)?;
self.op_stack.is_u32(ST0)?;
self.op_stack.is_u32(ST1)?;
let lhs = self.op_stack.pop_u32()?;
let rhs = self.op_stack.pop_u32()?;
let lt: u32 = (lhs < rhs).into();
Expand All @@ -595,8 +595,8 @@ impl VMState {
}

fn and(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.assert_is_u32(ST1)?;
self.op_stack.is_u32(ST0)?;
self.op_stack.is_u32(ST1)?;
let lhs = self.op_stack.pop_u32()?;
let rhs = self.op_stack.pop_u32()?;
let and = lhs & rhs;
Expand All @@ -610,8 +610,8 @@ impl VMState {
}

fn xor(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.assert_is_u32(ST1)?;
self.op_stack.is_u32(ST0)?;
self.op_stack.is_u32(ST1)?;
let lhs = self.op_stack.pop_u32()?;
let rhs = self.op_stack.pop_u32()?;
let xor = lhs ^ rhs;
Expand All @@ -628,7 +628,7 @@ impl VMState {
}

fn log_2_floor(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.is_u32(ST0)?;
let top_of_stack = self.op_stack[ST0];
if top_of_stack.is_zero() {
return Err(LogarithmOfZero);
Expand All @@ -645,7 +645,7 @@ impl VMState {
}

fn pow(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST1)?;
self.op_stack.is_u32(ST1)?;
let base = self.op_stack.pop()?;
let exponent = self.op_stack.pop_u32()?;
let base_pow_exponent = base.mod_pow(exponent.into());
Expand All @@ -660,8 +660,8 @@ impl VMState {
}

fn div_mod(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.assert_is_u32(ST1)?;
self.op_stack.is_u32(ST0)?;
self.op_stack.is_u32(ST1)?;
let denominator = self.op_stack[ST1];
if denominator.is_zero() {
return Err(DivisionByZero);
Expand All @@ -687,7 +687,7 @@ impl VMState {
}

fn pop_count(&mut self) -> Result<Vec<CoProcessorCall>> {
self.op_stack.assert_is_u32(ST0)?;
self.op_stack.is_u32(ST0)?;
let top_of_stack = self.op_stack.pop_u32()?;
let pop_count = top_of_stack.count_ones();
self.op_stack.push(pop_count.into());
Expand Down Expand Up @@ -768,7 +768,7 @@ impl VMState {
let current_instruction = self.current_instruction().unwrap_or(Nop);
let helper_variables = self.derive_helper_variables();

processor_row[CLK.base_table_index()] = (self.cycle_count as u64).into();
processor_row[CLK.base_table_index()] = u64::from(self.cycle_count).into();
processor_row[IP.base_table_index()] = (self.instruction_pointer as u32).into();
processor_row[CI.base_table_index()] = current_instruction.opcode_b();
processor_row[NIA.base_table_index()] = self.next_instruction_or_argument();
Expand Down Expand Up @@ -1670,7 +1670,7 @@ pub(crate) mod tests {
pub(crate) fn property_based_test_program_for_is_u32() -> ProgramAndInput {
let mut rng = ThreadRng::default();
let st0_u32 = rng.next_u32();
let st0_not_u32 = ((rng.next_u32() as u64) << 32) + (rng.next_u32() as u64);
let st0_not_u32 = (u64::from(rng.next_u32()) << 32) + u64::from(rng.next_u32());
let program = triton_program!(
push {st0_u32} call is_u32 assert
push {st0_not_u32} call is_u32 push 0 eq assert halt
Expand Down Expand Up @@ -1755,7 +1755,7 @@ pub(crate) mod tests {
#[proptest]
fn negative_property_is_u32(
#[strategy(arb())]
#[filter(#st0.value() > u32::MAX as u64)]
#[filter(#st0.value() > u64::from(u32::MAX))]
st0: BFieldElement,
) {
let program = triton_program!(
Expand Down Expand Up @@ -2246,7 +2246,7 @@ pub(crate) mod tests {

#[test]
fn instruction_divine_sibling_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} swap 5 divine_sibling halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 2);
}
Expand Down Expand Up @@ -2278,28 +2278,28 @@ pub(crate) mod tests {

#[test]
fn instruction_lt_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} lt halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 1);
}

#[test]
fn instruction_and_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} and halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 1);
}

#[test]
fn instruction_xor_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} xor halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 1);
}

#[test]
fn instruction_log_2_floor_on_non_u32_operand_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} log_2_floor halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 1);
}
Expand All @@ -2312,14 +2312,14 @@ pub(crate) mod tests {

#[test]
fn instruction_pow_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} push 0 pow halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 2);
}

#[test]
fn instruction_div_mod_on_non_u32_operand_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} push 0 div_mod halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 2);
}
Expand All @@ -2332,7 +2332,7 @@ pub(crate) mod tests {

#[test]
fn instruction_pop_count_does_not_change_vm_state_when_crashing_vm() {
let non_u32 = (u32::MAX as u64) + 1;
let non_u32 = u64::from(u32::MAX) + 1;
let program = triton_program! { push {non_u32} pop_count halt };
instruction_does_not_change_vm_state_when_crashing_vm(program, 1);
}
Expand Down

0 comments on commit 4e8b28b

Please sign in to comment.