Skip to content

Commit ec93c83

Browse files
committed
add keccak
2 parents 3733b59 + 2afb7cb commit ec93c83

33 files changed

Lines changed: 3846 additions & 72 deletions
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.attribute 5, "rv64i2p1_m2p0_zmmul1p0"
2+
.Lfunc_end0:
3+
.globl main
4+
main:
5+
# Allocate 200 bytes on the stack for the Keccak state (25 × u64)
6+
# Stack grows downward; sp is already initialized
7+
addi sp, sp, -200
8+
9+
# Zero out the state (200 bytes = 25 doublewords)
10+
# Using a loop to store zeros
11+
mv t0, sp # t0 = pointer to state
12+
li t1, 25 # t1 = counter (25 doublewords)
13+
.Lzero_loop:
14+
sd zero, 0(t0) # Store zero doubleword
15+
addi t0, t0, 8 # Next doubleword
16+
addi t1, t1, -1 # Decrement counter
17+
bnez t1, .Lzero_loop
18+
19+
# Call keccak-f[1600] permutation
20+
# a0 = pointer to 200-byte state
21+
# a7 = syscall number (0xFFFFFFFE = u64::MAX - 1)
22+
mv a0, sp
23+
li a7, -2 # 0xFFFFFFFFFFFFFFFE in 64-bit
24+
ecall
25+
26+
# Restore stack and halt
27+
addi sp, sp, 200
28+
li a0, 0 # exit code = 0
29+
li a7, 93 # sys_exit
30+
ecall
31+
.Lfunc_end1:
32+
.size main, .Lfunc_end1-main

executor/programs/rust/ethrex/Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

executor/programs/rust/ethrex/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ c-kzg = { version = "2.1.1", features = ["eip-7594"] }
1313

1414
[patch.crates-io]
1515
c-kzg = { git = "https://github.com/risc0/c-kzg-4844", tag = "c-kzg/v2.1.1-risczero.0" }
16+
tiny-keccak = { path = "../../tiny-keccak-patch" }
1617

executor/programs/tiny-keccak-patch/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
2+
#
3+
# When uploading crates to the registry Cargo will automatically
4+
# "normalize" Cargo.toml files for maximal compatibility
5+
# with all versions of Cargo and also rewrite `path` dependencies
6+
# to registry (e.g., crates.io) dependencies
7+
#
8+
# If you believe there's an error in this file please file an
9+
# issue against the rust-lang/cargo repository. If you're
10+
# editing this file be aware that the upstream Cargo.toml
11+
# will likely look very different (and much more reasonable)
12+
13+
[package]
14+
edition = "2018"
15+
name = "tiny-keccak"
16+
version = "2.0.2"
17+
authors = ["debris <marek.kotewicz@gmail.com>"]
18+
description = "An implementation of Keccak derived functions."
19+
homepage = "https://github.com/debris/tiny-keccak"
20+
keywords = ["hash", "sha3", "keccak", "crypto", "kangarootwelve"]
21+
categories = ["cryptography", "no-std"]
22+
license = "CC0-1.0"
23+
[package.metadata.docs.rs]
24+
all-features = true
25+
[profile.dev]
26+
opt-level = 3
27+
debug = false
28+
29+
[profile.test]
30+
opt-level = 3
31+
debug = false
32+
33+
[[example]]
34+
name = "sha3"
35+
required-features = ["sha3"]
36+
37+
[[test]]
38+
name = "keccak"
39+
required-features = ["keccak"]
40+
41+
[[test]]
42+
name = "cshake"
43+
required-features = ["cshake"]
44+
45+
[[test]]
46+
name = "tuple_hash"
47+
required-features = ["tuple_hash"]
48+
49+
[[test]]
50+
name = "kangaroo"
51+
required-features = ["k12"]
52+
53+
[[test]]
54+
name = "sha3"
55+
required-features = ["sha3"]
56+
57+
[[test]]
58+
name = "shake"
59+
required-features = ["shake"]
60+
61+
[[test]]
62+
name = "kmac"
63+
required-features = ["kmac"]
64+
65+
[[test]]
66+
name = "parallel_hash"
67+
required-features = ["parallel_hash"]
68+
69+
[[bench]]
70+
name = "keccak"
71+
required-features = ["keccak"]
72+
73+
[[bench]]
74+
name = "kangaroo"
75+
required-features = ["k12"]
76+
[dependencies.crunchy]
77+
version = "0.2.2"
78+
79+
[features]
80+
cshake = []
81+
default = []
82+
fips202 = ["keccak", "shake", "sha3"]
83+
k12 = []
84+
keccak = []
85+
kmac = ["cshake"]
86+
parallel_hash = ["cshake"]
87+
sha3 = []
88+
shake = []
89+
sp800 = ["cshake", "kmac", "tuple_hash"]
90+
tuple_hash = ["cshake"]
91+
[badges.travis-ci]
92+
branch = "master"
93+
repository = "debris/tiny-keccak"
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! The `cSHAKE` extendable-output functions defined in [`SP800-185`].
2+
//!
3+
//! [`SP800-185`]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
4+
5+
use crate::{bits_to_rate, keccakf::KeccakF, left_encode, Hasher, KeccakState, Xof};
6+
7+
/// The `cSHAKE` extendable-output functions defined in [`SP800-185`].
8+
///
9+
/// # Usage
10+
///
11+
/// ```toml
12+
/// [dependencies]
13+
/// tiny-keccak = { version = "2.0.0", features = ["cshake"] }
14+
/// ```
15+
///
16+
/// [`SP800-185`]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
17+
#[derive(Clone)]
18+
pub struct CShake {
19+
state: KeccakState<KeccakF>,
20+
}
21+
22+
impl CShake {
23+
const DELIM: u8 = 0x04;
24+
25+
/// Creates new [`CShake`] hasher with a security level of 128 bits.
26+
///
27+
/// [`CShake`]: struct.CShake.html
28+
pub fn v128(name: &[u8], custom_string: &[u8]) -> CShake {
29+
CShake::new(name, custom_string, 128)
30+
}
31+
32+
/// Creates new [`CShake`] hasher with a security level of 256 bits.
33+
///
34+
/// [`CShake`]: struct.CShake.html
35+
pub fn v256(name: &[u8], custom_string: &[u8]) -> CShake {
36+
CShake::new(name, custom_string, 256)
37+
}
38+
39+
pub(crate) fn new(name: &[u8], custom_string: &[u8], bits: usize) -> CShake {
40+
let rate = bits_to_rate(bits);
41+
// if there is no name and no customization string
42+
// cSHAKE is SHAKE
43+
if name.is_empty() && custom_string.is_empty() {
44+
let state = KeccakState::new(rate, 0x1f);
45+
return CShake { state };
46+
}
47+
48+
let mut state = KeccakState::new(rate, Self::DELIM);
49+
state.update(left_encode(rate).value());
50+
state.update(left_encode(name.len() * 8).value());
51+
state.update(name);
52+
state.update(left_encode(custom_string.len() * 8).value());
53+
state.update(custom_string);
54+
state.fill_block();
55+
CShake { state }
56+
}
57+
58+
pub(crate) fn fill_block(&mut self) {
59+
self.state.fill_block();
60+
}
61+
}
62+
63+
impl Hasher for CShake {
64+
fn update(&mut self, input: &[u8]) {
65+
self.state.update(input);
66+
}
67+
68+
fn finalize(self, output: &mut [u8]) {
69+
self.state.finalize(output);
70+
}
71+
}
72+
73+
impl Xof for CShake {
74+
fn squeeze(&mut self, output: &mut [u8]) {
75+
self.state.squeeze(output);
76+
}
77+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
//! The `KangarooTwelve` hash function defined [`here`].
2+
//!
3+
//! [`here`]: https://eprint.iacr.org/2016/770.pdf
4+
5+
use crate::{bits_to_rate, keccakp::KeccakP, EncodedLen, Hasher, IntoXof, KeccakState, Xof};
6+
7+
fn encode_len(len: usize) -> EncodedLen {
8+
let len_view = (len as u64).to_be_bytes();
9+
let offset = len_view.iter().position(|i| *i != 0).unwrap_or(8);
10+
let mut buffer = [0u8; 9];
11+
buffer[..8].copy_from_slice(&len_view);
12+
buffer[8] = 8 - offset as u8;
13+
14+
EncodedLen { offset, buffer }
15+
}
16+
17+
/// The `KangarooTwelve` hash function defined [`here`].
18+
///
19+
/// # Usage
20+
///
21+
/// ```toml
22+
/// [dependencies]
23+
/// tiny-keccak = { version = "2.0.0", features = ["k12"] }
24+
/// ```
25+
///
26+
/// [`here`]: https://eprint.iacr.org/2016/770.pdf
27+
#[derive(Clone)]
28+
pub struct KangarooTwelve<T> {
29+
state: KeccakState<KeccakP>,
30+
current_chunk: KeccakState<KeccakP>,
31+
custom_string: Option<T>,
32+
written: usize,
33+
chunks: usize,
34+
}
35+
36+
impl<T> KangarooTwelve<T> {
37+
const MAX_CHUNK_SIZE: usize = 8192;
38+
39+
/// Creates new [`KangarooTwelve`] hasher with a security level of 128 bits.
40+
///
41+
/// [`KangarooTwelve`]: struct.KangarooTwelve.html
42+
pub fn new(custom_string: T) -> Self {
43+
let rate = bits_to_rate(128);
44+
KangarooTwelve {
45+
state: KeccakState::new(rate, 0),
46+
current_chunk: KeccakState::new(rate, 0x0b),
47+
custom_string: Some(custom_string),
48+
written: 0,
49+
chunks: 0,
50+
}
51+
}
52+
}
53+
54+
impl<T: AsRef<[u8]>> Hasher for KangarooTwelve<T> {
55+
fn update(&mut self, input: &[u8]) {
56+
let mut to_absorb = input;
57+
if self.chunks == 0 {
58+
let todo = core::cmp::min(Self::MAX_CHUNK_SIZE - self.written, to_absorb.len());
59+
self.state.update(&to_absorb[..todo]);
60+
self.written += todo;
61+
to_absorb = &to_absorb[todo..];
62+
63+
if to_absorb.len() > 0 && self.written == Self::MAX_CHUNK_SIZE {
64+
self.state.update(&[0x03, 0, 0, 0, 0, 0, 0, 0]);
65+
self.written = 0;
66+
self.chunks += 1;
67+
}
68+
}
69+
70+
while to_absorb.len() > 0 {
71+
if self.written == Self::MAX_CHUNK_SIZE {
72+
let mut chunk_hash = [0u8; 32];
73+
let current_chunk = self.current_chunk.clone();
74+
self.current_chunk.reset();
75+
current_chunk.finalize(&mut chunk_hash);
76+
self.state.update(&chunk_hash);
77+
self.written = 0;
78+
self.chunks += 1;
79+
}
80+
81+
let todo = core::cmp::min(Self::MAX_CHUNK_SIZE - self.written, to_absorb.len());
82+
self.current_chunk.update(&to_absorb[..todo]);
83+
self.written += todo;
84+
to_absorb = &to_absorb[todo..];
85+
}
86+
}
87+
88+
fn finalize(self, output: &mut [u8]) {
89+
let mut xof = self.into_xof();
90+
xof.squeeze(output);
91+
}
92+
}
93+
94+
/// The `KangarooTwelve` extendable-output function defined [`here`].
95+
///
96+
/// # Usage
97+
///
98+
/// ```toml
99+
/// [dependencies]
100+
/// tiny-keccak = { version = "2.0.0", features = ["k12"] }
101+
/// ```
102+
///
103+
/// # Example
104+
///
105+
/// ```
106+
/// # use tiny_keccak::{KangarooTwelve, Xof, IntoXof, Hasher};
107+
/// let input = b"hello world";
108+
/// let mut output = [0u8; 64];
109+
/// let mut hasher = KangarooTwelve::new(b"");
110+
/// hasher.update(input);
111+
/// let mut xof = hasher.into_xof();
112+
/// xof.squeeze(&mut output[..32]);
113+
/// xof.squeeze(&mut output[32..]);
114+
/// ```
115+
///
116+
/// ---
117+
///
118+
/// [`KangarooTwelveXof`] can be created only by using [`KangarooTwelve::IntoXof`] interface.
119+
///
120+
/// [`here`]: https://eprint.iacr.org/2016/770.pdf
121+
/// [`KangarooTwelveXof`]: struct.KangarooTwelveXof.html
122+
/// [`KangarooTwelve::IntoXof`]: struct.KangarooTwelve.html#impl-IntoXof
123+
#[derive(Clone)]
124+
pub struct KangarooTwelveXof {
125+
state: KeccakState<KeccakP>,
126+
}
127+
128+
impl<T: AsRef<[u8]>> IntoXof for KangarooTwelve<T> {
129+
type Xof = KangarooTwelveXof;
130+
131+
fn into_xof(mut self) -> KangarooTwelveXof {
132+
let custom_string = self
133+
.custom_string
134+
.take()
135+
.expect("KangarooTwelve cannot be initialized without custom_string; qed");
136+
let encoded_len = encode_len(custom_string.as_ref().len());
137+
self.update(custom_string.as_ref());
138+
self.update(encoded_len.value());
139+
140+
if self.chunks == 0 {
141+
self.state.delim = 0x07;
142+
} else {
143+
let encoded_chunks = encode_len(self.chunks);
144+
let mut tmp_chunk = [0u8; 32];
145+
self.current_chunk.finalize(&mut tmp_chunk);
146+
self.state.update(&tmp_chunk);
147+
self.state.update(encoded_chunks.value());
148+
self.state.update(&[0xff, 0xff]);
149+
self.state.delim = 0x06;
150+
}
151+
152+
KangarooTwelveXof { state: self.state }
153+
}
154+
}
155+
156+
impl Xof for KangarooTwelveXof {
157+
fn squeeze(&mut self, output: &mut [u8]) {
158+
self.state.squeeze(output);
159+
}
160+
}

0 commit comments

Comments
 (0)