diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2022-05-03 07:59:24 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2022-05-03 07:59:24 +0200 |
| commit | 2cf5f650b8fdbd1992f3d65ad6e102087e74f896 (patch) | |
| tree | 7c501fcd82b33a6203cb6b55c4820d698c1d83e5 /src/account/prehashed.rs | |
| parent | 1c13a451bc2e56ce8531d5da683a3c7aa6d4d887 (diff) | |
Use Rust Secp256k1 implementation from RustCrypto project (#92)
This PR ports `hdwallet` to use the `k256` crate - a native Rust implementation of secp256k1 elliptic curve and ECDSA signing.
The motivation for this change is that we can just use the `wasm32-wasi` Rust target without requiring a WASI C toolchain! Notice that the CI no longer requires installing the WASI SDK in order to build the WASI target.
Diffstat (limited to 'src/account/prehashed.rs')
| -rw-r--r-- | src/account/prehashed.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/account/prehashed.rs b/src/account/prehashed.rs new file mode 100644 index 0000000..5952164 --- /dev/null +++ b/src/account/prehashed.rs @@ -0,0 +1,61 @@ +//! The `k256` crate only supports signing messages by passing message bytes and +//! specifying a hasher, or specifying an already partially updated hasher. +//! +//! This module provides a `Digest` implementation that can be used as an +//! allowing `k256` signing to be done on a prehashed messsage. + +use k256::ecdsa::digest::{ + consts::U32, generic_array::GenericArray, BlockInput, FixedOutput, Reset, Update, +}; +use sha2::{digest::core_api::BlockSizeUser, Digest as _, Sha256}; +use std::mem; + +/// A pre-hashed meessage. +#[derive(Clone)] +pub enum Prehashed { + /// The pre-hashed message to use for signing. + Message([u8; 32]), + /// The hasher used for rfc-6979 nonce generation for deterministic ECDSA + /// signature computation. + Rfc6979(Sha256), +} + +impl Default for Prehashed { + fn default() -> Self { + Self::Rfc6979(Sha256::default()) + } +} + +impl BlockInput for Prehashed { + type BlockSize = <Sha256 as BlockSizeUser>::BlockSize; +} + +impl FixedOutput for Prehashed { + type OutputSize = U32; + + fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>) { + match self { + Self::Message(message) => *out = message.into(), + Self::Rfc6979(hasher) => hasher.finalize_into(out), + } + } + + fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) { + mem::take(self).finalize_into(out) + } +} + +impl Reset for Prehashed { + fn reset(&mut self) { + *self = Default::default(); + } +} + +impl Update for Prehashed { + fn update(&mut self, data: impl AsRef<[u8]>) { + match self { + Prehashed::Message(_) => unimplemented!(), + Prehashed::Rfc6979(hasher) => hasher.update(data), + } + } +} |
