Skip to content

Commit

Permalink
feat: RV32 adapter/integration ALU runtime (#505)
Browse files Browse the repository at this point in the history
* feat: RV32 adapter/integration ALU runtime

* refactor: rename rv32_alu module to be more general + small PR comments
  • Loading branch information
stephenh-axiom-xyz authored Oct 7, 2024
1 parent 7795ba1 commit 4b4eba7
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 15 deletions.
28 changes: 24 additions & 4 deletions vm/src/arch/adapters/rv32_alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,43 @@ use p3_field::{AbstractField, Field, PrimeField32};
use super::RV32_REGISTER_NUM_LANES;
use crate::{
arch::{
ExecutionBridge, ExecutionState, InstructionOutput, IntegrationInterface, MachineAdapter,
MachineAdapterInterface, Result,
ExecutionBridge, ExecutionBus, ExecutionState, InstructionOutput, IntegrationInterface,
MachineAdapter, MachineAdapterInterface, Result,
},
memory::{
offline_checker::{MemoryBridge, MemoryReadAuxCols, MemoryWriteAuxCols},
MemoryChip, MemoryReadRecord, MemoryWriteRecord,
MemoryChip, MemoryChipRef, MemoryReadRecord, MemoryWriteRecord,
},
program::Instruction,
program::{bridge::ProgramBus, Instruction},
};

/// Reads instructions of the form OP a, b, c, d, e where [a:4]_d = [b:4]_d op [c:4]_e.
/// Operand d can only be 1, and e can be either 1 (for register reads) or 0 (when c
/// is an immediate).
#[derive(Debug)]
pub struct Rv32AluAdapter<F: Field> {
_marker: std::marker::PhantomData<F>,
pub air: Rv32AluAdapterAir,
}

impl<F: PrimeField32> Rv32AluAdapter<F> {
pub fn new(
execution_bus: ExecutionBus,
program_bus: ProgramBus,
memory_chip: MemoryChipRef<F>,
) -> Self {
let memory_bridge = memory_chip.borrow().memory_bridge();
Self {
_marker: std::marker::PhantomData,
air: Rv32AluAdapterAir {
_execution_bridge: ExecutionBridge::new(execution_bus, program_bus),
_memory_bridge: memory_bridge,
},
}
}
}

#[derive(Debug)]
pub struct Rv32AluReadRecord<F: Field> {
/// Read register value from address space d=1
pub rs1: MemoryReadRecord<F, RV32_REGISTER_NUM_LANES>,
Expand All @@ -36,6 +55,7 @@ pub struct Rv32AluReadRecord<F: Field> {
pub rs2_is_imm: bool,
}

#[derive(Debug)]
pub struct Rv32AluWriteRecord<F: Field> {
pub from_state: ExecutionState<usize>,
/// Write to destination register
Expand Down
3 changes: 3 additions & 0 deletions vm/src/arch/chips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{
memory::MemoryChipRef,
modular_addsub::ModularAddSubChip,
modular_multdiv::ModularMultDivChip,
new_alu::Rv32ArithmeticLogicChip,
program::{ExecutionError, Instruction, ProgramChip},
shift::ShiftChip,
ui::UiChip,
Expand Down Expand Up @@ -183,6 +184,7 @@ pub enum InstructionExecutorVariant<F: PrimeField32> {
ModularAddSub(Rc<RefCell<ModularAddSubChip<F, 32, 8>>>),
ModularMultDiv(Rc<RefCell<ModularMultDivChip<F, 63, 32, 8>>>),
ArithmeticLogicUnit256(Rc<RefCell<ArithmeticLogicChip<F, 32, 8>>>),
ArithmeticLogicUnitRv32(Rc<RefCell<Rv32ArithmeticLogicChip<F>>>),
U256Multiplication(Rc<RefCell<UintMultiplicationChip<F, 32, 8>>>),
Shift256(Rc<RefCell<ShiftChip<F, 32, 8>>>),
Ui(Rc<RefCell<UiChip<F>>>),
Expand All @@ -205,6 +207,7 @@ pub enum MachineChipVariant<F: PrimeField32> {
Keccak256(Rc<RefCell<KeccakVmChip<F>>>),
ByteXor(Arc<XorLookupChip<8>>),
ArithmeticLogicUnit256(Rc<RefCell<ArithmeticLogicChip<F, 32, 8>>>),
ArithmeticLogicUnitRv32(Rc<RefCell<Rv32ArithmeticLogicChip<F>>>),
U256Multiplication(Rc<RefCell<UintMultiplicationChip<F, 32, 8>>>),
Shift256(Rc<RefCell<ShiftChip<F, 32, 8>>>),
Ui(Rc<RefCell<UiChip<F>>>),
Expand Down
18 changes: 16 additions & 2 deletions vm/src/arch/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub trait UsizeOpcode {
}
}

pub fn with_default_offset<Opcode: UsizeOpcode>(opcode: Opcode) -> usize {
Opcode::default_offset() + opcode.as_usize()
}

#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, UsizeOpcode,
)]
Expand Down Expand Up @@ -178,6 +182,16 @@ pub enum U32Opcode {
AUIPC,
}

pub fn with_default_offset<Opcode: UsizeOpcode>(opcode: Opcode) -> usize {
Opcode::default_offset() + opcode.as_usize()
#[derive(
Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, EnumCount, EnumIter, FromRepr, UsizeOpcode,
)]
#[opcode_offset = 0x300]
#[repr(usize)]
#[allow(non_camel_case_types)]
pub enum AluOpcode {
ADD,
SUB,
XOR,
OR,
AND,
}
52 changes: 44 additions & 8 deletions vm/src/arch/integration_api.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::borrow::Borrow;

use afs_stark_backend::interaction::InteractionBuilder;
use afs_stark_backend::{interaction::InteractionBuilder, rap::AnyRap};
use p3_air::{Air, AirBuilderWithPublicValues, BaseAir, PairBuilder};
use p3_commit::PolynomialSpace;
use p3_field::{AbstractField, PrimeField32};
use p3_matrix::Matrix;
use p3_matrix::{dense::RowMajorMatrix, Matrix};
use p3_uni_stark::{Domain, StarkGenericConfig};

use super::{ExecutionState, InstructionExecutor, Result};
use super::{ExecutionState, InstructionExecutor, MachineChip, Result};
use crate::{
memory::{MemoryChip, MemoryChipRef},
program::Instruction,
Expand Down Expand Up @@ -130,6 +132,7 @@ pub struct IntegrationInterface<T, I: MachineAdapterInterface<T>> {
pub instruction: I::ProcessedInstruction,
}

#[derive(Debug)]
pub struct MachineChipWrapper<F: PrimeField32, A: MachineAdapter<F>, M: MachineIntegration<F, A>> {
pub adapter: A,
pub inner: M,
Expand Down Expand Up @@ -175,6 +178,22 @@ where
}
}

impl<F, A, M> MachineChipWrapper<F, A, M>
where
F: PrimeField32,
A: MachineAdapter<F>,
M: MachineIntegration<F, A>,
{
pub fn new(adapter: A, inner: M, memory: MemoryChipRef<F>) -> Self {
Self {
adapter,
inner,
records: vec![],
memory,
}
}
}

impl<F, A, M> InstructionExecutor<F> for MachineChipWrapper<F, A, M>
where
F: PrimeField32,
Expand Down Expand Up @@ -204,14 +223,13 @@ where
}
}

/*TODO
impl<F, A, M> MachineChip<F> for MachineChipWrapper<F, A, M>
where
F: PrimeField32,
A: MachineAdapter<F>,
M: MachineIntegration<F, A>,
[F]: BorrowMut<A::Cols<F>>,
[F]: BorrowMut<M::Cols<F>>,
// [F]: BorrowMut<A::Cols<F>>,
// [F]: BorrowMut<M::Cols<F>>,
{
fn generate_trace(self) -> RowMajorMatrix<F> {
let height = self.records.len().next_power_of_two();
Expand All @@ -221,10 +239,28 @@ where
let mut values = vec![F::zero(); height * width];
// This zip only goes through records. The padding rows between records.len()..height
// are filled with zeros.
for (row, record) in values.chunks_exact_mut(width).zip(self.records) {
for (_row, _record) in values.chunks_exact_mut(width).zip(self.records) {
todo!()
}
RowMajorMatrix::new(values, width)
}

fn air<SC: StarkGenericConfig>(&self) -> Box<dyn AnyRap<SC>>
where
Domain<SC>: PolynomialSpace<Val = F>,
{
todo!()
}

fn air_name(&self) -> String {
todo!()
}

fn current_trace_height(&self) -> usize {
todo!()
}

fn trace_width(&self) -> usize {
todo!()
}
}
*/
1 change: 1 addition & 0 deletions vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod hashes;
pub mod memory;
pub mod modular_addsub;
pub mod modular_multdiv;
pub mod new_alu;
pub mod program;
/// SDK functions for running and proving programs in the VM.
#[cfg(feature = "sdk")]
Expand Down
Loading

0 comments on commit 4b4eba7

Please sign in to comment.