Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use prehash directly and remove Identity256 #2012

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion contracts/burner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/crypto-verify/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/cyberpunk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/empty/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions contracts/floaty/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/hackatom/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/ibc-reflect-send/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/ibc-reflect/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/queue/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/reflect/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/staking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion contracts/virus/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ bench = false
[dependencies]
k256 = { version = "0.13.1", features = ["ecdsa"] }
ed25519-zebra = "3"
digest = "0.10"
rand_core = { version = "0.6", features = ["getrandom"] }
thiserror = "1.0.38"
# Not used directly, but needed to bump transitive dependency, see: https://github.com/CosmWasm/cosmwasm/pull/1899 for details.
ecdsa = "0.16.2"

[dev-dependencies]
criterion = "0.5.1"
digest = "0.10"
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }
serde_json = "1.0.40"
sha2 = "0.10"
Expand Down
39 changes: 0 additions & 39 deletions packages/crypto/src/identity_digest.rs

This file was deleted.

1 change: 0 additions & 1 deletion packages/crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
mod backtrace;
mod ed25519;
mod errors;
mod identity_digest;
mod secp256k1;

#[doc(hidden)]
Expand Down
13 changes: 4 additions & 9 deletions packages/crypto/src/secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use digest::{Digest, Update}; // trait
use k256::{
ecdsa::signature::DigestVerifier, // traits
ecdsa::signature::hazmat::PrehashVerifier, // traits
ecdsa::{RecoveryId, Signature, VerifyingKey}, // type aliases
};

use crate::errors::{CryptoError, CryptoResult};
use crate::identity_digest::Identity256;

/// Max length of a message hash for secp256k1 verification in bytes.
/// This is typically a 32 byte output of e.g. SHA-256 or Keccak256. In theory shorter values
Expand Down Expand Up @@ -47,9 +45,6 @@ pub fn secp256k1_verify(
let signature = read_signature(signature)?;
check_pubkey(public_key)?;

// Already hashed, just build Digest container
let message_digest = Identity256::new().chain(message_hash);

let mut signature = Signature::from_bytes(&signature.into())
.map_err(|e| CryptoError::generic_err(e.to_string()))?;

Expand All @@ -63,7 +58,7 @@ pub fn secp256k1_verify(
let public_key = VerifyingKey::from_sec1_bytes(public_key)
.map_err(|e| CryptoError::generic_err(e.to_string()))?;

match public_key.verify_digest(message_digest, &signature) {
match public_key.verify_prehash(&message_hash, &signature) {
Ok(()) => Ok(true),
Err(_) => Ok(false),
}
Expand Down Expand Up @@ -116,8 +111,7 @@ pub fn secp256k1_recover_pubkey(
.map_err(|e| CryptoError::generic_err(e.to_string()))?;

// Recover
let message_digest = Identity256::new().chain(message_hash);
let pubkey = VerifyingKey::recover_from_digest(message_digest, &signature, id)
let pubkey = VerifyingKey::recover_from_prehash(&message_hash, &signature, id)
.map_err(|e| CryptoError::generic_err(e.to_string()))?;
let encoded: Vec<u8> = pubkey.to_encoded_point(false).as_bytes().into();
Ok(encoded)
Expand Down Expand Up @@ -177,6 +171,7 @@ fn check_pubkey(data: &[u8]) -> Result<(), InvalidSecp256k1PubkeyFormat> {
mod tests {
use super::*;

use digest::{Digest, Update}; // trait
use hex_literal::hex;
use k256::{
ecdsa::signature::DigestSigner, // trait
Expand Down
Loading