stratum-node-config 0.1.0-alpha.4

Versioned stratum.toml schema, comment-preserving editor, semantic validator, change preview and public-config hash for Stratum nodes
Documentation
//! Shared test vectors: real age post-quantum hybrid recipients and builders
//! for malformed encodings of them.
//!
//! `tests/data/age-pq-recipients.txt` holds four recipients, one per line.
//! The first is `TEST_RECIPIENT` from `age` 0.13.0 (`src/native/pq.rs`), the
//! crate's own published vector. The other three were generated by a
//! throwaway program depending on `age = "=0.13.0"` that called
//! `age::pq::Identity::generate()`, printed `to_public().to_string()`, and
//! checked each line parses back as `age::pq::Recipient` to the same string.

#![allow(
    dead_code,
    reason = "each integration-test binary uses a different subset of these helpers"
)]

use bech32::{ByteIterExt, Fe32, Fe32IterExt, Hrp};

/// Four real post-quantum recipients, each 1959 characters.
pub(crate) fn pq_recipients() -> Vec<&'static str> {
    include_str!("../data/age-pq-recipients.txt")
        .lines()
        .filter(|l| !l.is_empty())
        .collect()
}

/// The `n`th real post-quantum recipient (0 to 3).
pub(crate) fn pq(n: usize) -> &'static str {
    pq_recipients()[n]
}

/// A classical X25519 recipient: `TEST_PK` from `age` 0.13.0
/// (`src/native/x25519.rs`).
pub(crate) const CLASSICAL_RECIPIENT: &str =
    "age1t7rxyev2z3rw82stdlrrepyc39nvn86l5078zqkf5uasdy86jp6svpy7pa";

/// The Bech32 (BIP-173) checksum without the 90-character limit, as `age`
/// uses for its long post-quantum recipients.
enum Bech32Long {}

impl bech32::Checksum for Bech32Long {
    type MidstateRepr = u32;
    type CorrectionField = <bech32::Bech32 as bech32::Checksum>::CorrectionField;
    const ROOT_GENERATOR: Self::CorrectionField = bech32::Bech32::ROOT_GENERATOR;
    const ROOT_EXPONENTS: core::ops::RangeInclusive<usize> = bech32::Bech32::ROOT_EXPONENTS;
    const CODE_LENGTH: usize = usize::MAX;
    const CHECKSUM_LENGTH: usize = bech32::Bech32::CHECKSUM_LENGTH;
    const GENERATOR_SH: [u32; 5] = bech32::Bech32::GENERATOR_SH;
    const TARGET_RESIDUE: u32 = bech32::Bech32::TARGET_RESIDUE;
}

/// Decodes a real recipient to its payload bytes.
pub(crate) fn payload_of(recipient: &str) -> Vec<u8> {
    bech32::primitives::decode::CheckedHrpstring::new::<Bech32Long>(recipient)
        .expect("fixture is valid bech32")
        .byte_iter()
        .collect()
}

/// Encodes `payload` under `hrp` with a valid long Bech32 checksum.
pub(crate) fn encode(hrp: &str, payload: &[u8]) -> String {
    let hrp = Hrp::parse(hrp).expect("test hrp is valid");
    bech32::encode_lower::<Bech32Long>(hrp, payload).expect("no length limit")
}

/// Encodes `payload` under `age1pq` with a valid checksum but non-zero
/// trailing padding bits, a textual alias a strict decoder must refuse.
pub(crate) fn encode_with_nonzero_padding(payload: &[u8]) -> String {
    let hrp = Hrp::parse("age1pq").expect("valid hrp");
    let mut fes: Vec<Fe32> = payload.iter().copied().bytes_to_fes().collect();
    let last = fes.pop().expect("payload is not empty");
    // 1216 bytes leave two padding bits in the last 5-bit group.
    let flipped = Fe32::try_from(last.to_u8() | 0b1).expect("value below 32");
    assert_ne!(flipped, last, "padding bit must change");
    fes.push(flipped);
    fes.into_iter()
        .with_checksum::<Bech32Long>(&hrp)
        .chars()
        .collect()
}