Skip to content

Commit

Permalink
chore: cononical order of mul_add return argument order
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Oct 4, 2024
1 parent 924d375 commit 0691494
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ceno_zkvm/src/instructions/riscv/divu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E
let mut outcome = UInt::new(|| "outcome", circuit_builder)?;
let r = UInt::new(|| "remainder", circuit_builder)?;

let (inter_mul_value, dividend) =
let (dividend, inter_mul_value) =
divisor.mul_add(|| "dividend", circuit_builder, &mut outcome, &r, true)?;

// div by zero check
Expand Down
13 changes: 6 additions & 7 deletions ceno_zkvm/src/instructions/riscv/shift_imm/shift_imm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ShiftImmInstructio
// Goal is to constrain:
// rs1 == rd_written * imm + remainder
let remainder = UInt::new(|| "remainder", circuit_builder)?;
let (rd_imm_mul, rs1) = rd_written.mul_add(
let (rs1, rd_imm_mul) = rd_written.mul_add(
|| "rd_written * imm +remainder ",
circuit_builder,
&mut imm,
Expand Down Expand Up @@ -76,15 +76,14 @@ impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ShiftImmInstructio
step: &StepRecord,
) -> Result<(), ZKVMError> {
let rd_written = Value::new(step.rd().unwrap().value.after, lk_multiplicity);
let imm = Value::new(step.insn().imm_or_funct7(), lk_multiplicity);

// TODO design MulAdd gadget and refactor this to there
let remainder = {
let (remainder, imm) = {
let rs1_read = step.rs1().unwrap().value;
let imm = step.insn().imm_or_funct7();
let result = rs1_read.wrapping_div(imm);
let remainder = rs1_read.wrapping_sub(result * imm);
Value::new(remainder, lk_multiplicity)
(
Value::new(rs1_read % imm, lk_multiplicity),
Value::new(imm, lk_multiplicity),
)
};

let (rs1, rd_imm_mul) = rd_written.mul_add(&imm, &remainder, lk_multiplicity, true);
Expand Down
5 changes: 1 addition & 4 deletions ceno_zkvm/src/uint/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,7 @@ impl<const M: usize, const C: usize, E: ExtensionField> UIntLimbs<M, C, E> {
) -> Result<(UIntLimbs<M, C, E>, UIntLimbs<M, C, E>), ZKVMError> {
circuit_builder.namespace(name_fn, |cb| {
let c = self.internal_mul(cb, multiplier, with_overflow, is_hi_limb)?;
Ok((
c.clone(),
c.internal_add(cb, &addend.expr(), with_overflow)?,
))
Ok((c.internal_add(cb, &addend.expr(), with_overflow)?, c))
})
}

Expand Down

0 comments on commit 0691494

Please sign in to comment.