mod.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 mod hash; 17 18 #[cfg(test)] 19 use alphavm_utilities::Uniform; 20 21 use crate::Hash; 22 use alphavm_console_types::environment::prelude::*; 23 24 use tiny_keccak::{Hasher, Keccak as TinyKeccak, Sha3 as TinySha3}; 25 26 /// The Keccak-224 hash function. 27 pub type Keccak224 = Keccak<{ KeccakType::Keccak as u8 }, 224>; 28 /// The Keccak-256 hash function. 29 pub type Keccak256 = Keccak<{ KeccakType::Keccak as u8 }, 256>; 30 /// The Keccak-384 hash function. 31 pub type Keccak384 = Keccak<{ KeccakType::Keccak as u8 }, 384>; 32 /// The Keccak-512 hash function. 33 pub type Keccak512 = Keccak<{ KeccakType::Keccak as u8 }, 512>; 34 35 /// The SHA3-224 hash function. 36 pub type Sha3_224 = Keccak<{ KeccakType::Sha3 as u8 }, 224>; 37 /// The SHA3-256 hash function. 38 pub type Sha3_256 = Keccak<{ KeccakType::Sha3 as u8 }, 256>; 39 /// The SHA3-384 hash function. 40 pub type Sha3_384 = Keccak<{ KeccakType::Sha3 as u8 }, 384>; 41 /// The SHA3-512 hash function. 42 pub type Sha3_512 = Keccak<{ KeccakType::Sha3 as u8 }, 512>; 43 44 /// A helper to specify the hash type. 45 enum KeccakType { 46 Keccak, 47 Sha3, 48 } 49 50 /// The sponge construction `Sponge[f, pad, r]` is a function that takes a variable-length input 51 /// and produces a fixed-length output (the hash value). 52 /// 53 /// The permutation `f` is a function that takes a fixed-length input and produces a fixed-length output, 54 /// defined as `f = Keccak-f[b]`, where `b := 25 * 2^l` is the width of the permutation, 55 /// and `l` is the log width of the permutation. 56 /// For our case, `l = 6`, thus `b = 1600`. 57 /// 58 /// The padding rule `pad` is a function that takes a variable-length input and produces a fixed-length output. 59 /// In Keccak, `pad` is a multi-rate padding, defined as `pad(M) = M || 0x01 || 0x00…0x00 || 0x80`, 60 /// where `M` is the input data, and `0x01 || 0x00…0x00 || 0x80` is the padding. 61 /// In SHA-3, `pad` is a SHAKE, defined as `pad(M) = M || 0x06 || 0x00…0x00 || 0x80`, 62 /// where `M` is the input data, and `0x06 || 0x00…0x00 || 0x80` is the padding. 63 /// 64 /// The bitrate `r` is the number of bits that are absorbed into the sponge state in each iteration 65 /// of the absorbing phase. 66 /// 67 /// In addition, the capacity is defined as `c := b - r`. 68 #[derive(Copy, Clone, Debug, Default, PartialEq)] 69 pub struct Keccak<const TYPE: u8, const VARIANT: usize>;