From be23042ca138b5a7c3105a5bd3633f0d3b23eea3 Mon Sep 17 00:00:00 2001 From: ennucore Date: Sat, 14 Mar 2020 12:43:06 +0300 Subject: [PATCH 1/3] something --- Cargo.lock | 24 +++++++++++++++++------- Cargo.toml | 3 ++- src/addresses.rs | 14 -------------- src/crypto.rs | 32 ++++++++++++++++++++++++++++++++ src/error.rs | 1 + src/ironforce.rs | 10 ++++------ src/lib.rs | 12 ++++-------- src/message.rs | 32 ++++++++++++++++++++++++++------ 8 files changed, 86 insertions(+), 42 deletions(-) delete mode 100644 src/addresses.rs create mode 100644 src/crypto.rs create mode 100644 src/error.rs diff --git a/Cargo.lock b/Cargo.lock index d3289b3..1321ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,5 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "alt_serde" -version = "1.0.104" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "block-buffer" version = "0.7.3" @@ -39,6 +34,14 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "core-error" +version = "0.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "curve25519-dalek" version = "2.0.0" @@ -113,12 +116,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "ironforest" version = "0.1.0" dependencies = [ - "alt_serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "core-error 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -260,6 +264,11 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "version_check" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -271,12 +280,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum alt_serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "03beeddedd09889b96def26f78ba46e34ffd9bdaaa33b2c980cbaa1d0e762686" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum core-error 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efcdb2972eb64230b4c50646d8498ff73f5128d196a90c7236eec4cbe8619b8f" "checksum curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26778518a7f6cffa1d25a44b602b62b979bd88adb9e99ffec546998cf3404839" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)" = "1d7e523a6e90b7682c0857c1d26cf06f3a0224bc2dfb0766f5e35a0a71ad3dbe" @@ -303,5 +312,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" +"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum zeroize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" diff --git a/Cargo.toml b/Cargo.toml index 0795beb..7c5651e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ edition = "2018" [dependencies] ecdsa = "0.5.0-pre" curve25519-dalek = "2" -alt_serde = "1.0.104" +sha2 = "0.8.1" +core-error = "0.0.0" rand = "*" serde = { version = "1.0", features = ["derive", "alloc"], default-features = false } serde_cbor = { version = "0.11", default-features = false } diff --git a/src/addresses.rs b/src/addresses.rs deleted file mode 100644 index 4947df5..0000000 --- a/src/addresses.rs +++ /dev/null @@ -1,14 +0,0 @@ -extern crate rand; -use serde::{Serialize, Deserialize}; - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -pub struct PublicKey { - pub id: u64 -} - -#[derive(Serialize, Deserialize, Debug, PartialEq)] -pub struct SecretKey {} - -pub fn gen_keys() -> (PublicKey, SecretKey) { - (PublicKey { id: rand::random::() }, SecretKey {}) -} diff --git a/src/crypto.rs b/src/crypto.rs new file mode 100644 index 0000000..6e37e13 --- /dev/null +++ b/src/crypto.rs @@ -0,0 +1,32 @@ +extern crate rand; + +use serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct PublicKey { + pub id: u64 +} + +#[derive(Serialize, Deserialize, Debug, PartialEq)] +pub struct KeyPack { + pub public_key: PublicKey, + secret_key: u64, +} + +impl KeyPack { + pub fn gen() -> KeyPack { + KeyPack { public_key: PublicKey { id: rand::random::() }, secret_key: 0 } + } +} + +impl PublicKey { + pub fn verify_sign(self, sign: u64) -> bool { + true + } +} + +impl KeyPack { + pub fn sign(self, msg: u64) -> u64 { + 0 + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..2d93388 --- /dev/null +++ b/src/error.rs @@ -0,0 +1 @@ +pub trait Error {} diff --git a/src/ironforce.rs b/src/ironforce.rs index eed2ccf..79768b7 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -1,16 +1,14 @@ use crate::transport::Transport; -use crate::addresses::{PublicKey, SecretKey, gen_keys}; +use crate::crypto::{PublicKey, KeyPack}; pub struct IronForce { transport: Transport, - public: PublicKey, - secret: SecretKey, + key_pack: KeyPack } impl IronForce { - pub fn new() -> IronForce { - let (public, secret) = gen_keys(); - IronForce { transport: Transport {}, public, secret } + pub fn gen() -> IronForce { + IronForce { transport: Transport {}, key_pack: KeyPack::gen() } } } diff --git a/src/lib.rs b/src/lib.rs index c83265b..155c9af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,8 @@ extern crate alloc; mod ironforce; mod transport; mod message; -mod addresses; +mod crypto; +mod error; #[cfg(test)] mod tests { @@ -16,17 +17,12 @@ mod tests { #[test] fn creation_works() { - IronForce::new(); + IronForce::gen(); } #[test] fn serialization() { - let msg = crate::message::Message { - msg_type: MsgType::MultiCast, - hash: 0, - body: 0, - source: crate::addresses::PublicKey { id: 0 }, - }; + let msg = crate::message::Message::new(MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen()); let serialized = msg.ser(); let msg2 = Message::deserialize(serialized); } diff --git a/src/message.rs b/src/message.rs index feda78b..b5704ad 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,6 +1,7 @@ use serde::{Serialize, Deserialize}; -use crate::addresses::PublicKey; +use crate::crypto::{PublicKey, KeyPack}; use alloc::vec::Vec; +use sha2::Digest; #[derive(Serialize, Deserialize, Debug, PartialEq)] @@ -12,25 +13,44 @@ pub enum MsgType { #[derive(Serialize, Deserialize, Debug, PartialEq)] pub struct Message { pub msg_type: MsgType, - pub hash: u64, - pub body: u64, + pub body: Vec, pub source: PublicKey, + hash: u64, + sign: u64, } impl Message { + fn get_hash(self) -> Vec { + let mut hasher = sha2::Sha256::new(); + hasher.input(self.ser()); + hasher.result().to_vec() + } + + pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { + let msg = Message { msg_type, body, source: key_pack.public_key, hash: 0, sign: 0 }; + msg + } + pub fn ser(self) -> Vec { let mut buf = [0u8; 8192]; let mut ser = serde_cbor::Serializer::new( serde_cbor::ser::SliceWrite::new(&mut buf[..]) ); - self.serialize(&mut ser).unwrap(); + self.serialize(&mut ser); let writer = ser.into_inner(); buf.to_vec() } pub fn deserialize(serialized: Vec) -> Self { let mut scratch = [0u8; 8192]; - let msg = serde_cbor::de::from_slice_with_scratch(&serialized.as_slice(), &mut scratch).unwrap(); - msg + let msg_r = serde_cbor::de::from_slice_with_scratch(&serialized.as_slice(), &mut scratch); + match msg_r { + Ok(msg) => { + msg + } + Err(e) => { + panic!("{}", e); + } + } } } From 7f7c61954f117f16c07fe9fbbadf7d87b66f47c6 Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 19 Mar 2020 12:19:01 +0300 Subject: [PATCH 2/3] trying --- Cargo.lock | 94 ++++++++++++++++++++++++++++++++++---------------- Cargo.toml | 4 +-- src/crypto.rs | 5 ++- src/lib.rs | 5 +-- src/message.rs | 42 +++++++++------------- 5 files changed, 88 insertions(+), 62 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1321ce8..5edaccb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,15 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "as-slice" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", + "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "block-buffer" version = "0.7.3" @@ -34,14 +44,6 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "core-error" -version = "0.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "curve25519-dalek" version = "2.0.0" @@ -97,6 +99,14 @@ dependencies = [ "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "generic-array" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "getrandom" version = "0.1.14" @@ -108,20 +118,34 @@ dependencies = [ ] [[package]] -name = "half" -version = "1.5.0" +name = "hash32" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "heapless" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "as-slice 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "ironforest" version = "0.1.0" dependencies = [ - "core-error 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)", + "heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "postcard 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -135,6 +159,22 @@ name = "opaque-debug" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "postcard" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "postcard-cobs 0.1.5-pre (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "postcard-cobs" +version = "0.1.5-pre" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ppv-lite86" version = "0.2.6" @@ -201,15 +241,6 @@ dependencies = [ "serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "serde_cbor" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "half 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "serde_derive" version = "1.0.104" @@ -239,6 +270,11 @@ dependencies = [ "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stable_deref_trait" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "subtle" version = "2.2.2" @@ -264,11 +300,6 @@ name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "version_check" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" @@ -280,22 +311,26 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum as-slice 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "37dfb65bc03b2bc85ee827004f14a6817e04160e3b1a28931986a666a9290e70" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -"checksum core-error 0.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "efcdb2972eb64230b4c50646d8498ff73f5128d196a90c7236eec4cbe8619b8f" "checksum curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26778518a7f6cffa1d25a44b602b62b979bd88adb9e99ffec546998cf3404839" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)" = "1d7e523a6e90b7682c0857c1d26cf06f3a0224bc2dfb0766f5e35a0a71ad3dbe" "checksum elliptic-curve 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "01f69be7d1feb7a7a04f158aaf32c7deaa7604e9bd58145525e536438c4e5096" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +"checksum generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" -"checksum half 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f36b5f248235f45773d4944f555f83ea61fe07b18b561ccf99d7483d7381e54d" +"checksum hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" +"checksum heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10b591a0032f114b7a77d4fbfab452660c553055515b7d7ece355db080d19087" "checksum libc 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "eb147597cdf94ed43ab7a9038716637d2d1bf2bc571da995d0028dec06bd3018" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +"checksum postcard 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f61f42c9617f3d7b447ee300bf80cb16c7cd7b28ca88555822793f073f69719f" +"checksum postcard-cobs 0.1.5-pre (registry+https://github.com/rust-lang/crates.io-index)" = "7c68cb38ed13fd7bc9dd5db8f165b7c8d9c1a315104083a2b10f11354c2af97f" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435" "checksum quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f" @@ -304,14 +339,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" "checksum rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" "checksum serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449" -"checksum serde_cbor 0.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" "checksum signature 1.0.0-pre.3 (registry+https://github.com/rust-lang/crates.io-index)" = "561619c00cf6a187ebfc21e46bc4c0ce4e4d5f67cd640e7b1c58d9c3754b38aa" +"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum subtle 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c65d530b10ccaeac294f349038a597e435b18fb456aadd0840a623f83b9e941" "checksum syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" -"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum zeroize 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" diff --git a/Cargo.toml b/Cargo.toml index 7c5651e..5cac694 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ edition = "2018" ecdsa = "0.5.0-pre" curve25519-dalek = "2" sha2 = "0.8.1" -core-error = "0.0.0" rand = "*" serde = { version = "1.0", features = ["derive", "alloc"], default-features = false } -serde_cbor = { version = "0.11", default-features = false } +postcard = "0.4.3" +heapless = "0.5.3" diff --git a/src/crypto.rs b/src/crypto.rs index 6e37e13..99c2326 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,13 +1,12 @@ extern crate rand; - use serde::{Serialize, Deserialize}; -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct PublicKey { pub id: u64 } -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct KeyPack { pub public_key: PublicKey, secret_key: u64, diff --git a/src/lib.rs b/src/lib.rs index 155c9af..d8a071f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate alloc; + mod ironforce; mod transport; mod message; @@ -13,7 +14,7 @@ mod error; mod tests { use crate::ironforce::IronForce; use crate::message::{MsgType, Message}; - use alloc::vec::Vec; + use heapless::{Vec, consts::*}; #[test] fn creation_works() { @@ -22,7 +23,7 @@ mod tests { #[test] fn serialization() { - let msg = crate::message::Message::new(MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen()); + let msg = crate::message::Message::new(MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen()); let serialized = msg.ser(); let msg2 = Message::deserialize(serialized); } diff --git a/src/message.rs b/src/message.rs index b5704ad..24591f5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,56 +1,48 @@ -use serde::{Serialize, Deserialize}; use crate::crypto::{PublicKey, KeyPack}; -use alloc::vec::Vec; use sha2::Digest; +use serde::{Serialize, Deserialize}; +use postcard::{to_vec, from_bytes}; +use heapless::{Vec, consts::*}; -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub enum MsgType { MultiCast, ToTarget(PublicKey), } -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Message { pub msg_type: MsgType, - pub body: Vec, + pub body: Vec, pub source: PublicKey, hash: u64, sign: u64, } impl Message { - fn get_hash(self) -> Vec { + fn get_hash(self) -> alloc::vec::Vec { let mut hasher = sha2::Sha256::new(); hasher.input(self.ser()); hasher.result().to_vec() } - pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { + pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { let msg = Message { msg_type, body, source: key_pack.public_key, hash: 0, sign: 0 }; msg } - pub fn ser(self) -> Vec { - let mut buf = [0u8; 8192]; - let mut ser = serde_cbor::Serializer::new( - serde_cbor::ser::SliceWrite::new(&mut buf[..]) - ); - self.serialize(&mut ser); - let writer = ser.into_inner(); - buf.to_vec() + pub fn ser(self) -> Vec { + to_vec(&self).unwrap() } - pub fn deserialize(serialized: Vec) -> Self { - let mut scratch = [0u8; 8192]; - let msg_r = serde_cbor::de::from_slice_with_scratch(&serialized.as_slice(), &mut scratch); - match msg_r { - Ok(msg) => { - msg - } - Err(e) => { - panic!("{}", e); - } + pub fn deserialize(serialized: Vec) -> Self { + Self{ + msg_type: MsgType::MultiCast, + body: Vec::::new(), + source: PublicKey { id: 0 }, + hash: 0, + sign: 0 } } } From bd851f01ec8231ea6d69c970abd1f1475b957d31 Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 19 Mar 2020 15:44:30 +0300 Subject: [PATCH 3/3] serialization is working --- Cargo.lock | 99 +++++++++++++++++++++++++------------------------- Cargo.toml | 3 +- src/lib.rs | 4 +- src/message.rs | 25 +++++-------- 4 files changed, 63 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5edaccb..8d511a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,15 +1,18 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "as-slice" -version = "0.1.3" +name = "ahash" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", - "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", - "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "autocfg" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "block-buffer" version = "0.7.3" @@ -44,6 +47,24 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "const-random" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "const-random-macro 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "const-random-macro" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "curve25519-dalek" version = "2.0.0" @@ -99,14 +120,6 @@ dependencies = [ "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "generic-array" -version = "0.13.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "getrandom" version = "0.1.14" @@ -118,21 +131,12 @@ dependencies = [ ] [[package]] -name = "hash32" -version = "0.1.1" +name = "hashbrown" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "heapless" -version = "0.5.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "as-slice 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", - "generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)", - "hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ahash 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -142,8 +146,7 @@ version = "0.1.0" dependencies = [ "curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)", - "heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "postcard 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "pinecone 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -160,25 +163,28 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "postcard" -version = "0.4.3" +name = "pinecone" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "postcard-cobs 0.1.5-pre (registry+https://github.com/rust-lang/crates.io-index)", + "hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "postcard-cobs" -version = "0.1.5-pre" +name = "ppv-lite86" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "ppv-lite86" -version = "0.2.6" +name = "proc-macro-hack" +version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "proc-macro2" @@ -270,11 +276,6 @@ dependencies = [ "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "stable_deref_trait" -version = "1.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "subtle" version = "2.2.2" @@ -311,27 +312,28 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] -"checksum as-slice 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "37dfb65bc03b2bc85ee827004f14a6817e04160e3b1a28931986a666a9290e70" +"checksum ahash 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "6f33b5018f120946c1dcf279194f238a9f146725593ead1c08fa47ff22b0b5d3" +"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2" "checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" "checksum block-padding 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum const-random 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "2f1af9ac737b2dd2d577701e59fd09ba34822f6f2ebdb30a7647405d9e55e16a" +"checksum const-random-macro 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "25e4c606eb459dd29f7c57b2e0879f2b6f14ee130918c2b78ccb58a9624e6c7a" "checksum curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26778518a7f6cffa1d25a44b602b62b979bd88adb9e99ffec546998cf3404839" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum ecdsa 0.5.0-pre (registry+https://github.com/rust-lang/crates.io-index)" = "1d7e523a6e90b7682c0857c1d26cf06f3a0224bc2dfb0766f5e35a0a71ad3dbe" "checksum elliptic-curve 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "01f69be7d1feb7a7a04f158aaf32c7deaa7604e9bd58145525e536438c4e5096" "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" -"checksum generic-array 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd" "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" -"checksum hash32 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d4041af86e63ac4298ce40e5cca669066e75b6f1aa3390fe2561ffa5e1d9f4cc" -"checksum heapless 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10b591a0032f114b7a77d4fbfab452660c553055515b7d7ece355db080d19087" +"checksum hashbrown 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8e6073d0ca812575946eb5f35ff68dbe519907b25c42530389ff946dc84c6ead" "checksum libc 0.2.67 (registry+https://github.com/rust-lang/crates.io-index)" = "eb147597cdf94ed43ab7a9038716637d2d1bf2bc571da995d0028dec06bd3018" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" -"checksum postcard 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f61f42c9617f3d7b447ee300bf80cb16c7cd7b28ca88555822793f073f69719f" -"checksum postcard-cobs 0.1.5-pre (registry+https://github.com/rust-lang/crates.io-index)" = "7c68cb38ed13fd7bc9dd5db8f165b7c8d9c1a315104083a2b10f11354c2af97f" +"checksum pinecone 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d428731d3fa65fb4be3a898fa9dc46f512b43874a621377d21c96582f1372e21" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" +"checksum proc-macro-hack 0.5.12 (registry+https://github.com/rust-lang/crates.io-index)" = "f918f2b601f93baa836c1c2945faef682ba5b6d4828ecb45eeb7cc3c71b811b4" "checksum proc-macro2 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6c09721c6781493a2a492a96b5a5bf19b65917fe6728884e7c44dd0c60ca3435" "checksum quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f" "checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" @@ -342,7 +344,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)" = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64" "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" "checksum signature 1.0.0-pre.3 (registry+https://github.com/rust-lang/crates.io-index)" = "561619c00cf6a187ebfc21e46bc4c0ce4e4d5f67cd640e7b1c58d9c3754b38aa" -"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum subtle 2.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c65d530b10ccaeac294f349038a597e435b18fb456aadd0840a623f83b9e941" "checksum syn 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)" = "123bd9499cfb380418d509322d7a6d52e5315f064fe4b3ad18a53d6b92c07859" "checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" diff --git a/Cargo.toml b/Cargo.toml index 5cac694..3afc2cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,4 @@ curve25519-dalek = "2" sha2 = "0.8.1" rand = "*" serde = { version = "1.0", features = ["derive", "alloc"], default-features = false } -postcard = "0.4.3" -heapless = "0.5.3" +pinecone = "0.2.3" diff --git a/src/lib.rs b/src/lib.rs index d8a071f..7b8f344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,7 +14,7 @@ mod error; mod tests { use crate::ironforce::IronForce; use crate::message::{MsgType, Message}; - use heapless::{Vec, consts::*}; + use alloc::vec::Vec; #[test] fn creation_works() { @@ -23,7 +23,7 @@ mod tests { #[test] fn serialization() { - let msg = crate::message::Message::new(MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen()); + let msg = crate::message::Message::new(MsgType::MultiCast, Vec::::new(), crate::crypto::KeyPack::gen()); let serialized = msg.ser(); let msg2 = Message::deserialize(serialized); } diff --git a/src/message.rs b/src/message.rs index 24591f5..d46daf1 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,8 +1,9 @@ use crate::crypto::{PublicKey, KeyPack}; use sha2::Digest; use serde::{Serialize, Deserialize}; -use postcard::{to_vec, from_bytes}; -use heapless::{Vec, consts::*}; +use alloc::vec::Vec; +use pinecone::{from_bytes, to_vec}; +use serde::private::de::size_hint::from_bounds; #[derive(Debug, PartialEq, Serialize, Deserialize)] @@ -14,35 +15,29 @@ pub enum MsgType { #[derive(Debug, PartialEq, Serialize, Deserialize)] pub struct Message { pub msg_type: MsgType, - pub body: Vec, + pub body: Vec, pub source: PublicKey, hash: u64, sign: u64, } impl Message { - fn get_hash(self) -> alloc::vec::Vec { + fn get_hash(self) -> Vec { let mut hasher = sha2::Sha256::new(); hasher.input(self.ser()); hasher.result().to_vec() } - pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { + pub fn new(msg_type: MsgType, body: Vec, key_pack: KeyPack) -> Message { let msg = Message { msg_type, body, source: key_pack.public_key, hash: 0, sign: 0 }; msg } - pub fn ser(self) -> Vec { - to_vec(&self).unwrap() + pub fn ser(self) -> Vec { + to_vec(&self).expect("Serialization failed") } - pub fn deserialize(serialized: Vec) -> Self { - Self{ - msg_type: MsgType::MultiCast, - body: Vec::::new(), - source: PublicKey { id: 0 }, - hash: 0, - sign: 0 - } + pub fn deserialize(serialized: Vec) -> Self { + from_bytes(&serialized).expect("Deserialization failed") } }