-
Notifications
You must be signed in to change notification settings - Fork 10
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
Dummy Circuit - Basic ECALL #369
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b79b4b9
dummy: DummyInstruction, missing instructions, ecall
e2b7ac5
Merge branch 'master' into dummy
naure 8538220
dummy: treat unknown ecall as I instruction
bdba9eb
dummy: integrate ECALL NOP
5c5026a
Merge branch 'master' into dummy
naure 9c14a34
dummy-ecall-power: Let ecall do anything (#577)
naure 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
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
236 changes: 236 additions & 0 deletions
236
ceno_zkvm/src/instructions/riscv/dummy/dummy_circuit.rs
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,236 @@ | ||
use std::marker::PhantomData; | ||
|
||
use ceno_emul::{InsnCategory, InsnCodes, InsnFormat, InsnKind, StepRecord}; | ||
use ff_ext::ExtensionField; | ||
|
||
use super::super::{ | ||
RIVInstruction, | ||
constants::UInt, | ||
insn_base::{ReadMEM, ReadRS1, ReadRS2, StateInOut, WriteMEM, WriteRD}, | ||
}; | ||
use crate::{ | ||
circuit_builder::CircuitBuilder, | ||
error::ZKVMError, | ||
expression::{ToExpr, WitIn}, | ||
instructions::Instruction, | ||
set_val, | ||
tables::InsnRecord, | ||
uint::Value, | ||
utils::i64_to_base, | ||
witness::LkMultiplicity, | ||
}; | ||
use core::mem::MaybeUninit; | ||
|
||
/// DummyInstruction can handle any instruction and produce its side-effects. | ||
pub struct DummyInstruction<E, I>(PhantomData<(E, I)>); | ||
|
||
impl<E: ExtensionField, I: RIVInstruction> Instruction<E> for DummyInstruction<E, I> { | ||
type InstructionConfig = DummyConfig<E>; | ||
|
||
fn name() -> String { | ||
format!("{:?}_DUMMY", I::INST_KIND) | ||
} | ||
|
||
fn construct_circuit( | ||
circuit_builder: &mut CircuitBuilder<E>, | ||
) -> Result<Self::InstructionConfig, ZKVMError> { | ||
DummyConfig::construct_circuit(circuit_builder, I::INST_KIND.codes()) | ||
} | ||
|
||
fn assign_instance( | ||
config: &Self::InstructionConfig, | ||
instance: &mut [MaybeUninit<<E as ExtensionField>::BaseField>], | ||
lk_multiplicity: &mut LkMultiplicity, | ||
step: &StepRecord, | ||
) -> Result<(), ZKVMError> { | ||
config.assign_instance(instance, lk_multiplicity, step) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct DummyConfig<E: ExtensionField> { | ||
vm_state: StateInOut<E>, | ||
|
||
rs1: Option<(ReadRS1<E>, UInt<E>)>, | ||
rs2: Option<(ReadRS2<E>, UInt<E>)>, | ||
rd: Option<(WriteRD<E>, UInt<E>)>, | ||
|
||
mem_addr_val: Option<[WitIn; 3]>, | ||
mem_read: Option<ReadMEM<E>>, | ||
mem_write: Option<WriteMEM>, | ||
|
||
imm: WitIn, | ||
} | ||
|
||
impl<E: ExtensionField> DummyConfig<E> { | ||
fn construct_circuit( | ||
circuit_builder: &mut CircuitBuilder<E>, | ||
codes: InsnCodes, | ||
) -> Result<Self, ZKVMError> { | ||
let (with_rs1, with_rs2, with_rd) = match (codes.format, codes.kind) { | ||
// ECALL reads its syscall_id, then do nothing. | ||
(_, InsnKind::EANY) => (true, false, false), | ||
// Regular instructions do what is implied by their format. | ||
(InsnFormat::R, _) => (true, true, true), | ||
(InsnFormat::I, _) => (true, false, true), | ||
(InsnFormat::S, _) => (true, true, false), | ||
(InsnFormat::B, _) => (true, true, false), | ||
(InsnFormat::U, _) => (false, false, true), | ||
(InsnFormat::J, _) => (false, false, true), | ||
}; | ||
let with_mem_write = matches!(codes.category, InsnCategory::Store); | ||
let with_mem_read = matches!(codes.category, InsnCategory::Load); | ||
let branching = matches!(codes.category, InsnCategory::Branch) | ||
kunxian-xia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|| matches!(codes.kind, InsnKind::JAL | InsnKind::JALR); | ||
|
||
// State in and out | ||
let vm_state = StateInOut::construct_circuit(circuit_builder, branching)?; | ||
|
||
// Registers | ||
let rs1 = if with_rs1 { | ||
let rs1_read = UInt::new_unchecked(|| "rs1_read", circuit_builder)?; | ||
let rs1_op = | ||
ReadRS1::construct_circuit(circuit_builder, rs1_read.register_expr(), vm_state.ts)?; | ||
Some((rs1_op, rs1_read)) | ||
} else { | ||
None | ||
}; | ||
|
||
let rs2 = if with_rs2 { | ||
let rs2_read = UInt::new_unchecked(|| "rs2_read", circuit_builder)?; | ||
let rs2_op = | ||
ReadRS2::construct_circuit(circuit_builder, rs2_read.register_expr(), vm_state.ts)?; | ||
Some((rs2_op, rs2_read)) | ||
} else { | ||
None | ||
}; | ||
|
||
let rd = if with_rd { | ||
let rd_written = UInt::new_unchecked(|| "rd_written", circuit_builder)?; | ||
let rd_op = WriteRD::construct_circuit( | ||
circuit_builder, | ||
rd_written.register_expr(), | ||
vm_state.ts, | ||
)?; | ||
Some((rd_op, rd_written)) | ||
} else { | ||
None | ||
}; | ||
|
||
// Memory | ||
let mem_addr_val = if with_mem_read || with_mem_write { | ||
Some([ | ||
circuit_builder.create_witin(|| "mem_addr"), | ||
circuit_builder.create_witin(|| "mem_before"), | ||
circuit_builder.create_witin(|| "mem_after"), | ||
]) | ||
} else { | ||
None | ||
}; | ||
|
||
let mem_read = if with_mem_read { | ||
Some(ReadMEM::construct_circuit( | ||
circuit_builder, | ||
mem_addr_val.as_ref().unwrap()[0].expr(), | ||
mem_addr_val.as_ref().unwrap()[1].expr(), | ||
vm_state.ts, | ||
)?) | ||
} else { | ||
None | ||
}; | ||
|
||
let mem_write = if with_mem_write { | ||
Some(WriteMEM::construct_circuit( | ||
circuit_builder, | ||
mem_addr_val.as_ref().unwrap()[0].expr(), | ||
mem_addr_val.as_ref().unwrap()[1].expr(), | ||
mem_addr_val.as_ref().unwrap()[2].expr(), | ||
vm_state.ts, | ||
)?) | ||
} else { | ||
None | ||
}; | ||
|
||
// Fetch instruction | ||
|
||
// The register IDs of ECALL is fixed, not encoded. | ||
let rs1_id = match (&rs1, codes.kind) { | ||
(None, _) | (_, InsnKind::EANY) => 0.into(), | ||
(Some((r, _)), _) => r.id.expr(), | ||
}; | ||
|
||
let imm = circuit_builder.create_witin(|| "imm"); | ||
|
||
circuit_builder.lk_fetch(&InsnRecord::new( | ||
vm_state.pc.expr(), | ||
codes.kind.into(), | ||
rd.as_ref().map(|(r, _)| r.id.expr()), | ||
rs1_id, | ||
rs2.as_ref().map(|(r, _)| r.id.expr()).unwrap_or(0.into()), | ||
imm.expr(), | ||
))?; | ||
|
||
Ok(DummyConfig { | ||
vm_state, | ||
rs1, | ||
rs2, | ||
rd, | ||
mem_addr_val, | ||
mem_read, | ||
mem_write, | ||
imm, | ||
}) | ||
} | ||
|
||
fn assign_instance( | ||
&self, | ||
instance: &mut [MaybeUninit<<E as ExtensionField>::BaseField>], | ||
lk_multiplicity: &mut LkMultiplicity, | ||
step: &StepRecord, | ||
) -> Result<(), ZKVMError> { | ||
// State in and out | ||
self.vm_state.assign_instance(instance, step)?; | ||
|
||
// Fetch instruction | ||
lk_multiplicity.fetch(step.pc().before.0); | ||
|
||
// Registers | ||
if let Some((rs1_op, rs1_read)) = &self.rs1 { | ||
naure marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rs1_op.assign_instance(instance, lk_multiplicity, step)?; | ||
|
||
let rs1_val = Value::new_unchecked(step.rs1().expect("rs1 value").value); | ||
rs1_read.assign_value(instance, rs1_val); | ||
} | ||
if let Some((rs2_op, rs2_read)) = &self.rs2 { | ||
rs2_op.assign_instance(instance, lk_multiplicity, step)?; | ||
|
||
let rs2_val = Value::new_unchecked(step.rs2().expect("rs2 value").value); | ||
rs2_read.assign_value(instance, rs2_val); | ||
} | ||
if let Some((rd_op, rd_written)) = &self.rd { | ||
rd_op.assign_instance(instance, lk_multiplicity, step)?; | ||
|
||
let rd_val = Value::new_unchecked(step.rd().expect("rd value").value.after); | ||
rd_written.assign_value(instance, rd_val); | ||
} | ||
|
||
// Memory | ||
if let Some([mem_addr, mem_before, mem_after]) = &self.mem_addr_val { | ||
let mem_op = step.memory_op().expect("memory operation"); | ||
set_val!(instance, mem_addr, u64::from(mem_op.addr)); | ||
set_val!(instance, mem_before, mem_op.value.before as u64); | ||
set_val!(instance, mem_after, mem_op.value.after as u64); | ||
} | ||
if let Some(mem_read) = &self.mem_read { | ||
mem_read.assign_instance(instance, lk_multiplicity, step)?; | ||
} | ||
if let Some(mem_write) = &self.mem_write { | ||
mem_write.assign_instance::<E>(instance, lk_multiplicity, step)?; | ||
} | ||
|
||
let imm = i64_to_base::<E::BaseField>(InsnRecord::imm_internal(&step.insn())); | ||
set_val!(instance, self.imm, imm); | ||
|
||
Ok(()) | ||
} | ||
} |
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.
Maybe we can get rid of them as opcodes that are not supported (i.e.
DIV / REM / REMU
) right now belong to theR
category.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.
Yes we have implemented almost everything in the mean time 😄 . Maybe there will be other use-cases: test, benchmarks, demos ?