Skip to content

Commit

Permalink
apply to b instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiWu123 committed Oct 17, 2024
1 parent fc59d50 commit eff9abf
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 140 deletions.
5 changes: 3 additions & 2 deletions ceno_emul/src/rv32im.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ impl EncodedInstruction {
let rs1 = rs1 & 0x1f;
let func3 = kind.codes().func3;
let opcode = kind.codes().opcode;
let imm_1_4 = imm & 0x1e; // skip imm[0]
let imm_5_10 = (imm >> 5) & 0x1f;
let imm_1_4 = (imm >> 1) & 0xf; // skip imm[0]
let imm_5_10 = (imm >> 5) & 0x3f;
println!("{:#b}: {:#b}, {:#b}", imm, imm_1_4, imm_5_10);
((imm >> 12) & 1) << 31
| imm_5_10 << 25
| rs2 << 20
Expand Down
6 changes: 3 additions & 3 deletions ceno_emul/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{collections::HashMap, fmt, mem};

use crate::{
CENO_PLATFORM, InsnKind, PC_STEP_SIZE,
CENO_PLATFORM, PC_STEP_SIZE,
addr::{ByteAddr, Cycle, RegIdx, Word, WordAddr},
rv32im::DecodedInstruction,
};
Expand Down Expand Up @@ -77,12 +77,12 @@ impl StepRecord {
pub fn new_b_instruction(
cycle: Cycle,
pc: Change<ByteAddr>,
insn_code: Word,
insn_code: u32,
rs1_read: Word,
rs2_read: Word,
prev_cycle: Cycle,
) -> StepRecord {
StepRecord::new_insn(
StepRecord::new_insn2(
cycle,
pc,
insn_code,
Expand Down
183 changes: 88 additions & 95 deletions ceno_zkvm/src/instructions/riscv/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod test {
use crate::{
circuit_builder::{CircuitBuilder, ConstraintSystem},
instructions::Instruction,
scheme::mock_prover::{MockProver, MOCK_PC_START},
scheme::mock_prover::{MOCK_PC_START, MockProver},
};

#[test]
Expand All @@ -197,20 +197,19 @@ mod test {
.unwrap();

let insn_code = EncodedInstruction::encode(InsnKind::ADD, 2, 3, 4, 0);
let (raw_witin, lkm) = AddInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
0xfffffffe,
Change::new(0, 11_u32.wrapping_add(0xfffffffe)),
0,
)],
)
.unwrap();
let (raw_witin, lkm) =
AddInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
0xfffffffe,
Change::new(0, 11_u32.wrapping_add(0xfffffffe)),
0,
),
])
.unwrap();

let expected_rd_written = UInt::from_const_unchecked(
Value::new_unchecked(11_u32.wrapping_add(0xfffffffe))
Expand Down Expand Up @@ -253,20 +252,19 @@ mod test {
.unwrap();

let insn_code = EncodedInstruction::encode(InsnKind::ADD, 2, 3, 4, 0);
let (raw_witin, _) = AddInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
u32::MAX - 1,
u32::MAX - 1,
Change::new(0, (u32::MAX - 1).wrapping_add(u32::MAX - 1)),
0,
)],
)
.unwrap();
let (raw_witin, lkm) =
AddInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
u32::MAX - 1,
u32::MAX - 1,
Change::new(0, (u32::MAX - 1).wrapping_add(u32::MAX - 1)),
0,
),
])
.unwrap();

let expected_rd_written = UInt::from_const_unchecked(
Value::new_unchecked((u32::MAX - 1).wrapping_add(u32::MAX - 1))
Expand Down Expand Up @@ -309,20 +307,19 @@ mod test {
.unwrap();

let insn_code = EncodedInstruction::encode(InsnKind::SUB, 2, 3, 4, 0);
let (raw_witin, _) = SubInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
2,
Change::new(0, 11_u32.wrapping_sub(2)),
0,
)],
)
.unwrap();
let (raw_witin, lkm) =
SubInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
2,
Change::new(0, 11_u32.wrapping_sub(2)),
0,
),
])
.unwrap();

let expected_rd_written = UInt::from_const_unchecked(
Value::new_unchecked(11_u32.wrapping_sub(2))
Expand Down Expand Up @@ -365,20 +362,19 @@ mod test {
.unwrap();

let insn_code = EncodedInstruction::encode(InsnKind::SUB, 2, 3, 4, 0);
let (raw_witin, _) = SubInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
3,
11,
Change::new(0, 3_u32.wrapping_sub(11)),
0,
)],
)
.unwrap();
let (raw_witin, _) =
SubInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
3,
11,
Change::new(0, 3_u32.wrapping_sub(11)),
0,
),
])
.unwrap();

let expected_rd_written = UInt::from_const_unchecked(
Value::new_unchecked(3_u32.wrapping_sub(11))
Expand Down Expand Up @@ -416,20 +412,19 @@ mod test {

// values assignment
let insn_code = EncodedInstruction::encode(InsnKind::MUL, 2, 3, 4, 0);
let (raw_witin, lkm) = MulInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
2,
Change::new(0, 22),
0,
)],
)
.unwrap();
let (raw_witin, lkm) =
MulInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
11,
2,
Change::new(0, 22),
0,
),
])
.unwrap();

let expected_rd_written =
UInt::from_const_unchecked(Value::new_unchecked(22u32).as_u16_limbs().to_vec());
Expand Down Expand Up @@ -464,20 +459,19 @@ mod test {

// values assignment
let insn_code = EncodedInstruction::encode(InsnKind::MUL, 2, 3, 4, 0);
let (raw_witin, lkm) = MulInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
u32::MAX / 2 + 1,
2,
Change::new(0, 0),
0,
)],
)
.unwrap();
let (raw_witin, lkm) =
MulInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
u32::MAX / 2 + 1,
2,
Change::new(0, 0),
0,
),
])
.unwrap();

let expected_rd_written = UInt::from_const_unchecked(vec![0u64, 0]);

Expand Down Expand Up @@ -516,16 +510,15 @@ mod test {

// values assignment
let insn_code = EncodedInstruction::encode(InsnKind::MUL, 2, 3, 4, 0);
let (raw_witin, lkm) = MulInstruction::assign_instances(
&config,
cb.cs.num_witin as usize,
vec![StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
a.as_u64() as u32,
b.as_u64() as u32,
Change::new(
let (raw_witin, lkm) =
MulInstruction::assign_instances(&config, cb.cs.num_witin as usize, vec![
StepRecord::new_r_instruction(
3,
MOCK_PC_START,
insn_code,
a.as_u64() as u32,
b.as_u64() as u32,
Change::new(0, 0),
0,
),
])
Expand Down
Loading

0 comments on commit eff9abf

Please sign in to comment.