Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding further deserialization tests taken from https://github.com/ethereum/bls12-381-tests #164

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
101 changes: 101 additions & 0 deletions crypto/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod blst;
mod both;

use crate::{CeremonyError, F, G1, G2};
use hex_literal::hex;
pub use secrecy::Secret;

#[cfg(feature = "arkworks")]
Expand Down Expand Up @@ -116,13 +117,113 @@ pub mod tests {
let g1 = G1([0u8; 48]);
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_not_in_G1
let g1 = G1(hex!("8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_not_in_curve
let g1 = G1(hex!("8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//Exactly the modulus, q
let g1 = G1(hex!("9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//One more than the modulus, q
let g1 = G1(hex!("9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_infinity_with_true_b_flag
let g1 = G1(hex!("c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_infinity_with_false_b_flag
let g1 = G1(hex!("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_with_wrong_c_flag
let g1 = G1(hex!("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_with_b_flag_and_x_nonzero
let g1 = G1(hex!("c123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());

//deserialization_fails_with_b_flag_and_a_flag_true
let g1 = G1(hex!("e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g1(&[g1]).is_err());
assert!(Arkworks::validate_g1(&[g1]).is_err());
}

#[test]
fn test_validate_g2() {
let g2 = G2([0u8; 96]);
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//xRe is exactly the modulus, q, xIm is zero
let g2 = G2(hex!("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//xIm is exactly the modulus, q, xRe is zero
let g2 = G2(hex!("9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//xRe is the modulus plus 1, xIm is zero
let g2 = G2(hex!("8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//xIm is the modulus plus 1, xRe is zero
let g2 = G2(hex!("9a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaac000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_not_in_G2
let g2 = G2(hex!("8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_not_in_curve
let g2 = G2(hex!("8123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde0"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_infinity_with_true_b_flag
let g2 = G2(hex!("c01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_infinity_with_false_b_flag
let g2 = G2(hex!("800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_with_wrong_c_flag
let g2 = G2(hex!("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_with_b_flag_and_x_nonzero
let g2 = G2(hex!("c123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());

//deserialization_fails_with_b_flag_and_a_flag_true
let g2 = G2(hex!("e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
assert!(BLST::validate_g2(&[g2]).is_err());
assert!(Arkworks::validate_g2(&[g2]).is_err());
}
}

Expand Down