Skip to content

Commit

Permalink
Refactor contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 2, 2024
1 parent eeaaf8d commit be1039e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 32 deletions.
26 changes: 20 additions & 6 deletions contracts/crypto-verify/src/bls12_381.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use cosmwasm_std::{Api, HashFunction, StdResult};
use cosmwasm_std::{
Api, HashFunction, StdResult, BLS12_381_G1_GENERATOR_COMPRESSED,
BLS12_381_G2_GENERATOR_COMPRESSED,
};

pub fn verify(
pub fn verify_g1(
api: &dyn Api,
p: &[u8],
q: &[u8],
r: &[u8],
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(p, q, r, &s)
api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR_COMPRESSED, signature, pubkey, &s)
.map_err(Into::into)
}

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_COMPRESSED, &s, pubkey)
.map_err(Into::into)
}
39 changes: 31 additions & 8 deletions contracts/crypto-verify/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,22 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
&r,
&s,
)?),
QueryMsg::VerifyBls12PairingEquality { p, q, r, msg, dst } => {
to_json_binary(&query_verify_bls12_pairing(deps, &p, &q, &r, &msg, &dst)?)
}
QueryMsg::VerifyBls12PairingEqualityG1 {
signature,
pubkey,
msg,
dst,
} => to_json_binary(&query_verify_bls12_pairing_g1(
deps, &signature, &pubkey, &msg, &dst,
)?),
QueryMsg::VerifyBls12PairingEqualityG2 {
signature,
pubkey,
msg,
dst,
} => to_json_binary(&query_verify_bls12_pairing_g2(
deps, &signature, &pubkey, &msg, &dst,
)?),
}
}

Expand Down Expand Up @@ -285,15 +298,25 @@ pub fn query_list_verifications(deps: Deps) -> StdResult<ListVerificationsRespon
})
}

pub fn query_verify_bls12_pairing(
pub fn query_verify_bls12_pairing_g1(
deps: Deps,
p: &[u8],
q: &[u8],
r: &[u8],
signature: &[u8],
pubkey: &[u8],
msg: &[u8],
dst: &[u8],
) -> StdResult<VerifyResponse> {
let verifies = crate::bls12_381::verify_g1(deps.api, signature, pubkey, msg, dst)?;
Ok(VerifyResponse { verifies })
}

pub fn query_verify_bls12_pairing_g2(
deps: Deps,
signature: &[u8],
pubkey: &[u8],
msg: &[u8],
dst: &[u8],
) -> StdResult<VerifyResponse> {
let verifies = crate::bls12_381::verify(deps.api, p, q, r, msg, dst)?;
let verifies = crate::bls12_381::verify_g2(deps.api, signature, pubkey, msg, dst)?;
Ok(VerifyResponse { verifies })
}

Expand Down
26 changes: 18 additions & 8 deletions contracts/crypto-verify/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,25 @@ pub enum QueryMsg {
/// The representation of this component is a big-endian encoded 256bit integer
s: Binary,
},
/// BLS12-381 pairing equality verification
/// BLS12-381 pairing equality verification (where the key is an element of G1)
#[returns(VerifyResponse)]
VerifyBls12PairingEquality {
/// The point $p$ in its compressed format (element of G1)
p: Binary,
/// The point $q$ in its compressed format (element of G2)
q: Binary,
/// The point $r$ in its compressed format (element of G1)
r: Binary,
VerifyBls12PairingEqualityG1 {
/// The signature point in its compressed format (element of G2)
signature: Binary,
/// The public key point in its compressed format (element of G1)
pubkey: Binary,
/// The message that should be verified
msg: Binary,
/// The `dst` component used to hash the message to the curve
dst: Binary,
},
/// BLS12-381 pairing equality verification (where the key is an element of G2)
#[returns(VerifyResponse)]
VerifyBls12PairingEqualityG2 {
/// The signature point in its compressed format (element of G1)
signature: Binary,
/// The public key point in its compressed format (element of G2)
pubkey: Binary,
/// The message that should be verified
msg: Binary,
/// The `dst` component used to hash the message to the curve
Expand Down
44 changes: 34 additions & 10 deletions contracts/crypto-verify/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! 5. Anywhere you see query(deps.as_ref(), ...) you must replace it with query(&mut deps, ...)
//! (Use cosmwasm_vm::testing::{init, execute, query}, instead of the contract variants).

use cosmwasm_std::{Binary, Response, Uint128, BLS12_381_G1_GENERATOR_COMPRESSED};
use cosmwasm_std::{Binary, Response, Uint128};
use cosmwasm_vm::testing::{
instantiate, mock_env, mock_info, mock_instance, query, MockApi, MockQuerier, MockStorage,
};
Expand Down Expand Up @@ -76,11 +76,15 @@ const WEBAUTHN_SIGNATURE_S: &[u8] =

// See https://github.com/drand/kyber-bls12381/issues/22 and
// https://github.com/drand/drand/pull/1249
const DOMAIN_HASH_TO_G1: &[u8] = b"BLS_SIG_BLS12381G1_XMD:SHA-256_SSWU_RO_NUL_";
const DOMAIN_HASH_TO_G2: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_NUL_";

/// Public key League of Entropy Mainnet (curl -sS https://drand.cloudflare.com/info)
const PK_LEO_MAINNET: [u8; 48] = hex!("868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31");

// Tests from quicknet (https://api.drand.sh/52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971/info)
const PK_QUICKNET: [u8; 96] = hex!("83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a");

fn build_drand_message(round: u64, previous_signature: &[u8]) -> Vec<u8> {
Sha256::new()
.chain_update(previous_signature)
Expand All @@ -106,7 +110,7 @@ fn instantiate_works() {
}

#[test]
fn bls12_381_verifies() {
fn bls12_381_verifies_g1() {
let mut deps = setup();

let previous_signature = hex::decode("a609e19a03c2fcc559e8dae14900aaefe517cb55c840f6e69bc8e4f66c8d18e8a609685d9917efbfb0c37f058c2de88f13d297c7e19e0ab24813079efe57a182554ff054c7638153f9b26a60e7111f71a0ff63d9571704905d3ca6df0b031747").unwrap();
Expand All @@ -115,10 +119,9 @@ fn bls12_381_verifies() {

let msg = build_drand_message(round, &previous_signature);

let verify_msg = QueryMsg::VerifyBls12PairingEquality {
p: BLS12_381_G1_GENERATOR_COMPRESSED.into(),
q: signature.into(),
r: PK_LEO_MAINNET.into(),
let verify_msg = QueryMsg::VerifyBls12PairingEqualityG1 {
signature: signature.into(),
pubkey: PK_LEO_MAINNET.into(),
msg: msg.into(),
dst: DOMAIN_HASH_TO_G2.into(),
};
Expand All @@ -129,6 +132,28 @@ fn bls12_381_verifies() {
assert_eq!(res, VerifyResponse { verifies: true });
}

#[test]
fn bls12_381_verifies_g2() {
let mut deps = setup();

let signature = hex::decode("b75c69d0b72a5d906e854e808ba7e2accb1542ac355ae486d591aa9d43765482e26cd02df835d3546d23c4b13e0dfc92").unwrap();
let round: u64 = 123;

let msg = build_drand_message(round, b"");

let verify_msg = QueryMsg::VerifyBls12PairingEqualityG2 {
signature: signature.into(),
pubkey: PK_QUICKNET.into(),
msg: msg.into(),
dst: DOMAIN_HASH_TO_G1.into(),
};

let raw = query(&mut deps, mock_env(), verify_msg).unwrap();
let res: VerifyResponse = from_slice(&raw, DESERIALIZATION_LIMIT).unwrap();

assert_eq!(res, VerifyResponse { verifies: true });
}

#[test]
fn bls12_381_errors() {
let mut deps = setup();
Expand All @@ -141,10 +166,9 @@ fn bls12_381_errors() {

let msg = build_drand_message(round, &previous_signature);

let verify_msg = QueryMsg::VerifyBls12PairingEquality {
p: BLS12_381_G1_GENERATOR_COMPRESSED.into(),
q: signature.into(),
r: PK_LEO_MAINNET.into(),
let verify_msg = QueryMsg::VerifyBls12PairingEqualityG1 {
signature: signature.into(),
pubkey: PK_LEO_MAINNET.into(),
msg: msg.into(),
dst: DOMAIN_HASH_TO_G2.into(),
};
Expand Down

0 comments on commit be1039e

Please sign in to comment.