Skip to content

Commit

Permalink
derive pad key for filler generation
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Sep 4, 2024
1 parent 590877c commit 9886a18
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
[dependencies]
secp256k1 = { version = "0.28.0", features = ["serde"] }
sha2 = "0.10.8"
hmac = "0.12.1"
thiserror = "1.0"

[dev-dependencies]
hex-literal = "0.4.1"
Expand Down
34 changes: 32 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use hmac::{Hmac, Mac as _};
use secp256k1::{ecdh::SharedSecret, PublicKey, Scalar, Secp256k1, SecretKey, Signing};
use sha2::{Digest as _, Sha256};
use thiserror::Error;

pub struct OnionPacket;
pub struct Error;

#[derive(Error, Debug)]
pub enum SphinxError {
#[error("unknown sphinx error")]
Unknown,
}

/// Derives the ephemeral key for the next hop.
///
Expand Down Expand Up @@ -52,12 +59,18 @@ pub fn derive_hop_shared_secrets<C: Signing>(
.collect()
}

pub fn derive_key(hmac_key: &[u8], shared_secret: &[u8]) -> [u8; 32] {
let mut mac = Hmac::<Sha256>::new_from_slice(hmac_key).expect("valid hmac key");
mac.update(shared_secret);
mac.finalize().into_bytes().into()
}

pub fn new_onion_packet(
_payment_path: Vec<PublicKey>,
_session_key: SecretKey,
_hops_data: Vec<Vec<u8>>,
_assoc_data: Vec<u8>,
) -> Result<OnionPacket, Error> {
) -> Result<OnionPacket, SphinxError> {
Ok(OnionPacket)
}

Expand Down Expand Up @@ -110,4 +123,21 @@ mod tests {
assert_eq!(ss, expected_ss);
}
}

#[test]
fn test_derive_um_key() {
let shared_secret =
hex!("b5756b9b542727dbafc6765a49488b023a725d631af688fc031217e90770c328");
let expected_um_key = "4da7f2923edce6c2d85987d1d9fa6d88023e6c3a9c3d20f07d3b10b61a78d646";
let um_key = derive_key(b"um", &shared_secret);
assert_eq!(base16::encode_lower(&um_key), expected_um_key);
}

#[test]
fn test_derive_pad_key() {
let shared_secret = [0x41; 32];
let expected_pad_key = "70fa47d28edc4faf3e733ae0f4d2a12b8c5f09cbd74408eb7bc6ba2f1ebf88a2";
let pad_key = derive_key(b"pad", &shared_secret);
assert_eq!(base16::encode_lower(&pad_key), expected_pad_key);
}
}

0 comments on commit 9886a18

Please sign in to comment.