From de68f28e28b42d15543c8abcc5f437a846c120f1 Mon Sep 17 00:00:00 2001 From: peg Date: Fri, 7 Jun 2024 09:26:23 +0200 Subject: [PATCH 1/5] Add blake2 as built in hash fn and make HashingAlgorithm non-exaustive --- crates/shared/src/types.rs | 2 ++ crates/threshold-signature-server/src/helpers/user.rs | 4 +++- crates/threshold-signature-server/src/user/errors.rs | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/shared/src/types.rs b/crates/shared/src/types.rs index d26b11429..b1ae9840a 100644 --- a/crates/shared/src/types.rs +++ b/crates/shared/src/types.rs @@ -91,11 +91,13 @@ pub struct OcwMessageProactiveRefresh { #[derive(Clone, Debug, Eq, PartialEq)] #[cfg_attr(feature = "std", serde(rename = "hash"))] #[cfg_attr(feature = "std", serde(rename_all = "lowercase"))] +#[non_exhaustive] pub enum HashingAlgorithm { Sha1, Sha2, Sha3, Keccak, + Blake2_256, Custom(usize), } diff --git a/crates/threshold-signature-server/src/helpers/user.rs b/crates/threshold-signature-server/src/helpers/user.rs index a5aeadd66..89602b5d9 100644 --- a/crates/threshold-signature-server/src/helpers/user.rs +++ b/crates/threshold-signature-server/src/helpers/user.rs @@ -27,7 +27,7 @@ use reqwest::StatusCode; use sha1::{Digest as Sha1Digest, Sha1}; use sha2::{Digest as Sha256Digest, Sha256}; use sha3::{Digest as Sha3Digest, Keccak256, Sha3_256}; -use sp_core::{sr25519, Pair}; +use sp_core::{hashing::blake2_256, sr25519, Pair}; use subxt::{backend::legacy::LegacyRpcMethods, tx::PairSigner, utils::AccountId32, OnlineClient}; use synedrion::KeyShare; use tokio::time::timeout; @@ -212,9 +212,11 @@ pub async fn compute_hash( hash.copy_from_slice(&result); Ok(hash) }, + HashingAlgorithm::Blake2_256 => Ok(blake2_256(message)), HashingAlgorithm::Custom(i) => { let program = get_program(api, rpc, &programs_data[*i].program_pointer).await?; runtime.custom_hash(program.as_slice(), message).map_err(|e| e.into()) }, + _ => return Err(UserErr::UnknownHashingAlgorithm), } } diff --git a/crates/threshold-signature-server/src/user/errors.rs b/crates/threshold-signature-server/src/user/errors.rs index 9badb9c5f..8ad8cc04b 100644 --- a/crates/threshold-signature-server/src/user/errors.rs +++ b/crates/threshold-signature-server/src/user/errors.rs @@ -161,6 +161,8 @@ pub enum UserErr { SubstrateClient(#[from] entropy_client::substrate::SubstrateError), #[error("Cannot get subgroup signers: {0}")] SubgroupGet(#[from] entropy_client::user::SubgroupGetError), + #[error("Unknown hashing algorthim - user is using a newer version than us")] + UnknownHashingAlgorithm, } impl From for UserErr { From 85b07a3b2d2784078fef103d67fe3b590af8bdfa Mon Sep 17 00:00:00 2001 From: peg Date: Fri, 7 Jun 2024 09:36:00 +0200 Subject: [PATCH 2/5] Changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b4f80f2..67a59755a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). At the moment this project **does not** adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [[Unreleased]](https://github.com/entropyxyz/entropy-core/compare/release/v0.1.0...master) +## [Unreleased](https://github.com/entropyxyz/entropy-core/compare/release/v0.1.0...master) ### Breaking Changes - In [#866](https://github.com/entropyxyz/entropy-core/pull/866) timestamp was removed from `UserSignatureRequest` and replaced with block_number. Thus check_stale now uses block_number for stale checks @@ -16,6 +16,7 @@ At the moment this project **does not** adhere to - Add a way to change program modification account ([#843](https://github.com/entropyxyz/entropy-core/pull/843)) - Add support for `--mnemonic-file` and `THRESHOLD_SERVER_MNEMONIC` ([#864](https://github.com/entropyxyz/entropy-core/pull/864)) - Add validator helpers to cli ([#870](https://github.com/entropyxyz/entropy-core/pull/870)) +- Add blake2 as built in hash function and make HashingAlgorithm non-exhaustive ([#881](https://github.com/entropyxyz/entropy-core/pull/881) ### Changed - Move TSS mnemonic out of keystore [#853](https://github.com/entropyxyz/entropy-core/pull/853) From 1965279483e6171fa7ae150d77e2ee125727cc72 Mon Sep 17 00:00:00 2001 From: peg Date: Fri, 7 Jun 2024 09:59:41 +0200 Subject: [PATCH 3/5] Clippy --- crates/threshold-signature-server/src/helpers/user.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/threshold-signature-server/src/helpers/user.rs b/crates/threshold-signature-server/src/helpers/user.rs index 89602b5d9..8da19671d 100644 --- a/crates/threshold-signature-server/src/helpers/user.rs +++ b/crates/threshold-signature-server/src/helpers/user.rs @@ -217,6 +217,6 @@ pub async fn compute_hash( let program = get_program(api, rpc, &programs_data[*i].program_pointer).await?; runtime.custom_hash(program.as_slice(), message).map_err(|e| e.into()) }, - _ => return Err(UserErr::UnknownHashingAlgorithm), + _ => Err(UserErr::UnknownHashingAlgorithm), } } From 132ec5da8dcb0fe307ecb7ff2620569119e6db75 Mon Sep 17 00:00:00 2001 From: peg Date: Fri, 7 Jun 2024 10:49:27 +0200 Subject: [PATCH 4/5] Update hashes_test --- crates/threshold-signature-server/src/node_info/tests.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/threshold-signature-server/src/node_info/tests.rs b/crates/threshold-signature-server/src/node_info/tests.rs index 67498bdfc..19c33c564 100644 --- a/crates/threshold-signature-server/src/node_info/tests.rs +++ b/crates/threshold-signature-server/src/node_info/tests.rs @@ -45,6 +45,7 @@ async fn hashes_test() { HashingAlgorithm::Sha2, HashingAlgorithm::Sha3, HashingAlgorithm::Keccak, + HashingAlgorithm::Blake2_256, HashingAlgorithm::Custom(0), ] ); From 376d647029b50db0b68c877ce384f00b7021e6cb Mon Sep 17 00:00:00 2001 From: peg Date: Mon, 10 Jun 2024 10:26:00 +0200 Subject: [PATCH 5/5] Changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 67a59755a..106931eb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ At the moment this project **does not** adhere to ### Breaking Changes - In [#866](https://github.com/entropyxyz/entropy-core/pull/866) timestamp was removed from `UserSignatureRequest` and replaced with block_number. Thus check_stale now uses block_number for stale checks +- In [#881](https://github.com/entropyxyz/entropy-core/pull/881) the `HashingAlgorithm` enum is + given an additional variant `Blake2_256` and marked as `non_exhaustive` meaning we must handle the + case that an unknown variant is added in the future. ### Added - Add a way to change program modification account ([#843](https://github.com/entropyxyz/entropy-core/pull/843))