From c2be2fd48e3f63261d855e9194d635be1e47aaa3 Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Fri, 20 Sep 2024 02:08:21 +0800 Subject: [PATCH] Use blake3 across the board. --- tig-challenges/src/knapsack.rs | 6 ++--- tig-challenges/src/lib.rs | 22 ++++++++--------- tig-challenges/src/satisfiability.rs | 6 ++--- tig-challenges/src/vector_search.rs | 6 ++--- tig-challenges/src/vehicle_routing.rs | 6 ++--- tig-structs/src/core.rs | 12 +++------ tig-utils/Cargo.toml | 4 +-- tig-utils/src/hash.rs | 35 ++++++--------------------- tig-worker/src/main.rs | 13 ++++------ 9 files changed, 39 insertions(+), 71 deletions(-) diff --git a/tig-challenges/src/knapsack.rs b/tig-challenges/src/knapsack.rs index 461c96d..42842c2 100644 --- a/tig-challenges/src/knapsack.rs +++ b/tig-challenges/src/knapsack.rs @@ -48,7 +48,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seeds: [u64; 8], + pub seeds: [u64; 4], pub difficulty: Difficulty, pub weights: Vec, pub values: Vec, @@ -63,7 +63,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -72,7 +72,7 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seeds, difficulty) } - fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result { + fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result { let mut rngs = RngArray::new(seeds); let weights: Vec = (0..difficulty.num_items) diff --git a/tig-challenges/src/lib.rs b/tig-challenges/src/lib.rs index 5b30761..0c953e0 100644 --- a/tig-challenges/src/lib.rs +++ b/tig-challenges/src/lib.rs @@ -19,30 +19,30 @@ where T: SolutionTrait, U: DifficultyTrait, { - fn generate_instance(seeds: [u64; 8], difficulty: &U) -> Result; - fn generate_instance_from_str(seeds: [u64; 8], difficulty: &str) -> Result { + fn generate_instance(seeds: [u64; 4], difficulty: &U) -> Result; + fn generate_instance_from_str(seeds: [u64; 4], difficulty: &str) -> Result { Self::generate_instance(seeds, &serde_json::from_str(difficulty)?) } - fn generate_instance_from_vec(seeds: [u64; 8], difficulty: &Vec) -> Result { + fn generate_instance_from_vec(seeds: [u64; 4], difficulty: &Vec) -> Result { match difficulty.as_slice().try_into() { Ok(difficulty) => Self::generate_instance_from_arr(seeds, &difficulty), Err(_) => Err(anyhow!("Invalid difficulty length")), } } - fn generate_instance_from_arr(seeds: [u64; 8], difficulty: &[i32; N]) -> Result { + fn generate_instance_from_arr(seeds: [u64; 4], difficulty: &[i32; N]) -> Result { Self::generate_instance(seeds, &U::from_arr(difficulty)) } #[cfg(feature = "cuda")] fn cuda_generate_instance( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &U, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, ) -> Result; #[cfg(feature = "cuda")] fn cuda_generate_instance_from_str( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &str, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, @@ -51,7 +51,7 @@ where } #[cfg(feature = "cuda")] fn cuda_generate_instance_from_vec( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &Vec, dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, @@ -63,7 +63,7 @@ where } #[cfg(feature = "cuda")] fn cuda_generate_instance_from_arr( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &[i32; N], dev: &Arc, funcs: HashMap<&'static str, CudaFunction>, @@ -95,18 +95,18 @@ pub struct CudaKernel { } pub struct RngArray { - rngs: [SmallRng; 8], + rngs: [SmallRng; 4], index: usize, } impl RngArray { - pub fn new(seeds: [u64; 8]) -> Self { + pub fn new(seeds: [u64; 4]) -> Self { let rngs = seeds.map(SmallRng::seed_from_u64); RngArray { rngs, index: 0 } } pub fn get_mut(&mut self) -> &mut SmallRng { - self.index = (self.index + 1) % 8; + self.index = (self.index + 1) % 4; &mut self.rngs[self.index] } } diff --git a/tig-challenges/src/satisfiability.rs b/tig-challenges/src/satisfiability.rs index f9ccb21..1793d67 100644 --- a/tig-challenges/src/satisfiability.rs +++ b/tig-challenges/src/satisfiability.rs @@ -56,7 +56,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seeds: [u64; 8], + pub seeds: [u64; 4], pub difficulty: Difficulty, pub clauses: Vec>, } @@ -68,7 +68,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -77,7 +77,7 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seeds, difficulty) } - fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result { + fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result { let mut rngs = RngArray::new(seeds); let num_clauses = (difficulty.num_variables as f64 * difficulty.clauses_to_variables_percent as f64 diff --git a/tig-challenges/src/vector_search.rs b/tig-challenges/src/vector_search.rs index 859876f..a97b944 100644 --- a/tig-challenges/src/vector_search.rs +++ b/tig-challenges/src/vector_search.rs @@ -47,7 +47,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seeds: [u64; 8], + pub seeds: [u64; 4], pub difficulty: Difficulty, pub vector_database: Vec>, pub query_vectors: Vec>, @@ -69,7 +69,7 @@ pub const KERNEL: Option = None; impl ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -78,7 +78,7 @@ impl ChallengeTrait for Challenge { Self::generate_instance(seeds, difficulty) } - fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result { + fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result { let mut rngs = RngArray::new(seeds); let uniform = Uniform::from(0.0..1.0); let search_vectors = (0..100000) diff --git a/tig-challenges/src/vehicle_routing.rs b/tig-challenges/src/vehicle_routing.rs index c7bcef3..4712467 100644 --- a/tig-challenges/src/vehicle_routing.rs +++ b/tig-challenges/src/vehicle_routing.rs @@ -47,7 +47,7 @@ impl TryFrom> for Solution { #[derive(Serialize, Deserialize, Debug)] pub struct Challenge { - pub seeds: [u64; 8], + pub seeds: [u64; 4], pub difficulty: Difficulty, pub demands: Vec, pub distance_matrix: Vec>, @@ -62,7 +62,7 @@ pub const KERNEL: Option = None; impl crate::ChallengeTrait for Challenge { #[cfg(feature = "cuda")] fn cuda_generate_instance( - seeds: [u64; 8], + seeds: [u64; 4], difficulty: &Difficulty, dev: &Arc, mut funcs: HashMap<&'static str, CudaFunction>, @@ -71,7 +71,7 @@ impl crate::ChallengeTrait for Challenge { Self::generate_instance(seeds, difficulty) } - fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result { + fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result { let mut rngs = RngArray::new(seeds); let num_nodes = difficulty.num_nodes; diff --git a/tig-structs/src/core.rs b/tig-structs/src/core.rs index c279fa7..db20051 100644 --- a/tig-structs/src/core.rs +++ b/tig-structs/src/core.rs @@ -2,7 +2,7 @@ use crate::{config::ProtocolConfig, serializable_struct_with_getters}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use std::collections::{HashMap, HashSet}; -use tig_utils::{jsonify, u64s_from_str}; +use tig_utils::{jsonify, u64s_from_str, u8s_from_str}; pub use tig_utils::{Frontier, MerkleBranch, MerkleHash, Point, PreciseNumber, Transaction, U256}; serializable_struct_with_getters! { @@ -129,7 +129,7 @@ serializable_struct_with_getters! { } } impl BenchmarkSettings { - pub fn calc_seeds(&self, nonce: u64) -> [u64; 8] { + pub fn calc_seeds(&self, nonce: u64) -> [u64; 4] { let mut seeds = u64s_from_str(jsonify(&self).as_str()); for seed in seeds.iter_mut() { *seed ^= nonce; @@ -169,13 +169,7 @@ impl From for OutputMetaData { } impl From for MerkleHash { fn from(data: OutputMetaData) -> Self { - let u64s = u64s_from_str(&jsonify(&data)); - let mut hash = [0u8; 32]; - hash[0..8].copy_from_slice(&u64s[0].to_le_bytes()); - hash[8..16].copy_from_slice(&u64s[1].to_le_bytes()); - hash[16..24].copy_from_slice(&u64s[2].to_le_bytes()); - hash[24..32].copy_from_slice(&u64s[3].to_le_bytes()); - MerkleHash(hash) + MerkleHash(u8s_from_str(&jsonify(&data))) } } diff --git a/tig-utils/Cargo.toml b/tig-utils/Cargo.toml index 495a358..1bfd8a0 100644 --- a/tig-utils/Cargo.toml +++ b/tig-utils/Cargo.toml @@ -10,16 +10,14 @@ edition.workspace = true [dependencies] anyhow = "1.0.81" base64 = "0.22.0" +blake3 = "1.5.4" flate2 = "1.0.28" hex = "0.4.3" js-sys = { version = "0.3.68", optional = true } -md5 = "0.7.0" -blake3 = "1.5.4" rand = { version = "0.8.5", default-features = false, features = ["std_rng"] } reqwest = { version = "0.12.2", optional = true } serde = { version = "1.0.196", features = ["derive"] } serde_json = { version = "1.0.113", features = ["preserve_order"] } -sha3 = "0.10.8" uint = "0.9.5" wasm-bindgen = { version = "0.2.91", optional = true } wasm-bindgen-futures = { version = "0.4.41", optional = true } diff --git a/tig-utils/src/hash.rs b/tig-utils/src/hash.rs index ad52b12..84b8d97 100644 --- a/tig-utils/src/hash.rs +++ b/tig-utils/src/hash.rs @@ -1,34 +1,13 @@ -use md5; -use sha3::{Digest, Keccak512}; - -pub fn md5_from_str(input: &str) -> String { - md5_from_bytes(input.as_bytes()) +pub fn u8s_from_str(input: &str) -> [u8; 32] { + blake3::hash(input.as_bytes()).into() } -pub fn md5_from_bytes(input: &[u8]) -> String { - format!("{:x}", md5::compute(input)) -} +pub fn u64s_from_str(input: &str) -> [u64; 4] { + let u8s = u8s_from_str(input); -pub fn u32_from_str(input: &str) -> u32 { - let result = md5::compute(input.as_bytes()); - let bytes = result[12..16].try_into().expect("Should not ever panic.."); - u32::from_le_bytes(bytes) -} - -pub fn u64_from_str(input: &str) -> u64 { - let result = md5::compute(input.as_bytes()); - let bytes = result[8..16].try_into().expect("Should not ever panic.."); - u64::from_le_bytes(bytes) -} - -pub fn u64s_from_str(input: &str) -> [u64; 8] { - let mut hasher = Keccak512::new(); - hasher.update(input.as_bytes()); - let result = hasher.finalize(); - - let mut output = [0u64; 8]; - for i in 0..8 { - let bytes = result[i * 8..(i + 1) * 8] + let mut output = [0u64; 4]; + for i in 0..4 { + let bytes = u8s[i * 8..(i + 1) * 8] .try_into() .expect("Should not ever panic.."); output[i] = u64::from_le_bytes(bytes); diff --git a/tig-worker/src/main.rs b/tig-worker/src/main.rs index ba1b42d..cd2935c 100644 --- a/tig-worker/src/main.rs +++ b/tig-worker/src/main.rs @@ -237,16 +237,13 @@ fn compute_batch( let tree = MerkleTree::new(hashes, batch_size as usize)?; let merkle_root = tree.calc_merkle_root(); - let mut merkle_proofs = HashMap::new(); + let mut merkle_proofs = Vec::new(); for (nonce, output_data) in output_data_map { let branch = tree.calc_merkle_branch((nonce - start_nonce) as usize)?; - merkle_proofs.insert( - nonce, - MerkleProof { - leaf: output_data, - branch: Some(branch), - }, - ); + merkle_proofs.push(MerkleProof { + leaf: output_data, + branch: Some(branch), + }); } let result = json!({