Skip to content

Commit f511582

Browse files
committed
Route ethrex's sha256 through the precompile
1 parent f07c553 commit f511582

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

  • crypto/ethrex-crypto/src

‎crypto/ethrex-crypto/src/lib.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//! Accelerated today:
99
//! - `keccak256`: a sponge over the `keccak_permute` precompile (riscv64; on
1010
//! host it falls back to software keccak for tests).
11+
//! - `sha256`: the padding wrapper over the SHA-256 compression precompile
12+
//! (riscv64; on host it falls back to the trait's own sha2 default).
1113
//! - `secp256k1_ecrecover`: the ECDSA recovery's 2-term linear combination is
1214
//! evaluated through the ECSM `ecsm_mul` precompile (riscv64), reconstructing
1315
//! the full point from x-only queries; on host / degenerate inputs it falls
@@ -63,6 +65,18 @@ impl Crypto for LambdaVmEcsmCrypto {
6365
#[cfg(not(target_arch = "riscv64"))]
6466
return keccak_hash(input);
6567
}
68+
69+
fn sha256(&self, input: &[u8]) -> [u8; 32] {
70+
// riscv64 guest: IV, padding and the length encoding in the wrapper,
71+
// the 64 rounds per block in the compression accelerator.
72+
#[cfg(target_arch = "riscv64")]
73+
return lambda_vm_syscalls::sha256::sha256(input);
74+
// host (tests / non-guest): the ecall isn't available off-target.
75+
// `NativeCrypto` takes every trait default, so this is the same sha2
76+
// call the default `sha256` would have made, with no extra dependency.
77+
#[cfg(not(target_arch = "riscv64"))]
78+
return ethrex_crypto::NativeCrypto.sha256(input);
79+
}
6680
}
6781

6882
// ── ECDSA secp256k1 recovery via the ECSM precompile ────────────────────────

0 commit comments

Comments
 (0)