Skip to content

Commit

Permalink
chore: update to new strict encode APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 12, 2024
1 parent ce4d602 commit 9562563
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 85 deletions.
46 changes: 23 additions & 23 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
52 changes: 0 additions & 52 deletions commit_verify/src/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<T> CommitVerify<T, StrictEncodedProtocol> 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 {
Expand Down Expand Up @@ -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<T> CommitVerify<T, UntaggedProtocol> 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::<SmallVec<u8>, DummyHashCommitment>(gen_messages());
}
}
8 changes: 6 additions & 2 deletions commit_verify/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -43,7 +46,8 @@ impl CommitEngine {

pub fn commit_to<T: StrictEncode>(&mut self, value: &T) {
debug_assert!(!self.finished);
let ok = value.strict_write(usize::MAX, &mut self.hasher).is_ok();
let writer = StreamWriter::new::<COMMIT_MAX_LEN>(&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"),
Expand Down
2 changes: 1 addition & 1 deletion commit_verify/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
13 changes: 8 additions & 5 deletions commit_verify/src/mpc/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
);
}

Expand Down

0 comments on commit 9562563

Please sign in to comment.