Skip to content

Commit

Permalink
multi-pairing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-lj committed Apr 16, 2024
1 parent bc2a0a7 commit 77199dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
11 changes: 10 additions & 1 deletion fastcrypto/src/groups/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,16 @@ impl Pairing for G1Element {
{
if points_g1.len() != points_g2.len() {
return Err(FastCryptoError::InvalidInput);
} else if points_g1.is_empty() {
}

let (points_g1, points_g2): (Vec<_>, Vec<_>) = points_g1
.iter()
.zip(points_g2.iter())
.filter(|(&g1, &g2)| g1 != G1Element::zero() && g2 != G2Element::zero())
.map(|(&g1, &g2)| (g1, g2))
.unzip();

if points_g1.is_empty() {
return Ok(<Self as Pairing>::Output::zero());
}

Expand Down
40 changes: 30 additions & 10 deletions fastcrypto/src/tests/bls12381_group_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,25 @@ fn test_pairing_and_hash_to_curve() {
let sig2 = e2 * sk2;
assert_eq!(pk2.pairing(&e2), G1Element::generator().pairing(&sig2));

assert_eq!(
G1Element::zero().pairing(&G2Element::zero()),
GTElement::zero()
);
assert_eq!(
G1Element::zero().pairing(&G2Element::generator()),
GTElement::zero()
);
assert_eq!(
G1Element::generator().pairing(&G2Element::zero()),
GTElement::zero()
);

// next should not fail
let _ = G1Element::hash_to_group_element(&[]);
let _ = G2Element::hash_to_group_element(&[]);
let _ = G1Element::hash_to_group_element(&[1]);
let _ = G2Element::hash_to_group_element(&[1]);

// Test multi-pairing
assert!(G1Element::multi_pairing(&[], &[pk1]).is_err());
assert_eq!(
Expand All @@ -343,25 +362,26 @@ fn test_pairing_and_hash_to_curve() {
G1Element::multi_pairing(&[e1, pk2], &[pk1, e2]).unwrap(),
e1.pairing(&pk1) + pk2.pairing(&e2)
);

assert_eq!(
G1Element::zero().pairing(&G2Element::zero()),
G1Element::multi_pairing(&[G1Element::zero()], &[G2Element::zero()]).unwrap(),
GTElement::zero()
);
assert_eq!(
G1Element::zero().pairing(&G2Element::generator()),
G1Element::multi_pairing(
&[G1Element::zero(), G1Element::zero()],
&[G2Element::zero(), G2Element::zero()]
)
.unwrap(),
GTElement::zero()
);
assert_eq!(
G1Element::generator().pairing(&G2Element::zero()),
G1Element::multi_pairing(&[G1Element::generator()], &[G2Element::zero()]).unwrap(),
GTElement::zero()
);
assert_eq!(
G1Element::multi_pairing(&[G1Element::zero()], &[G2Element::generator()]).unwrap(),
GTElement::zero()
);

// next should not fail
let _ = G1Element::hash_to_group_element(&[]);
let _ = G2Element::hash_to_group_element(&[]);
let _ = G1Element::hash_to_group_element(&[1]);
let _ = G2Element::hash_to_group_element(&[1]);
}

#[test]
Expand Down

0 comments on commit 77199dd

Please sign in to comment.