Skip to content

Commit

Permalink
Add docs and clarifications to verify_g1/verify_g2
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 authored and aumetra committed May 10, 2024
1 parent 34afd26 commit bcfae4f
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions contracts/crypto-verify/src/bls12_381.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
use cosmwasm_std::{Api, HashFunction, StdResult, BLS12_381_G1_GENERATOR, BLS12_381_G2_GENERATOR};

/// Signature verification with public key in G1 (e.g. drand classic mainnet, ETH2 block headers).
///
/// See https://hackmd.io/@benjaminion/bls12-381#Verification.
pub fn verify_g1(
api: &dyn Api,
signature: &[u8],
pubkey: &[u8],
msg: &[u8],
dst: &[u8],
) -> StdResult<bool> {
let s = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?;
api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, signature, pubkey, &s)
// The H(m) from the docs
let msg_hash = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?;
api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR, signature, pubkey, &msg_hash)
.map_err(Into::into)
}

/// Signature verification with public key in G2 (e.g. drand Quicknet)
///
/// See https://hackmd.io/@benjaminion/bls12-381#Verification in combination with
/// https://hackmd.io/@benjaminion/bls12-381#Swapping-G1-and-G2.
pub fn verify_g2(
api: &dyn Api,
signature: &[u8],
pubkey: &[u8],
msg: &[u8],
dst: &[u8],
) -> StdResult<bool> {
let s = api.bls12_381_hash_to_g1(HashFunction::Sha256, msg, dst)?;
api.bls12_381_pairing_equality(signature, &BLS12_381_G2_GENERATOR, &s, pubkey)
// The H(m) from the docs
let msg_hash = api.bls12_381_hash_to_g1(HashFunction::Sha256, msg, dst)?;
api.bls12_381_pairing_equality(signature, &BLS12_381_G2_GENERATOR, &msg_hash, pubkey)
.map_err(Into::into)
}

0 comments on commit bcfae4f

Please sign in to comment.