Use blake3 across the board.

This commit is contained in:
FiveMovesAhead 2024-09-20 02:08:21 +08:00
parent 020854c4c6
commit c2be2fd48e
9 changed files with 39 additions and 71 deletions

View File

@ -48,7 +48,7 @@ impl TryFrom<Map<String, Value>> for Solution {
#[derive(Serialize, Deserialize, Debug)]
pub struct Challenge {
pub seeds: [u64; 8],
pub seeds: [u64; 4],
pub difficulty: Difficulty,
pub weights: Vec<u32>,
pub values: Vec<u32>,
@ -63,7 +63,7 @@ pub const KERNEL: Option<CudaKernel> = None;
impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
#[cfg(feature = "cuda")]
fn cuda_generate_instance(
seeds: [u64; 8],
seeds: [u64; 4],
difficulty: &Difficulty,
dev: &Arc<CudaDevice>,
mut funcs: HashMap<&'static str, CudaFunction>,
@ -72,7 +72,7 @@ impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
Self::generate_instance(seeds, difficulty)
}
fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result<Challenge> {
fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result<Challenge> {
let mut rngs = RngArray::new(seeds);
let weights: Vec<u32> = (0..difficulty.num_items)

View File

@ -19,30 +19,30 @@ where
T: SolutionTrait,
U: DifficultyTrait<N>,
{
fn generate_instance(seeds: [u64; 8], difficulty: &U) -> Result<Self>;
fn generate_instance_from_str(seeds: [u64; 8], difficulty: &str) -> Result<Self> {
fn generate_instance(seeds: [u64; 4], difficulty: &U) -> Result<Self>;
fn generate_instance_from_str(seeds: [u64; 4], difficulty: &str) -> Result<Self> {
Self::generate_instance(seeds, &serde_json::from_str(difficulty)?)
}
fn generate_instance_from_vec(seeds: [u64; 8], difficulty: &Vec<i32>) -> Result<Self> {
fn generate_instance_from_vec(seeds: [u64; 4], difficulty: &Vec<i32>) -> Result<Self> {
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<Self> {
fn generate_instance_from_arr(seeds: [u64; 4], difficulty: &[i32; N]) -> Result<Self> {
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<CudaDevice>,
funcs: HashMap<&'static str, CudaFunction>,
) -> Result<Self>;
#[cfg(feature = "cuda")]
fn cuda_generate_instance_from_str(
seeds: [u64; 8],
seeds: [u64; 4],
difficulty: &str,
dev: &Arc<CudaDevice>,
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<i32>,
dev: &Arc<CudaDevice>,
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<CudaDevice>,
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]
}
}

View File

@ -56,7 +56,7 @@ impl TryFrom<Map<String, Value>> for Solution {
#[derive(Serialize, Deserialize, Debug)]
pub struct Challenge {
pub seeds: [u64; 8],
pub seeds: [u64; 4],
pub difficulty: Difficulty,
pub clauses: Vec<Vec<i32>>,
}
@ -68,7 +68,7 @@ pub const KERNEL: Option<CudaKernel> = None;
impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
#[cfg(feature = "cuda")]
fn cuda_generate_instance(
seeds: [u64; 8],
seeds: [u64; 4],
difficulty: &Difficulty,
dev: &Arc<CudaDevice>,
mut funcs: HashMap<&'static str, CudaFunction>,
@ -77,7 +77,7 @@ impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
Self::generate_instance(seeds, difficulty)
}
fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result<Self> {
fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result<Self> {
let mut rngs = RngArray::new(seeds);
let num_clauses = (difficulty.num_variables as f64
* difficulty.clauses_to_variables_percent as f64

View File

@ -47,7 +47,7 @@ impl TryFrom<Map<String, Value>> for Solution {
#[derive(Serialize, Deserialize, Debug)]
pub struct Challenge {
pub seeds: [u64; 8],
pub seeds: [u64; 4],
pub difficulty: Difficulty,
pub vector_database: Vec<Vec<f32>>,
pub query_vectors: Vec<Vec<f32>>,
@ -69,7 +69,7 @@ pub const KERNEL: Option<CudaKernel> = None;
impl ChallengeTrait<Solution, Difficulty, 2> for Challenge {
#[cfg(feature = "cuda")]
fn cuda_generate_instance(
seeds: [u64; 8],
seeds: [u64; 4],
difficulty: &Difficulty,
dev: &Arc<CudaDevice>,
mut funcs: HashMap<&'static str, CudaFunction>,
@ -78,7 +78,7 @@ impl ChallengeTrait<Solution, Difficulty, 2> for Challenge {
Self::generate_instance(seeds, difficulty)
}
fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result<Self> {
fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result<Self> {
let mut rngs = RngArray::new(seeds);
let uniform = Uniform::from(0.0..1.0);
let search_vectors = (0..100000)

View File

@ -47,7 +47,7 @@ impl TryFrom<Map<String, Value>> for Solution {
#[derive(Serialize, Deserialize, Debug)]
pub struct Challenge {
pub seeds: [u64; 8],
pub seeds: [u64; 4],
pub difficulty: Difficulty,
pub demands: Vec<i32>,
pub distance_matrix: Vec<Vec<i32>>,
@ -62,7 +62,7 @@ pub const KERNEL: Option<CudaKernel> = None;
impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
#[cfg(feature = "cuda")]
fn cuda_generate_instance(
seeds: [u64; 8],
seeds: [u64; 4],
difficulty: &Difficulty,
dev: &Arc<CudaDevice>,
mut funcs: HashMap<&'static str, CudaFunction>,
@ -71,7 +71,7 @@ impl crate::ChallengeTrait<Solution, Difficulty, 2> for Challenge {
Self::generate_instance(seeds, difficulty)
}
fn generate_instance(seeds: [u64; 8], difficulty: &Difficulty) -> Result<Challenge> {
fn generate_instance(seeds: [u64; 4], difficulty: &Difficulty) -> Result<Challenge> {
let mut rngs = RngArray::new(seeds);
let num_nodes = difficulty.num_nodes;

View File

@ -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<OutputData> for OutputMetaData {
}
impl From<OutputMetaData> 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)))
}
}

View File

@ -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 }

View File

@ -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);

View File

@ -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!({