openvm_keccak256_circuit/
lib.rs

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