From f6341825326468406a9e47ef56b545f8a1f81082 Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Tue, 27 Aug 2024 12:45:52 +0000 Subject: [PATCH] update deps, remove onion code --- Cargo.lock | 204 +++++++++---------- Cargo.toml | 36 ++-- onion/Cargo.toml | 38 ---- onion/src/crypto/comsig.rs | 189 ------------------ onion/src/crypto/dalek.rs | 272 ------------------------- onion/src/crypto/mod.rs | 3 - onion/src/crypto/secp.rs | 62 ------ onion/src/lib.rs | 171 ---------------- onion/src/onion.rs | 400 ------------------------------------- onion/src/util.rs | 164 --------------- src/bin/mwixnet.rs | 1 + 11 files changed, 116 insertions(+), 1424 deletions(-) delete mode 100644 onion/Cargo.toml delete mode 100644 onion/src/crypto/comsig.rs delete mode 100644 onion/src/crypto/dalek.rs delete mode 100644 onion/src/crypto/mod.rs delete mode 100644 onion/src/crypto/secp.rs delete mode 100644 onion/src/lib.rs delete mode 100644 onion/src/onion.rs delete mode 100644 onion/src/util.rs diff --git a/Cargo.lock b/Cargo.lock index 388937d..1fd90f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1275,6 +1275,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core 0.13.4", + "darling_macro 0.13.4", +] + [[package]] name = "darling" version = "0.14.4" @@ -1295,6 +1305,20 @@ dependencies = [ "darling_macro 0.20.8", ] +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2 1.0.70", + "quote 1.0.33", + "strsim 0.10.0", + "syn 1.0.109", +] + [[package]] name = "darling_core" version = "0.14.4" @@ -1323,6 +1347,17 @@ dependencies = [ "syn 2.0.39", ] +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core 0.13.4", + "quote 1.0.33", + "syn 1.0.109", +] + [[package]] name = "darling_macro" version = "0.14.4" @@ -1849,12 +1884,6 @@ dependencies = [ "pin-project-lite 0.2.13", ] -[[package]] -name = "fake-simd" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" - [[package]] name = "fallible-iterator" version = "0.3.0" @@ -2360,9 +2389,8 @@ dependencies = [ [[package]] name = "grin_api" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "371de4435e3681a028a0741e4c4c9fa0be7ec95b86a89fef82e534c7de6e79ca" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "bytes 0.5.6", "easy-jsonrpc-mw", @@ -2393,9 +2421,8 @@ dependencies = [ [[package]] name = "grin_chain" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1987d095cb41dffb7f67f4ae97acd0141fc77df28e1af376f8b4ab784d5ab54" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "bit-vec", "bitflags 1.3.2", @@ -2417,9 +2444,8 @@ dependencies = [ [[package]] name = "grin_core" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03d3887c1090b18862424ed4ad1929f6c9b81709cfcca8bf8b88c928d4af2167" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "blake2-rfc", "byteorder", @@ -2444,9 +2470,8 @@ dependencies = [ [[package]] name = "grin_keychain" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2864c09ee9ac184a2a580a6f34debd642011a34fd40af6ab26c084b1aea951ac" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "blake2-rfc", "byteorder", @@ -2465,47 +2490,10 @@ dependencies = [ "zeroize", ] -[[package]] -name = "grin_onion" -version = "0.1.0" -dependencies = [ - "blake2-rfc", - "byteorder", - "bytes 0.5.6", - "chacha20 0.8.2", - "curve25519-dalek 2.1.3", - "ed25519-dalek 1.0.1", - "grin_api", - "grin_chain", - "grin_core", - "grin_keychain", - "grin_secp256k1zkp", - "grin_servers", - "grin_store", - "grin_util", - "grin_wallet_api", - "grin_wallet_impls", - "grin_wallet_libwallet", - "grin_wallet_util", - "hmac 0.12.1", - "itertools 0.10.5", - "lazy_static", - "rand 0.7.3", - "rpassword", - "serde", - "serde_derive", - "serde_json", - "sha2 0.10.8", - "thiserror", - "toml 0.5.11", - "x25519-dalek 0.6.0", -] - [[package]] name = "grin_p2p" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8968c6b250cc928eb8467c0ed283d53676ae5896f8396d6136b0d2dc5fa77f" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "bitflags 1.3.2", "bytes 0.5.6", @@ -2526,9 +2514,8 @@ dependencies = [ [[package]] name = "grin_pool" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1828edf8e05010fbd3a19ff6e73740a696fbb681859617805c25eb66480ce0d8" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "blake2-rfc", "chrono", @@ -2559,9 +2546,8 @@ dependencies = [ [[package]] name = "grin_servers" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dde13741fc12858b6eb10936bcbe4b7c3b9f79fa7a75a7e083445b76e0bb6547" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "chrono", "fs2", @@ -2590,9 +2576,8 @@ dependencies = [ [[package]] name = "grin_store" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51a81f65738f02e2af799e3b596996a7be3087df8ae478241b41a62c00623840" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "byteorder", "croaring", @@ -2610,9 +2595,8 @@ dependencies = [ [[package]] name = "grin_util" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e644f4a7e648c7038226417de9e2101743ce337ad60539a1ec6580571a04ad3" +version = "5.4.0-alpha.0" +source = "git+https://github.com/mimblewimble/grin?branch=master#845c41de13e9bdeb0f0b4667fbc7ef8be921a2f4" dependencies = [ "backtrace", "base64 0.12.3", @@ -2632,9 +2616,8 @@ dependencies = [ [[package]] name = "grin_wallet_api" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d99b560f3c1257833b5abea1db06ac4736a2501421074768b3dcb775a1a8508e" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "base64 0.12.3", "chrono", @@ -2658,9 +2641,8 @@ dependencies = [ [[package]] name = "grin_wallet_config" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bfa93a3be58950ac13dd6d4aa51737e1bbc0befb9f44dfde12e233eb169071b0" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "dirs 2.0.2", "grin_core", @@ -2674,9 +2656,8 @@ dependencies = [ [[package]] name = "grin_wallet_controller" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "321061a9ba48b0241d07dd4c2dcf69acd0923d7f7b040265d1d8a224fb9c0ac9" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "chrono", "easy-jsonrpc-mw", @@ -2709,9 +2690,8 @@ dependencies = [ [[package]] name = "grin_wallet_impls" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5630eca4cf5dca99d89753571a112fd13f2b29d8c05faabbaeebec065deb2bf2" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "base64 0.12.3", "blake2-rfc", @@ -2749,9 +2729,8 @@ dependencies = [ [[package]] name = "grin_wallet_libwallet" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d27b3a75037f69e51bd1b52bb6a4a13d8dd8a764d2477ab944c9e67410d913df" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "age", "base64 0.9.3", @@ -2759,15 +2738,18 @@ dependencies = [ "blake2-rfc", "bs58", "byteorder", + "chacha20 0.8.2", "chrono", "curve25519-dalek 2.1.3", "ed25519-dalek 1.0.1", "grin_core", "grin_keychain", + "grin_secp256k1zkp", "grin_store", "grin_util", "grin_wallet_config", "grin_wallet_util", + "hmac 0.12.1", "lazy_static", "log", "num-bigint 0.2.6", @@ -2777,7 +2759,8 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "sha2 0.8.2", + "serde_with 1.14.0", + "sha2 0.10.8", "strum 0.18.0", "strum_macros 0.18.0", "thiserror", @@ -2787,9 +2770,8 @@ dependencies = [ [[package]] name = "grin_wallet_util" -version = "5.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3ffdc9f591de5ba9194e2add286a4c37d04ad5c3f502dada10de8c282b2990c" +version = "5.4.0-contracts.0" +source = "git+https://github.com/mimblewimble/grin-wallet?branch=contracts#96b5d240bbc82df25e308ac5f90ec3b2b7b9fd94" dependencies = [ "data-encoding", "ed25519-dalek 1.0.1", @@ -3933,7 +3915,6 @@ dependencies = [ "grin_chain", "grin_core", "grin_keychain", - "grin_onion", "grin_p2p", "grin_secp256k1zkp", "grin_servers", @@ -5750,6 +5731,17 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_with" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678b5a069e50bf00ecd22d0cd8ddf7c236f68581b03db652061ed5eb13a312ff" +dependencies = [ + "chrono", + "serde", + "serde_with_macros 1.5.2", +] + [[package]] name = "serde_with" version = "3.7.0" @@ -5764,10 +5756,22 @@ dependencies = [ "serde", "serde_derive", "serde_json", - "serde_with_macros", + "serde_with_macros 3.7.0", "time", ] +[[package]] +name = "serde_with_macros" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e182d6ec6f05393cc0e5ed1bf81ad6db3a8feedf8ee515ecdd369809bcce8082" +dependencies = [ + "darling 0.13.4", + "proc-macro2 1.0.70", + "quote 1.0.33", + "syn 1.0.109", +] + [[package]] name = "serde_with_macros" version = "3.7.0" @@ -5803,18 +5807,6 @@ dependencies = [ "digest 0.10.7", ] -[[package]] -name = "sha2" -version = "0.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" -dependencies = [ - "block-buffer 0.7.3", - "digest 0.8.1", - "fake-simd", - "opaque-debug 0.2.3", -] - [[package]] name = "sha2" version = "0.9.9" @@ -7092,7 +7084,7 @@ dependencies = [ "rangemap", "safelog", "serde", - "serde_with", + "serde_with 3.7.0", "thiserror", "tor-async-utils", "tor-cell", @@ -7133,7 +7125,7 @@ dependencies = [ "retry-error", "safelog", "serde", - "serde_with", + "serde_with 3.7.0", "strum 0.26.2", "thiserror", "tor-async-utils", @@ -7208,7 +7200,7 @@ dependencies = [ "itertools 0.12.0", "safelog", "serde", - "serde_with", + "serde_with 3.7.0", "strum 0.26.2", "thiserror", "tor-basic-utils", @@ -7318,7 +7310,7 @@ dependencies = [ "phf", "rand 0.8.5", "serde", - "serde_with", + "serde_with 3.7.0", "signature 2.2.0", "smallvec", "subtle", diff --git a/Cargo.toml b/Cargo.toml index 1aee15a..9a61375 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,9 +5,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[workspace] -members = ["onion"] - [dependencies] arti-client = { version = "0.18.0", default-features = false, features = ["async-std", "rustls", "onion-service-client", "onion-service-service"] } arti-hyper = "0.18.0" @@ -56,20 +53,21 @@ tor-llcrypto = "0.18.0" tor-keymgr = "0.18.0" tor-rtcompat = "0.18.0" x25519-dalek = "0.6.0" -grin_onion = { path = "./onion" } +log = "0.4.20" + +# Bleeding Edge Grin Deps grin_secp256k1zkp = { version = "0.7.14", features = ["bullet-proof-sizing"] } -grin_api = "5.3.1" -grin_core = "5.3.1" -grin_chain = "5.3.1" -grin_keychain = "5.3.1" -grin_p2p = "5.3.1" -grin_servers = "5.3.1" -grin_store = "5.3.1" -grin_util = "5.3.1" -grin_wallet_api = "5.3.1" -grin_wallet_config = "5.3.1" -grin_wallet_controller = "5.3.1" -grin_wallet_impls = "5.3.1" -grin_wallet_libwallet = "5.3.1" -grin_wallet_util = "5.3.1" -log = "0.4.20" \ No newline at end of file +grin_api = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_core = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_chain = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_keychain = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_p2p = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_servers = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_store = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_util = { git = "https://github.com/mimblewimble/grin", branch = "master" } +grin_wallet_api = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } +grin_wallet_config = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } +grin_wallet_controller = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } +grin_wallet_impls = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } +grin_wallet_libwallet = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } +grin_wallet_util = { git = "https://github.com/mimblewimble/grin-wallet", branch = "contracts" } diff --git a/onion/Cargo.toml b/onion/Cargo.toml deleted file mode 100644 index a7eab60..0000000 --- a/onion/Cargo.toml +++ /dev/null @@ -1,38 +0,0 @@ -[package] -name = "grin_onion" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -blake2 = { package = "blake2-rfc", version = "0.2" } -byteorder = "1" -bytes = "0.5.6" -chacha20 = "0.8.1" -curve25519-dalek = "2.1" -ed25519-dalek = "1.0.1" -hmac = { version = "0.12.0", features = ["std"] } -itertools = { version = "0.10.3" } -lazy_static = "1" -rand = "0.7.3" -rpassword = "4.0" -serde = { version = "1", features = ["derive"] } -serde_derive = "1" -serde_json = "1" -sha2 = "0.10.0" -thiserror = "1" -toml = "0.5" -x25519-dalek = "0.6.0" -grin_secp256k1zkp = { version = "0.7.14", features = ["bullet-proof-sizing"] } -grin_api = "5.3.1" -grin_core = "5.3.1" -grin_chain = "5.3.1" -grin_keychain = "5.3.1" -grin_servers = "5.3.1" -grin_store = "5.3.1" -grin_util = "5.3.1" -grin_wallet_api = "5.3.1" -grin_wallet_impls = "5.3.1" -grin_wallet_libwallet = "5.3.1" -grin_wallet_util = "5.3.1" \ No newline at end of file diff --git a/onion/src/crypto/comsig.rs b/onion/src/crypto/comsig.rs deleted file mode 100644 index dd8a0c5..0000000 --- a/onion/src/crypto/comsig.rs +++ /dev/null @@ -1,189 +0,0 @@ -use crate::crypto::secp::{self, Commitment, ContextFlag, Secp256k1, SecretKey}; - -use blake2::blake2b::Blake2b; -use byteorder::{BigEndian, ByteOrder}; -use grin_core::ser::{self, Readable, Reader, Writeable, Writer}; -use secp256k1zkp::rand::thread_rng; -use thiserror::Error; - -/// A generalized Schnorr signature with a pedersen commitment value & blinding factors as the keys -#[derive(Clone, Debug)] -pub struct ComSignature { - pub_nonce: Commitment, - s: SecretKey, - t: SecretKey, -} - -/// Error types for Commitment Signatures -#[derive(Error, Debug)] -pub enum ComSigError { - #[error("Commitment signature is invalid")] - InvalidSig, - #[error("Secp256k1zkp error: {0:?}")] - Secp256k1zkp(secp256k1zkp::Error), -} - -impl From for ComSigError { - fn from(err: secp256k1zkp::Error) -> ComSigError { - ComSigError::Secp256k1zkp(err) - } -} - -impl ComSignature { - pub fn new(pub_nonce: &Commitment, s: &SecretKey, t: &SecretKey) -> ComSignature { - ComSignature { - pub_nonce: pub_nonce.to_owned(), - s: s.to_owned(), - t: t.to_owned(), - } - } - - #[allow(dead_code)] - pub fn sign( - amount: u64, - blind: &SecretKey, - msg: &Vec, - ) -> Result { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - - let mut amt_bytes = [0; 32]; - BigEndian::write_u64(&mut amt_bytes[24..32], amount); - let k_amt = SecretKey::from_slice(&secp, &amt_bytes)?; - - let k_1 = SecretKey::new(&secp, &mut thread_rng()); - let k_2 = SecretKey::new(&secp, &mut thread_rng()); - - let commitment = secp.commit(amount, blind.clone())?; - let nonce_commitment = secp.commit_blind(k_1.clone(), k_2.clone())?; - - let e = ComSignature::calc_challenge(&secp, &commitment, &nonce_commitment, &msg)?; - - // s = k_1 + (e * amount) - let mut s = k_amt.clone(); - s.mul_assign(&secp, &e)?; - s.add_assign(&secp, &k_1)?; - - // t = k_2 + (e * blind) - let mut t = blind.clone(); - t.mul_assign(&secp, &e)?; - t.add_assign(&secp, &k_2)?; - - Ok(ComSignature::new(&nonce_commitment, &s, &t)) - } - - #[allow(non_snake_case)] - pub fn verify(&self, commit: &Commitment, msg: &Vec) -> Result<(), ComSigError> { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - - let S1 = secp.commit_blind(self.s.clone(), self.t.clone())?; - - let mut Ce = commit.to_pubkey(&secp)?; - let e = ComSignature::calc_challenge(&secp, &commit, &self.pub_nonce, &msg)?; - Ce.mul_assign(&secp, &e)?; - - let commits = vec![Commitment::from_pubkey(&secp, &Ce)?, self.pub_nonce.clone()]; - let S2 = secp.commit_sum(commits, Vec::new())?; - - if S1 != S2 { - return Err(ComSigError::InvalidSig); - } - - Ok(()) - } - - fn calc_challenge( - secp: &Secp256k1, - commit: &Commitment, - nonce_commit: &Commitment, - msg: &Vec, - ) -> Result { - let mut challenge_hasher = Blake2b::new(32); - challenge_hasher.update(&commit.0); - challenge_hasher.update(&nonce_commit.0); - challenge_hasher.update(msg); - - let mut challenge = [0; 32]; - challenge.copy_from_slice(challenge_hasher.finalize().as_bytes()); - - Ok(SecretKey::from_slice(&secp, &challenge)?) - } -} - -/// Serializes a ComSignature to and from hex -pub mod comsig_serde { - use super::ComSignature; - use grin_core::ser::{self, ProtocolVersion}; - use grin_util::ToHex; - use serde::{Deserialize, Serializer}; - - /// Serializes a ComSignature as a hex string - pub fn serialize(comsig: &ComSignature, serializer: S) -> Result - where - S: Serializer, - { - use serde::ser::Error; - let bytes = ser::ser_vec(&comsig, ProtocolVersion::local()).map_err(Error::custom)?; - serializer.serialize_str(&bytes.to_hex()) - } - - /// Creates a ComSignature from a hex string - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - use serde::de::Error; - let bytes = String::deserialize(deserializer) - .and_then(|string| grin_util::from_hex(&string).map_err(Error::custom))?; - let sig: ComSignature = ser::deserialize_default(&mut &bytes[..]).map_err(Error::custom)?; - Ok(sig) - } -} - -#[allow(non_snake_case)] -impl Readable for ComSignature { - fn read(reader: &mut R) -> Result { - let R = Commitment::read(reader)?; - let s = secp::read_secret_key(reader)?; - let t = secp::read_secret_key(reader)?; - Ok(ComSignature::new(&R, &s, &t)) - } -} - -impl Writeable for ComSignature { - fn write(&self, writer: &mut W) -> Result<(), ser::Error> { - writer.write_fixed_bytes(self.pub_nonce.0)?; - writer.write_fixed_bytes(self.s.0)?; - writer.write_fixed_bytes(self.t.0)?; - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use super::{ComSigError, ComSignature, ContextFlag, Secp256k1, SecretKey}; - - use rand::Rng; - use secp256k1zkp::rand::{thread_rng, RngCore}; - - /// Test signing and verification of ComSignatures - #[test] - fn verify_comsig() -> Result<(), ComSigError> { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - - let amount = thread_rng().next_u64(); - let blind = SecretKey::new(&secp, &mut thread_rng()); - let msg: [u8; 16] = rand::thread_rng().gen(); - let comsig = ComSignature::sign(amount, &blind, &msg.to_vec())?; - - let commit = secp.commit(amount, blind.clone())?; - assert!(comsig.verify(&commit, &msg.to_vec()).is_ok()); - - let wrong_msg: [u8; 16] = rand::thread_rng().gen(); - assert!(comsig.verify(&commit, &wrong_msg.to_vec()).is_err()); - - let wrong_commit = secp.commit(amount, SecretKey::new(&secp, &mut thread_rng()))?; - assert!(comsig.verify(&wrong_commit, &msg.to_vec()).is_err()); - - Ok(()) - } -} diff --git a/onion/src/crypto/dalek.rs b/onion/src/crypto/dalek.rs deleted file mode 100644 index ab107bc..0000000 --- a/onion/src/crypto/dalek.rs +++ /dev/null @@ -1,272 +0,0 @@ -use crate::crypto::secp::SecretKey; - -use ed25519_dalek::{Keypair, PublicKey, Signature, Signer, Verifier}; -use grin_core::ser::{self, Readable, Reader, Writeable, Writer}; -use grin_util::ToHex; -use thiserror::Error; - -/// Error types for Dalek structures and logic -#[derive(Clone, Error, Debug, PartialEq)] -pub enum DalekError { - #[error("Hex error {0:?}")] - HexError(String), - #[error("Failed to parse secret key")] - KeyParseError, - #[error("Failed to verify signature")] - SigVerifyFailed, -} - -/// Encapsulates an ed25519_dalek::PublicKey and provides (de-)serialization -#[derive(Clone, Debug, PartialEq)] -pub struct DalekPublicKey(PublicKey); - -impl DalekPublicKey { - /// Convert DalekPublicKey to hex string - pub fn to_hex(&self) -> String { - self.0.to_hex() - } - - /// Convert hex string to DalekPublicKey. - pub fn from_hex(hex: &str) -> Result { - let bytes = grin_util::from_hex(hex) - .map_err(|_| DalekError::HexError(format!("failed to decode {}", hex)))?; - let pk = PublicKey::from_bytes(bytes.as_ref()) - .map_err(|_| DalekError::HexError(format!("failed to decode {}", hex)))?; - Ok(DalekPublicKey(pk)) - } - - /// Compute DalekPublicKey from a SecretKey - pub fn from_secret(key: &SecretKey) -> Self { - let secret = ed25519_dalek::SecretKey::from_bytes(&key.0).unwrap(); - let pk: PublicKey = (&secret).into(); - DalekPublicKey(pk) - } -} - -impl AsRef for DalekPublicKey { - fn as_ref(&self) -> &PublicKey { - &self.0 - } -} - -/// Serializes an Option to and from hex -pub mod option_dalek_pubkey_serde { - use super::DalekPublicKey; - use grin_util::ToHex; - use serde::de::Error; - use serde::{Deserialize, Deserializer, Serializer}; - - /// - pub fn serialize(pk: &Option, serializer: S) -> Result - where - S: Serializer, - { - match pk { - Some(pk) => serializer.serialize_str(&pk.0.to_hex()), - None => serializer.serialize_none(), - } - } - - /// - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - Option::::deserialize(deserializer).and_then(|res| match res { - Some(string) => DalekPublicKey::from_hex(&string) - .map_err(|e| Error::custom(e.to_string())) - .and_then(|pk: DalekPublicKey| Ok(Some(pk))), - None => Ok(None), - }) - } -} - -impl Readable for DalekPublicKey { - fn read(reader: &mut R) -> Result { - let pk = PublicKey::from_bytes(&reader.read_fixed_bytes(32)?) - .map_err(|_| ser::Error::CorruptedData)?; - Ok(DalekPublicKey(pk)) - } -} - -impl Writeable for DalekPublicKey { - fn write(&self, writer: &mut W) -> Result<(), ser::Error> { - writer.write_fixed_bytes(self.0.to_bytes())?; - Ok(()) - } -} - -/// Encapsulates an ed25519_dalek::Signature and provides (de-)serialization -#[derive(Clone, Debug, PartialEq)] -pub struct DalekSignature(Signature); - -impl DalekSignature { - /// Convert hex string to DalekSignature. - pub fn from_hex(hex: &str) -> Result { - let bytes = grin_util::from_hex(hex) - .map_err(|_| DalekError::HexError(format!("failed to decode {}", hex)))?; - let sig = Signature::from_bytes(bytes.as_ref()) - .map_err(|_| DalekError::HexError(format!("failed to decode {}", hex)))?; - Ok(DalekSignature(sig)) - } - - /// Verifies DalekSignature - pub fn verify(&self, pk: &DalekPublicKey, msg: &[u8]) -> Result<(), DalekError> { - pk.as_ref() - .verify(&msg, &self.0) - .map_err(|_| DalekError::SigVerifyFailed) - } -} - -impl AsRef for DalekSignature { - fn as_ref(&self) -> &Signature { - &self.0 - } -} - -/// Serializes a DalekSignature to and from hex -pub mod dalek_sig_serde { - use super::DalekSignature; - use grin_util::ToHex; - use serde::de::Error; - use serde::{Deserialize, Deserializer, Serializer}; - - /// - pub fn serialize(sig: &DalekSignature, serializer: S) -> Result - where - S: Serializer, - { - serializer.serialize_str(&sig.0.to_hex()) - } - - /// - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let str = String::deserialize(deserializer)?; - let sig = DalekSignature::from_hex(&str).map_err(|e| Error::custom(e.to_string()))?; - Ok(sig) - } -} - -pub fn sign(sk: &SecretKey, message: &[u8]) -> Result { - let secret = - ed25519_dalek::SecretKey::from_bytes(&sk.0).map_err(|_| DalekError::KeyParseError)?; - let public: PublicKey = (&secret).into(); - let keypair = Keypair { secret, public }; - let sig = keypair.sign(&message); - Ok(DalekSignature(sig)) -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::test_util::rand_keypair; - use grin_core::ser::{self, ProtocolVersion}; - use grin_util::ToHex; - use rand::Rng; - use serde::{Deserialize, Serialize}; - use serde_json::Value; - - #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] - struct TestPubKeySerde { - #[serde(with = "option_dalek_pubkey_serde", default)] - pk: Option, - } - - #[test] - fn pubkey_test() -> Result<(), Box> { - // Test from_hex - let rand_pk = rand_keypair().1; - let pk_from_hex = DalekPublicKey::from_hex(rand_pk.0.to_hex().as_str()).unwrap(); - assert_eq!(rand_pk.0, pk_from_hex.0); - - // Test ser (de-)serialization - let bytes = ser::ser_vec(&rand_pk, ProtocolVersion::local()).unwrap(); - assert_eq!(bytes.len(), 32); - let pk_from_deser: DalekPublicKey = ser::deserialize_default(&mut &bytes[..]).unwrap(); - assert_eq!(rand_pk.0, pk_from_deser.0); - - // Test serde with Some(rand_pk) - let some = TestPubKeySerde { - pk: Some(rand_pk.clone()), - }; - let val = serde_json::to_value(some.clone()).unwrap(); - if let Value::Object(o) = &val { - if let Value::String(s) = o.get("pk").unwrap() { - assert_eq!(s, &rand_pk.0.to_hex()); - } else { - panic!("Invalid type"); - } - } else { - panic!("Invalid type") - } - assert_eq!(some, serde_json::from_value(val).unwrap()); - - // Test serde with empty pk field - let none = TestPubKeySerde { pk: None }; - let val = serde_json::to_value(none.clone()).unwrap(); - if let Value::Object(o) = &val { - if let Value::Null = o.get("pk").unwrap() { - // ok - } else { - panic!("Invalid type"); - } - } else { - panic!("Invalid type") - } - assert_eq!(none, serde_json::from_value(val).unwrap()); - - // Test serde with no pk field - let none2 = serde_json::from_str::("{}").unwrap(); - assert_eq!(none, none2); - - Ok(()) - } - - #[derive(Serialize, Deserialize, PartialEq, Clone, Debug)] - struct TestSigSerde { - #[serde(with = "dalek_sig_serde")] - sig: DalekSignature, - } - - #[test] - fn sig_test() -> Result<(), Box> { - // Sign a message - let (sk, pk) = rand_keypair(); - let msg: [u8; 16] = rand::thread_rng().gen(); - let sig = sign(&sk, &msg).unwrap(); - - // Verify signature - assert!(sig.verify(&pk, &msg).is_ok()); - - // Wrong message - let wrong_msg: [u8; 16] = rand::thread_rng().gen(); - assert!(sig.verify(&pk, &wrong_msg).is_err()); - - // Wrong pubkey - let wrong_pk = rand_keypair().1; - assert!(sig.verify(&wrong_pk, &msg).is_err()); - - // Test from_hex - let sig_from_hex = DalekSignature::from_hex(sig.0.to_hex().as_str()).unwrap(); - assert_eq!(sig.0, sig_from_hex.0); - - // Test serde (de-)serialization - let serde_test = TestSigSerde { sig: sig.clone() }; - let val = serde_json::to_value(serde_test.clone()).unwrap(); - if let Value::Object(o) = &val { - if let Value::String(s) = o.get("sig").unwrap() { - assert_eq!(s, &sig.0.to_hex()); - } else { - panic!("Invalid type"); - } - } else { - panic!("Invalid type") - } - assert_eq!(serde_test, serde_json::from_value(val).unwrap()); - - Ok(()) - } -} diff --git a/onion/src/crypto/mod.rs b/onion/src/crypto/mod.rs deleted file mode 100644 index 2fdcdb2..0000000 --- a/onion/src/crypto/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod comsig; -pub mod dalek; -pub mod secp; diff --git a/onion/src/crypto/secp.rs b/onion/src/crypto/secp.rs deleted file mode 100644 index 89923b9..0000000 --- a/onion/src/crypto/secp.rs +++ /dev/null @@ -1,62 +0,0 @@ -pub use secp256k1zkp::aggsig; -pub use secp256k1zkp::constants::{ - AGG_SIGNATURE_SIZE, COMPRESSED_PUBLIC_KEY_SIZE, MAX_PROOF_SIZE, PEDERSEN_COMMITMENT_SIZE, - SECRET_KEY_SIZE, -}; -pub use secp256k1zkp::ecdh::SharedSecret; -pub use secp256k1zkp::key::{PublicKey, SecretKey, ZERO_KEY}; -pub use secp256k1zkp::pedersen::{Commitment, RangeProof}; -pub use secp256k1zkp::{ContextFlag, Message, Secp256k1, Signature}; - -use grin_core::ser::{self, Reader}; -use secp256k1zkp::rand::thread_rng; - -/// Generate a random SecretKey. -pub fn random_secret() -> SecretKey { - let secp = Secp256k1::new(); - SecretKey::new(&secp, &mut thread_rng()) -} - -/// Deserialize a SecretKey from a Reader -pub fn read_secret_key(reader: &mut R) -> Result { - let buf = reader.read_fixed_bytes(SECRET_KEY_SIZE)?; - let secp = Secp256k1::with_caps(ContextFlag::None); - let pk = SecretKey::from_slice(&secp, &buf).map_err(|_| ser::Error::CorruptedData)?; - Ok(pk) -} - -/// Build a Pedersen Commitment using the provided value and blinding factor -pub fn commit(value: u64, blind: &SecretKey) -> Result { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - let commit = secp.commit(value, blind.clone())?; - Ok(commit) -} - -/// Add a blinding factor to an existing Commitment -pub fn add_excess( - commitment: &Commitment, - excess: &SecretKey, -) -> Result { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - let excess_commit: Commitment = secp.commit(0, excess.clone())?; - - let commits = vec![commitment.clone(), excess_commit.clone()]; - let sum = secp.commit_sum(commits, Vec::new())?; - Ok(sum) -} - -/// Subtracts a value (v*H) from an existing commitment -pub fn sub_value(commitment: &Commitment, value: u64) -> Result { - let secp = Secp256k1::with_caps(ContextFlag::Commit); - let neg_commit: Commitment = secp.commit(value, ZERO_KEY)?; - let sum = secp.commit_sum(vec![commitment.clone()], vec![neg_commit.clone()])?; - Ok(sum) -} - -/// Signs the message with the provided SecretKey -pub fn sign(sk: &SecretKey, msg: &Message) -> Result { - let secp = Secp256k1::with_caps(ContextFlag::Full); - let pubkey = PublicKey::from_secret_key(&secp, &sk)?; - let sig = aggsig::sign_single(&secp, &msg, &sk, None, None, None, Some(&pubkey), None)?; - Ok(sig) -} diff --git a/onion/src/lib.rs b/onion/src/lib.rs deleted file mode 100644 index 5c89a5d..0000000 --- a/onion/src/lib.rs +++ /dev/null @@ -1,171 +0,0 @@ -use chacha20::cipher::StreamCipher; -use grin_core::core::FeeFields; -use secp256k1zkp::pedersen::RangeProof; -use x25519_dalek::{SharedSecret, StaticSecret}; -use x25519_dalek::PublicKey as xPublicKey; - -use crate::crypto::secp::{Commitment, random_secret, SecretKey}; -use crate::onion::{new_stream_cipher, Onion, OnionError, Payload, RawBytes}; - -pub mod crypto; -pub mod onion; -pub mod util; - -#[derive(Clone)] -pub struct Hop { - pub server_pubkey: xPublicKey, - pub excess: SecretKey, - pub fee: FeeFields, - pub rangeproof: Option, -} - -pub fn new_hop( - server_key: &SecretKey, - hop_excess: &SecretKey, - fee: u32, - proof: Option, -) -> Hop { - Hop { - server_pubkey: xPublicKey::from(&StaticSecret::from(server_key.0.clone())), - excess: hop_excess.clone(), - fee: FeeFields::from(fee), - rangeproof: proof, - } -} - -/// Create an Onion for the Commitment, encrypting the payload for each hop -pub fn create_onion(commitment: &Commitment, hops: &Vec) -> Result { - if hops.is_empty() { - return Ok(Onion { - ephemeral_pubkey: xPublicKey::from([0u8; 32]), - commit: commitment.clone(), - enc_payloads: vec![], - }); - } - - let mut shared_secrets: Vec = Vec::new(); - let mut enc_payloads: Vec = Vec::new(); - let mut ephemeral_sk = StaticSecret::from(random_secret().0); - let onion_ephemeral_pk = xPublicKey::from(&ephemeral_sk); - for i in 0..hops.len() { - let hop = &hops[i]; - let shared_secret = ephemeral_sk.diffie_hellman(&hop.server_pubkey); - shared_secrets.push(shared_secret); - - ephemeral_sk = StaticSecret::from(random_secret().0); - let next_ephemeral_pk = if i < (hops.len() - 1) { - xPublicKey::from(&ephemeral_sk) - } else { - xPublicKey::from([0u8; 32]) - }; - - let payload = Payload { - next_ephemeral_pk, - excess: hop.excess.clone(), - fee: hop.fee.clone(), - rangeproof: hop.rangeproof.clone(), - }; - enc_payloads.push(payload.serialize()?); - } - - for i in (0..shared_secrets.len()).rev() { - let mut cipher = new_stream_cipher(&shared_secrets[i])?; - for j in i..shared_secrets.len() { - cipher.apply_keystream(enc_payloads[j].as_mut_slice()); - } - } - - let onion = Onion { - ephemeral_pubkey: onion_ephemeral_pk, - commit: commitment.clone(), - enc_payloads, - }; - Ok(onion) -} - -pub mod test_util { - use grin_core::core::hash::Hash; - use grin_util::ToHex; - use rand::{RngCore, thread_rng}; - use secp256k1zkp::Secp256k1; - - use crate::crypto::dalek::DalekPublicKey; - use crate::crypto::secp; - - use super::*; - - pub fn rand_onion() -> Onion { - let commit = rand_commit(); - let mut hops = Vec::new(); - let k = (thread_rng().next_u64() % 5) + 1; - for i in 0..k { - let rangeproof = if i == (k - 1) { - Some(rand_proof()) - } else { - None - }; - let hop = new_hop( - &random_secret(), - &random_secret(), - thread_rng().next_u32(), - rangeproof, - ); - hops.push(hop); - } - - create_onion(&commit, &hops).unwrap() - } - - pub fn rand_commit() -> Commitment { - secp::commit(thread_rng().next_u64(), &random_secret()).unwrap() - } - - pub fn rand_hash() -> Hash { - Hash::from_hex(random_secret().to_hex().as_str()).unwrap() - } - - pub fn rand_proof() -> RangeProof { - let secp = Secp256k1::new(); - secp.bullet_proof( - thread_rng().next_u64(), - random_secret(), - random_secret(), - random_secret(), - None, - None, - ) - } - - pub fn proof( - value: u64, - fee: u32, - input_blind: &SecretKey, - hop_excesses: &Vec<&SecretKey>, - ) -> (Commitment, RangeProof) { - let secp = Secp256k1::new(); - - let mut blind = input_blind.clone(); - for hop_excess in hop_excesses { - blind.add_assign(&secp, &hop_excess).unwrap(); - } - - let out_value = value - (fee as u64); - - let rp = secp.bullet_proof( - out_value, - blind.clone(), - random_secret(), - random_secret(), - None, - None, - ); - - (secp::commit(out_value, &blind).unwrap(), rp) - } - - pub fn rand_keypair() -> (SecretKey, DalekPublicKey) { - let sk = random_secret(); - let pk = DalekPublicKey::from_secret(&sk); - (sk, pk) - } -} diff --git a/onion/src/onion.rs b/onion/src/onion.rs deleted file mode 100644 index 2533aaf..0000000 --- a/onion/src/onion.rs +++ /dev/null @@ -1,400 +0,0 @@ -use std::fmt; -use std::hash::{Hash, Hasher}; -use std::result::Result; - -use chacha20::{ChaCha20, Key, Nonce}; -use chacha20::cipher::{NewCipher, StreamCipher}; -use grin_core::core::FeeFields; -use grin_core::ser::{self, Readable, Reader, Writeable, Writer}; -use grin_util::{self, ToHex}; -use hmac::{Hmac, Mac}; -use hmac::digest::InvalidLength; -use serde::Deserialize; -use serde::ser::SerializeStruct; -use sha2::Sha256; -use thiserror::Error; -use x25519_dalek::{PublicKey as xPublicKey, SharedSecret, StaticSecret}; - -use crate::crypto::secp::{self, Commitment, RangeProof, SecretKey}; -use crate::util::{read_optional, vec_to_array, write_optional}; - -type HmacSha256 = Hmac; -pub type RawBytes = Vec; - -const CURRENT_ONION_VERSION: u8 = 0; - -/// A data packet with layers of encryption -#[derive(Clone, Debug)] -pub struct Onion { - /// The onion originator's portion of the shared secret - pub ephemeral_pubkey: xPublicKey, - /// The pedersen commitment before adjusting the excess and subtracting the fee - pub commit: Commitment, - /// The encrypted payloads which represent the layers of the onion - pub enc_payloads: Vec, -} - -impl PartialEq for Onion { - fn eq(&self, other: &Onion) -> bool { - *self.ephemeral_pubkey.as_bytes() == *other.ephemeral_pubkey.as_bytes() - && self.commit == other.commit - && self.enc_payloads == other.enc_payloads - } -} - -impl Eq for Onion {} - -impl Hash for Onion { - fn hash(&self, state: &mut H) { - state.write(self.ephemeral_pubkey.as_bytes()); - state.write(self.commit.as_ref()); - state.write_usize(self.enc_payloads.len()); - for p in &self.enc_payloads { - state.write(p.as_slice()); - } - } -} - -/// A single, decrypted/peeled layer of an Onion. -#[derive(Debug, Clone)] -pub struct Payload { - pub next_ephemeral_pk: xPublicKey, - pub excess: SecretKey, - pub fee: FeeFields, - pub rangeproof: Option, -} - -impl Payload { - pub fn deserialize(bytes: &Vec) -> Result { - let payload: Payload = ser::deserialize_default(&mut &bytes[..])?; - Ok(payload) - } - - pub fn serialize(&self) -> Result, ser::Error> { - let mut vec = vec![]; - ser::serialize_default(&mut vec, &self)?; - Ok(vec) - } -} - -impl Readable for Payload { - fn read(reader: &mut R) -> Result { - let version = reader.read_u8()?; - if version != CURRENT_ONION_VERSION { - return Err(ser::Error::UnsupportedProtocolVersion); - } - - let next_ephemeral_pk = - xPublicKey::from(vec_to_array::<32>(&reader.read_fixed_bytes(32)?)?); - let excess = secp::read_secret_key(reader)?; - let fee = FeeFields::try_from(reader.read_u64()?).map_err(|_| ser::Error::CorruptedData)?; - let rangeproof = read_optional(reader)?; - Ok(Payload { - next_ephemeral_pk, - excess, - fee, - rangeproof, - }) - } -} - -impl Writeable for Payload { - fn write(&self, writer: &mut W) -> Result<(), ser::Error> { - writer.write_u8(CURRENT_ONION_VERSION)?; - writer.write_fixed_bytes(&self.next_ephemeral_pk.as_bytes())?; - writer.write_fixed_bytes(&self.excess)?; - writer.write_u64(self.fee.into())?; - write_optional(writer, &self.rangeproof)?; - Ok(()) - } -} - -/// An onion with a layer decrypted -#[derive(Clone, Debug)] -pub struct PeeledOnion { - /// The payload from the peeled layer - pub payload: Payload, - /// The onion remaining after a layer was peeled - pub onion: Onion, -} - -impl Onion { - pub fn serialize(&self) -> Result, ser::Error> { - let mut vec = vec![]; - ser::serialize_default(&mut vec, &self)?; - Ok(vec) - } - - /// Peel a single layer off of the Onion, returning the peeled Onion and decrypted Payload - pub fn peel_layer(&self, server_key: &SecretKey) -> Result { - let shared_secret = StaticSecret::from(server_key.0).diffie_hellman(&self.ephemeral_pubkey); - let mut cipher = new_stream_cipher(&shared_secret)?; - - let mut decrypted_bytes = self.enc_payloads[0].clone(); - cipher.apply_keystream(&mut decrypted_bytes); - let decrypted_payload = Payload::deserialize(&decrypted_bytes) - .map_err(|e| OnionError::DeserializationError(e))?; - - let enc_payloads: Vec = self - .enc_payloads - .iter() - .enumerate() - .filter(|&(i, _)| i != 0) - .map(|(_, enc_payload)| { - let mut p = enc_payload.clone(); - cipher.apply_keystream(&mut p); - p - }) - .collect(); - - let mut commitment = self.commit.clone(); - commitment = secp::add_excess(&commitment, &decrypted_payload.excess) - .map_err(|e| OnionError::CalcCommitError(e))?; - commitment = secp::sub_value(&commitment, decrypted_payload.fee.into()) - .map_err(|e| OnionError::CalcCommitError(e))?; - - let peeled_onion = Onion { - ephemeral_pubkey: decrypted_payload.next_ephemeral_pk, - commit: commitment.clone(), - enc_payloads, - }; - Ok(PeeledOnion { - payload: decrypted_payload, - onion: peeled_onion, - }) - } -} - -pub fn new_stream_cipher(shared_secret: &SharedSecret) -> Result { - let mut mu_hmac = HmacSha256::new_from_slice(b"MWIXNET")?; - mu_hmac.update(shared_secret.as_bytes()); - let mukey = mu_hmac.finalize().into_bytes(); - - let key = Key::from_slice(&mukey[0..32]); - let nonce = Nonce::from_slice(b"NONCE1234567"); - - Ok(ChaCha20::new(&key, &nonce)) -} - -impl Writeable for Onion { - fn write(&self, writer: &mut W) -> Result<(), ser::Error> { - writer.write_fixed_bytes(self.ephemeral_pubkey.as_bytes())?; - writer.write_fixed_bytes(&self.commit)?; - writer.write_u64(self.enc_payloads.len() as u64)?; - for p in &self.enc_payloads { - writer.write_u64(p.len() as u64)?; - p.write(writer)?; - } - Ok(()) - } -} - -impl Readable for Onion { - fn read(reader: &mut R) -> Result { - let pubkey_bytes: [u8; 32] = vec_to_array(&reader.read_fixed_bytes(32)?)?; - let ephemeral_pubkey = xPublicKey::from(pubkey_bytes); - let commit = Commitment::read(reader)?; - let mut enc_payloads: Vec = Vec::new(); - let len = reader.read_u64()?; - for _ in 0..len { - let size = reader.read_u64()?; - let bytes = reader.read_fixed_bytes(size as usize)?; - enc_payloads.push(bytes); - } - Ok(Onion { - ephemeral_pubkey, - commit, - enc_payloads, - }) - } -} - -impl serde::ser::Serialize for Onion { - fn serialize(&self, serializer: S) -> Result - where - S: serde::ser::Serializer, - { - let mut state = serializer.serialize_struct("Onion", 3)?; - - state.serialize_field("pubkey", &self.ephemeral_pubkey.as_bytes().to_hex())?; - state.serialize_field("commit", &self.commit.to_hex())?; - - let hex_payloads: Vec = self.enc_payloads.iter().map(|v| v.to_hex()).collect(); - state.serialize_field("data", &hex_payloads)?; - state.end() - } -} - -impl<'de> serde::de::Deserialize<'de> for Onion { - fn deserialize(deserializer: D) -> Result - where - D: serde::de::Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(field_identifier, rename_all = "snake_case")] - enum Field { - Pubkey, - Commit, - Data, - } - - struct OnionVisitor; - - impl<'de> serde::de::Visitor<'de> for OnionVisitor { - type Value = Onion; - - fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str("an Onion") - } - - fn visit_map(self, mut map: A) -> Result - where - A: serde::de::MapAccess<'de>, - { - let mut pubkey = None; - let mut commit = None; - let mut data = None; - - while let Some(key) = map.next_key()? { - match key { - Field::Pubkey => { - let val: String = map.next_value()?; - let vec = - grin_util::from_hex(&val).map_err(serde::de::Error::custom)?; - pubkey = - Some(xPublicKey::from(vec_to_array::<32>(&vec).map_err( - |_| serde::de::Error::custom("Invalid length pubkey"), - )?)); - } - Field::Commit => { - let val: String = map.next_value()?; - let vec = - grin_util::from_hex(&val).map_err(serde::de::Error::custom)?; - commit = Some(Commitment::from_vec(vec)); - } - Field::Data => { - let val: Vec = map.next_value()?; - let mut vec: Vec> = Vec::new(); - for hex in val { - vec.push( - grin_util::from_hex(&hex).map_err(serde::de::Error::custom)?, - ); - } - data = Some(vec); - } - } - } - - Ok(Onion { - ephemeral_pubkey: pubkey.unwrap(), - commit: commit.unwrap(), - enc_payloads: data.unwrap(), - }) - } - } - - const FIELDS: &[&str] = &["pubkey", "commit", "data"]; - deserializer.deserialize_struct("Onion", &FIELDS, OnionVisitor) - } -} - -/// Error types for creating and peeling Onions -#[derive(Clone, Error, Debug, PartialEq)] -pub enum OnionError { - #[error("Invalid key length for MAC initialization")] - InvalidKeyLength, - #[error("Serialization error occurred: {0:?}")] - SerializationError(ser::Error), - #[error("Deserialization error occurred: {0:?}")] - DeserializationError(ser::Error), - #[error("Error calculating blinding factor: {0:?}")] - CalcBlindError(secp256k1zkp::Error), - #[error("Error calculating ephemeral pubkey: {0:?}")] - CalcPubKeyError(secp256k1zkp::Error), - #[error("Error calculating commitment: {0:?}")] - CalcCommitError(secp256k1zkp::Error), -} - -impl From for OnionError { - fn from(_err: InvalidLength) -> OnionError { - OnionError::InvalidKeyLength - } -} - -impl From for OnionError { - fn from(err: ser::Error) -> OnionError { - OnionError::SerializationError(err) - } -} - -#[cfg(test)] -pub mod tests { - use grin_core::core::FeeFields; - - use crate::{create_onion, Hop, new_hop}; - use crate::crypto::secp::random_secret; - - use super::*; - - /// Test end-to-end Onion creation and unwrapping logic. - #[test] - fn onion() { - let total_fee: u64 = 10; - let fee_per_hop: u32 = 2; - let in_value: u64 = 1000; - let out_value: u64 = in_value - total_fee; - let blind = random_secret(); - let commitment = secp::commit(in_value, &blind).unwrap(); - - let mut hops: Vec = Vec::new(); - let mut keys: Vec = Vec::new(); - let mut final_commit = secp::commit(out_value, &blind).unwrap(); - let mut final_blind = blind.clone(); - for i in 0..5 { - keys.push(random_secret()); - - let excess = random_secret(); - - let secp = secp256k1zkp::Secp256k1::with_caps(secp256k1zkp::ContextFlag::Commit); - final_blind.add_assign(&secp, &excess).unwrap(); - final_commit = secp::add_excess(&final_commit, &excess).unwrap(); - let proof = if i == 4 { - let n1 = random_secret(); - let rp = secp.bullet_proof( - out_value, - final_blind.clone(), - n1.clone(), - n1.clone(), - None, - None, - ); - assert!(secp.verify_bullet_proof(final_commit, rp, None).is_ok()); - Some(rp) - } else { - None - }; - - let hop = new_hop(&keys[i], &excess, fee_per_hop, proof); - hops.push(hop); - } - - let mut onion_packet = create_onion(&commitment, &hops).unwrap(); - - let mut payload = Payload { - next_ephemeral_pk: onion_packet.ephemeral_pubkey.clone(), - excess: random_secret(), - fee: FeeFields::from(fee_per_hop), - rangeproof: None, - }; - for i in 0..5 { - let peeled = onion_packet.peel_layer(&keys[i]).unwrap(); - payload = peeled.payload; - onion_packet = peeled.onion; - } - - assert!(payload.rangeproof.is_some()); - assert_eq!(payload.rangeproof.unwrap(), hops[4].rangeproof.unwrap()); - assert_eq!(secp::commit(out_value, &final_blind).unwrap(), final_commit); - assert_eq!(payload.fee, FeeFields::from(fee_per_hop)); - } -} diff --git a/onion/src/util.rs b/onion/src/util.rs deleted file mode 100644 index 2b2f134..0000000 --- a/onion/src/util.rs +++ /dev/null @@ -1,164 +0,0 @@ -use grin_core::ser::{self, Readable, Reader, Writeable, Writer}; - -/// Writes an optional value as '1' + value if Some, or '0' if None -/// -/// This function is used to serialize an optional value into a Writer. If the option -/// contains Some value, it writes '1' followed by the serialized value. If the option -/// is None, it just writes '0'. -/// -/// # Arguments -/// -/// * `writer` - A Writer instance where the data will be written. -/// * `o` - The Optional value that will be written. -/// -/// # Returns -/// -/// * If successful, returns Ok with nothing. -/// * If an error occurs during writing, returns Err wrapping the error. -/// -/// # Example -/// -/// ``` -/// let mut buf = vec![]; -/// let optional_value: Option = Some(10); -/// grin_onion::util::write_optional(&mut grin_core::ser::BinWriter::default(&mut buf), &optional_value).unwrap(); -/// assert_eq!(&buf, &[1, 0, 0, 0, 10]); -/// ``` -pub fn write_optional( - writer: &mut W, - o: &Option, -) -> Result<(), ser::Error> { - match &o { - Some(o) => { - writer.write_u8(1)?; - o.write(writer)?; - } - None => writer.write_u8(0)?, - }; - Ok(()) -} - -/// Reads an optional value as '1' + value if Some, or '0' if None -/// -/// This function is used to deserialize an optional value from a Reader. If the first byte -/// read is '0', it returns None. If the first byte is '1', it reads the next value and -/// returns Some(value). -/// -/// # Arguments -/// -/// * `reader` - A Reader instance from where the data will be read. -/// -/// # Returns -/// -/// * If successful, returns Ok wrapping an optional value. If the first byte read was '0', -/// returns None. If it was '1', returns Some(value). -/// * If an error occurs during reading, returns Err wrapping the error. -/// -/// # Example -/// -/// ``` -/// let mut buf: &[u8] = &[1, 0, 0, 0, 10]; -/// let mut reader = grin_core::ser::BinReader::new(&mut buf, grin_core::ser::ProtocolVersion::local(), grin_core::ser::DeserializationMode::default()); -/// let optional_value: Option = grin_onion::util::read_optional(&mut reader).unwrap(); -/// assert_eq!(optional_value, Some(10)); -/// ``` -pub fn read_optional(reader: &mut R) -> Result, ser::Error> { - let o = if reader.read_u8()? == 0 { - None - } else { - Some(O::read(reader)?) - }; - Ok(o) -} - -/// Convert a vector to an array of size `S`. -/// -/// # Arguments -/// -/// * `vec` - The input vector. -/// -/// # Returns -/// -/// * If successful, returns an `Ok` wrapping an array of size `S` containing -/// the first `S` bytes of `vec`. -/// * If `vec` is smaller than `S`, returns an `Err` indicating a count error. -/// -/// # Example -/// -/// ``` -/// let v = vec![0, 1, 2, 3, 4, 5]; -/// let a = grin_onion::util::vec_to_array::<4>(&v).unwrap(); -/// assert_eq!(a, [0, 1, 2, 3]); -/// ``` -pub fn vec_to_array(vec: &Vec) -> Result<[u8; S], ser::Error> { - if vec.len() < S { - return Err(ser::Error::CountError); - } - let arr: [u8; S] = vec[0..S].try_into().unwrap(); - Ok(arr) -} - -#[cfg(test)] -mod tests { - use super::*; - use grin_core::ser::{BinReader, BinWriter, DeserializationMode, ProtocolVersion}; - - #[test] - fn test_write_optional() { - // Test with Some value - let mut buf: Vec = vec![]; - let val: Option = Some(10); - write_optional(&mut BinWriter::default(&mut buf), &val).unwrap(); - assert_eq!(buf, &[1, 0, 0, 0, 10]); // 1 for Some, then 10 as a little-endian u32 - - // Test with None value - buf.clear(); - let val: Option = None; - write_optional(&mut BinWriter::default(&mut buf), &val).unwrap(); - assert_eq!(buf, &[0]); // 0 for None - } - - #[test] - fn test_read_optional() { - // Test with Some value - let mut buf: &[u8] = &[1, 0, 0, 0, 10]; // 1 for Some, then 10 as a little-endian u32 - let val: Option = read_optional(&mut BinReader::new( - &mut buf, - ProtocolVersion::local(), - DeserializationMode::default(), - )) - .unwrap(); - assert_eq!(val, Some(10)); - - // Test with None value - buf = &[0]; // 0 for None - let val: Option = read_optional(&mut BinReader::new( - &mut buf, - ProtocolVersion::local(), - DeserializationMode::default(), - )) - .unwrap(); - assert_eq!(val, None); - } - - #[test] - fn test_vec_to_array_success() { - let v = vec![1, 2, 3, 4, 5, 6, 7, 8]; - let a = vec_to_array::<4>(&v).unwrap(); - assert_eq!(a, [1, 2, 3, 4]); - } - - #[test] - fn test_vec_to_array_too_small() { - let v = vec![1, 2, 3]; - let res = vec_to_array::<4>(&v); - assert!(res.is_err()); - } - - #[test] - fn test_vec_to_array_empty() { - let v = vec![]; - let res = vec_to_array::<4>(&v); - assert!(res.is_err()); - } -} diff --git a/src/bin/mwixnet.rs b/src/bin/mwixnet.rs index b790b7b..2bc0ce9 100644 --- a/src/bin/mwixnet.rs +++ b/src/bin/mwixnet.rs @@ -10,6 +10,7 @@ use clap::App; use grin_core::global; use grin_core::global::ChainTypes; use grin_util::{StopState, ZeroingString}; +use grin_wallet_libwallet::mwixnet::onion as grin_onion; use rand::{Rng, thread_rng}; use rpassword; use tor_rtcompat::PreferredRuntime;