openvm_rv32im_circuit/hintstore/
core.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use std::{
    array,
    borrow::{Borrow, BorrowMut},
    sync::{Arc, OnceLock},
};

use openvm_circuit::arch::{
    AdapterAirContext, AdapterRuntimeContext, ExecutionError, MinimalInstruction, Result, Streams,
    VmAdapterInterface, VmCoreAir, VmCoreChip,
};
use openvm_circuit_primitives::bitwise_op_lookup::{
    BitwiseOperationLookupBus, BitwiseOperationLookupChip,
};
use openvm_circuit_primitives_derive::AlignedBorrow;
use openvm_instructions::{instruction::Instruction, UsizeOpcode};
use openvm_rv32im_transpiler::Rv32HintStoreOpcode;
use openvm_stark_backend::{
    interaction::InteractionBuilder,
    p3_air::BaseAir,
    p3_field::{AbstractField, Field, PrimeField32},
    rap::BaseAirWithPublicValues,
};
use parking_lot::Mutex;

use crate::adapters::{RV32_CELL_BITS, RV32_REGISTER_NUM_LIMBS};

/// HintStore Core Chip handles the range checking of the data to be written to memory
#[repr(C)]
#[derive(Debug, Clone, AlignedBorrow)]
pub struct Rv32HintStoreCoreCols<T> {
    pub is_valid: T,
    pub data: [T; RV32_REGISTER_NUM_LIMBS],
}

#[derive(Debug, Clone)]
pub struct Rv32HintStoreCoreRecord<F> {
    pub data: [F; RV32_REGISTER_NUM_LIMBS],
}

#[derive(Debug, Clone)]
pub struct Rv32HintStoreCoreAir {
    pub bus: BitwiseOperationLookupBus,
    pub offset: usize,
}

impl<F: Field> BaseAir<F> for Rv32HintStoreCoreAir {
    fn width(&self) -> usize {
        Rv32HintStoreCoreCols::<F>::width()
    }
}

impl<F: Field> BaseAirWithPublicValues<F> for Rv32HintStoreCoreAir {}

impl<AB, I> VmCoreAir<AB, I> for Rv32HintStoreCoreAir
where
    AB: InteractionBuilder,
    I: VmAdapterInterface<AB::Expr>,
    I::Reads: From<[[AB::Expr; RV32_REGISTER_NUM_LIMBS]; 0]>,
    I::Writes: From<[[AB::Expr; RV32_REGISTER_NUM_LIMBS]; 1]>,
    I::ProcessedInstruction: From<MinimalInstruction<AB::Expr>>,
{
    fn eval(
        &self,
        builder: &mut AB,
        local_core: &[AB::Var],
        _from_pc: AB::Var,
    ) -> AdapterAirContext<AB::Expr, I> {
        let cols: &Rv32HintStoreCoreCols<AB::Var> = (*local_core).borrow();

        builder.assert_bool(cols.is_valid);

        let expected_opcode =
            AB::Expr::from_canonical_usize(Rv32HintStoreOpcode::HINT_STOREW as usize)
                + AB::Expr::from_canonical_usize(self.offset);

        for i in 0..RV32_REGISTER_NUM_LIMBS / 2 {
            self.bus
                .send_range(cols.data[i * 2], cols.data[i * 2 + 1])
                .eval(builder, cols.is_valid);
        }

        AdapterAirContext {
            to_pc: None,
            reads: [].into(),
            writes: [cols.data.map(|x| x.into())].into(),
            instruction: MinimalInstruction {
                is_valid: cols.is_valid.into(),
                opcode: expected_opcode,
            }
            .into(),
        }
    }
}

#[derive(Debug)]
pub struct Rv32HintStoreCoreChip<F: Field> {
    pub air: Rv32HintStoreCoreAir,
    pub streams: OnceLock<Arc<Mutex<Streams<F>>>>,
    pub bitwise_lookup_chip: Arc<BitwiseOperationLookupChip<RV32_CELL_BITS>>,
}

impl<F: PrimeField32> Rv32HintStoreCoreChip<F> {
    pub fn new(
        bitwise_lookup_chip: Arc<BitwiseOperationLookupChip<RV32_CELL_BITS>>,
        offset: usize,
    ) -> Self {
        Self {
            air: Rv32HintStoreCoreAir {
                bus: bitwise_lookup_chip.bus(),
                offset,
            },
            streams: OnceLock::new(),
            bitwise_lookup_chip,
        }
    }
    pub fn set_streams(&mut self, streams: Arc<Mutex<Streams<F>>>) {
        self.streams.set(streams).unwrap();
    }
}

impl<F: PrimeField32, I: VmAdapterInterface<F>> VmCoreChip<F, I> for Rv32HintStoreCoreChip<F>
where
    I::Reads: Into<[[F; RV32_REGISTER_NUM_LIMBS]; 0]>,
    I::Writes: From<[[F; RV32_REGISTER_NUM_LIMBS]; 1]>,
{
    type Record = Rv32HintStoreCoreRecord<F>;
    type Air = Rv32HintStoreCoreAir;

    #[allow(clippy::type_complexity)]
    fn execute_instruction(
        &self,
        _instruction: &Instruction<F>,
        from_pc: u32,
        _reads: I::Reads,
    ) -> Result<(AdapterRuntimeContext<F, I>, Self::Record)> {
        let mut streams = self.streams.get().unwrap().lock();
        if streams.hint_stream.len() < RV32_REGISTER_NUM_LIMBS {
            return Err(ExecutionError::HintOutOfBounds { pc: from_pc });
        }
        let data: [F; RV32_REGISTER_NUM_LIMBS] =
            array::from_fn(|_| streams.hint_stream.pop_front().unwrap());
        let write_data = data;

        let output = AdapterRuntimeContext::without_pc([write_data]);
        for i in 0..(RV32_REGISTER_NUM_LIMBS / 2) {
            self.bitwise_lookup_chip.request_range(
                write_data[2 * i].as_canonical_u32(),
                write_data[2 * i + 1].as_canonical_u32(),
            );
        }
        Ok((output, Rv32HintStoreCoreRecord { data: write_data }))
    }

    fn get_opcode_name(&self, opcode: usize) -> String {
        format!(
            "{:?}",
            Rv32HintStoreOpcode::from_usize(opcode - self.air.offset)
        )
    }

    fn generate_trace_row(&self, row_slice: &mut [F], record: Self::Record) {
        let core_cols: &mut Rv32HintStoreCoreCols<F> = row_slice.borrow_mut();
        core_cols.is_valid = F::ONE;
        core_cols.data = record.data;
    }

    fn air(&self) -> &Self::Air {
        &self.air
    }
}