Skip to content

Commit

Permalink
Merge pull request #2791 from o1-labs/dw/riscv32-impl-mul-lo-signed
Browse files Browse the repository at this point in the history
o1vm/riscv32: implement mul_lo_signed
  • Loading branch information
dannywillems authored Nov 25, 2024
2 parents 171e066 + 95098af commit 403763e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions o1vm/src/interpreters/riscv32im/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
self.variable(position)
}

unsafe fn mul_lo_signed(
&mut self,
_x: &Self::Variable,
_y: &Self::Variable,
position: Self::Position,
) -> Self::Variable {
self.variable(position)
}

unsafe fn mul_hi_lo(
&mut self,
_x: &Self::Variable,
Expand Down
14 changes: 14 additions & 0 deletions o1vm/src/interpreters/riscv32im/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,20 @@ pub trait InterpreterEnv {
position: Self::Position,
) -> Self::Variable;

/// Returns `(x * y) & ((1 << 32) - 1))`, storing the results in `position`
///
/// # Safety
///
/// There are no constraints on the returned values; callers must manually add constraints to
/// ensure that the pair of returned values correspond to the given values `x` and `y`, and
/// that they fall within the desired range.
unsafe fn mul_lo_signed(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

/// Returns `((x * y) >> 32, (x * y) & ((1 << 32) - 1))`, storing the results in `position_hi`
/// and `position_lo` respectively.
///
Expand Down
15 changes: 15 additions & 0 deletions o1vm/src/interpreters/riscv32im/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,21 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
res
}

unsafe fn mul_lo_signed(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable {
let x: i32 = (*x).try_into().unwrap();
let y: i32 = (*y).try_into().unwrap();
let res = ((x as i64) * (y as i64)) as u64;
let res = (res & ((1 << 32) - 1)) as u32;
let res = res as u64;
self.write_column(position, res);
res
}

unsafe fn mul_hi_lo_signed(
&mut self,
x: &Self::Variable,
Expand Down

0 comments on commit 403763e

Please sign in to comment.