From 5485860860316ec39007f59c6e6d03ea2cb9d72e Mon Sep 17 00:00:00 2001 From: Rigidity Date: Fri, 27 Sep 2024 11:01:46 -0400 Subject: [PATCH 1/2] Add chia-sha2 crate to prevent cyclic dependency with clvmr --- Cargo.lock | 21 ++++++- Cargo.toml | 7 ++- crates/chia-bls/Cargo.toml | 1 + crates/chia-bls/src/bls_cache.rs | 2 +- crates/chia-bls/src/gtelement.rs | 2 +- crates/chia-bls/src/public_key.rs | 2 +- crates/chia-bls/src/secret_key.rs | 2 +- crates/chia-bls/src/signature.rs | 2 +- crates/chia-consensus/Cargo.toml | 1 + crates/chia-consensus/fuzz/Cargo.toml | 1 + .../fuzz/fuzz_targets/merkle-set.rs | 2 +- crates/chia-consensus/src/gen/coin_id.rs | 2 +- crates/chia-consensus/src/gen/conditions.rs | 2 +- .../src/gen/get_puzzle_and_solution.rs | 2 +- crates/chia-consensus/src/merkle_set.rs | 2 +- crates/chia-consensus/src/merkle_tree.rs | 2 +- .../src/spendbundle_validation.rs | 2 +- crates/chia-protocol/Cargo.toml | 1 + crates/chia-protocol/fuzz/Cargo.toml | 1 + .../fuzz/fuzz_targets/streamable.rs | 2 +- crates/chia-protocol/src/bytes.rs | 2 +- crates/chia-protocol/src/coin.rs | 2 +- crates/chia-protocol/src/program.rs | 2 +- crates/chia-puzzles/Cargo.toml | 1 + crates/chia-puzzles/src/derive_synthetic.rs | 2 +- crates/chia-sha2/Cargo.toml | 19 ++++++ crates/chia-sha2/src/lib.rs | 61 +++++++++++++++++++ crates/chia-traits/Cargo.toml | 1 + crates/chia-traits/src/streamable.rs | 2 +- crates/chia_py_streamable_macro/src/lib.rs | 2 +- crates/chia_streamable_macro/src/lib.rs | 8 +-- crates/clvm-utils/Cargo.toml | 1 + crates/clvm-utils/src/tree_hash.rs | 2 +- src/lib.rs | 1 + 34 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 crates/chia-sha2/Cargo.toml create mode 100644 crates/chia-sha2/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 9c4217bfc..6d4775ec7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -278,6 +278,7 @@ dependencies = [ "chia-consensus", "chia-protocol", "chia-puzzles", + "chia-sha2", "chia-ssl", "chia-traits 0.11.0", "clvm-traits", @@ -307,6 +308,7 @@ version = "0.14.0" dependencies = [ "arbitrary", "blst", + "chia-sha2", "chia-traits 0.11.0", "chia_py_streamable_macro", "clvmr", @@ -350,6 +352,7 @@ dependencies = [ "chia-bls 0.14.0", "chia-protocol", "chia-puzzles", + "chia-sha2", "chia-traits 0.11.0", "chia_py_streamable_macro", "chia_streamable_macro 0.11.0", @@ -373,6 +376,7 @@ version = "0.13.0" dependencies = [ "chia-consensus", "chia-protocol", + "chia-sha2", "chia-traits 0.11.0", "clvm-traits", "clvm-utils", @@ -387,6 +391,7 @@ version = "0.14.0" dependencies = [ "arbitrary", "chia-bls 0.14.0", + "chia-sha2", "chia-traits 0.11.0", "chia_py_streamable_macro", "chia_streamable_macro 0.11.0", @@ -404,6 +409,7 @@ version = "0.13.0" dependencies = [ "arbitrary", "chia-protocol", + "chia-sha2", "chia-traits 0.11.0", "clvm-traits", "clvmr", @@ -418,6 +424,7 @@ dependencies = [ "arbitrary", "chia-bls 0.14.0", "chia-protocol", + "chia-sha2", "clvm-traits", "clvm-utils", "clvmr", @@ -436,6 +443,14 @@ dependencies = [ "libfuzzer-sys", ] +[[package]] +name = "chia-sha2" +version = "0.14.0" +dependencies = [ + "openssl", + "sha2", +] + [[package]] name = "chia-ssl" version = "0.11.0" @@ -482,6 +497,7 @@ dependencies = [ name = "chia-traits" version = "0.11.0" dependencies = [ + "chia-sha2", "chia_streamable_macro 0.11.0", "clvmr", "pyo3", @@ -644,6 +660,7 @@ dependencies = [ name = "clvm-utils" version = "0.14.0" dependencies = [ + "chia-sha2", "clvm-traits", "clvmr", "hex", @@ -1570,9 +1587,9 @@ dependencies = [ [[package]] name = "openssl-src" -version = "300.3.1+3.3.1" +version = "300.3.2+3.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7259953d42a81bf137fbbd73bd30a8e1914d6dce43c2b90ed575783a22608b91" +checksum = "a211a18d945ef7e648cc6e0058f4c548ee46aab922ea203e0d30e966ea23647b" dependencies = [ "cc", ] diff --git a/Cargo.toml b/Cargo.toml index 15af42f95..0fe04a9d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,7 @@ chia-protocol = { workspace = true, optional = true } chia-ssl = { workspace = true, optional = true } chia-traits = { workspace = true, optional = true } chia-puzzles = { workspace = true, optional = true } +chia-sha2 = { workspace = true, optional = true } clvm-traits = { workspace = true, optional = true } clvm-utils = { workspace = true, optional = true } clvmr = { workspace = true } @@ -76,6 +77,7 @@ default = [ "ssl", "traits", "puzzles", + "sha2", "clvm-traits", "clvm-utils" ] @@ -87,10 +89,11 @@ protocol = ["dep:chia-protocol"] ssl = ["dep:chia-ssl"] traits = ["dep:chia-traits"] puzzles = ["dep:chia-puzzles"] +sha2 = ["dep:chia-sha2"] clvm-traits = ["dep:clvm-traits"] clvm-utils = ["dep:clvm-utils"] -openssl = ["clvmr/openssl"] +openssl = ["chia-sha2/openssl", "clvmr/openssl"] [profile.release] lto = "thin" @@ -105,6 +108,7 @@ chia-protocol = { path = "./crates/chia-protocol", version = "0.14.0" } chia-ssl = { path = "./crates/chia-ssl", version = "0.11.0" } chia-traits = { path = "./crates/chia-traits", version = "0.11.0" } chia-puzzles = { path = "./crates/chia-puzzles", version = "0.14.0" } +chia-sha2 = { path = "./crates/chia-sha2", version = "0.14.0" } clvm-traits = { path = "./crates/clvm-traits", version = "0.14.0" } clvm-utils = { path = "./crates/clvm-utils", version = "0.14.0" } clvm-derive = { path = "./crates/clvm-derive", version = "0.13.0" } @@ -144,3 +148,4 @@ zstd = "0.13.2" blocking-threadpool = "1.0.1" libfuzzer-sys = "0.4" wasm-bindgen = "0.2.93" +openssl = "0.10.66" diff --git a/crates/chia-bls/Cargo.toml b/crates/chia-bls/Cargo.toml index f35f6a713..352e2e4b0 100644 --- a/crates/chia-bls/Cargo.toml +++ b/crates/chia-bls/Cargo.toml @@ -17,6 +17,7 @@ arbitrary = ["dep:arbitrary"] [dependencies] chia-traits = { workspace = true } +chia-sha2 = { workspace = true } chia_py_streamable_macro = { workspace = true, optional = true } sha2 = { workspace = true } clvmr = { workspace = true } diff --git a/crates/chia-bls/src/bls_cache.rs b/crates/chia-bls/src/bls_cache.rs index 23b8aae15..a059ec825 100644 --- a/crates/chia-bls/src/bls_cache.rs +++ b/crates/chia-bls/src/bls_cache.rs @@ -1,7 +1,7 @@ use std::borrow::Borrow; use std::num::NonZeroUsize; -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use lru::LruCache; use crate::{aggregate_verify_gt, hash_to_g2}; diff --git a/crates/chia-bls/src/gtelement.rs b/crates/chia-bls/src/gtelement.rs index f3245692d..373890c2f 100644 --- a/crates/chia-bls/src/gtelement.rs +++ b/crates/chia-bls/src/gtelement.rs @@ -1,7 +1,7 @@ use blst::*; +use chia_sha2::Sha256; use chia_traits::chia_error::Result; use chia_traits::{read_bytes, Streamable}; -use clvmr::sha2::Sha256; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyNotImplementedError; #[cfg(feature = "py-bindings")] diff --git a/crates/chia-bls/src/public_key.rs b/crates/chia-bls/src/public_key.rs index cd95b5a85..8c6140083 100644 --- a/crates/chia-bls/src/public_key.rs +++ b/crates/chia-bls/src/public_key.rs @@ -2,8 +2,8 @@ use crate::secret_key::is_all_zero; use crate::{DerivableKey, Error, Result}; use blst::*; +use chia_sha2::Sha256; use chia_traits::{read_bytes, Streamable}; -use clvmr::sha2::Sha256; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyNotImplementedError; #[cfg(feature = "py-bindings")] diff --git a/crates/chia-bls/src/secret_key.rs b/crates/chia-bls/src/secret_key.rs index a93c8d3aa..591787aa4 100644 --- a/crates/chia-bls/src/secret_key.rs +++ b/crates/chia-bls/src/secret_key.rs @@ -1,7 +1,7 @@ use crate::{DerivableKey, Error, PublicKey, Result}; use blst::*; +use chia_sha2::Sha256; use chia_traits::{read_bytes, Streamable}; -use clvmr::sha2::Sha256; use hkdf::HkdfExtract; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyNotImplementedError; diff --git a/crates/chia-bls/src/signature.rs b/crates/chia-bls/src/signature.rs index f87b22a68..01eed3b9b 100644 --- a/crates/chia-bls/src/signature.rs +++ b/crates/chia-bls/src/signature.rs @@ -1,7 +1,7 @@ use crate::{Error, GTElement, PublicKey, Result, SecretKey}; use blst::*; +use chia_sha2::Sha256; use chia_traits::{read_bytes, Streamable}; -use clvmr::sha2::Sha256; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyNotImplementedError; #[cfg(feature = "py-bindings")] diff --git a/crates/chia-consensus/Cargo.toml b/crates/chia-consensus/Cargo.toml index 6fb9869d7..6df32abb4 100644 --- a/crates/chia-consensus/Cargo.toml +++ b/crates/chia-consensus/Cargo.toml @@ -21,6 +21,7 @@ pyo3 = { workspace = true, optional = true } chia_streamable_macro = { workspace = true } chia_py_streamable_macro = { workspace = true, optional = true } clvm-utils = { workspace = true } +chia-sha2 = { workspace = true } chia-traits = { workspace = true } clvm-traits = { workspace = true } chia-protocol = { workspace = true } diff --git a/crates/chia-consensus/fuzz/Cargo.toml b/crates/chia-consensus/fuzz/Cargo.toml index b4fb0499b..126f1f604 100644 --- a/crates/chia-consensus/fuzz/Cargo.toml +++ b/crates/chia-consensus/fuzz/Cargo.toml @@ -17,6 +17,7 @@ clvmr = { workspace = true } clvm-utils = { workspace = true } clvm-traits = { workspace = true } chia-protocol = { workspace = true } +chia-sha2 = { workspace = true } chia-traits = { workspace = true } chia-consensus = { workspace = true } hex-literal = { workspace = true } diff --git a/crates/chia-consensus/fuzz/fuzz_targets/merkle-set.rs b/crates/chia-consensus/fuzz/fuzz_targets/merkle-set.rs index d68d276f8..bc3886405 100644 --- a/crates/chia-consensus/fuzz/fuzz_targets/merkle-set.rs +++ b/crates/chia-consensus/fuzz/fuzz_targets/merkle-set.rs @@ -1,6 +1,6 @@ #![no_main] use chia_consensus::merkle_tree::{validate_merkle_proof, MerkleSet}; -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use libfuzzer_sys::fuzz_target; fuzz_target!(|data: &[u8]| { diff --git a/crates/chia-consensus/src/gen/coin_id.rs b/crates/chia-consensus/src/gen/coin_id.rs index 7b09e84f1..9b06c18f2 100644 --- a/crates/chia-consensus/src/gen/coin_id.rs +++ b/crates/chia-consensus/src/gen/coin_id.rs @@ -1,6 +1,6 @@ use chia_protocol::Bytes32; +use chia_sha2::Sha256; use clvmr::allocator::{Allocator, NodePtr}; -use clvmr::sha2::Sha256; pub fn compute_coin_id( a: &Allocator, diff --git a/crates/chia-consensus/src/gen/conditions.rs b/crates/chia-consensus/src/gen/conditions.rs index 77ff4cc04..3fd16e15b 100644 --- a/crates/chia-consensus/src/gen/conditions.rs +++ b/crates/chia-consensus/src/gen/conditions.rs @@ -23,9 +23,9 @@ use crate::gen::spend_visitor::SpendVisitor; use crate::gen::validation_error::check_nil; use chia_bls::PublicKey; use chia_protocol::Bytes32; +use chia_sha2::Sha256; use clvmr::allocator::{Allocator, NodePtr, SExp}; use clvmr::cost::Cost; -use clvmr::sha2::Sha256; use std::cmp::{max, min}; use std::collections::{HashMap, HashSet}; use std::hash::{Hash, Hasher}; diff --git a/crates/chia-consensus/src/gen/get_puzzle_and_solution.rs b/crates/chia-consensus/src/gen/get_puzzle_and_solution.rs index 6f242d2d1..37dc735e8 100644 --- a/crates/chia-consensus/src/gen/get_puzzle_and_solution.rs +++ b/crates/chia-consensus/src/gen/get_puzzle_and_solution.rs @@ -65,11 +65,11 @@ mod test { use crate::gen::flags::{ALLOW_BACKREFS, MEMPOOL_MODE}; use crate::gen::run_block_generator::{run_block_generator2, setup_generator_args}; use chia_protocol::Bytes32; + use chia_sha2::Sha256; use clvm_traits::FromClvm; use clvm_utils::tree_hash; use clvmr::reduction::Reduction; use clvmr::serde::node_from_bytes_backrefs; - use clvmr::sha2::Sha256; use clvmr::{run_program, ChiaDialect}; use rstest::rstest; use std::collections::HashSet; diff --git a/crates/chia-consensus/src/merkle_set.rs b/crates/chia-consensus/src/merkle_set.rs index fd8135440..5ff480eb0 100644 --- a/crates/chia-consensus/src/merkle_set.rs +++ b/crates/chia-consensus/src/merkle_set.rs @@ -1,4 +1,4 @@ -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use hex_literal::hex; fn get_bit(val: &[u8; 32], bit: u8) -> u8 { diff --git a/crates/chia-consensus/src/merkle_tree.rs b/crates/chia-consensus/src/merkle_tree.rs index 1533c8141..52a3d53b3 100644 --- a/crates/chia-consensus/src/merkle_tree.rs +++ b/crates/chia-consensus/src/merkle_tree.rs @@ -5,9 +5,9 @@ use hex_literal::hex; #[cfg(feature = "py-bindings")] use chia_protocol::Bytes32; +use chia_sha2::Sha256; #[cfg(feature = "py-bindings")] use chia_traits::ChiaToPython; -use clvmr::sha2::Sha256; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyValueError; #[cfg(feature = "py-bindings")] diff --git a/crates/chia-consensus/src/spendbundle_validation.rs b/crates/chia-consensus/src/spendbundle_validation.rs index dacde6639..ef670fe0f 100644 --- a/crates/chia-consensus/src/spendbundle_validation.rs +++ b/crates/chia-consensus/src/spendbundle_validation.rs @@ -12,7 +12,7 @@ use crate::spendbundle_conditions::get_conditions_from_spendbundle; use chia_bls::GTElement; use chia_bls::{aggregate_verify_gt, hash_to_g2}; use chia_protocol::SpendBundle; -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use clvmr::LIMIT_HEAP; use std::time::{Duration, Instant}; diff --git a/crates/chia-protocol/Cargo.toml b/crates/chia-protocol/Cargo.toml index 19c0825a5..c38be2472 100644 --- a/crates/chia-protocol/Cargo.toml +++ b/crates/chia-protocol/Cargo.toml @@ -22,6 +22,7 @@ chia_streamable_macro = { workspace = true } chia_py_streamable_macro = { workspace = true, optional = true } clvmr = { workspace = true } chia-traits = { workspace = true } +chia-sha2 = { workspace = true } clvm-traits = { workspace = true, features = ["derive"] } clvm-utils = { workspace = true } chia-bls = { workspace = true } diff --git a/crates/chia-protocol/fuzz/Cargo.toml b/crates/chia-protocol/fuzz/Cargo.toml index 09ee88cba..4ca5c79d5 100644 --- a/crates/chia-protocol/fuzz/Cargo.toml +++ b/crates/chia-protocol/fuzz/Cargo.toml @@ -14,6 +14,7 @@ workspace = true [dependencies] libfuzzer-sys = { workspace = true } clvmr = { workspace = true } +chia-sha2 = { workspace = true } chia-traits = { workspace = true } clvm-traits = { workspace = true } chia-protocol = { workspace = true, features = ["arbitrary"] } diff --git a/crates/chia-protocol/fuzz/fuzz_targets/streamable.rs b/crates/chia-protocol/fuzz/fuzz_targets/streamable.rs index 300d242b7..684576e5d 100644 --- a/crates/chia-protocol/fuzz/fuzz_targets/streamable.rs +++ b/crates/chia-protocol/fuzz/fuzz_targets/streamable.rs @@ -1,8 +1,8 @@ #![no_main] use arbitrary::{Arbitrary, Unstructured}; use chia_protocol::*; +use chia_sha2::Sha256; use chia_traits::Streamable; -use clvmr::sha2::Sha256; use libfuzzer_sys::fuzz_target; pub fn test_streamable(obj: &T) { diff --git a/crates/chia-protocol/src/bytes.rs b/crates/chia-protocol/src/bytes.rs index 87426e71b..aaf87b2ca 100644 --- a/crates/chia-protocol/src/bytes.rs +++ b/crates/chia-protocol/src/bytes.rs @@ -1,7 +1,7 @@ +use chia_sha2::Sha256; 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 std::array::TryFromSliceError; use std::fmt; diff --git a/crates/chia-protocol/src/coin.rs b/crates/chia-protocol/src/coin.rs index 2ae83b2fa..4fbb74775 100644 --- a/crates/chia-protocol/src/coin.rs +++ b/crates/chia-protocol/src/coin.rs @@ -1,10 +1,10 @@ use crate::{Bytes32, BytesImpl}; +use chia_sha2::Sha256; use chia_streamable_macro::streamable; use clvm_traits::{ clvm_list, destructure_list, match_list, ClvmDecoder, ClvmEncoder, FromClvm, FromClvmError, ToClvm, ToClvmError, }; -use clvmr::sha2::Sha256; #[cfg(feature = "py-bindings")] use pyo3::exceptions::PyNotImplementedError; diff --git a/crates/chia-protocol/src/program.rs b/crates/chia-protocol/src/program.rs index 5c05686ec..45067a97a 100644 --- a/crates/chia-protocol/src/program.rs +++ b/crates/chia-protocol/src/program.rs @@ -1,4 +1,5 @@ use crate::bytes::Bytes; +use chia_sha2::Sha256; use chia_traits::chia_error::{Error, Result}; use chia_traits::Streamable; use clvm_traits::{FromClvm, FromClvmError, ToClvm, ToClvmError}; @@ -10,7 +11,6 @@ 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}; #[cfg(feature = "py-bindings")] use pyo3::prelude::*; diff --git a/crates/chia-puzzles/Cargo.toml b/crates/chia-puzzles/Cargo.toml index e554d16a4..166f3144b 100644 --- a/crates/chia-puzzles/Cargo.toml +++ b/crates/chia-puzzles/Cargo.toml @@ -20,6 +20,7 @@ num-bigint = { workspace = true } hex-literal = { workspace = true } clvm-utils = { workspace = true } clvm-traits = { workspace = true, features = ["chia-bls"] } +chia-sha2 = { workspace = true } chia-bls = { workspace = true } chia-protocol = { workspace = true } arbitrary = { workspace = true, features = ["derive"], optional = true } diff --git a/crates/chia-puzzles/src/derive_synthetic.rs b/crates/chia-puzzles/src/derive_synthetic.rs index d845586f0..9e03d7b90 100644 --- a/crates/chia-puzzles/src/derive_synthetic.rs +++ b/crates/chia-puzzles/src/derive_synthetic.rs @@ -1,5 +1,5 @@ use chia_bls::{PublicKey, SecretKey}; -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use hex_literal::hex; use num_bigint::BigInt; diff --git a/crates/chia-sha2/Cargo.toml b/crates/chia-sha2/Cargo.toml new file mode 100644 index 000000000..ad8a6a6be --- /dev/null +++ b/crates/chia-sha2/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "chia-sha2" +version = "0.14.0" +edition = "2021" +license = "Apache-2.0" +description = "A common wrapper around OpenSSL and sha2 implementations of sha256." +authors = ["Brandon Haggstrom "] +homepage = "https://github.com/Chia-Network/chia_rs" +repository = "https://github.com/Chia-Network/chia_rs" + +[lints] +workspace = true + +[features] +openssl = ["dep:openssl"] + +[dependencies] +sha2 = { workspace = true } +openssl = { workspace = true, optional = true } diff --git a/crates/chia-sha2/src/lib.rs b/crates/chia-sha2/src/lib.rs new file mode 100644 index 000000000..267392057 --- /dev/null +++ b/crates/chia-sha2/src/lib.rs @@ -0,0 +1,61 @@ +#[cfg(not(feature = "openssl"))] +use sha2::Digest; + +#[derive(Default, Clone)] +pub struct Sha256 { + #[cfg(feature = "openssl")] + ctx: openssl::sha::Sha256, + + #[cfg(not(feature = "openssl"))] + ctx: sha2::Sha256, +} + +impl Sha256 { + pub fn new() -> Self { + Self::default() + } + pub fn update(&mut self, buf: impl AsRef<[u8]>) { + self.ctx.update(buf.as_ref()); + } + pub fn finalize(self) -> [u8; 32] { + #[cfg(feature = "openssl")] + { + self.ctx.finish() + } + #[cfg(not(feature = "openssl"))] + { + self.ctx.finalize().into() + } + } +} + +#[test] +fn test_sha256() { + // https://www.di-mgt.com.au/sha_testvectors.html + + let output = &[ + 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, + 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, + 0x15, 0xad, + ]; + + let mut ctx = Sha256::new(); + ctx.update([0x61, 0x62, 0x63]); + assert_eq!(&ctx.finalize().as_slice(), output); + + let mut ctx = Sha256::new(); + ctx.update([0x61]); + ctx.update([0x62]); + ctx.update([0x63]); + assert_eq!(&ctx.finalize().as_slice(), output); + + let mut ctx = Sha256::new(); + ctx.update([0x61, 0x62]); + ctx.update([0x63]); + assert_eq!(&ctx.finalize().as_slice(), output); + + let mut ctx = Sha256::new(); + ctx.update([0x61]); + ctx.update([0x62, 0x63]); + assert_eq!(&ctx.finalize().as_slice(), output); +} diff --git a/crates/chia-traits/Cargo.toml b/crates/chia-traits/Cargo.toml index 9dadf80f9..9e3d21c13 100644 --- a/crates/chia-traits/Cargo.toml +++ b/crates/chia-traits/Cargo.toml @@ -13,6 +13,7 @@ workspace = true py-bindings = ["dep:pyo3"] [dependencies] +chia-sha2 = { workspace = true } pyo3 = { workspace = true, features = ["multiple-pymethods"], optional = true } chia_streamable_macro = { workspace = true } clvmr = { workspace = true } diff --git a/crates/chia-traits/src/streamable.rs b/crates/chia-traits/src/streamable.rs index 5e07576ff..6be2dab34 100644 --- a/crates/chia-traits/src/streamable.rs +++ b/crates/chia-traits/src/streamable.rs @@ -1,5 +1,5 @@ use crate::chia_error::{Error, Result}; -use clvmr::sha2::Sha256; +use chia_sha2::Sha256; use std::io::Cursor; use std::mem; diff --git a/crates/chia_py_streamable_macro/src/lib.rs b/crates/chia_py_streamable_macro/src/lib.rs index 3ecd92423..1d77d83d6 100644 --- a/crates/chia_py_streamable_macro/src/lib.rs +++ b/crates/chia_py_streamable_macro/src/lib.rs @@ -285,7 +285,7 @@ pub fn py_streamable_macro(input: proc_macro::TokenStream) -> proc_macro::TokenS use pyo3::IntoPy; use pyo3::types::PyModule; use pyo3::prelude::PyAnyMethods; - let mut ctx = clvmr::sha2::Sha256::new(); + let mut ctx = chia_sha2::Sha256::new(); #crate_name::Streamable::update_digest(self, &mut ctx); let bytes_module = PyModule::import_bound(py, "chia_rs.sized_bytes")?; let ty = bytes_module.getattr("bytes32")?; diff --git a/crates/chia_streamable_macro/src/lib.rs b/crates/chia_streamable_macro/src/lib.rs index 357dbd16e..e04f51df8 100644 --- a/crates/chia_streamable_macro/src/lib.rs +++ b/crates/chia_streamable_macro/src/lib.rs @@ -165,7 +165,7 @@ pub fn chia_streamable_macro(input: TokenStream) -> TokenStream { } let ret = quote! { impl #crate_name::Streamable for #ident { - fn update_digest(&self, digest: &mut clvmr::sha2::Sha256) { + fn update_digest(&self, digest: &mut chia_sha2::Sha256) { ::update_digest(&(*self as u8), digest); } fn stream(&self, out: &mut Vec) -> #crate_name::chia_error::Result<()> { @@ -205,7 +205,7 @@ pub fn chia_streamable_macro(input: TokenStream) -> TokenStream { if !fnames.is_empty() { let ret = quote! { impl #crate_name::Streamable for #ident { - fn update_digest(&self, digest: &mut clvmr::sha2::Sha256) { + fn update_digest(&self, digest: &mut chia_sha2::Sha256) { #(self.#fnames.update_digest(digest);)* } fn stream(&self, out: &mut Vec) -> #crate_name::chia_error::Result<()> { @@ -221,7 +221,7 @@ pub fn chia_streamable_macro(input: TokenStream) -> TokenStream { } else if !findices.is_empty() { let ret = quote! { impl #crate_name::Streamable for #ident { - fn update_digest(&self, digest: &mut clvmr::sha2::Sha256) { + fn update_digest(&self, digest: &mut chia_sha2::Sha256) { #(self.#findices.update_digest(digest);)* } fn stream(&self, out: &mut Vec) -> #crate_name::chia_error::Result<()> { @@ -238,7 +238,7 @@ pub fn chia_streamable_macro(input: TokenStream) -> TokenStream { // this is an empty type (Unit) let ret = quote! { impl #crate_name::Streamable for #ident { - fn update_digest(&self, _digest: &mut clvmr::sha2::Sha256) {} + fn update_digest(&self, _digest: &mut chia_sha2::Sha256) {} fn stream(&self, _out: &mut Vec) -> #crate_name::chia_error::Result<()> { Ok(()) } diff --git a/crates/clvm-utils/Cargo.toml b/crates/clvm-utils/Cargo.toml index 293ac4e37..09d636fd2 100644 --- a/crates/clvm-utils/Cargo.toml +++ b/crates/clvm-utils/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/Chia-Network/chia_rs" workspace = true [dependencies] +chia-sha2 = { workspace = true } clvmr = { workspace = true } clvm-traits = { workspace = true } hex = { workspace = true } diff --git a/crates/clvm-utils/src/tree_hash.rs b/crates/clvm-utils/src/tree_hash.rs index df9e12038..2f57312be 100644 --- a/crates/clvm-utils/src/tree_hash.rs +++ b/crates/clvm-utils/src/tree_hash.rs @@ -1,6 +1,6 @@ +use chia_sha2::Sha256; use clvmr::allocator::{Allocator, NodePtr, SExp}; use clvmr::serde::node_from_bytes_backrefs_record; -use clvmr::sha2::Sha256; use std::collections::{HashMap, HashSet}; use std::ops::Deref; use std::{fmt, io}; diff --git a/src/lib.rs b/src/lib.rs index 808dde2ee..1df6d0b21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ pub use chia_client as client; pub use chia_consensus as consensus; pub use chia_protocol as protocol; pub use chia_puzzles as puzzles; +pub use chia_sha2 as sha2; pub use chia_ssl as ssl; pub use chia_traits as traits; pub use clvm_traits; From 8479d9b0f5b729f298c9d3a4961b04227a5696ed Mon Sep 17 00:00:00 2001 From: Rigidity Date: Fri, 27 Sep 2024 11:03:33 -0400 Subject: [PATCH 2/2] Remove clvmr from dependencies for 3 crates --- Cargo.lock | 3 --- crates/chia-bls/Cargo.toml | 1 - crates/chia-traits/Cargo.toml | 1 - crates/chia_streamable_macro/Cargo.toml | 1 - 4 files changed, 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d4775ec7..422e5b1fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -311,7 +311,6 @@ dependencies = [ "chia-sha2", "chia-traits 0.11.0", "chia_py_streamable_macro", - "clvmr", "criterion", "hex", "hkdf", @@ -499,7 +498,6 @@ version = "0.11.0" dependencies = [ "chia-sha2", "chia_streamable_macro 0.11.0", - "clvmr", "pyo3", "thiserror", ] @@ -543,7 +541,6 @@ dependencies = [ name = "chia_streamable_macro" version = "0.11.0" dependencies = [ - "clvmr", "proc-macro-crate 1.3.1", "proc-macro2", "quote", diff --git a/crates/chia-bls/Cargo.toml b/crates/chia-bls/Cargo.toml index 352e2e4b0..89e6ed631 100644 --- a/crates/chia-bls/Cargo.toml +++ b/crates/chia-bls/Cargo.toml @@ -20,7 +20,6 @@ chia-traits = { workspace = true } chia-sha2 = { workspace = true } chia_py_streamable_macro = { workspace = true, optional = true } sha2 = { workspace = true } -clvmr = { workspace = true } hkdf = { workspace = true } blst = { workspace = true } hex = { workspace = true } diff --git a/crates/chia-traits/Cargo.toml b/crates/chia-traits/Cargo.toml index 9e3d21c13..09d5df737 100644 --- a/crates/chia-traits/Cargo.toml +++ b/crates/chia-traits/Cargo.toml @@ -16,5 +16,4 @@ py-bindings = ["dep:pyo3"] chia-sha2 = { workspace = true } pyo3 = { workspace = true, features = ["multiple-pymethods"], optional = true } chia_streamable_macro = { workspace = true } -clvmr = { workspace = true } thiserror = { workspace = true } diff --git a/crates/chia_streamable_macro/Cargo.toml b/crates/chia_streamable_macro/Cargo.toml index 9e6e3c3e5..7ef694065 100644 --- a/crates/chia_streamable_macro/Cargo.toml +++ b/crates/chia_streamable_macro/Cargo.toml @@ -15,7 +15,6 @@ workspace = true proc-macro = true [dependencies] -clvmr = { workspace = true } syn = { workspace = true } quote = { workspace = true } proc-macro-crate = { workspace = true }