From 9562563e1fde21fe7ae6a6a9d36c0115f654a59e Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 12 Feb 2024 17:16:20 +0100 Subject: [PATCH] chore: update to new strict encode APIs --- Cargo.lock | 46 +++++++++++++++---------------- Cargo.toml | 4 +-- commit_verify/src/commit.rs | 52 ----------------------------------- commit_verify/src/id.rs | 8 ++++-- commit_verify/src/lib.rs | 2 +- commit_verify/src/mpc/tree.rs | 13 +++++---- 6 files changed, 40 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5af29d99..f4c74cdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,15 +397,15 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" +checksum = "d0c62115964e08cb8039170eb33c1d0e2388a256930279edca206fff675f82c3" [[package]] name = "indexmap" -version = "2.2.2" +version = "2.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520" +checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" dependencies = [ "equivalent", "hashbrown", @@ -714,7 +714,7 @@ dependencies = [ [[package]] name = "strict_encoding" version = "2.7.0" -source = "git+https://github.com/strict-types/strict-encoding#c5c120b74d52fcc6a2e1ac66211741be4e470740" +source = "git+https://github.com/strict-types/strict-encoding?branch=refactor/io#bdd35d550369590a3e6a86439a55e0b2f2d06766" dependencies = [ "amplify", "half", @@ -724,7 +724,7 @@ dependencies = [ [[package]] name = "strict_encoding_derive" version = "2.7.0" -source = "git+https://github.com/strict-types/strict-encoding#c5c120b74d52fcc6a2e1ac66211741be4e470740" +source = "git+https://github.com/strict-types/strict-encoding?branch=refactor/io#bdd35d550369590a3e6a86439a55e0b2f2d06766" dependencies = [ "amplify_syn", "heck", @@ -736,7 +736,7 @@ dependencies = [ [[package]] name = "strict_types" version = "2.7.0" -source = "git+https://github.com/strict-types/strict-types#907c02c2b39675b57a127c31caabeb656017b35e" +source = "git+https://github.com/strict-types/strict-types?branch=refactor/io#abc6133b6cfdb9699714c493e5f62175d99fd752" dependencies = [ "amplify", "baid58", @@ -806,18 +806,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", @@ -905,9 +905,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1223296a201415c7fad14792dbefaace9bd52b62d33453ade1c5b5f07555406" +checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -915,9 +915,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcdc935b63408d58a32f8cc9738a0bffd8f05cc7c002086c6ef20b7312ad9dcd" +checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b" dependencies = [ "bumpalo", "log", @@ -930,9 +930,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e4c238561b2d428924c49815533a8b9121c664599558a5d9ec51f8a1740a999" +checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -940,9 +940,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bae1abb6806dc1ad9e560ed242107c0f6c84335f1749dd4e8ddb012ebd5e25a7" +checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66" dependencies = [ "proc-macro2", "quote", @@ -953,9 +953,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.90" +version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d91413b1c31d7539ba5ef2451af3f0b833a005eb27a631cec32bc0635a8602b" +checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838" [[package]] name = "winapi" @@ -1047,9 +1047,9 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "winnow" -version = "0.5.37" +version = "0.5.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7cad8365489051ae9f054164e459304af2e7e9bb407c958076c8bf4aef52da5" +checksum = "5389a154b01683d28c77f8f68f49dea75f0a4da32557a58f68ee51ebba472d29" dependencies = [ "memchr", ] diff --git a/Cargo.toml b/Cargo.toml index 35f32503..40f69aef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,5 +58,5 @@ serde = ["serde_crate", "commit_verify/serde"] features = [ "all" ] [patch.crates-io] -strict_encoding = { git = "https://github.com/strict-types/strict-encoding" } -strict_types = { git = "https://github.com/strict-types/strict-types" } +strict_encoding = { git = "https://github.com/strict-types/strict-encoding", branch = "refactor/io" } +strict_types = { git = "https://github.com/strict-types/strict-types", branch = "refactor/io" } diff --git a/commit_verify/src/commit.rs b/commit_verify/src/commit.rs index ea34c727..e0aee7c6 100644 --- a/commit_verify/src/commit.rs +++ b/commit_verify/src/commit.rs @@ -21,11 +21,6 @@ //! Base commit-verify scheme interface. -use amplify::Bytes32; -use sha2::Sha256; -use strict_encoding::{StrictEncode, StrictWriter}; - -use crate::digest::DigestExt; use crate::CommitmentProtocol; /// Error during commitment verification @@ -82,22 +77,6 @@ where Self: Eq + Sized } } -/// Commitment protocol which writes strict-encoded data into a hasher. -pub struct StrictEncodedProtocol; - -impl CommitmentProtocol for StrictEncodedProtocol {} - -impl CommitVerify for Bytes32 -where T: StrictEncode -{ - fn commit(msg: &T) -> Self { - let mut engine = Sha256::from_tag(*b"urn:lnpbp:lnpbp0007:strict:v01#A"); - let w = StrictWriter::with(u32::MAX as usize, &mut engine); - msg.strict_encode(w).ok(); - engine.finish().into() - } -} - /// Helpers for writing test functions working with commit-verify scheme #[cfg(test)] pub(crate) mod test_helpers { @@ -148,34 +127,3 @@ pub(crate) mod test_helpers { }); } } - -#[cfg(test)] -mod test { - use core::fmt::Debug; - use core::hash::Hash; - - use amplify::confinement::SmallVec; - use sha2::Digest; - - use super::test_helpers::*; - use super::*; - use crate::test_helpers::gen_messages; - use crate::UntaggedProtocol; - - #[derive(Debug, Display, Error)] - #[display(Debug)] - struct Error; - - #[derive(Clone, PartialEq, Eq, Debug, Hash)] - struct DummyHashCommitment([u8; 32]); - impl CommitVerify for DummyHashCommitment - where T: AsRef<[u8]> - { - fn commit(msg: &T) -> Self { Self(Sha256::digest(msg.as_ref()).into()) } - } - - #[test] - fn test_commit_verify() { - commit_verify_suite::, DummyHashCommitment>(gen_messages()); - } -} diff --git a/commit_verify/src/id.rs b/commit_verify/src/id.rs index aa398640..baa7ab58 100644 --- a/commit_verify/src/id.rs +++ b/commit_verify/src/id.rs @@ -19,13 +19,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +use amplify::confinement::U64 as U64MAX; use amplify::Bytes32; use sha2::Sha256; -use strict_encoding::{StrictEncode, StrictType}; +use strict_encoding::{StreamWriter, StrictEncode, StrictType}; use strict_types::typesys::TypeFqn; use crate::{DigestExt, LIB_NAME_COMMIT_VERIFY}; +const COMMIT_MAX_LEN: usize = U64MAX; + pub struct CommitEngine { finished: bool, hasher: Sha256, @@ -43,7 +46,8 @@ impl CommitEngine { pub fn commit_to(&mut self, value: &T) { debug_assert!(!self.finished); - let ok = value.strict_write(usize::MAX, &mut self.hasher).is_ok(); + let writer = StreamWriter::new::(&mut self.hasher); + let ok = value.strict_write(writer).is_ok(); let fqn = TypeFqn::with( libname!(T::STRICT_LIB_NAME), T::strict_name().expect("commit encoder can commit only to named types"), diff --git a/commit_verify/src/lib.rs b/commit_verify/src/lib.rs index 00ea5ada..0e0d9945 100644 --- a/commit_verify/src/lib.rs +++ b/commit_verify/src/lib.rs @@ -57,7 +57,7 @@ pub mod merkle; pub mod mpc; mod digest; -pub use commit::{CommitVerify, StrictEncodedProtocol, TryCommitVerify, VerifyError}; +pub use commit::{CommitVerify, TryCommitVerify, VerifyError}; pub use conceal::Conceal; pub use convolve::{ConvolveCommit, ConvolveCommitProof, ConvolveVerifyError}; pub use digest::{Digest, DigestExt, Ripemd160, Sha256}; diff --git a/commit_verify/src/mpc/tree.rs b/commit_verify/src/mpc/tree.rs index 3ebc2164..bcb5e699 100644 --- a/commit_verify/src/mpc/tree.rs +++ b/commit_verify/src/mpc/tree.rs @@ -244,9 +244,9 @@ mod test { use std::collections::BTreeSet; use amplify::num::u5; - use amplify::{Wrapper, WriteCounter}; + use amplify::Wrapper; use rand::random; - use strict_encoding::StrictEncode; + use strict_encoding::{StreamWriter, StrictEncode}; use crate::mpc::tree::test_helpers::{make_random_messages, make_random_tree}; use crate::mpc::MerkleBlock; @@ -284,11 +284,14 @@ mod test { let count = 1_048_576 / 128; let msgs = make_random_messages(count); let tree = make_random_tree(&msgs); - let mut counter = WriteCounter::default(); - tree.strict_write(usize::MAX, &mut counter).unwrap(); + let mut counter = StreamWriter::counter::<{ usize::MAX }>(); + tree.strict_write(&mut counter).unwrap(); eprintln!( "Tree with {} protocol-messages: depth {}, cofactor {}. Serialized length {} bytes", - count, tree.depth, tree.cofactor, counter.count + count, + tree.depth, + tree.cofactor, + counter.unconfine().count ); }