Skip to content

Commit

Permalink
ecdsa: add more secp256k1 wycheproof cases
Browse files Browse the repository at this point in the history
test data is generated from ecdsa_secp256k1_sha256_p1363_test.json
with following command:
wycheproof2blb ~/projects/wycheproof secp256k1-p1316 256 ./wycheproof-p1316.blb ./desc-p1316.txt
  • Loading branch information
XuJiandong committed Jul 26, 2024
1 parent 893f5cc commit 9340502
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions k256/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ num-traits = "0.2"
proptest = "1.5"
rand_core = { version = "0.6", features = ["getrandom"] }
sha3 = { version = "=0.11.0-pre.3", default-features = false }
hex = "0.4.3"

[features]
default = ["arithmetic", "ecdsa", "pkcs8", "precomputed-tables", "schnorr", "std"]
Expand Down
67 changes: 43 additions & 24 deletions k256/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ mod tests {
msg: &[u8],
sig: &[u8],
pass: bool,
p1363_sig: bool,
) -> Option<&'static str> {
let x = element_from_padded_slice::<Secp256k1>(wx);
let y = element_from_padded_slice::<Secp256k1>(wy);
Expand All @@ -302,10 +303,18 @@ mod tests {
let verifying_key =
ecdsa_core::VerifyingKey::from_encoded_point(&q_encoded).unwrap();

let sig = match Signature::<Secp256k1>::from_der(sig) {
Ok(s) => s.normalize_s(),
Err(_) if !pass => return None,
Err(_) => return Some("failed to parse signature ASN.1"),
let sig = if p1363_sig {
match Signature::<Secp256k1>::from_slice(sig) {
Ok(s) => s.normalize_s(),
Err(_) if !pass => return None,
Err(_) => return Some("failed to parse signature P1363"),
}
} else {
match Signature::<Secp256k1>::from_der(sig) {
Ok(s) => s.normalize_s(),
Err(_) if !pass => return None,
Err(_) => return Some("failed to parse signature ASN.1"),
}
};

match verifying_key.verify(msg, &sig) {
Expand All @@ -316,28 +325,38 @@ mod tests {
}
}

let data = include_bytes!(concat!("test_vectors/data/", "wycheproof", ".blb"));

for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() {
let [wx, wy, msg, sig, status] = row.unwrap();
let pass = match status[0] {
0 => false,
1 => true,
_ => panic!("invalid value for pass flag"),
};
if let Some(desc) = run_test(wx, wy, msg, sig, pass) {
panic!(
"\n\
Failed test №{}: {}\n\
wx:\t{:?}\n\
wy:\t{:?}\n\
msg:\t{:?}\n\
sig:\t{:?}\n\
pass:\t{}\n",
i, desc, wx, wy, msg, sig, pass,
);
fn run(data: &[u8], p1363_sig: bool) {
for (i, row) in Blob5Iterator::new(data).unwrap().enumerate() {
let [wx, wy, msg, sig, status] = row.unwrap();
let pass = match status[0] {
0 => false,
1 => true,
_ => panic!("invalid value for pass flag"),
};
if let Some(desc) = run_test(wx, wy, msg, sig, pass, p1363_sig) {
panic!(
"\n\
Failed test №{}: {}\n\
wx:\t{:?}\n\
wy:\t{:?}\n\
msg:\t{:?}\n\
sig:\t{:?}\n\
pass:\t{}\n",
i,
desc,
hex::encode(wx),
hex::encode(wy),
hex::encode(msg),
hex::encode(sig),
pass,
);
}
}
}
let data = include_bytes!(concat!("test_vectors/data/", "wycheproof", ".blb"));
run(data, false);
let data2 = include_bytes!(concat!("test_vectors/data/", "wycheproof-p1316", ".blb"));
run(data2, true);
}
}
}
Binary file added k256/src/test_vectors/data/wycheproof-p1316.blb
Binary file not shown.

0 comments on commit 9340502

Please sign in to comment.