Skip to content

Commit

Permalink
JEQB paritytech#195: Verify returning false (#27)
Browse files Browse the repository at this point in the history
* Change sign and verify mock and test

* Uncomment tests
  • Loading branch information
Wolmin authored Jan 26, 2023
1 parent c14400a commit a3a5a85
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
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

0 comments on commit a3a5a85

Please sign in to comment.