teter-core 26.11.7

What teter's clients share without I/O: end-to-end encryption, account crypto, signed commands
Documentation
//! Seals made on one target open on the other (#142): `vectors/native.txt`
//! was made by this test's generator built for the machine, and
//! `vectors/wasm.txt` by the same generator built as WebAssembly
//! (`wasm32-wasip1`, run by `scripts/wasi-run.mjs`). Each target opens both,
//! and makes the same signature. To make a vector again:
//! `cargo test -p teter-core --test vectors -- --ignored --nocapture`, with
//! `--target wasm32-wasip1` and the runner for the other.
// An allow, not an expect: clippy exempts a #[test] function already, so an expect would go
// unfulfilled where only those use these. `allow_attributes` skips inner attributes.
#![allow(
    clippy::unwrap_used,
    clippy::print_stdout,
    reason = "a test reports by panicking; the generator prints its vector"
)]

use teter_common::SessionId;
use teter_core::account::{AccountKeys, seal_to};
use teter_core::e2e::{Direction, Keys, Kind, Secret};

const SECRET: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
const SESSION: &str = "6f1d3c52-7a49-4e0b-9a8e-2b6c1d0e4f73";
const CHANNEL: &str = "0123456789abcdef0123456789abcdef";
const INFO: &[u8] = b"teter-seal-v1 session key 0123456789abcdef0123456789abcdef";
const PLAIN: &[u8] = b"printf 'te%s\\n' ter";

/// The account's fixed private keys: X25519, then the Ed25519 seed.
fn account() -> AccountKeys {
    let bytes: Vec<u8> = (0..64_u8)
        .map(|n| n.wrapping_mul(7).wrapping_add(1))
        .collect();
    AccountKeys::from_private_bytes(&bytes).unwrap()
}

fn keys() -> Keys {
    Keys::derive_on(
        &Secret::parse(SECRET).unwrap(),
        SessionId::parse_str(SESSION).unwrap(),
        CHANNEL,
    )
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}

fn unhex(text: &str) -> Vec<u8> {
    (0..text.len())
        .step_by(2)
        .map(|at| u8::from_str_radix(text.get(at..at + 2).unwrap(), 16).unwrap())
        .collect()
}

/// The value of `name` in a vector file.
fn field<'a>(vector: &'a str, name: &str) -> &'a str {
    vector
        .lines()
        .find_map(|line| line.strip_prefix(name)?.strip_prefix('='))
        .unwrap()
}

/// Opens the seals of `vector` and checks its signature.
fn opens(vector: &str) {
    let mut opener = keys().opener(Direction::ToFrontend);
    let plain = opener
        .open(Kind::Output, &unhex(field(vector, "e2e")))
        .unwrap();
    assert_eq!(plain.as_slice(), PLAIN);
    let plain = account()
        .open_sealed(INFO, &unhex(field(vector, "sealed_to")))
        .unwrap();
    assert_eq!(plain.as_slice(), PLAIN);
    assert_eq!(hex(&account().sign(PLAIN)), field(vector, "signature"));
}

#[test]
fn a_seal_made_natively_opens() {
    opens(include_str!("vectors/native.txt"));
}

#[test]
fn a_seal_made_in_webassembly_opens() {
    opens(include_str!("vectors/wasm.txt"));
}

#[test]
#[ignore = "the generator: run it to make a vector"]
fn make_a_vector() {
    let mut sealer = keys().sealer(Direction::ToFrontend).unwrap();
    println!("e2e={}", hex(&sealer.seal(Kind::Output, PLAIN)));
    let sealed = seal_to(&account().public_keys(), INFO, PLAIN).unwrap();
    println!("sealed_to={}", hex(&sealed));
    println!("signature={}", hex(&account().sign(PLAIN)));
}