Skip to content

Commit

Permalink
Big refactor according to updated spec
Browse files Browse the repository at this point in the history
Reference C implementation:
cryptape/omnilock#3

Changes:

- add lazy reader support
- API changed
- add testing
- add documents. make it ready for publishing
- fix according to updated spec
- add logs
  • Loading branch information
XuJiandong committed Mar 25, 2024
1 parent e29514f commit d904f9f
Show file tree
Hide file tree
Showing 34 changed files with 5,086 additions and 389 deletions.
67 changes: 59 additions & 8 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = ["ckb-transaction-cobuild", "contracts/transaction-cobuild-lock-demo", "contracts/transaction-cobuild-type-demo", "contracts/transaction-cobuild-otx-lock-demo"]
members = ["ckb-transaction-cobuild", "contracts/transaction-cobuild-lock-demo", "contracts/transaction-cobuild-type-demo", "contracts/transaction-cobuild-otx-lock-demo", "contracts/always-success"]
exclude = ["tests"]
resolver = "2"

[profile.release]
overflow-checks = true
Expand All @@ -16,3 +17,6 @@ opt-level = 1
debug = false
panic = 'abort'
debug-assertions = true

[patch.crates-io]
molecule = { git = "https://github.com/XuJiandong/molecule.git", rev = "785a309" }
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ mol:
moleculec --language rust --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas/top_level.rs
cargo fmt

mol2:
moleculec --language rust-lazy-reader --schema-file schemas/basic.mol > ckb-transaction-cobuild/src/schemas2/basic.rs
moleculec --language rust-lazy-reader --schema-file schemas/top_level.mol > ckb-transaction-cobuild/src/schemas2/top_level.rs
moleculec --language rust-lazy-reader --schema-file schemas/blockchain.mol > ckb-transaction-cobuild/src/schemas2/blockchain.rs
cargo fmt


install:
rustup target add riscv64imac-unknown-none-elf
cargo install cross --git https://github.com/cross-rs/cross
Expand All @@ -19,3 +26,7 @@ install:
ci:
capsule build --release
cd tests && cargo test && cd ..

debug:
capsule build --release
cd tests && RUST_LOG=debug cargo test -- --nocapture && cd ..
4 changes: 4 additions & 0 deletions capsule.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ template_type = "Rust"
[[contracts]]
name = "transaction-cobuild-otx-lock-demo"
template_type = "Rust"

[[contracts]]
name = "always-success"
template_type = "Rust"
6 changes: 5 additions & 1 deletion ckb-transaction-cobuild/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = []
log = []

[dependencies]
blake2b-ref = "0.3.1"
ckb-std = { version = "0.14.3", default-features = false, features = ["ckb-types"] }
molecule = { version = "0.7.5", default-features = false }
molecule = { version = "0.7.5", default-features = false, features = ["bytes_vec"] }
ckb-gen-types = { version = "0.111.0", default-features = false }
66 changes: 54 additions & 12 deletions ckb-transaction-cobuild/src/blake2b.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
pub use blake2b_ref::{Blake2b, Blake2bBuilder};
pub use molecule::lazy_reader::Cursor;

pub const PERSONALIZATION_SIGHASH_ALL: &[u8] = b"ckb-tcob-sighash";
pub const PERSONALIZATION_SIGHASH_ALL_ONLY: &[u8] = b"ckb-tcob-sgohash";
pub const PERSONALIZATION_OTX: &[u8] = b"ckb-tcob-otxhash";

const BATCH_SIZE: usize = 2048;

/// return a blake2b instance with personalization for SighashAll
pub fn new_sighash_all_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL)
.build()
pub fn new_sighash_all_blake2b() -> Blake2bStatistics {
Blake2bStatistics::new(
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL)
.build(),
)
}

/// return a blake2b instance with personalization for SighashAllOnly
pub fn new_sighash_all_only_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL_ONLY)
.build()
pub fn new_sighash_all_only_blake2b() -> Blake2bStatistics {
Blake2bStatistics::new(
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_SIGHASH_ALL_ONLY)
.build(),
)
}

/// return a blake2b instance with personalization for OTX
pub fn new_otx_blake2b() -> Blake2b {
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_OTX)
.build()
pub fn new_otx_blake2b() -> Blake2bStatistics {
Blake2bStatistics::new(
Blake2bBuilder::new(32)
.personal(PERSONALIZATION_OTX)
.build(),
)
}

pub struct Blake2bStatistics {
count: usize,
blake2b: Blake2b,
}

impl Blake2bStatistics {
pub fn new(blake2b: Blake2b) -> Self {
Self { count: 0, blake2b }
}

pub fn update(&mut self, data: &[u8]) {
self.blake2b.update(data);
self.count += data.len();
}
pub fn update_cursor(&mut self, mut cursor: Cursor) {
let mut buf = [0u8; BATCH_SIZE];
while cursor.size > 0 {
let read_len = cursor.read_at(&mut buf).unwrap();
if read_len > 0 {
self.update(&buf[0..read_len]);
cursor = cursor.slice_by_start(read_len).unwrap();
}
}
}

pub fn finalize(self, dst: &mut [u8]) {
self.blake2b.finalize(dst)
}
pub fn count(&self) -> usize {
self.count
}
}
36 changes: 36 additions & 0 deletions ckb-transaction-cobuild/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use ckb_std::error::SysError;
use molecule::error::VerificationError;
pub use molecule::lazy_reader::Error as LazyReaderError;

#[derive(Debug)]
pub enum Error {
Sys(SysError),
LazyReader(LazyReaderError),
MoleculeEncoding,
WrongSighashAll,
WrongWitnessLayout,
WrongOtxStart,
WrongOtx,
NoSealFound,
AuthError,
ScriptHashAbsent,
WrongCount,
}

impl From<SysError> for Error {
fn from(e: SysError) -> Self {
Error::Sys(e)
}
}

impl From<VerificationError> for Error {
fn from(_: VerificationError) -> Self {
Error::MoleculeEncoding
}
}

impl From<LazyReaderError> for Error {
fn from(e: LazyReaderError) -> Self {
Error::LazyReader(e)
}
}
Loading

0 comments on commit d904f9f

Please sign in to comment.