diff --git a/crates/lxmf-core/src/message.rs b/crates/lxmf-core/src/message.rs index 57539d0..c1e3214 100644 --- a/crates/lxmf-core/src/message.rs +++ b/crates/lxmf-core/src/message.rs @@ -485,9 +485,12 @@ impl LxMessage { } } let stamp = <&[u8; 32]>::try_from(stamp).ok()?; - let work = crate::stamper::stamp_workblock(&self.message_id, STAMP_WORKBLOCK_EXPAND_ROUNDS); - crate::stamper::stamp_valid(stamp, target_cost, &work) - .then(|| crate::stamper::stamp_value(&work, stamp)) + crate::stamper::validated_stamp_value( + &self.message_id, + stamp, + target_cost, + STAMP_WORKBLOCK_EXPAND_ROUNDS, + ) } /// Update [`auto_compress`](MessageMetadata::auto_compress) based on a peer's cached announce app_data. /// diff --git a/crates/lxmf-core/src/message/tests.rs b/crates/lxmf-core/src/message/tests.rs index 717fd52..ba0a3c8 100644 --- a/crates/lxmf-core/src/message/tests.rs +++ b/crates/lxmf-core/src/message/tests.rs @@ -375,6 +375,21 @@ fn recipient_stamp_array_range_and_malformed_dispositions() { } } #[test] +fn non_expanded_recipient_stamp_validates_with_its_own_value() { + let signed = draft().sign(&key()).unwrap(); + let (stamp, value) = crate::stamper::generate_stamp( + &signed.message_id(), + 6.0, + 0, + &crate::stamper::StampControl::default(), + ) + .unwrap() + .unwrap(); + let mut m = signed.finalize(RecipientStamp::from_bytes(&stamp)).unwrap(); + assert_eq!(m.validated_stamp_value(6.0, None), Some(value)); + assert!(m.check_stamp(6.0, None)); +} +#[test] fn recipient_ticket_stamp_and_missing_stamp_policy() { let mut d = draft(); d.outbound_ticket = Some([3; 16]); diff --git a/crates/lxmf-core/src/propagation_node.rs b/crates/lxmf-core/src/propagation_node.rs index 4c4ad99..a6ac216 100644 --- a/crates/lxmf-core/src/propagation_node.rs +++ b/crates/lxmf-core/src/propagation_node.rs @@ -402,10 +402,13 @@ impl ValidatedOpaquePropagationBlob { let body = &bytes[..bytes.len() - 32]; let stamp: &[u8; 32] = bytes[bytes.len() - 32..].try_into().ok()?; let transient_id = rns_crypto::sha::full_hash(body); - let workblock = - crate::stamper::stamp_workblock(&transient_id, STAMP_WORKBLOCK_EXPAND_ROUNDS_PN); - let stamp_value = crate::stamper::stamp_value(&workblock, stamp); - crate::stamper::stamp_valid(stamp, minimum_stamp_cost, &workblock).then_some(Self { + let stamp_value = crate::stamper::validated_stamp_value( + &transient_id, + stamp, + minimum_stamp_cost, + STAMP_WORKBLOCK_EXPAND_ROUNDS_PN, + )?; + Some(Self { bytes, transient_id, stamp_value, @@ -896,8 +899,10 @@ impl PropagationNode { } /// Validate and reserve a new opaque propagation image plus its 32-byte stamp. - /// Checks the measured stamp value against `stamp_value` and node policy; - /// this method does not persist the returned plan. + /// `stamp_value` must be one the stamp measures in either workblock form, + /// since a check at another cost can report the other form; the plan + /// records the value measured at the node's cost. This method does not + /// persist the returned plan. /// /// Stored images retain `lxmf_data || stamp` for peer sync. Client `/get` /// strips that stamp. Duplicates return `None`; use [`Self::plan_opaque_admission`] @@ -917,7 +922,13 @@ impl PropagationNode { let blob = ValidatedOpaquePropagationBlob::validate(bytes.into(), self.config.min_stamp_cost)?; if blob.stamp_value() != stamp_value { - return None; + let tid = blob.transient_id(); + let expanded = crate::stamper::stamp_workblock(&tid, STAMP_WORKBLOCK_EXPAND_ROUNDS_PN); + if stamp_value != crate::stamper::stamp_value(&tid, stamp_data) + && stamp_value != crate::stamper::stamp_value(&expanded, stamp_data) + { + return None; + } } match self.plan_opaque_admission(blob) { PropagationAdmission::Reserved(plan) => Some(*plan), @@ -3313,6 +3324,49 @@ pub(crate) mod tests { let _ = std::fs::remove_dir_all(&dir); } + // Upstream LXMF 1.3.1 `validate_pn_stamp` over `range(150)`: a + // non-expanded stamp (8.0703125 at cost 8) and an expanded one (8.1953125 + // at cost 8, 2.19921875 at cost 0, from the non-expanded check). + const PN_NE_STAMP: &str = "228341ee29d47d4d9defe50c2704b1feb4bc2ef066a740637916cc690aaa4fd1"; + const PN_EXPANDED_STAMP: &str = + "7e340b0726c99d8f60ae845912ecccc8aa7363db7a971a09132acd83c88ad771"; + + fn pn_vector(stamp: &str) -> (Vec, [u8; 32]) { + let stamp = hex::decode(stamp).unwrap().try_into().unwrap(); + ((0..150).collect(), stamp) + } + + #[test] + fn non_expanded_deposits_validate_with_their_own_value() { + let (data, stamp) = pn_vector(PN_NE_STAMP); + let blob = ValidatedOpaquePropagationBlob::validate( + [data.as_slice(), &stamp].concat().into(), + 8.0, + ) + .unwrap(); + assert_eq!(blob.stamp_value(), 8.0703125); + let config = PropagationNodeConfig { + min_stamp_cost: 8.0, + ..Default::default() + }; + let mut node = durable_test_node(config, [0xAA; 16]); + assert!(node.accept_stamped_propagated_blob(&data, &stamp, 8.0703125)); + } + + #[test] + fn a_value_measured_at_another_cost_is_accepted_at_the_node_value() { + let (data, stamp) = pn_vector(PN_EXPANDED_STAMP); + let config = PropagationNodeConfig { + min_stamp_cost: 8.0, + ..Default::default() + }; + let mut node = durable_test_node(config, [0xAA; 16]); + assert!(!node.accept_stamped_propagated_blob(&data, &stamp, 9.0)); + assert!(node.accept_stamped_propagated_blob(&data, &stamp, 2.19921875)); + let tid = rns_crypto::sha::full_hash(&data); + assert_eq!(node.store.get_stamp_value(&tid), Some(8.1953125)); + } + #[test] fn test_accept_propagated_blob_enforces_min_stamp_cost() { let config = PropagationNodeConfig { diff --git a/crates/lxmf-core/src/propagation_offer.rs b/crates/lxmf-core/src/propagation_offer.rs index 555bae0..ef94aa4 100644 --- a/crates/lxmf-core/src/propagation_offer.rs +++ b/crates/lxmf-core/src/propagation_offer.rs @@ -276,6 +276,22 @@ mod tests { ); } + #[test] + fn a_non_expanded_peering_key_is_accepted() { + // Upstream LXMF 1.3.1 `validate_peering_key` accepts this key at cost 8 + // for the peering id `[0x11; 16] || [0x22; 16]`. + let key = hex::decode("d63d05f5dbe354b33213745d275a343924b38da05098d2b7cbc521ca4ff103d1") + .unwrap(); + let request = encode_offer( + Value::Binary(key), + Value::Array(vec![Value::Binary(id(3).to_vec())]), + ); + assert_eq!( + evaluate(&request, &[0x11; 16], &[0x22; 16], 8.0, 1024, |_| false), + Ok(PnOfferEvaluation::WantAll) + ); + } + #[test] fn empty_key_is_valid_only_when_peering_work_is_disabled() { let request = encode_offer( diff --git a/crates/lxmf-core/src/propagation_store_v2/recovery.rs b/crates/lxmf-core/src/propagation_store_v2/recovery.rs index 3f6a271..b717a5d 100644 --- a/crates/lxmf-core/src/propagation_store_v2/recovery.rs +++ b/crates/lxmf-core/src/propagation_store_v2/recovery.rs @@ -58,14 +58,13 @@ fn validate( } let stamp_value = if record.stamped { let stamp: &[u8; 32] = bytes[bytes.len() - 32..].try_into().map_err(err)?; - let workblock = crate::stamper::stamp_workblock( + crate::stamper::validated_stamp_value( &record.transient_id, + stamp, + minimum_stamp_cost, crate::constants::STAMP_WORKBLOCK_EXPAND_ROUNDS_PN, - ); - if !crate::stamper::stamp_valid(stamp, minimum_stamp_cost, &workblock) { - return Err(invalid("recovered stamp does not meet node policy")); - } - crate::stamper::stamp_value(&workblock, stamp) + ) + .ok_or_else(|| invalid("recovered stamp does not meet node policy"))? } else { if minimum_stamp_cost > 0.0 { return Err(invalid("unstamped recovery does not meet node policy")); diff --git a/crates/lxmf-core/src/stamper.rs b/crates/lxmf-core/src/stamper.rs index a97d7c0..7f33e89 100644 --- a/crates/lxmf-core/src/stamper.rs +++ b/crates/lxmf-core/src/stamper.rs @@ -4,6 +4,10 @@ //! validity and generation from `RNS.Stamps`. Only the expand-round counts per //! stamp kind are LXMF's own (`STAMP_WORKBLOCK_EXPAND_ROUNDS{,_PN,_PEERING}` //! in constants.rs). Costs and values are fractional on the 1/256 grid. +//! +//! Validation accepts the non-expanded workblock (the material itself) as well +//! as the expanded one, as LXMF 1.3.1 does while senders move to it. +//! Generation stays expanded: LXMF 1.3.0 receivers accept only that form. use std::sync::atomic::Ordering; @@ -50,20 +54,43 @@ pub fn generate_stamp( })) } +/// The stamp's value when it meets `cost` over the non-expanded workblock, +/// else over `expand_rounds`, from the workblock that passed (LXMF 1.3.1 +/// `LXMessage.validate_stamp`, `LXStamper.validate_pn_stamp`). The value can +/// depend on `cost`: below about cost 4 an expanded stamp often passes the +/// non-expanded check too, and at cost 0 it always does. +pub fn validated_stamp_value( + material: &[u8], + stamp: &[u8], + cost: f64, + expand_rounds: usize, +) -> Option { + if stamp_valid(stamp, cost, material) { + return Some(stamp_value(material, stamp)); + } + let workblock = stamp_workblock(material, expand_rounds); + stamp_valid(stamp, cost, &workblock).then(|| stamp_value(&workblock, stamp)) +} + +/// Whether `stamp` meets `cost` in either workblock form; see [`validated_stamp_value`]. pub fn validate_stamp( message_id: &[u8; 32], stamp: &[u8], cost: f64, expand_rounds: usize, ) -> bool { - let workblock = stamp_workblock(message_id, expand_rounds); - stamp_valid(stamp, cost, &workblock) + validated_stamp_value(message_id, stamp, cost, expand_rounds).is_some() } /// Python reference: `LXStamper.validate_peering_key`. pub fn validate_peering_key(peering_id: &[u8], peering_key: &[u8], target_cost: f64) -> bool { - let workblock = stamp_workblock(peering_id, STAMP_WORKBLOCK_EXPAND_ROUNDS_PEERING); - stamp_valid(peering_key, target_cost, &workblock) + validated_stamp_value( + peering_id, + peering_key, + target_cost, + STAMP_WORKBLOCK_EXPAND_ROUNDS_PEERING, + ) + .is_some() } /// Python reference: `LXStamper.validate_pn_stamp`. @@ -77,11 +104,12 @@ pub fn validate_pn_stamp(transient_data: &[u8], target_cost: f64) -> Option [u8; 32] { + hex::decode(hex).unwrap().try_into().unwrap() + } + + #[test] + fn message_stamps_validate_in_both_forms_as_upstream() { + let id = bytes32("179201d9189af65b2fee7b97dc90a4e9232121e084050aff94d27f171dc71f55"); + let ne = bytes32("84276a6ab90c48763474eef01f7ce3e48b8ffad7f4b0eb1de8dd5761f9614ccd"); + let ex = bytes32("a824ef0289c7542b417588d5e93fd3e8e2a0f624c6dc27c1a02e3f864c45fba4"); + let rounds = STAMP_WORKBLOCK_EXPAND_ROUNDS; + assert_eq!( + validated_stamp_value(&id, &ne, 8.0, rounds), + Some(11.84765625) + ); + assert_eq!(validated_stamp_value(&id, &ex, 8.0, rounds), Some(8.09375)); + assert_eq!( + validated_stamp_value(&id, &ex, 0.0, rounds), + Some(0.37109375) + ); + assert_eq!(validated_stamp_value(&id, &ne, 11.8515625, rounds), None); + assert!(validate_stamp(&id, &ne, 8.0, rounds)); + } + + #[test] + fn propagation_stamps_validate_in_both_forms_as_upstream() { + let lxm_data: Vec = (0..150).collect(); + let entry = |stamp: &str| [lxm_data.clone(), bytes32(stamp).to_vec()].concat(); + let ne = entry("228341ee29d47d4d9defe50c2704b1feb4bc2ef066a740637916cc690aaa4fd1"); + let ex = entry("7e340b0726c99d8f60ae845912ecccc8aa7363db7a971a09132acd83c88ad771"); + let value = |entry: &[u8], cost| validate_pn_stamp(entry, cost).map(|parts| parts.2); + assert_eq!( + validate_pn_stamp(&ne, 8.0).unwrap().0, + bytes32("f22b2e614e92d6453612b707385038300293d2cc292b148bc5335754b5ea30fd") + ); + assert_eq!(value(&ne, 8.0), Some(8.0703125)); + assert_eq!(value(&ex, 8.0), Some(8.1953125)); + assert_eq!(value(&ex, 0.0), Some(2.19921875)); + assert_eq!(value(&ne, 8.07421875), None); + } + + #[test] + fn peering_keys_validate_in_both_forms_as_upstream() { + let id: Vec = [[0x11; 16], [0x22; 16]].concat(); + let ne = bytes32("d63d05f5dbe354b33213745d275a343924b38da05098d2b7cbc521ca4ff103d1"); + let ex = bytes32("4269201b6742c33c84098644f37c3f159c66235bda2c26f108cc7c824936ac7f"); + assert!(validate_peering_key(&id, &ne, 8.0)); + assert!(validate_peering_key(&id, &ex, 8.0)); + assert!(!validate_peering_key(&id, &ne, 8.2578125)); + } + fn found(material: &[u8], cost: f64, rounds: usize) -> ([u8; 32], f64) { generate_stamp(material, cost, rounds, &StampControl::default()) .unwrap()