diff --git a/crypto/src/engine/mod.rs b/crypto/src/engine/mod.rs index f61fd50..58fd35a 100644 --- a/crypto/src/engine/mod.rs +++ b/crypto/src/engine/mod.rs @@ -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")] @@ -116,6 +117,51 @@ 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] @@ -123,6 +169,61 @@ pub mod tests { 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()); } }