Skip to content

Commit

Permalink
Merge pull request #2804 from o1-labs/dw/riscv32/implement-div
Browse files Browse the repository at this point in the history
o1vm/riscv32: implement div for unsigned 32bits
  • Loading branch information
dannywillems authored Dec 2, 2024
2 parents 574d76e + 85639ca commit eb330fc
Show file tree
Hide file tree
Showing 3 changed files with 37 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 @@ -362,6 +362,15 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
self.variable(position)
}

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

unsafe fn mul_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 @@ -1285,6 +1285,20 @@ pub trait InterpreterEnv {
position: Self::Position,
) -> Self::Variable;

/// Returns `x / y`, 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 div(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable;

/// Returns `(x / y, x % y)`, storing the results in `position_quotient` and
/// `position_remainder` respectively.
///
Expand Down
14 changes: 14 additions & 0 deletions o1vm/src/interpreters/riscv32im/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,20 @@ impl<Fp: Field> InterpreterEnv for Env<Fp> {
res
}

unsafe fn div(
&mut self,
x: &Self::Variable,
y: &Self::Variable,
position: Self::Position,
) -> Self::Variable {
let x: u32 = (*x).try_into().unwrap();
let y: u32 = (*y).try_into().unwrap();
let res = x / y;
let res = res as u64;
self.write_column(position, res);
res
}

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

0 comments on commit eb330fc

Please sign in to comment.