openvm_sha2_circuit/sha2_chips/main_chip/
mod.rs

1mod air;
2mod columns;
3mod config;
4mod trace;
5
6use std::{
7    marker::PhantomData,
8    sync::{Arc, Mutex},
9};
10
11pub use air::*;
12pub use columns::*;
13pub use config::*;
14use openvm_circuit::system::memory::SharedMemoryHelper;
15use openvm_circuit_primitives::bitwise_op_lookup::SharedBitwiseOperationLookupChip;
16use openvm_stark_backend::p3_matrix::dense::RowMajorMatrix;
17
18use crate::Sha2Config;
19
20// Record struct for sharing between the main chip and the block hasher chip
21pub struct Sha2SharedRecords<F> {
22    // note that we can't just do matrix.height() because the height is padded to the next power of
23    // two by MatrixRecordArena::into_matrix()
24    pub num_records: usize,
25    pub matrix: RowMajorMatrix<F>,
26}
27pub struct Sha2MainChip<F, C: Sha2Config> {
28    // This Arc<Mutex<Option<RA>>> is shared with the block hasher chip (Sha2BlockHasherChip).
29    // When the main chip's tracegen is done, it will set the value of the mutex to Some(records)
30    // and then the block hasher chip can see the records and use them to generate its trace.
31    // The arc mutex is not strictly necessary (we could just use a Cell) because tracegen is done
32    // sequentially over the list of chips (although it is parallelized within each chip), but the
33    // overhead of using a thread-safe type is negligible since we only access the 'records' field
34    // twice (once to set the value and once to get the value).
35    // So, we will just use an arc mutex to avoid overcomplicating things.
36    pub records: Arc<Mutex<Option<Sha2SharedRecords<F>>>>,
37    pub bitwise_lookup_chip: SharedBitwiseOperationLookupChip<8>,
38    pub pointer_max_bits: usize,
39    pub mem_helper: SharedMemoryHelper<F>,
40    _phantom: PhantomData<C>,
41}
42
43impl<F, C: Sha2Config> Sha2MainChip<F, C> {
44    pub fn new(
45        records: Arc<Mutex<Option<Sha2SharedRecords<F>>>>,
46        bitwise_lookup_chip: SharedBitwiseOperationLookupChip<8>,
47        pointer_max_bits: usize,
48        mem_helper: SharedMemoryHelper<F>,
49    ) -> Self {
50        Self {
51            records,
52            bitwise_lookup_chip,
53            pointer_max_bits,
54            mem_helper,
55            _phantom: PhantomData,
56        }
57    }
58}