From bcfae4f1d1a419c85c6a4c9caa523559c11ad89d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 7 May 2024 12:34:06 +0200 Subject: [PATCH] Add docs and clarifications to verify_g1/verify_g2 --- contracts/crypto-verify/src/bls12_381.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/contracts/crypto-verify/src/bls12_381.rs b/contracts/crypto-verify/src/bls12_381.rs index 4063f553d4..93d7120ffe 100644 --- a/contracts/crypto-verify/src/bls12_381.rs +++ b/contracts/crypto-verify/src/bls12_381.rs @@ -1,5 +1,8 @@ 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], @@ -7,11 +10,16 @@ pub fn verify_g1( msg: &[u8], dst: &[u8], ) -> StdResult { - 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], @@ -19,7 +27,8 @@ pub fn verify_g2( msg: &[u8], dst: &[u8], ) -> StdResult { - 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) }