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