-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/#98 riscv mul opcode #219
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8e03dc0
add mul gadget
KimiWu123 d9b2c96
test: add mul tests
KimiWu123 0a59d7c
refactor assign_instance
KimiWu123 6d90541
chore: using WordAddr type for addr in testing
KimiWu123 312795b
fix lint
KimiWu123 e376e15
add sanity check in test
KimiWu123 dbde521
fix for interface change
KimiWu123 e3e6499
draft
KimiWu123 ec57ea9
adopt new instruction design
KimiWu123 2bd9fe8
fix: adding prev_carry in result limbs and adding testing
KimiWu123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ pub mod addsub; | |
pub mod blt; | ||
pub mod config; | ||
pub mod constants; | ||
pub mod mul; | ||
|
||
mod r_insn; | ||
|
||
#[cfg(test)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
use ceno_emul::{InsnKind, StepRecord}; | ||
use ff_ext::ExtensionField; | ||
use itertools::Itertools; | ||
|
||
use super::{constants::RegUInt, r_insn::RInstructionConfig, RIVInstruction}; | ||
use crate::{ | ||
circuit_builder::CircuitBuilder, error::ZKVMError, instructions::Instruction, uint::UIntValue, | ||
witness::LkMultiplicity, | ||
}; | ||
use core::mem::MaybeUninit; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug)] | ||
pub struct ArithConfig<E: ExtensionField> { | ||
r_insn: RInstructionConfig<E>, | ||
|
||
multiplier_1: RegUInt<E>, | ||
multiplier_2: RegUInt<E>, | ||
outcome: RegUInt<E>, | ||
} | ||
|
||
pub struct ArithInstruction<E, I>(PhantomData<(E, I)>); | ||
|
||
pub struct MulOp; | ||
impl RIVInstruction for MulOp { | ||
const INST_KIND: InsnKind = InsnKind::MUL; | ||
} | ||
pub type MulInstruction<E> = ArithInstruction<E, MulOp>; | ||
|
||
impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for ArithInstruction<E, I> { | ||
type InstructionConfig = ArithConfig<E>; | ||
|
||
fn name() -> String { | ||
format!("{:?}", I::INST_KIND) | ||
} | ||
|
||
fn construct_circuit( | ||
circuit_builder: &mut CircuitBuilder<E>, | ||
) -> Result<Self::InstructionConfig, ZKVMError> { | ||
let mut multiplier_1 = RegUInt::new_unchecked(|| "multiplier_1", circuit_builder)?; | ||
let mut multiplier_2 = RegUInt::new_unchecked(|| "multiplier_2", circuit_builder)?; | ||
let outcome = multiplier_1.mul(|| "outcome", circuit_builder, &mut multiplier_2, true)?; | ||
|
||
let r_insn = RInstructionConfig::<E>::construct_circuit( | ||
circuit_builder, | ||
I::INST_KIND, | ||
&multiplier_1, | ||
&multiplier_2, | ||
&outcome, | ||
)?; | ||
|
||
Ok(ArithConfig { | ||
r_insn, | ||
multiplier_1, | ||
multiplier_2, | ||
outcome, | ||
}) | ||
} | ||
|
||
fn assign_instance( | ||
config: &Self::InstructionConfig, | ||
instance: &mut [MaybeUninit<E::BaseField>], | ||
lkm: &mut LkMultiplicity, | ||
step: &StepRecord, | ||
) -> Result<(), ZKVMError> { | ||
config.r_insn.assign_instance(instance, lkm, step)?; | ||
|
||
let multiplier_1 = UIntValue::new_unchecked(step.rs1().unwrap().value); | ||
let multiplier_2 = UIntValue::new_unchecked(step.rs2().unwrap().value); | ||
let outcome = UIntValue::new_unchecked(step.rd().unwrap().value.after); | ||
|
||
config | ||
.multiplier_1 | ||
.assign_limbs(instance, multiplier_1.u16_fields()); | ||
config | ||
.multiplier_2 | ||
.assign_limbs(instance, multiplier_2.u16_fields()); | ||
let (_, carries) = multiplier_1.mul(&multiplier_2, lkm, true); | ||
|
||
config.outcome.assign_limbs(instance, outcome.u16_fields()); | ||
config.outcome.assign_carries( | ||
instance, | ||
carries | ||
.into_iter() | ||
.map(|carry| E::BaseField::from(carry as u64)) | ||
.collect_vec(), | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use ceno_emul::{Change, StepRecord}; | ||
use goldilocks::GoldilocksExt2; | ||
use itertools::Itertools; | ||
use multilinear_extensions::mle::IntoMLEs; | ||
|
||
use crate::{ | ||
circuit_builder::{CircuitBuilder, ConstraintSystem}, | ||
instructions::Instruction, | ||
scheme::mock_prover::{MockProver, MOCK_PC_MUL, MOCK_PROGRAM}, | ||
}; | ||
|
||
use super::MulInstruction; | ||
|
||
#[test] | ||
fn test_opcode_mul() { | ||
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv"); | ||
let mut cb = CircuitBuilder::new(&mut cs); | ||
let config = cb | ||
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb))) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// values assignment | ||
let (raw_witin, _) = MulInstruction::assign_instances( | ||
&config, | ||
cb.cs.num_witin as usize, | ||
vec![StepRecord::new_r_instruction( | ||
3, | ||
MOCK_PC_MUL, | ||
MOCK_PROGRAM[2], | ||
11, | ||
2, | ||
Change::new(0, 22), | ||
0, | ||
)], | ||
) | ||
.unwrap(); | ||
|
||
MockProver::assert_satisfied( | ||
&mut cb, | ||
&raw_witin | ||
.de_interleaving() | ||
.into_mles() | ||
.into_iter() | ||
.map(|v| v.into()) | ||
.collect_vec(), | ||
None, | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_opcode_mul_overflow() { | ||
hero78119 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv"); | ||
let mut cb = CircuitBuilder::new(&mut cs); | ||
let config = cb | ||
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb))) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// values assignment | ||
let (raw_witin, _) = MulInstruction::assign_instances( | ||
&config, | ||
cb.cs.num_witin as usize, | ||
vec![StepRecord::new_r_instruction( | ||
3, | ||
MOCK_PC_MUL, | ||
MOCK_PROGRAM[2], | ||
u32::MAX / 2 + 1, | ||
2, | ||
Change::new(0, 0), | ||
0, | ||
)], | ||
) | ||
.unwrap(); | ||
|
||
MockProver::assert_satisfied( | ||
&mut cb, | ||
&raw_witin | ||
.de_interleaving() | ||
.into_mles() | ||
.into_iter() | ||
.map(|v| v.into()) | ||
.collect_vec(), | ||
None, | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_opcode_mul_overflow2() { | ||
let mut cs = ConstraintSystem::<GoldilocksExt2>::new(|| "riscv"); | ||
let mut cb = CircuitBuilder::new(&mut cs); | ||
let config = cb | ||
.namespace(|| "mul", |cb| Ok(MulInstruction::construct_circuit(cb))) | ||
.unwrap() | ||
.unwrap(); | ||
|
||
// values assignment | ||
let (raw_witin, _) = MulInstruction::assign_instances( | ||
&config, | ||
cb.cs.num_witin as usize, | ||
vec![StepRecord::new_r_instruction( | ||
3, | ||
MOCK_PC_MUL, | ||
MOCK_PROGRAM[2], | ||
4294901760, | ||
4294901760, | ||
Change::new(0, 0), | ||
0, | ||
)], | ||
) | ||
.unwrap(); | ||
|
||
MockProver::assert_satisfied( | ||
&mut cb, | ||
&raw_witin | ||
.de_interleaving() | ||
.into_mles() | ||
.into_iter() | ||
.map(|v| v.into()) | ||
.collect_vec(), | ||
None, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not right. See above: