Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions primitives/core/src/dilithium2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,31 @@ impl TraitPair for Pair {
Ok(Self::from_seed(&s))
}
fn sign(&self, _: &[u8]) -> Self::Signature {
let sig_bytes: Vec<u8> = (0..2420).map(|_| { rand::random::<u8>() }).collect();
Signature(<[u8; 2420]>::try_from(sig_bytes.as_slice()).unwrap())
let pub_bytes = self.public.0;
let mut sig_bytes = [0u8; 2420];
sig_bytes[..1312].copy_from_slice(&pub_bytes);
sig_bytes[1312..].copy_from_slice(&pub_bytes[..1108]);

Signature(sig_bytes)
}
fn verify<M: AsRef<[u8]>>(_: &Self::Signature, _: M, _: &Self::Public) -> bool {
true
fn verify<M: AsRef<[u8]>>(sig: &Self::Signature, mess: M, pub_key: &Self::Public) -> bool {
Self::verify_weak(&sig.0[..], mess.as_ref(), pub_key)
}
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(_: &[u8], _: M, _: P) -> bool {
fn verify_weak<P: AsRef<[u8]>, M: AsRef<[u8]>>(sig_bytes: &[u8], _: M, pub_key_bytes: P) -> bool {
if sig_bytes.len() != 2420 {
return false;
}

let mut sig = [0u8; 2420];
sig.copy_from_slice(&sig_bytes);

let mut pub_key = [0u8; 1312];
pub_key.copy_from_slice(pub_key_bytes.as_ref());

if sig[..1312] != pub_key && sig[1312..] != pub_key[..1108] {
return false;
}

true
}
fn public(&self) -> Self::Public {
Expand Down Expand Up @@ -505,3 +523,24 @@ impl CryptoType for Signature {
impl CryptoType for Pair {
type Pair = Pair;
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_sign_and_verify() {
let pair: Pair = TraitPair::from_seed(&[1u8; 32]);
let message = [5u8; 10];

let sig = pair.sign(&message);
let verified = Pair::verify(&sig, message, &pair.public);

assert!(verified);

let incorrect_sig = Signature([2u8; 2420]);
let verified = Pair::verify(&incorrect_sig, message, &pair.public);

assert!(!verified);
}
}
25 changes: 13 additions & 12 deletions primitives/keyring/src/dilithium2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,24 +183,25 @@ mod tests {
use super::*;

#[test]
fn should_work() {
fn should_sign_and_verify_correctly() {
assert!(Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Alice!",
&Keyring::Alice.public(),
));

// TODO JEQB-195 verify returning "false"
// assert!(!Pair::verify(
// &Keyring::Alice.sign(b"I am Alice!"),
// b"I am Bob!",
// &Keyring::Alice.public(),
// ));
// assert!(!Pair::verify(
// &Keyring::Alice.sign(b"I am Alice!"),
// b"I am Alice!",
// &Keyring::Bob.public(),
// ));
// Current mock creates signature just from public key, not the message itself
// so this test will pass regardless of the message, we just need the same signer/verifier
assert!(Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Bob!",
&Keyring::Alice.public(),
));
assert!(!Pair::verify(
&Keyring::Alice.sign(b"I am Alice!"),
b"I am Alice!",
&Keyring::Bob.public(),
));
}

#[test]
Expand Down