2018-12-30 01:59:24 +00:00
|
|
|
// -*- mode: rust; -*-
|
|
|
|
|
//
|
|
|
|
|
// This file is part of ed25519-dalek.
|
2019-01-17 23:28:13 +00:00
|
|
|
// Copyright (c) 2017-2019 isis lovecruft
|
2018-12-30 01:59:24 +00:00
|
|
|
// See LICENSE for licensing information.
|
|
|
|
|
//
|
|
|
|
|
// Authors:
|
|
|
|
|
// - isis agora lovecruft <isis@patternsinthevoid.net>
|
|
|
|
|
|
|
|
|
|
//! Integration tests for ed25519-dalek.
|
|
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
use curve25519_dalek;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
use ed25519_dalek::*;
|
|
|
|
|
|
|
|
|
|
use hex::FromHex;
|
2022-12-02 04:55:16 +00:00
|
|
|
use hex_literal::hex;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-20 09:48:55 +00:00
|
|
|
#[cfg(feature = "rand")]
|
2018-12-30 01:59:24 +00:00
|
|
|
use sha2::Sha512;
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2018-12-30 02:17:56 +00:00
|
|
|
mod vectors {
|
2020-10-14 19:13:34 +00:00
|
|
|
use curve25519_dalek::{edwards::EdwardsPoint, scalar::Scalar};
|
|
|
|
|
use sha2::{digest::Digest, Sha512};
|
|
|
|
|
use std::convert::TryFrom;
|
2020-03-17 17:25:33 +00:00
|
|
|
|
2018-12-30 02:17:56 +00:00
|
|
|
use std::fs::File;
|
2022-12-08 07:39:48 +00:00
|
|
|
use std::io::BufRead;
|
|
|
|
|
use std::io::BufReader;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2018-12-30 02:17:56 +00:00
|
|
|
use super::*;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
// TESTVECTORS is taken from sign.input.gz in agl's ed25519 Golang
|
|
|
|
|
// package. It is a selection of test cases from
|
|
|
|
|
// http://ed25519.cr.yp.to/python/sign.input
|
|
|
|
|
#[test]
|
2022-12-08 07:39:48 +00:00
|
|
|
fn against_reference_implementation() {
|
|
|
|
|
// TestGolden
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut line: String;
|
|
|
|
|
let mut lineno: usize = 0;
|
|
|
|
|
|
|
|
|
|
let f = File::open("TESTVECTORS");
|
|
|
|
|
if f.is_err() {
|
2022-12-08 07:39:48 +00:00
|
|
|
println!(
|
|
|
|
|
"This test is only available when the code has been cloned \
|
|
|
|
|
from the git repository, since the TESTVECTORS file is large \
|
|
|
|
|
and is therefore not included within the distributed crate."
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
panic!();
|
|
|
|
|
}
|
|
|
|
|
let file = BufReader::new(f.unwrap());
|
|
|
|
|
|
|
|
|
|
for l in file.lines() {
|
|
|
|
|
lineno += 1;
|
|
|
|
|
line = l.unwrap();
|
|
|
|
|
|
|
|
|
|
let parts: Vec<&str> = line.split(':').collect();
|
|
|
|
|
assert_eq!(parts.len(), 5, "wrong number of fields in line {}", lineno);
|
|
|
|
|
|
|
|
|
|
let sec_bytes: Vec<u8> = FromHex::from_hex(&parts[0]).unwrap();
|
|
|
|
|
let pub_bytes: Vec<u8> = FromHex::from_hex(&parts[1]).unwrap();
|
|
|
|
|
let msg_bytes: Vec<u8> = FromHex::from_hex(&parts[2]).unwrap();
|
|
|
|
|
let sig_bytes: Vec<u8> = FromHex::from_hex(&parts[3]).unwrap();
|
|
|
|
|
|
2022-12-02 04:55:16 +00:00
|
|
|
let sec_bytes = &sec_bytes[..SECRET_KEY_LENGTH].try_into().unwrap();
|
|
|
|
|
let pub_bytes = &pub_bytes[..PUBLIC_KEY_LENGTH].try_into().unwrap();
|
|
|
|
|
|
|
|
|
|
let signing_key = SigningKey::from_bytes(sec_bytes);
|
2022-12-18 20:56:41 +00:00
|
|
|
let expected_verifying_key = VerifyingKey::from_bytes(pub_bytes).unwrap();
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(expected_verifying_key, signing_key.verifying_key());
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
// The signatures in the test vectors also include the message
|
|
|
|
|
// at the end, but we just want R and S.
|
2022-12-10 02:14:38 +00:00
|
|
|
let sig1: Signature = Signature::try_from(&sig_bytes[..64]).unwrap();
|
2022-12-18 06:24:58 +00:00
|
|
|
let sig2: Signature = signing_key.sign(&msg_bytes);
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
assert!(sig1 == sig2, "Signature bytes not equal on line {}", lineno);
|
2022-12-08 07:39:48 +00:00
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key.verify(&msg_bytes, &sig2).is_ok(),
|
2022-12-08 07:39:48 +00:00
|
|
|
"Signature verification failed on line {}",
|
|
|
|
|
lineno
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// From https://tools.ietf.org/html/rfc8032#section-7.3
|
|
|
|
|
#[test]
|
|
|
|
|
fn ed25519ph_rf8032_test_vector() {
|
2022-12-02 04:55:16 +00:00
|
|
|
let sec_bytes = hex!("833fe62409237b9d62ec77587520911e9a759cec1d19755b7da901b96dca3d42");
|
|
|
|
|
let pub_bytes = hex!("ec172b93ad5e563bf4932c70e1245034c35467ef2efd4d64ebf819683467e2bf");
|
|
|
|
|
let msg_bytes = hex!("616263");
|
|
|
|
|
let sig_bytes = hex!("98a70222f0b8121aa9d30f813d683f809e462b469c7ff87639499bb94e6dae4131f85042463c2a355a2003d062adf5aaa10b8c61e636062aaad11c2a26083406");
|
|
|
|
|
|
|
|
|
|
let signing_key = SigningKey::from_bytes(&sec_bytes);
|
2022-12-18 20:56:41 +00:00
|
|
|
let expected_verifying_key = VerifyingKey::from_bytes(&pub_bytes).unwrap();
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(expected_verifying_key, signing_key.verifying_key());
|
2022-12-02 04:55:16 +00:00
|
|
|
let sig1 = Signature::try_from(&sig_bytes[..]).unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-02 04:55:16 +00:00
|
|
|
let mut prehash_for_signing = Sha512::default();
|
|
|
|
|
let mut prehash_for_verifying = Sha512::default();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2020-08-19 21:58:00 +00:00
|
|
|
prehash_for_signing.update(&msg_bytes[..]);
|
|
|
|
|
prehash_for_verifying.update(&msg_bytes[..]);
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
let sig2: Signature = signing_key
|
|
|
|
|
.sign_prehashed(prehash_for_signing, None)
|
|
|
|
|
.unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
assert!(
|
|
|
|
|
sig1 == sig2,
|
|
|
|
|
"Original signature from test vectors doesn't equal signature produced:\
|
|
|
|
|
\noriginal:\n{:?}\nproduced:\n{:?}",
|
|
|
|
|
sig1,
|
|
|
|
|
sig2
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.verify_prehashed(prehash_for_verifying, None, &sig2)
|
|
|
|
|
.is_ok(),
|
|
|
|
|
"Could not verify ed25519ph signature!"
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
2020-10-14 19:13:34 +00:00
|
|
|
|
|
|
|
|
// Taken from curve25519_dalek::constants::EIGHT_TORSION[4]
|
|
|
|
|
const EIGHT_TORSION_4: [u8; 32] = [
|
|
|
|
|
236, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
|
|
|
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 127,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
fn compute_hram(message: &[u8], pub_key: &EdwardsPoint, signature_r: &EdwardsPoint) -> Scalar {
|
|
|
|
|
let k_bytes = Sha512::default()
|
2022-11-20 20:08:05 +00:00
|
|
|
.chain_update(&signature_r.compress().as_bytes())
|
|
|
|
|
.chain_update(&pub_key.compress().as_bytes()[..])
|
|
|
|
|
.chain_update(&message);
|
2020-10-14 19:13:34 +00:00
|
|
|
let mut k_output = [0u8; 64];
|
|
|
|
|
k_output.copy_from_slice(k_bytes.finalize().as_slice());
|
|
|
|
|
Scalar::from_bytes_mod_order_wide(&k_output)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn serialize_signature(r: &EdwardsPoint, s: &Scalar) -> Vec<u8> {
|
|
|
|
|
[&r.compress().as_bytes()[..], &s.as_bytes()[..]].concat()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn repudiation() {
|
|
|
|
|
use curve25519_dalek::traits::IsIdentity;
|
|
|
|
|
use std::ops::Neg;
|
|
|
|
|
|
|
|
|
|
let message1 = b"Send 100 USD to Alice";
|
|
|
|
|
let message2 = b"Send 100000 USD to Alice";
|
|
|
|
|
|
|
|
|
|
// Pick a random Scalar
|
|
|
|
|
fn non_null_scalar() -> Scalar {
|
|
|
|
|
let mut rng = rand::rngs::OsRng;
|
|
|
|
|
let mut s_candidate = Scalar::random(&mut rng);
|
2022-12-13 23:19:31 +00:00
|
|
|
while s_candidate == Scalar::ZERO {
|
2020-10-14 19:13:34 +00:00
|
|
|
s_candidate = Scalar::random(&mut rng);
|
|
|
|
|
}
|
|
|
|
|
s_candidate
|
|
|
|
|
}
|
|
|
|
|
let mut s: Scalar = non_null_scalar();
|
|
|
|
|
|
|
|
|
|
fn pick_r_and_pubkey(s: Scalar) -> (EdwardsPoint, EdwardsPoint) {
|
|
|
|
|
let r0 = s * curve25519_dalek::constants::ED25519_BASEPOINT_POINT;
|
|
|
|
|
// Pick a torsion point of order 2
|
|
|
|
|
let pub_key = curve25519_dalek::edwards::CompressedEdwardsY(EIGHT_TORSION_4)
|
|
|
|
|
.decompress()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let r = r0 + pub_key.neg();
|
|
|
|
|
(r, pub_key)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (mut r, mut pub_key) = pick_r_and_pubkey(s);
|
|
|
|
|
|
|
|
|
|
while !(pub_key.neg() + compute_hram(message1, &pub_key, &r) * pub_key).is_identity()
|
|
|
|
|
|| !(pub_key.neg() + compute_hram(message2, &pub_key, &r) * pub_key).is_identity()
|
|
|
|
|
{
|
|
|
|
|
s = non_null_scalar();
|
|
|
|
|
let key = pick_r_and_pubkey(s);
|
|
|
|
|
r = key.0;
|
|
|
|
|
pub_key = key.1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let signature = serialize_signature(&r, &s);
|
2022-12-02 04:55:16 +00:00
|
|
|
let pk = VerifyingKey::from_bytes(&pub_key.compress().as_bytes()).unwrap();
|
2020-10-14 19:13:34 +00:00
|
|
|
let sig = Signature::try_from(&signature[..]).unwrap();
|
|
|
|
|
// The same signature verifies for both messages
|
|
|
|
|
assert!(pk.verify(message1, &sig).is_ok() && pk.verify(message2, &sig).is_ok());
|
|
|
|
|
// But not with a strict signature: verify_strict refuses small order keys
|
|
|
|
|
assert!(
|
|
|
|
|
pk.verify_strict(message1, &sig).is_err() || pk.verify_strict(message2, &sig).is_err()
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-12-30 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-20 09:48:55 +00:00
|
|
|
#[cfg(feature = "rand")]
|
2018-12-30 02:17:56 +00:00
|
|
|
mod integrations {
|
|
|
|
|
use super::*;
|
2019-10-21 15:03:08 +00:00
|
|
|
use rand::rngs::OsRng;
|
2018-12-30 02:17:56 +00:00
|
|
|
|
|
|
|
|
#[test]
|
2022-12-08 07:39:48 +00:00
|
|
|
fn sign_verify() {
|
|
|
|
|
// TestSignVerify
|
2022-12-18 06:24:58 +00:00
|
|
|
let signing_key: SigningKey;
|
2018-12-30 02:17:56 +00:00
|
|
|
let good_sig: Signature;
|
2022-12-08 07:39:48 +00:00
|
|
|
let bad_sig: Signature;
|
2018-12-30 02:17:56 +00:00
|
|
|
|
|
|
|
|
let good: &[u8] = "test message".as_bytes();
|
2022-12-08 07:39:48 +00:00
|
|
|
let bad: &[u8] = "wrong message".as_bytes();
|
2018-12-30 02:17:56 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
let mut csprng = OsRng {};
|
2019-04-02 01:46:23 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key = SigningKey::generate(&mut csprng);
|
|
|
|
|
good_sig = signing_key.sign(&good);
|
|
|
|
|
bad_sig = signing_key.sign(&bad);
|
2022-12-08 07:39:48 +00:00
|
|
|
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key.verify(&good, &good_sig).is_ok(),
|
2022-12-08 07:39:48 +00:00
|
|
|
"Verification of a valid signature failed!"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key.verify(&good, &bad_sig).is_err(),
|
2022-12-08 07:39:48 +00:00
|
|
|
"Verification of a signature on a different message passed!"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key.verify(&bad, &good_sig).is_err(),
|
2022-12-08 07:39:48 +00:00
|
|
|
"Verification of a signature on a different message passed!"
|
|
|
|
|
);
|
2018-12-30 02:17:56 +00:00
|
|
|
}
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn ed25519ph_sign_verify() {
|
2022-12-18 06:24:58 +00:00
|
|
|
let signing_key: SigningKey;
|
2018-12-30 01:59:24 +00:00
|
|
|
let good_sig: Signature;
|
2022-12-08 07:39:48 +00:00
|
|
|
let bad_sig: Signature;
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
let good: &[u8] = b"test message";
|
2022-12-08 07:39:48 +00:00
|
|
|
let bad: &[u8] = b"wrong message";
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
let mut csprng = OsRng;
|
2019-04-02 01:46:23 +00:00
|
|
|
|
2018-12-30 01:59:24 +00:00
|
|
|
// ugh… there's no `impl Copy for Sha512`… i hope we can all agree these are the same hashes
|
|
|
|
|
let mut prehashed_good1: Sha512 = Sha512::default();
|
2020-08-19 21:58:00 +00:00
|
|
|
prehashed_good1.update(good);
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut prehashed_good2: Sha512 = Sha512::default();
|
2020-08-19 21:58:00 +00:00
|
|
|
prehashed_good2.update(good);
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut prehashed_good3: Sha512 = Sha512::default();
|
2020-08-19 21:58:00 +00:00
|
|
|
prehashed_good3.update(good);
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
let mut prehashed_bad1: Sha512 = Sha512::default();
|
2020-08-19 21:58:00 +00:00
|
|
|
prehashed_bad1.update(bad);
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut prehashed_bad2: Sha512 = Sha512::default();
|
2020-08-19 21:58:00 +00:00
|
|
|
prehashed_bad2.update(bad);
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
let context: &[u8] = b"testing testing 1 2 3";
|
|
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key = SigningKey::generate(&mut csprng);
|
|
|
|
|
good_sig = signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.sign_prehashed(prehashed_good1, Some(context))
|
|
|
|
|
.unwrap();
|
2022-12-18 06:24:58 +00:00
|
|
|
bad_sig = signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.sign_prehashed(prehashed_bad1, Some(context))
|
|
|
|
|
.unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.verify_prehashed(prehashed_good2, Some(context), &good_sig)
|
|
|
|
|
.is_ok(),
|
|
|
|
|
"Verification of a valid signature failed!"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.verify_prehashed(prehashed_good3, Some(context), &bad_sig)
|
|
|
|
|
.is_err(),
|
|
|
|
|
"Verification of a signature on a different message passed!"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key
|
2022-12-08 07:39:48 +00:00
|
|
|
.verify_prehashed(prehashed_bad2, Some(context), &good_sig)
|
|
|
|
|
.is_err(),
|
|
|
|
|
"Verification of a signature on a different message passed!"
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-02 01:46:23 +00:00
|
|
|
#[cfg(feature = "batch")]
|
2018-12-30 01:59:24 +00:00
|
|
|
#[test]
|
|
|
|
|
fn verify_batch_seven_signatures() {
|
|
|
|
|
let messages: [&[u8]; 7] = [
|
|
|
|
|
b"Watch closely everyone, I'm going to show you how to kill a god.",
|
|
|
|
|
b"I'm not a cryptographer I just encrypt a lot.",
|
|
|
|
|
b"Still not a cryptographer.",
|
|
|
|
|
b"This is a test of the tsunami alert system. This is only a test.",
|
|
|
|
|
b"Fuck dumbin' it down, spit ice, skip jewellery: Molotov cocktails on me like accessories.",
|
|
|
|
|
b"Hey, I never cared about your bucks, so if I run up with a mask on, probably got a gas can too.",
|
|
|
|
|
b"And I'm not here to fill 'er up. Nope, we came to riot, here to incite, we don't want any of your stuff.", ];
|
2022-12-08 07:39:48 +00:00
|
|
|
let mut csprng = OsRng;
|
2022-12-18 06:24:58 +00:00
|
|
|
let mut signing_keys: Vec<SigningKey> = Vec::new();
|
2018-12-30 01:59:24 +00:00
|
|
|
let mut signatures: Vec<Signature> = Vec::new();
|
|
|
|
|
|
|
|
|
|
for i in 0..messages.len() {
|
2022-12-18 06:24:58 +00:00
|
|
|
let signing_key: SigningKey = SigningKey::generate(&mut csprng);
|
|
|
|
|
signatures.push(signing_key.sign(&messages[i]));
|
|
|
|
|
signing_keys.push(signing_key);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
2022-12-18 06:24:58 +00:00
|
|
|
let verifying_keys: Vec<VerifyingKey> =
|
|
|
|
|
signing_keys.iter().map(|key| key.verifying_key()).collect();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
let result = verify_batch(&messages, &signatures[..], &verifying_keys[..]);
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
assert!(result.is_ok());
|
|
|
|
|
}
|
2018-12-30 02:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 23:16:30 +00:00
|
|
|
#[cfg(all(test, feature = "serde"))]
|
2022-12-20 09:48:55 +00:00
|
|
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
|
|
|
|
#[serde(crate = "serde")]
|
2020-06-30 22:40:24 +00:00
|
|
|
struct Demo {
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key: SigningKey,
|
2020-06-30 22:40:24 +00:00
|
|
|
}
|
|
|
|
|
|
2018-12-30 02:17:56 +00:00
|
|
|
#[cfg(all(test, feature = "serde"))]
|
|
|
|
|
mod serialisation {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2020-08-07 20:39:57 +00:00
|
|
|
// The size for bincode to serialize the length of a byte array.
|
|
|
|
|
static BINCODE_INT_LENGTH: usize = 8;
|
|
|
|
|
|
2018-12-30 02:17:56 +00:00
|
|
|
static PUBLIC_KEY_BYTES: [u8; PUBLIC_KEY_LENGTH] = [
|
2022-12-08 07:39:48 +00:00
|
|
|
130, 039, 155, 015, 062, 076, 188, 063, 124, 122, 026, 251, 233, 253, 225, 220, 014, 041,
|
|
|
|
|
166, 120, 108, 035, 254, 077, 160, 083, 172, 058, 219, 042, 086, 120,
|
|
|
|
|
];
|
2018-12-30 02:17:56 +00:00
|
|
|
|
|
|
|
|
static SECRET_KEY_BYTES: [u8; SECRET_KEY_LENGTH] = [
|
2022-12-08 07:39:48 +00:00
|
|
|
062, 070, 027, 163, 092, 182, 011, 003, 077, 234, 098, 004, 011, 127, 079, 228, 243, 187,
|
|
|
|
|
150, 073, 201, 137, 076, 022, 085, 251, 152, 002, 241, 042, 072, 054,
|
|
|
|
|
];
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
/// Signature with the above signing_key of a blank message.
|
2018-12-30 02:17:56 +00:00
|
|
|
static SIGNATURE_BYTES: [u8; SIGNATURE_LENGTH] = [
|
2022-12-08 07:39:48 +00:00
|
|
|
010, 126, 151, 143, 157, 064, 047, 001, 196, 140, 179, 058, 226, 152, 018, 102, 160, 123,
|
|
|
|
|
080, 016, 210, 086, 196, 028, 053, 231, 012, 157, 169, 019, 158, 063, 045, 154, 238, 007,
|
|
|
|
|
053, 185, 227, 229, 079, 108, 213, 080, 124, 252, 084, 167, 216, 085, 134, 144, 129, 149,
|
|
|
|
|
041, 081, 063, 120, 126, 100, 092, 059, 050, 011,
|
|
|
|
|
];
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
#[test]
|
2020-08-07 20:39:57 +00:00
|
|
|
fn serialize_deserialize_signature_bincode() {
|
|
|
|
|
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
|
|
|
|
|
let encoded_signature: Vec<u8> = bincode::serialize(&signature).unwrap();
|
|
|
|
|
let decoded_signature: Signature = bincode::deserialize(&encoded_signature).unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(signature, decoded_signature);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn serialize_deserialize_signature_json() {
|
2018-12-30 01:59:24 +00:00
|
|
|
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
|
2020-08-07 20:39:57 +00:00
|
|
|
let encoded_signature = serde_json::to_string(&signature).unwrap();
|
|
|
|
|
let decoded_signature: Signature = serde_json::from_str(&encoded_signature).unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
|
|
|
|
assert_eq!(signature, decoded_signature);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_deserialize_verifying_key_bincode() {
|
|
|
|
|
let verifying_key: VerifyingKey = VerifyingKey::from_bytes(&PUBLIC_KEY_BYTES).unwrap();
|
|
|
|
|
let encoded_verifying_key: Vec<u8> = bincode::serialize(&verifying_key).unwrap();
|
|
|
|
|
let decoded_verifying_key: VerifyingKey =
|
|
|
|
|
bincode::deserialize(&encoded_verifying_key).unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2022-12-08 07:39:48 +00:00
|
|
|
assert_eq!(
|
|
|
|
|
&PUBLIC_KEY_BYTES[..],
|
2022-12-18 06:24:58 +00:00
|
|
|
&encoded_verifying_key[encoded_verifying_key.len() - PUBLIC_KEY_LENGTH..]
|
2022-12-08 07:39:48 +00:00
|
|
|
);
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(verifying_key, decoded_verifying_key);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_deserialize_verifying_key_json() {
|
|
|
|
|
let verifying_key: VerifyingKey = VerifyingKey::from_bytes(&PUBLIC_KEY_BYTES).unwrap();
|
|
|
|
|
let encoded_verifying_key = serde_json::to_string(&verifying_key).unwrap();
|
|
|
|
|
let decoded_verifying_key: VerifyingKey =
|
|
|
|
|
serde_json::from_str(&encoded_verifying_key).unwrap();
|
2020-08-07 20:39:57 +00:00
|
|
|
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(verifying_key, decoded_verifying_key);
|
2020-08-07 20:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_deserialize_signing_key_bincode() {
|
|
|
|
|
let signing_key = SigningKey::from_bytes(&SECRET_KEY_BYTES);
|
|
|
|
|
let encoded_signing_key: Vec<u8> = bincode::serialize(&signing_key).unwrap();
|
|
|
|
|
let decoded_signing_key: SigningKey = bincode::deserialize(&encoded_signing_key).unwrap();
|
2018-12-30 01:59:24 +00:00
|
|
|
|
2020-08-07 20:39:57 +00:00
|
|
|
for i in 0..SECRET_KEY_LENGTH {
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(SECRET_KEY_BYTES[i], decoded_signing_key.to_bytes()[i]);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-07 20:39:57 +00:00
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_deserialize_signing_key_json() {
|
|
|
|
|
let signing_key = SigningKey::from_bytes(&SECRET_KEY_BYTES);
|
|
|
|
|
let encoded_signing_key = serde_json::to_string(&signing_key).unwrap();
|
|
|
|
|
let decoded_signing_key: SigningKey = serde_json::from_str(&encoded_signing_key).unwrap();
|
2020-08-07 20:39:57 +00:00
|
|
|
|
|
|
|
|
for i in 0..SECRET_KEY_LENGTH {
|
2022-12-18 06:24:58 +00:00
|
|
|
assert_eq!(SECRET_KEY_BYTES[i], decoded_signing_key.to_bytes()[i]);
|
2020-06-30 22:40:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_deserialize_signing_key_toml() {
|
2022-12-08 07:39:48 +00:00
|
|
|
let demo = Demo {
|
2022-12-18 06:24:58 +00:00
|
|
|
signing_key: SigningKey::from_bytes(&SECRET_KEY_BYTES),
|
2022-12-08 07:39:48 +00:00
|
|
|
};
|
2020-06-30 22:40:24 +00:00
|
|
|
|
|
|
|
|
println!("\n\nWrite to toml");
|
|
|
|
|
let demo_toml = toml::to_string(&demo).unwrap();
|
|
|
|
|
println!("{}", demo_toml);
|
|
|
|
|
let demo_toml_rebuild: Result<Demo, _> = toml::from_str(&demo_toml);
|
|
|
|
|
println!("{:?}", demo_toml_rebuild);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 01:59:24 +00:00
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_verifying_key_size() {
|
|
|
|
|
let verifying_key: VerifyingKey = VerifyingKey::from_bytes(&PUBLIC_KEY_BYTES).unwrap();
|
2022-12-08 07:39:48 +00:00
|
|
|
assert_eq!(
|
2022-12-18 06:24:58 +00:00
|
|
|
bincode::serialized_size(&verifying_key).unwrap() as usize,
|
2022-12-08 07:39:48 +00:00
|
|
|
BINCODE_INT_LENGTH + PUBLIC_KEY_LENGTH
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn serialize_signature_size() {
|
|
|
|
|
let signature: Signature = Signature::from_bytes(&SIGNATURE_BYTES).unwrap();
|
2022-12-08 07:39:48 +00:00
|
|
|
assert_eq!(
|
|
|
|
|
bincode::serialized_size(&signature).unwrap() as usize,
|
|
|
|
|
SIGNATURE_LENGTH
|
|
|
|
|
);
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-12-18 06:24:58 +00:00
|
|
|
fn serialize_signing_key_size() {
|
|
|
|
|
let signing_key = SigningKey::from_bytes(&SECRET_KEY_BYTES);
|
2022-10-15 19:04:03 +00:00
|
|
|
assert_eq!(
|
2022-12-18 06:24:58 +00:00
|
|
|
bincode::serialized_size(&signing_key).unwrap() as usize,
|
2022-10-15 19:04:03 +00:00
|
|
|
BINCODE_INT_LENGTH + SECRET_KEY_LENGTH
|
|
|
|
|
);
|
2020-08-07 20:39:57 +00:00
|
|
|
}
|
2018-12-30 01:59:24 +00:00
|
|
|
}
|