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

CHIA-622 switch to use clvmr hashing only + clvm_rs 0.8.0 #609

Closed
wants to merge 5 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
78 changes: 69 additions & 9 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ chia-traits = { workspace = true, optional = true }
chia-puzzles = { workspace = true, optional = true }
clvm-traits = { workspace = true, optional = true }
clvm-utils = { workspace = true, optional = true }
clvmr = { workspace = true }

[features]
default = [
Expand All @@ -83,6 +84,8 @@ puzzles = ["dep:chia-puzzles"]
clvm-traits = ["dep:clvm-traits"]
clvm-utils = ["dep:clvm-utils"]

openssl = ["clvmr/openssl"]

[profile.release]
lto = "thin"

Expand All @@ -101,7 +104,7 @@ clvm-utils = { path = "./crates/clvm-utils", version = "0.10.0" }
clvm-derive = { path = "./crates/clvm-derive", version = "0.10.0" }
chia-fuzz = { path = "./crates/chia-consensus/fuzz", version = "0.10.0" }
blst = { version = "0.3.12", features = ["portable"] }
clvmr = "0.7.0"
clvmr = "0.8.0"
syn = "2.0.70"
quote = "1.0.32"
proc-macro2 = "1.0.84"
Expand Down
1 change: 1 addition & 0 deletions crates/chia-bls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chia-traits = { workspace = true }
chia_py_streamable_macro = { workspace = true, optional = true }
anyhow = { workspace = true }
sha2 = { workspace = true }
clvmr = { workspace = true }
hkdf = { workspace = true }
blst = { workspace = true }
hex = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/chia-bls/src/bls_cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Borrow;
use std::num::NonZeroUsize;

use clvmr::sha2::Sha256;
use lru::LruCache;
use sha2::{Digest, Sha256};

use crate::{aggregate_verify_gt, hash_to_g2};
use crate::{GTElement, PublicKey, Signature};
Expand Down Expand Up @@ -55,7 +55,7 @@ impl BlsCache {
let mut hasher = Sha256::new();
hasher.update(pk.borrow().to_bytes());
hasher.update(msg.as_ref());
let hash: [u8; 32] = hasher.finalize().into();
let hash: [u8; 32] = hasher.finalize();

// If the pairing is in the cache, we don't need to recalculate it.
if let Some(pairing) = self.cache.get(&hash).cloned() {
Expand All @@ -69,7 +69,7 @@ impl BlsCache {

let mut hasher = Sha256::new();
hasher.update(&aug_msg);
let hash: [u8; 32] = hasher.finalize().into();
let hash: [u8; 32] = hasher.finalize();

let pairing = aug_hash.pair(pk.borrow());
self.cache.put(hash, pairing.clone());
Expand Down Expand Up @@ -272,7 +272,7 @@ pub mod tests {

let mut hasher = Sha256::new();
hasher.update(aug_msg);
let hash: [u8; 32] = hasher.finalize().into();
let hash: [u8; 32] = hasher.finalize();

// The first key should have been removed, since it's the oldest that's been accessed.
assert!(!bls_cache.cache.contains(&hash));
Expand Down
2 changes: 1 addition & 1 deletion crates/chia-bls/src/gtelement.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use blst::*;
use chia_traits::chia_error::Result;
use chia_traits::{read_bytes, Streamable};
use sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::io::Cursor;
Expand Down
6 changes: 3 additions & 3 deletions crates/chia-bls/src/public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{DerivableKey, Error, Result};

use blst::*;
use chia_traits::{read_bytes, Streamable};
use sha2::{digest::FixedOutput, Digest, Sha256};
use clvmr::sha2::Sha256;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::io::Cursor;
Expand Down Expand Up @@ -143,7 +143,7 @@ impl PublicKey {
pub fn get_fingerprint(&self) -> u32 {
let mut hasher = Sha256::new();
hasher.update(self.to_bytes());
let hash: [u8; 32] = hasher.finalize_fixed().into();
let hash: [u8; 32] = hasher.finalize();
u32::from_be_bytes(hash[0..4].try_into().unwrap())
}
}
Expand Down Expand Up @@ -252,7 +252,7 @@ impl DerivableKey for PublicKey {
let mut hasher = Sha256::new();
hasher.update(self.to_bytes());
hasher.update(idx.to_be_bytes());
let digest: [u8; 32] = hasher.finalize_fixed().into();
let digest: [u8; 32] = hasher.finalize();

let p1 = unsafe {
let mut nonce = MaybeUninit::<blst_scalar>::uninit();
Expand Down
8 changes: 4 additions & 4 deletions crates/chia-bls/src/secret_key.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{DerivableKey, Error, PublicKey, Result};
use blst::*;
use chia_traits::{read_bytes, Streamable};
use clvmr::sha2::Sha256;
use hkdf::HkdfExtract;
use sha2::{Digest, Sha256};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::io::Cursor;
Expand Down Expand Up @@ -35,7 +35,7 @@ fn flip_bits(input: [u8; 32]) -> [u8; 32] {
}

fn ikm_to_lamport_sk(ikm: &[u8; 32], salt: [u8; 4]) -> [u8; 255 * 32] {
let mut extracter = HkdfExtract::<Sha256>::new(Some(&salt));
let mut extracter = HkdfExtract::<sha2::Sha256>::new(Some(&salt));
extracter.input_ikm(ikm);
let (_, h) = extracter.finalize();

Expand Down Expand Up @@ -63,13 +63,13 @@ fn to_lamport_pk(ikm: [u8; 32], idx: u32) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(lamport0);
hasher.update(lamport1);
hasher.finalize().into()
hasher.finalize()
}

fn sha256(bytes: &[u8]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update(bytes);
hasher.finalize().into()
hasher.finalize()
}

pub fn is_all_zero(buf: &[u8]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/chia-bls/src/signature.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Error, GTElement, PublicKey, Result, SecretKey};
use blst::*;
use chia_traits::{read_bytes, Streamable};
use sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
use std::borrow::Borrow;
use std::fmt;
use std::hash::{Hash, Hasher};
Expand Down
1 change: 0 additions & 1 deletion crates/chia-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ py-bindings = ["dep:pyo3", "dep:chia_py_streamable_macro"]
clvmr = { workspace = true }
hex = { workspace = true }
pyo3 = { workspace = true, optional = true }
sha2 = { workspace = true }
chia_streamable_macro = { workspace = true }
chia_py_streamable_macro = { workspace = true, optional = true }
clvm-utils = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/fuzz/fuzz_targets/merkle-set.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![no_main]
use chia_consensus::merkle_tree::{validate_merkle_proof, MerkleSet};
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
Expand All @@ -19,7 +19,7 @@ fuzz_target!(|data: &[u8]| {
// proofs-of-exclusion
let mut hasher = Sha256::new();
hasher.update(data);
leafs.push(hasher.finalize().into());
leafs.push(hasher.finalize());

for (idx, item) in leafs.iter().enumerate() {
let expect_included = idx < num_leafs;
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/gen/coin_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chia_protocol::Bytes32;
use clvmr::allocator::{Allocator, NodePtr};
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;

pub fn compute_coin_id(
a: &Allocator,
Expand All @@ -12,7 +12,7 @@ pub fn compute_coin_id(
hasher.update(a.atom(parent_id));
hasher.update(a.atom(puzzle_hash));
hasher.update(amount);
let coin_id: [u8; 32] = hasher.finalize().into();
let coin_id: [u8; 32] = hasher.finalize();
coin_id.into()
}

Expand Down
8 changes: 4 additions & 4 deletions crates/chia-consensus/src/gen/conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use chia_bls::PublicKey;
use chia_protocol::Bytes32;
use clvmr::allocator::{Allocator, NodePtr, SExp};
use clvmr::cost::Cost;
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
use std::cmp::{max, min};
use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -1369,7 +1369,7 @@ pub fn validate_conditions(
let mut hasher = Sha256::new();
hasher.update(*coin_id);
hasher.update(a.atom(announce));
let announcement_id: [u8; 32] = hasher.finalize().into();
let announcement_id: [u8; 32] = hasher.finalize();
announcements.insert(announcement_id.into());
}

Expand Down Expand Up @@ -1412,7 +1412,7 @@ pub fn validate_conditions(
let mut hasher = Sha256::new();
hasher.update(a.atom(puzzle_hash));
hasher.update(a.atom(announce));
let announcement_id: [u8; 32] = hasher.finalize().into();
let announcement_id: [u8; 32] = hasher.finalize();
announcements.insert(announcement_id.into());
}

Expand Down Expand Up @@ -1556,7 +1556,7 @@ fn test_coin_id(parent_id: &[u8; 32], puzzle_hash: &[u8; 32], amount: u64) -> By
hasher.update(puzzle_hash);
let buf = u64_to_bytes(amount);
hasher.update(&buf);
let coin_id: [u8; 32] = hasher.finalize().into();
let coin_id: [u8; 32] = hasher.finalize();
coin_id.into()
}

Expand Down
Loading
Loading