#secret-key #onion #fiber #public-key #mix #sphinx #networking

fiber-sphinx

A Rust implementation of the Sphinx mix network

7 releases (stable)

new 2.2.0 Dec 12, 2024
2.1.0 Nov 29, 2024
1.0.1 Sep 29, 2024
0.2.0 Sep 25, 2024
0.1.1 Sep 23, 2024

#586 in Network programming

Download history 297/week @ 2024-09-20 258/week @ 2024-09-27 126/week @ 2024-10-04 311/week @ 2024-10-11 848/week @ 2024-10-18 336/week @ 2024-10-25 90/week @ 2024-11-01 357/week @ 2024-11-08 551/week @ 2024-11-15 581/week @ 2024-11-22 798/week @ 2024-11-29 1141/week @ 2024-12-06

3,193 downloads per month

Custom license

270KB
770 lines

Fiber Sphinx

A Rust implementation of Sphinx (a.k.a. Onion Message) for Fiber.


lib.rs:

A Rust implementation of Sphinx (a.k.a. Onion Message) for Fiber.

See more in the Specification.

Example

use secp256k1::{PublicKey, SecretKey, Secp256k1};
use fiber_sphinx::OnionPacket;

let secp = Secp256k1::new();
let hops_keys = vec![
    SecretKey::from_slice(&[0x20; 32]).expect("32 bytes, within curve order"),
    SecretKey::from_slice(&[0x21; 32]).expect("32 bytes, within curve order"),
    SecretKey::from_slice(&[0x22; 32]).expect("32 bytes, within curve order"),
];
let hops_path = hops_keys.iter().map(|sk| sk.public_key(&secp)).collect();
let session_key = SecretKey::from_slice(&[0x41; 32]).expect("32 bytes, within curve order");
// Use the first byte to indicate the data len
let hops_data = vec![vec![0], vec![1, 0], vec![5, 0, 1, 2, 3, 4]];
let get_length = |packet_data: &[u8]| Some(packet_data[0] as usize + 1);
let assoc_data = vec![0x42u8; 32];

let packet = OnionPacket::create(
    session_key,
    hops_path,
    hops_data.clone(),
    Some(assoc_data.clone()),
    1300,
    &secp,
).expect("new onion packet");

// Hop 0
let res = packet.peel(&hops_keys[0], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, packet) = res.unwrap();
assert_eq!(data, hops_data[0]);

// Hop 1
let res = packet.peel(&hops_keys[1], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, packet) = res.unwrap();
assert_eq!(data, hops_data[1]);

// Hop 2
let res = packet.peel(&hops_keys[2], Some(&assoc_data), &secp, get_length);
assert!(res.is_ok());
let (data, _packet) = res.unwrap();
assert_eq!(data, hops_data[2]);

Dependencies

~6.5MB
~86K SLoC