This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 791
feat: convert signing to k256 #72
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
953e50a
feat: convert signing to k256
roynalnaruto d303f60
fix: pass pre-hashed message to sig verification
gakonst f0fbed8
feat: wrap the hash to a Digest implementation
shamatar 27a3182
refactor: cleanup and move digest impl to separate file
gakonst 8b66c71
chore: adjust abigen tests due to rust update
gakonst e6d6f37
test: add byte equality test between ethers-rs / web3.js signatures
gakonst 8d7a165
fix(keys): use 512 blocks for sha256
shamatar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//! This is a helper module used to pass the pre-hashed message for signing to the | ||
//! `sign_digest` methods of K256. | ||
use crate::types::H256; | ||
use k256::ecdsa::signature::digest::{ | ||
generic_array::GenericArray, BlockInput, Digest, FixedOutput, Output, Reset, Update, | ||
}; | ||
|
||
pub type Sha256Proxy = ProxyDigest<sha2::Sha256>; | ||
|
||
#[derive(Clone)] | ||
pub enum ProxyDigest<D: Digest> { | ||
Proxy(Output<D>), | ||
Digest(D), | ||
} | ||
|
||
impl<D: Digest + Clone> From<H256> for ProxyDigest<D> | ||
where | ||
GenericArray<u8, <D as Digest>::OutputSize>: Copy, | ||
{ | ||
fn from(src: H256) -> Self { | ||
ProxyDigest::Proxy(*GenericArray::from_slice(src.as_bytes())) | ||
} | ||
} | ||
|
||
impl<D: Digest> Default for ProxyDigest<D> { | ||
fn default() -> Self { | ||
ProxyDigest::Digest(D::new()) | ||
} | ||
} | ||
|
||
impl<D: Digest> Update for ProxyDigest<D> { | ||
// we update only if we are digest | ||
fn update(&mut self, data: impl AsRef<[u8]>) { | ||
match self { | ||
ProxyDigest::Digest(ref mut d) => { | ||
d.update(data); | ||
} | ||
ProxyDigest::Proxy(..) => { | ||
unreachable!("can not update if we are proxy"); | ||
} | ||
} | ||
} | ||
|
||
// we chain only if we are digest | ||
fn chain(self, data: impl AsRef<[u8]>) -> Self { | ||
match self { | ||
ProxyDigest::Digest(d) => ProxyDigest::Digest(d.chain(data)), | ||
ProxyDigest::Proxy(..) => { | ||
unreachable!("can not update if we are proxy"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<D: Digest> Reset for ProxyDigest<D> { | ||
// make new one | ||
fn reset(&mut self) { | ||
*self = Self::default(); | ||
} | ||
} | ||
|
||
impl<D: Digest> BlockInput for ProxyDigest<D> { | ||
type BlockSize = D::OutputSize; | ||
} | ||
|
||
impl<D: Digest> FixedOutput for ProxyDigest<D> { | ||
// we default to the output of the orignal digest | ||
type OutputSize = D::OutputSize; | ||
|
||
fn finalize_into(self, out: &mut GenericArray<u8, Self::OutputSize>) { | ||
match self { | ||
ProxyDigest::Digest(d) => { | ||
*out = d.finalize(); | ||
} | ||
ProxyDigest::Proxy(p) => { | ||
*out = p; | ||
} | ||
} | ||
} | ||
|
||
fn finalize_into_reset(&mut self, out: &mut GenericArray<u8, Self::OutputSize>) { | ||
let s = core::mem::replace(self, Self::default()); | ||
s.finalize_into(out); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a bit more straightforward to use
RecoverableSignPrimitive:: try_sign_recoverable_prehashed
(which is impl'd onk256::Scalar
) for this:https://docs.rs/ecdsa/0.8.3/ecdsa/hazmat/trait.RecoverableSignPrimitive.html
The API to compute an ephemeral scalar with RFC6979 is public:
https://docs.rs/ecdsa/0.8.3/ecdsa/rfc6979/fn.generate_k.html
You can convert the raw message digest to a scalar using
Scalar::from_bytes_reduced