This commit is contained in:
Matthew LeVan 2023-12-18 15:03:44 -05:00
parent 228ab7f5b7
commit 7878b775ab

View File

@ -1,7 +1,5 @@
use sha2::{Digest, Sha512};
use curve25519_dalek::edwards::CompressedEdwardsY;
use ed25519_dalek::{SigningKey, VerifyingKey, Signer};
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use x25519_dalek::{PublicKey, StaticSecret};
/// Generate a public key from the given seed and write it to the given output slice.
@ -33,9 +31,21 @@ pub fn ac_ed_sign(msg: &[u8], seed: &[u8; 32], out: &mut [u8; 64]) {
*out = signature.to_bytes();
}
pub fn ac_ed_veri(msg: &[u8], public: &[u8; 32], signature: &[u8; 64]) -> bool {
if let Ok(verifying_key) = VerifyingKey::from_bytes(public) {
verifying_key
.verify(msg, &Signature::from_bytes(signature))
.is_ok()
} else {
false
}
}
#[cfg(test)]
mod tests {
use super::ac_ed_puck;
use ibig::ubig;
use super::{ac_ed_puck, ac_ed_shar, ac_ed_sign, ac_ed_veri};
#[test]
fn test_ed_puck() {
@ -50,18 +60,61 @@ mod tests {
]
);
}
// #[test]
// fn test_ed_shar() {
// }
#[test]
fn test_ed_sign() {
let msg = b"Ares has long exerted a pull on the human imagination.";
let seed_src = &ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb)
.to_le_bytes();
let mut seed: [u8; 32] = [0; 32];
seed.copy_from_slice(seed_src);
let mut signature: [u8; 64] = [0; 64];
ac_ed_sign(msg, &seed, &mut signature);
// from urcrypt_ed_sign()
let constant_signature = [
112, 132, 235, 218, 21, 180, 5, 48, 145, 211, 212, 153, 255, 229, 198, 165, 64, 140,
246, 27, 0, 97, 185, 143, 180, 10, 38, 68, 200, 71, 231, 108, 141, 26, 97, 207, 199,
204, 0, 123, 250, 161, 182, 92, 48, 116, 144, 42, 204, 6, 199, 162, 10, 66, 173, 185,
155, 96, 240, 56, 224, 187, 160, 1,
];
assert_eq!(signature, constant_signature);
}
#[test]
fn test_ed_veri() {
let msg = b"The erratically moving red star in the sky was seen as sinister or violent by the ancients.";
let mut seed = [42; 32];
let mut public_key: [u8; 32] = [0; 32];
ac_ed_puck(&mut seed, &mut public_key);
let mut signature: [u8; 64] = [0; 64];
ac_ed_sign(msg, &seed, &mut signature);
let valid = ac_ed_veri(msg, &public_key, &signature);
assert!(valid);
}
}
#[cfg(test)]
#[cfg(feature = "test_vs_urcrypt")]
mod ucrypt_tests {
use super::{ac_ed_puck, ac_ed_shar, ac_ed_sign};
use super::{ac_ed_puck, ac_ed_shar, ac_ed_sign, ac_ed_veri};
use ibig::ubig;
use urcrypt_sys::{urcrypt_ed_puck, urcrypt_ed_shar, urcrypt_ed_sign};
use urcrypt_sys::{urcrypt_ed_puck, urcrypt_ed_shar, urcrypt_ed_sign, urcrypt_ed_veri};
#[test]
fn test_ed_puck() {
let mut seed: [u8; 32] = [0; 32];
let mut seed: [u8; 32] = [42; 32];
let mut uc_out: [u8; 32] = [0; 32];
unsafe { urcrypt_ed_puck(seed.as_ptr(), uc_out.as_mut_ptr()) };
@ -96,7 +149,9 @@ mod ucrypt_tests {
#[test]
fn test_ed_sign() {
let msg = b"test";
// let msg = b"The Greeks identified it with Ares, the god of war.";
let msg = b"Ares has long exerted a pull on the human imagination.";
let seed_src = &ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb)
.to_le_bytes();
let mut seed: [u8; 32] = [0; 32];
@ -105,9 +160,40 @@ mod ucrypt_tests {
let mut uc_out: [u8; 64] = [0; 64];
unsafe { urcrypt_ed_sign(msg.as_ptr(), msg.len(), seed.as_ptr(), uc_out.as_mut_ptr()) };
eprintln!("uc_out: {:x?}", uc_out);
let mut ac_out: [u8; 64] = [0; 64];
ac_ed_sign(msg, &seed, &mut ac_out);
assert_eq!(ac_out, uc_out);
}
#[test]
fn test_ed_veri() {
let msg = b"The Babylonians named it after Nergal, god of the underworld.";
let seed_src = &ubig!(_0x4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb)
.to_le_bytes();
let mut seed: [u8; 32] = [0; 32];
seed.copy_from_slice(seed_src);
let mut public_key: [u8; 32] = [0; 32];
ac_ed_puck(&mut seed, &mut public_key);
let mut signature: [u8; 64] = [0; 64];
ac_ed_sign(msg, &seed, &mut signature);
let uc_out = unsafe {
urcrypt_ed_veri(
msg.as_ptr(),
msg.len(),
public_key.as_ptr(),
signature.as_ptr(),
)
};
let ac_out = ac_ed_veri(msg, &public_key, &signature);
assert_eq!(ac_out, uc_out);
}
}