From f3ea6cc92ad584997bb65ca28e1a420476de2962 Mon Sep 17 00:00:00 2001 From: Dave Grantham Date: Wed, 2 Sep 2026 18:26:08 -0600 Subject: [PATCH] dep bumps and merkle enforcement Signed-off-by: Dave Grantham --- .github/workflows/rust.yml | 6 +- CHANGELOG.md | 14 +++++ Cargo.toml | 10 ++-- src/vm/context.rs | 106 +++++++++++++++++++++++++---------- src/vm/xmss_guard.rs | 111 ++++++++++++++++++++++++++++++++++++- 5 files changed, 207 insertions(+), 40 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a23d341..7db3b75 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -37,14 +37,14 @@ jobs: run: cargo doc --no-deps msrv: - name: Verify MSRV (1.95) + name: Verify MSRV (1.96) runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - - name: Install Rust Toolchain (1.95) - uses: dtolnay/rust-toolchain@1.95.0 + - name: Install Rust Toolchain (1.96) + uses: dtolnay/rust-toolchain@1.96.0 - name: Check run: cargo check diff --git a/CHANGELOG.md b/CHANGELOG.md index 0af967d..96e7b40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,19 @@ All notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [2.1.0] - 2026-09-01 + +### Added + +- Merkle-tree Lamport leaf reuse enforcement. The 11 `LamportMerkle*Sig` codecs are now stateful in the VM: `check_signature` reads the consumed leaf index from the `MtSignature` wire data (byte 1, after the depth byte), validates it against the declared tree depth, and enforces strictly-increasing leaf consumption per public key across a verification pass (`enforce_lamport_merkle` in `xmss_guard.rs`, committed per entry by the existing `XmssEnforcement` guard). Reused or rolled-back leaf indices fail verification. +- Unit tests for the merkle guard: monotonic acceptance, duplicate rejection, rollback rejection, per-entry retry tolerance, and no-guard no-op behavior. + +### Changed + +- Updated dependencies: `multi-codec` 1.2 → 1.3, `multi-key` 1.1 → 1.2, `multi-sig` 1.2 → 1.3. +- Raised `rust-version` from 1.95 to 1.96 (required by `multi-key` 1.2 / `lamport_signature_plus` 0.5.0) and updated the CI MSRV job to 1.96. +- Refactored `check_signature`'s stateful-key checks (XMSS, one-time Lamport, merkle Lamport) into a shared `enforce_stateful_key_rules` helper. + ## [2.0.0] - 2026-08-13 ### Summary @@ -59,5 +72,6 @@ different from 1.0.5. - Previous standalone release. Used `wasmtime 19.0`, `thiserror 1.0`, and FSL-1.1 license. Git deps for `multicid`, `multihash`, `multikey`, `multisig`, `multitrait`, `multiutil`. +[2.1.0]: https://github.com/cryptidtech/wacc/compare/v2.0.0...v2.1.0 [2.0.0]: https://github.com/cryptidtech/wacc/releases/tag/v2.0.0 [1.0.5]: https://github.com/cryptidtech/wacc/releases/tag/v1.0.5 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 5d9098c..083a29b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "wacc" -version = "2.0.0" +version = "2.1.0" edition = "2021" -rust-version = "1.95" +rust-version = "1.96" authors = ["Dave Grantham "] description = "Web Assembly Cryptographic Constructs VM implementation" repository = "https://github.com/cryptidtech/wacc.git" @@ -15,10 +15,10 @@ categories = ["cryptography", "wasm"] blake3 = { version = "1.8.1", features = ["traits-preview"] } log = "0.4" multi-cid = "0.2" -multi-codec = "1.2" +multi-codec = "1.3" multi-hash = "1.1" -multi-key = "1.1" -multi-sig = "1.2" +multi-key = "1.2" +multi-sig = "1.3" multi-trait = "1.0" multi-util = "1.1" thiserror = "2.0" diff --git a/src/vm/context.rs b/src/vm/context.rs index bc9ec37..3354c14 100644 --- a/src/vm/context.rs +++ b/src/vm/context.rs @@ -9,7 +9,7 @@ use log::info; use multi_codec::Codec; use multi_hash::{mh, Multihash}; use multi_key::{Multikey, Views}; -use multi_sig::Multisig; +use multi_sig::{Multisig, Views as _}; use multi_util::CodecInfo; use std::{fmt, io::Write}; use wasmtime::{StoreLimits, Val}; @@ -42,6 +42,26 @@ const fn is_lamport_sig(codec: Codec) -> bool { ) } +/// Returns true if the codec is one of the merkle-tree Lamport signature +/// codecs (stateful: each signature consumes a leaf index carried in the +/// signature wire data, byte 1 of the `MtSignature` encoding). +const fn is_lamport_merkle_sig(codec: Codec) -> bool { + matches!( + codec, + Codec::LamportMerkleSha3512Sig + | Codec::LamportMerkleSha3384Sig + | Codec::LamportMerkleSha3256Sig + | Codec::LamportMerkleSha2512Sig + | Codec::LamportMerkleSha2384Sig + | Codec::LamportMerkleSha2256Sig + | Codec::LamportMerkleBlake2B512Sig + | Codec::LamportMerkleBlake2S256Sig + | Codec::LamportMerkleBlake3256Sig + | Codec::LamportMerkleShake128Sig + | Codec::LamportMerkleShake256Sig + ) +} + /// Represents the application state for each instance of a WACC execution. pub struct Context { /// The key-value store of the current state @@ -428,35 +448,8 @@ impl Context { match verification_result { Ok(()) => { info!("check_signature({key}, {msg}) -> true"); - // XMSS is stateful: enforce that the consumed leaf index has not - // been reused or rolled back for this public key across the log. - if is_xmss_msig(sig.codec()) { - let index = match sig.sig_index() { - Some(i) => i, - None => return self.check_fail("XMSS multisig missing sig-index"), - }; - let pubkey_bytes = match pubkey.data_view().and_then(|dv| dv.key_bytes()) { - Ok(b) => b, - Err(e) => return self.check_fail(&e.to_string()), - }; - if let Err(e) = - crate::vm::xmss_guard::enforce_xmss_index(pubkey_bytes.as_slice(), index) - { - return self.check_fail(&e); - } - } - // Lamport keys are one-time: enforce that this public key has not - // already signed an earlier entry in the log. - if is_lamport_sig(sig.codec()) { - let pubkey_bytes = match pubkey.data_view().and_then(|dv| dv.key_bytes()) { - Ok(b) => b, - Err(e) => return self.check_fail(&e.to_string()), - }; - if let Err(e) = - crate::vm::xmss_guard::enforce_lamport_once(pubkey_bytes.as_slice()) - { - return self.check_fail(&e); - } + if let Err(e) = self.enforce_stateful_key_rules(&sig, &pubkey) { + return self.check_fail(&e); } // the signature verification worked so pop the signature argument off // of the stack before continuing @@ -469,4 +462,57 @@ impl Context { } } } + + /// Enforce the stateful/one-time key rules after a successful signature + /// verification: XMSS leaf-index monotonicity, one-time Lamport key use, + /// and merkle-tree Lamport leaf monotonicity. + fn enforce_stateful_key_rules(&self, sig: &Multisig, pubkey: &Multikey) -> Result<(), String> { + let codec = sig.codec(); + // XMSS is stateful: enforce that the consumed leaf index has not + // been reused or rolled back for this public key across the log. + if is_xmss_msig(codec) { + let index = match sig.sig_index() { + Some(i) => i, + None => return Err("XMSS multisig missing sig-index".into()), + }; + let pubkey_bytes = match pubkey.data_view().and_then(|dv| dv.key_bytes()) { + Ok(b) => b, + Err(e) => return Err(e.to_string()), + }; + return crate::vm::xmss_guard::enforce_xmss_index(pubkey_bytes.as_slice(), index); + } + // Lamport keys are one-time: enforce that this public key has not + // already signed an earlier entry in the log. + if is_lamport_sig(codec) { + let pubkey_bytes = match pubkey.data_view().and_then(|dv| dv.key_bytes()) { + Ok(b) => b, + Err(e) => return Err(e.to_string()), + }; + return crate::vm::xmss_guard::enforce_lamport_once(pubkey_bytes.as_slice()); + } + // Merkle-tree Lamport keys are stateful: each signature embeds + // its consumed leaf index in the MtSignature wire data (byte 1, + // after the depth byte). Enforce monotonic consumption across + // the log. + if is_lamport_merkle_sig(codec) { + let pubkey_bytes = match pubkey.data_view().and_then(|dv| dv.key_bytes()) { + Ok(b) => b, + Err(e) => return Err(e.to_string()), + }; + let sig_bytes = match sig.data_view().and_then(|dv| dv.sig_bytes()) { + Ok(b) => b, + Err(e) => return Err(e.to_string()), + }; + let depth = match sig_bytes.first() { + Some(&d) if (1..=3).contains(&d) => d, + _ => return Err("merkle-Lamport signature has invalid depth byte".into()), + }; + let index = match sig_bytes.get(1) { + Some(&i) if usize::from(i) < (1usize << depth) => usize::from(i), + _ => return Err("merkle-Lamport signature has invalid leaf index".into()), + }; + return crate::vm::xmss_guard::enforce_lamport_merkle(pubkey_bytes.as_slice(), index); + } + Ok(()) + } } diff --git a/src/vm/xmss_guard.rs b/src/vm/xmss_guard.rs index b3544bc..7808429 100644 --- a/src/vm/xmss_guard.rs +++ b/src/vm/xmss_guard.rs @@ -1,5 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 -//! Thread-local enforcement of XMSS stateful-signature leaf-index monotonicity. +//! Thread-local enforcement of XMSS stateful-signature leaf-index monotonicity +//! and merkle-tree Lamport leaf reuse prevention. //! //! XMSS is a stateful hash-based signature scheme: each signature consumes a //! one-time leaf `index` that must never be reused. A provenance log records the @@ -9,6 +10,11 @@ //! makes a key-state rollback (e.g. restoring a key from an old snapshot and //! re-signing at an already-consumed index) fail verification. //! +//! Merkle-tree Lamport signatures carry their consumed leaf index inside the +//! signature wire data (`MtSignature` encodes `[depth, index, ...]`, so the +//! index is byte 1). Merkle leaves are also consumed in order, so the same +//! strictly-increasing monotonicity rule applies per merkle public key. +//! //! The state is thread-local because a log is verified sequentially on one //! thread (the wacc VM runs scripts synchronously on the calling thread). The //! provenance-log verifier installs an [`XmssEnforcement`] guard for the @@ -20,7 +26,8 @@ use std::cell::RefCell; use std::collections::{HashMap, HashSet}; /// Per-key stateful-signature state for one verification pass. Tracks XMSS leaf -/// index monotonicity and Lamport one-time-key single use. +/// index monotonicity, Lamport one-time-key single use, and merkle-tree Lamport +/// leaf monotonicity. #[derive(Default)] struct XmssIndexState { /// max leaf index committed by previously-validated entries, keyed by the @@ -33,6 +40,11 @@ struct XmssIndexState { lamport_used: HashSet>, /// Lamport public keys observed while verifying the current entry lamport_observed: Vec>, + /// max merkle leaf index committed by previously-validated entries, keyed + /// by the merkle public-key bytes + merkle_committed: HashMap, usize>, + /// merkle leaf indices observed while verifying the current entry + merkle_observed: Vec<(Vec, usize)>, } impl XmssIndexState { @@ -65,6 +77,22 @@ impl XmssIndexState { Ok(()) } + /// Check that `index` is strictly greater than any committed merkle leaf + /// index for this key, and record it as observed for the current entry. + /// Returns an error if the leaf index is reused or rolled back. + fn check_and_observe_merkle(&mut self, pubkey: &[u8], index: usize) -> Result<(), String> { + if let Some(&max) = self.merkle_committed.get(pubkey) { + if index <= max { + return Err(format!( + "merkle-Lamport leaf index {index} reused or rolled back (last committed \ + index {max})" + )); + } + } + self.merkle_observed.push((pubkey.to_vec(), index)); + Ok(()) + } + /// Commit the current entry's observations into the committed state. fn commit(&mut self) { for (pubkey, index) in self.observed.drain(..) { @@ -76,12 +104,19 @@ impl XmssIndexState { for pubkey in self.lamport_observed.drain(..) { self.lamport_used.insert(pubkey); } + for (pubkey, index) in self.merkle_observed.drain(..) { + let slot = self.merkle_committed.entry(pubkey).or_insert(index); + if index > *slot { + *slot = index; + } + } } /// Discard the current entry's observations (entry did not validate). fn discard(&mut self) { self.observed.clear(); self.lamport_observed.clear(); + self.merkle_observed.clear(); } } @@ -150,6 +185,16 @@ pub(crate) fn enforce_lamport_once(pubkey: &[u8]) -> Result<(), String> { }) } +/// Enforce leaf-index monotonicity for one verified merkle-tree Lamport +/// signature. `index` is the consumed leaf index from the `MtSignature` wire +/// data (byte 1). No-op (returns `Ok`) when no enforcement guard is installed. +pub(crate) fn enforce_lamport_merkle(pubkey: &[u8], index: usize) -> Result<(), String> { + XMSS_STATE.with(|s| match s.borrow_mut().as_mut() { + Some(state) => state.check_and_observe_merkle(pubkey, index), + None => Ok(()), + }) +} + #[cfg(test)] mod tests { use super::*; @@ -245,4 +290,66 @@ mod tests { // guard dropped → enforcement disabled again assert!(enforce_xmss_index(K1, 0).is_ok()); } + + #[test] + fn merkle_monotonic_index_per_key() { + let mut s = XmssIndexState::default(); + // first use of a merkle key at leaf 0 is allowed + assert!(s.check_and_observe_merkle(K1, 0).is_ok()); + s.commit(); + // reusing leaf 0 for the same key is rejected + assert!(s.check_and_observe_merkle(K1, 0).is_err()); + s.discard(); + // the next leaf in order is allowed + assert!(s.check_and_observe_merkle(K1, 1).is_ok()); + s.commit(); + // rolling back to a consumed leaf is rejected + assert!(s.check_and_observe_merkle(K1, 0).is_err()); + assert!(s.check_and_observe_merkle(K1, 1).is_err()); + // a different merkle key is tracked independently + assert!(s.check_and_observe_merkle(K2, 0).is_ok()); + } + + #[test] + fn merkle_same_index_within_one_entry_is_tolerated_until_commit() { + // a lock-script retry may re-observe the same (key, leaf) before commit + let mut s = XmssIndexState::default(); + assert!(s.check_and_observe_merkle(K1, 1).is_ok()); + assert!(s.check_and_observe_merkle(K1, 1).is_ok()); + s.commit(); + assert!(s.check_and_observe_merkle(K1, 1).is_err()); + } + + #[test] + fn merkle_observations_only_count_after_commit() { + let mut s = XmssIndexState::default(); + // observe leaf 1 but do NOT commit (entry failed) + assert!(s.check_and_observe_merkle(K1, 1).is_ok()); + s.discard(); + // leaf 0 is still allowed because 1 was never committed + assert!(s.check_and_observe_merkle(K1, 0).is_ok()); + } + + #[test] + fn merkle_enforce_is_noop_without_installed_guard() { + assert!(enforce_lamport_merkle(K1, 0).is_ok()); + assert!(enforce_lamport_merkle(K1, 0).is_ok()); + } + + #[test] + fn merkle_raii_guard_installs_and_restores() { + assert!(enforce_lamport_merkle(K1, 5).is_ok()); // no guard → ok + { + let guard = XmssEnforcement::install(); + assert!(enforce_lamport_merkle(K1, 0).is_ok()); + guard.commit_entry(); + // reuse now rejected while guard is installed + assert!(enforce_lamport_merkle(K1, 0).is_err()); + // strictly greater leaf is accepted + assert!(enforce_lamport_merkle(K1, 1).is_ok()); + guard.discard_entry(); + } + // guard dropped → enforcement disabled again + assert!(enforce_lamport_merkle(K1, 0).is_ok()); + } }