|
1 | 1 | use cosmwasm_std::{Api, HashFunction, StdResult, BLS12_381_G1_GENERATOR, BLS12_381_G2_GENERATOR};
|
2 | 2 |
|
| 3 | +/// Signature verification with public key in G1 (e.g. drand classic mainnet, ETH2 block headers). |
| 4 | +/// |
| 5 | +/// See https://hackmd.io/@benjaminion/bls12-381#Verification. |
3 | 6 | pub fn verify_g1(
|
4 | 7 | api: &dyn Api,
|
5 | 8 | signature: &[u8],
|
6 | 9 | pubkey: &[u8],
|
7 | 10 | msg: &[u8],
|
8 | 11 | dst: &[u8],
|
9 | 12 | ) -> StdResult<bool> {
|
10 |
| - let s = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?; |
11 |
| - api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, signature, pubkey, &s) |
| 13 | + // The H(m) from the docs |
| 14 | + let msg_hash = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?; |
| 15 | + api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, signature, pubkey, &msg_hash) |
12 | 16 | .map_err(Into::into)
|
13 | 17 | }
|
14 | 18 |
|
| 19 | +/// Signature verification with public key in G2 (e.g. drand Quicknet) |
| 20 | +/// |
| 21 | +/// See https://hackmd.io/@benjaminion/bls12-381#Verification in combination with |
| 22 | +/// https://hackmd.io/@benjaminion/bls12-381#Swapping-G1-and-G2. |
15 | 23 | pub fn verify_g2(
|
16 | 24 | api: &dyn Api,
|
17 | 25 | signature: &[u8],
|
18 | 26 | pubkey: &[u8],
|
19 | 27 | msg: &[u8],
|
20 | 28 | dst: &[u8],
|
21 | 29 | ) -> StdResult<bool> {
|
22 |
| - let s = api.bls12_381_hash_to_g1(HashFunction::Sha256, msg, dst)?; |
23 |
| - api.bls12_381_pairing_equality(signature, &BLS12_381_G2_GENERATOR, &s, pubkey) |
| 30 | + // The H(m) from the docs |
| 31 | + let msg_hash = api.bls12_381_hash_to_g1(HashFunction::Sha256, msg, dst)?; |
| 32 | + api.bls12_381_pairing_equality(signature, &BLS12_381_G2_GENERATOR, &msg_hash, pubkey) |
24 | 33 | .map_err(Into::into)
|
25 | 34 | }
|
0 commit comments