Skip to content

Commit

Permalink
add Null impl for Nonce and Multikey
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Huseby <dwh@linuxprogrammer.org>
  • Loading branch information
dhuseby committed Apr 3, 2024
1 parent a16fd96 commit 14124df
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "multikey"
version = "0.8.7"
version = "0.8.8"
edition = "2021"
authors = ["Dave Huseby <dwh@linuxprogrammer.org>"]
authors = ["Dave Grantham <dwg@linuxprogrammer.org>"]
description = "Multikey self-describing cryptographic key data"
repository = "https://github.com/cryptidtech/multikey.git"
readme = "README.md"
Expand Down
36 changes: 30 additions & 6 deletions src/mk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{

use multibase::Base;
use multicodec::Codec;
use multitrait::TryDecodeFrom;
use multitrait::{Null, TryDecodeFrom};
use multiutil::{BaseEncoded, CodecInfo, EncodingInfo, Varbytes, Varuint};
use rand::{CryptoRng, RngCore};
use ssh_key::{
Expand Down Expand Up @@ -135,6 +135,16 @@ impl<'a> TryDecodeFrom<'a> for Multikey {
}
}

impl Null for Multikey {
fn null() -> Self {
Self::default()
}

fn is_null(&self) -> bool {
*self == Self::null()
}
}

impl fmt::Debug for Multikey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// get an attributes view on the key
Expand Down Expand Up @@ -854,6 +864,7 @@ impl Builder {
mod tests {
use super::*;
use crate::{cipher, kdf, views};
use multisig::EncodedMultisig;

#[test]
fn test_ed25519_random() {
Expand All @@ -877,7 +888,7 @@ mod tests {
.try_build_encoded()
.unwrap();
let s = mk.to_string();
//println!("ed25519: {}", s);
println!("ed25519: {}", s);
assert_eq!(mk, EncodedMultikey::try_from(s.as_str()).unwrap());
}

Expand All @@ -903,7 +914,7 @@ mod tests {
.try_build_encoded()
.unwrap();
let s = mk.to_string();
//println!("secp256k1: {}", s);
println!("secp256k1: {}", s);
assert_eq!(mk, EncodedMultikey::try_from(s.as_str()).unwrap());
}

Expand All @@ -929,7 +940,7 @@ mod tests {
.try_build_encoded()
.unwrap();
let s = mk.to_string();
//println!("bls12381g2: {}", s);
println!("bls12381g2: {}", s);
assert_eq!(mk, EncodedMultikey::try_from(s.as_str()).unwrap());
}

Expand Down Expand Up @@ -1141,12 +1152,16 @@ mod tests {
let kd = mk.data_view().unwrap();
assert!(kd.key_bytes().is_ok());
assert!(kd.secret_bytes().is_ok());
let conv = mk.conv_view().unwrap();
let pk = EncodedMultikey::new(Base::Base16Lower, conv.to_public_key().unwrap());
println!("ed25519 pubkey: {}", pk.to_string());

let msg = hex::decode("8bb78be51ac7cc98f44e38947ff8a128764ec039b89687a790dfa8444ba97682")
.unwrap();
let msg = b"for great justice, move every zig!".to_vec();

let signmk = mk.sign_view().unwrap();
let signature = signmk.sign(msg.as_slice(), false, None).unwrap();
let sig = EncodedMultisig::new(Base::Base16Lower, signature.clone());
println!("signaure: {}", sig.to_string());

let verifymk = mk.verify_view().unwrap();
assert!(verifymk.verify(&signature, Some(&msg)).is_ok());
Expand Down Expand Up @@ -1566,4 +1581,13 @@ mod tests {
assert!(kd.key_bytes().is_ok());
assert!(kd.secret_bytes().is_ok());
}

#[test]
fn test_null() {
let mk1 = Multikey::null();
assert!(mk1.is_null());
let mk2 = Multikey::default();
assert_eq!(mk1, mk2);
assert!(mk2.is_null());
}
}
21 changes: 20 additions & 1 deletion src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{error::NonceError, Error};
use core::fmt;
use multibase::Base;
use multicodec::Codec;
use multitrait::TryDecodeFrom;
use multitrait::{Null, TryDecodeFrom};
use multiutil::{BaseEncoded, CodecInfo, EncodingInfo, Varbytes};
use rand::{CryptoRng, RngCore};

Expand Down Expand Up @@ -94,6 +94,16 @@ impl<'a> TryDecodeFrom<'a> for Nonce {
}
}

impl Null for Nonce {
fn null() -> Self {
Self::default()
}

fn is_null(&self) -> bool {
*self == Self::null()
}
}

impl fmt::Debug for Nonce {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} - {}", SIGIL, hex::encode(&self.nonce))
Expand Down Expand Up @@ -209,4 +219,13 @@ mod tests {
let s = n.to_string();
assert_eq!(n, EncodedNonce::try_from(s.as_str()).unwrap());
}

#[test]
fn test_null() {
let n1 = Nonce::null();
assert!(n1.is_null());
let n2 = Nonce::default();
assert_eq!(n1, n2);
assert!(n2.is_null());
}
}

0 comments on commit 14124df

Please sign in to comment.