openvm_keccak256_circuit/
lib.rs

1#![cfg_attr(feature = "tco", allow(incomplete_features))]
2#![cfg_attr(feature = "tco", feature(explicit_tail_calls))]
3//! Stateful keccak256 hasher. Handles full keccak sponge (padding, absorb, keccak-f) on
4//! variable length inputs read from VM memory.
5
6use openvm_circuit_primitives::bitwise_op_lookup::SharedBitwiseOperationLookupChip;
7
8pub mod air;
9pub mod columns;
10pub mod execution;
11pub mod trace;
12pub mod utils;
13
14#[cfg(feature = "cuda")]
15mod cuda;
16#[cfg(feature = "cuda")]
17pub use cuda::*;
18
19mod extension;
20#[cfg(test)]
21mod tests;
22pub use air::KeccakVmAir;
23pub use extension::*;
24use openvm_circuit::arch::*;
25
26// ==== Constants for register/memory adapter ====
27/// Register reads to get dst, src, len
28const KECCAK_REGISTER_READS: usize = 3;
29/// Number of cells to read/write in a single memory access
30const KECCAK_WORD_SIZE: usize = 4;
31/// Memory reads for absorb per row
32const KECCAK_ABSORB_READS: usize = KECCAK_RATE_BYTES / KECCAK_WORD_SIZE;
33/// Memory writes for digest per row
34const KECCAK_DIGEST_WRITES: usize = KECCAK_DIGEST_BYTES / KECCAK_WORD_SIZE;
35
36// ==== Do not change these constants! ====
37/// Total number of sponge bytes: number of rate bytes + number of capacity
38/// bytes.
39pub const KECCAK_WIDTH_BYTES: usize = 200;
40/// Total number of 16-bit limbs in the sponge.
41pub const KECCAK_WIDTH_U16S: usize = KECCAK_WIDTH_BYTES / 2;
42/// Number of rate bytes.
43pub const KECCAK_RATE_BYTES: usize = 136;
44/// Number of 16-bit rate limbs.
45pub const KECCAK_RATE_U16S: usize = KECCAK_RATE_BYTES / 2;
46/// Number of absorb rounds, equal to rate in u64s.
47pub const NUM_ABSORB_ROUNDS: usize = KECCAK_RATE_BYTES / 8;
48/// Number of capacity bytes.
49pub const KECCAK_CAPACITY_BYTES: usize = 64;
50/// Number of 16-bit capacity limbs.
51pub const KECCAK_CAPACITY_U16S: usize = KECCAK_CAPACITY_BYTES / 2;
52/// Number of output digest bytes used during the squeezing phase.
53pub const KECCAK_DIGEST_BYTES: usize = 32;
54/// Number of 64-bit digest limbs.
55pub const KECCAK_DIGEST_U64S: usize = KECCAK_DIGEST_BYTES / 8;
56
57pub type KeccakVmChip<F> = VmChipWrapper<F, KeccakVmFiller>;
58
59#[derive(derive_new::new, Clone, Copy)]
60pub struct KeccakVmExecutor {
61    pub offset: usize,
62    pub pointer_max_bits: usize,
63}
64
65#[derive(derive_new::new)]
66pub struct KeccakVmFiller {
67    pub bitwise_lookup_chip: SharedBitwiseOperationLookupChip<8>,
68    pub pointer_max_bits: usize,
69}