Skip to content

Commit

Permalink
Merge pull request #635 from Chia-Network/update-clvmr
Browse files Browse the repository at this point in the history
Update clvmr
  • Loading branch information
Rigidity authored Aug 5, 2024
2 parents 044bc77 + e0cc115 commit 9e06845
Show file tree
Hide file tree
Showing 30 changed files with 325 additions and 355 deletions.
569 changes: 269 additions & 300 deletions Cargo.lock

Large diffs are not rendered by default.

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.72"
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 @@ -54,7 +54,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 @@ -68,7 +68,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 @@ -266,7 +266,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
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/gen/get_puzzle_and_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ fn u64_to_bytes(n: u64) -> Vec<u8> {
}

#[cfg(test)]
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;

#[cfg(test)]
fn make_dummy_id(seed: u64) -> Bytes32 {
let mut sha256 = Sha256::new();
sha256.update(seed.to_be_bytes());
let id: [u8; 32] = sha256.finalize().into();
let id: [u8; 32] = sha256.finalize();
id.into()
}

Expand Down
10 changes: 5 additions & 5 deletions crates/chia-consensus/src/merkle_set.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
use hex_literal::hex;

fn get_bit(val: &[u8; 32], bit: u8) -> u8 {
Expand Down Expand Up @@ -39,7 +39,7 @@ pub(crate) fn hash(
hasher.update([encode_type(ltype), encode_type(rtype)]);
hasher.update(left);
hasher.update(right);
hasher.finalize().into()
hasher.finalize()
}

pub(crate) const BLANK: [u8; 32] =
Expand Down Expand Up @@ -167,7 +167,7 @@ pub fn compute_merkle_set_root(leafs: &mut [[u8; 32]]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update([NodeType::Term as u8]);
hasher.update(hash);
hasher.finalize().into()
hasher.finalize()
}
(hash, NodeType::Mid | NodeType::MidDbl) => hash,
(_, NodeType::Empty) => panic!("unexpected"),
Expand All @@ -182,7 +182,7 @@ pub mod test {
let mut hasher = Sha256::new();
hasher.update(buf1);
hasher.update(buf2);
hasher.finalize().into()
hasher.finalize()
}

const PREFIX: [u8; 30] = hex!("000000000000000000000000000000000000000000000000000000000000");
Expand All @@ -193,7 +193,7 @@ pub mod test {
hasher.update(buf1);
hasher.update(buf2);
hasher.update(buf3);
hasher.finalize().into()
hasher.finalize()
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-consensus/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hex_literal::hex;
use chia_protocol::Bytes32;
#[cfg(feature = "py-bindings")]
use chia_traits::ChiaToPython;
use clvmr::sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;
#[cfg(feature = "py-bindings")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "py-bindings")]
Expand Down Expand Up @@ -388,7 +388,7 @@ fn hash_leaf(leaf: &[u8; 32]) -> [u8; 32] {
let mut hasher = Sha256::new();
hasher.update([NodeType::Term as u8]);
hasher.update(leaf);
hasher.finalize().into()
hasher.finalize()
}

impl MerkleSet {
Expand Down
1 change: 0 additions & 1 deletion crates/chia-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ arbitrary = ["dep:arbitrary", "chia-bls/arbitrary"]

[dependencies]
pyo3 = { workspace = true, features = ["multiple-pymethods", "num-bigint"], optional = true }
sha2 = { workspace = true }
hex = { workspace = true }
chia_streamable_macro = { workspace = true }
chia_py_streamable_macro = { workspace = true, optional = true }
Expand Down
1 change: 0 additions & 1 deletion crates/chia-protocol/fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ chia-traits = { workspace = true }
clvm-traits = { workspace = true }
chia-protocol = { workspace = true, features = ["arbitrary"] }
arbitrary = { workspace = true }
sha2 = { workspace = true }
hex = { workspace = true }

[[bin]]
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-protocol/fuzz/fuzz_targets/streamable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use arbitrary::{Arbitrary, Unstructured};
use chia_protocol::*;
use chia_traits::Streamable;
use clvmr::sha2::Sha256;
use libfuzzer_sys::fuzz_target;
use sha2::{Digest, Sha256};

pub fn test_streamable<T: Streamable + std::fmt::Debug + PartialEq>(obj: &T) {
let bytes = obj.to_bytes().unwrap();
Expand All @@ -21,7 +21,7 @@ pub fn test_streamable<T: Streamable + std::fmt::Debug + PartialEq>(obj: &T) {

let mut ctx = Sha256::new();
ctx.update(&bytes);
let expect_hash: [u8; 32] = ctx.finalize().into();
let expect_hash: [u8; 32] = ctx.finalize();
assert_eq!(obj.hash(), expect_hash);

// make sure input too large is an error
Expand Down
2 changes: 1 addition & 1 deletion crates/chia-protocol/src/bytes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use chia_traits::{chia_error, read_bytes, Streamable};
use clvm_traits::{ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError};
use clvm_utils::TreeHash;
use clvmr::sha2::Sha256;
use clvmr::Atom;
use sha2::{Digest, Sha256};
use std::array::TryFromSliceError;
use std::fmt;
use std::io::Cursor;
Expand Down
2 changes: 1 addition & 1 deletion crates/chia-protocol/src/coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clvm_traits::{
clvm_list, destructure_list, match_list, ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError,
ToClvm, ToClvmError,
};
use sha2::{Digest, Sha256};
use clvmr::sha2::Sha256;

#[cfg(feature = "py-bindings")]
use pyo3::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/chia-protocol/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use clvmr::serde::{
node_from_bytes, node_from_bytes_backrefs, node_to_bytes, serialized_length_from_bytes,
serialized_length_from_bytes_trusted,
};
use clvmr::sha2::Sha256;
use clvmr::{Allocator, ChiaDialect};
use sha2::{Digest, Sha256};
use std::io::Cursor;
use std::ops::Deref;

Expand Down
1 change: 0 additions & 1 deletion crates/chia-puzzles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ arbitrary = ["dep:arbitrary", "chia-protocol/arbitrary"]

[dependencies]
clvmr = { workspace = true }
sha2 = { workspace = true }
num-bigint = { workspace = true }
hex-literal = { workspace = true }
clvm-utils = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/chia-puzzles/src/derive_synthetic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chia_bls::{PublicKey, SecretKey};
use clvmr::sha2::Sha256;
use hex_literal::hex;
use num_bigint::BigInt;
use sha2::{digest::FixedOutput, Digest, Sha256};

use crate::standard::DEFAULT_HIDDEN_PUZZLE_HASH;

Expand Down Expand Up @@ -52,7 +52,7 @@ fn synthetic_offset(public_key: &PublicKey, hidden_puzzle_hash: &[u8; 32]) -> Se
let mut hasher = Sha256::new();
hasher.update(public_key.to_bytes());
hasher.update(hidden_puzzle_hash);
let bytes: [u8; 32] = hasher.finalize_fixed().into();
let bytes: [u8; 32] = hasher.finalize();
SecretKey::from_bytes(&mod_by_group_order(bytes)).unwrap()
}

Expand Down
Loading

0 comments on commit 9e06845

Please sign in to comment.