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 49291f1
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 57 deletions.
85 changes: 72 additions & 13 deletions contracts/crypto-verify/schema/crypto-verify.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,20 +388,19 @@
"additionalProperties": false
},
{
"description": "BLS12-381 pairing equality verification",
"description": "BLS12-381 pairing equality verification (where the key is an element of G1)",
"type": "object",
"required": [
"verify_bls12_pairing_equality"
"verify_bls12_pairing_equality_g1"
],
"properties": {
"verify_bls12_pairing_equality": {
"verify_bls12_pairing_equality_g1": {
"type": "object",
"required": [
"dst",
"msg",
"p",
"q",
"r"
"pubkey",
"signature"
],
"properties": {
"dst": {
Expand All @@ -420,24 +419,70 @@
}
]
},
"p": {
"description": "The point $p$ in its compressed format (element of G1)",
"pubkey": {
"description": "The public key point in its compressed format (element of G1)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"q": {
"description": "The point $q$ in its compressed format (element of G2)",
"signature": {
"description": "The signature point in its compressed format (element of G2)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "BLS12-381 pairing equality verification (where the key is an element of G2)",
"type": "object",
"required": [
"verify_bls12_pairing_equality_g2"
],
"properties": {
"verify_bls12_pairing_equality_g2": {
"type": "object",
"required": [
"dst",
"msg",
"pubkey",
"signature"
],
"properties": {
"dst": {
"description": "The `dst` component used to hash the message to the curve",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"r": {
"description": "The point $r$ in its compressed format (element of G1)",
"msg": {
"description": "The message that should be verified",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"pubkey": {
"description": "The public key point in its compressed format (element of G2)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"signature": {
"description": "The signature point in its compressed format (element of G1)",
"allOf": [
{
"$ref": "#/definitions/Binary"
Expand Down Expand Up @@ -482,7 +527,21 @@
},
"additionalProperties": false
},
"verify_bls12_pairing_equality": {
"verify_bls12_pairing_equality_g1": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "VerifyResponse",
"type": "object",
"required": [
"verifies"
],
"properties": {
"verifies": {
"type": "boolean"
}
},
"additionalProperties": false
},
"verify_bls12_pairing_equality_g2": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "VerifyResponse",
"type": "object",
Expand Down
69 changes: 57 additions & 12 deletions contracts/crypto-verify/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -377,20 +377,19 @@
"additionalProperties": false
},
{
"description": "BLS12-381 pairing equality verification",
"description": "BLS12-381 pairing equality verification (where the key is an element of G1)",
"type": "object",
"required": [
"verify_bls12_pairing_equality"
"verify_bls12_pairing_equality_g1"
],
"properties": {
"verify_bls12_pairing_equality": {
"verify_bls12_pairing_equality_g1": {
"type": "object",
"required": [
"dst",
"msg",
"p",
"q",
"r"
"pubkey",
"signature"
],
"properties": {
"dst": {
Expand All @@ -409,24 +408,70 @@
}
]
},
"p": {
"description": "The point $p$ in its compressed format (element of G1)",
"pubkey": {
"description": "The public key point in its compressed format (element of G1)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"signature": {
"description": "The signature point in its compressed format (element of G2)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "BLS12-381 pairing equality verification (where the key is an element of G2)",
"type": "object",
"required": [
"verify_bls12_pairing_equality_g2"
],
"properties": {
"verify_bls12_pairing_equality_g2": {
"type": "object",
"required": [
"dst",
"msg",
"pubkey",
"signature"
],
"properties": {
"dst": {
"description": "The `dst` component used to hash the message to the curve",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"msg": {
"description": "The message that should be verified",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"q": {
"description": "The point $q$ in its compressed format (element of G2)",
"pubkey": {
"description": "The public key point in its compressed format (element of G2)",
"allOf": [
{
"$ref": "#/definitions/Binary"
}
]
},
"r": {
"description": "The point $r$ in its compressed format (element of G1)",
"signature": {
"description": "The signature point in its compressed format (element of G1)",
"allOf": [
{
"$ref": "#/definitions/Binary"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "VerifyResponse",
"type": "object",
"required": [
"verifies"
],
"properties": {
"verifies": {
"type": "boolean"
}
},
"additionalProperties": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "VerifyResponse",
"type": "object",
"required": [
"verifies"
],
"properties": {
"verifies": {
"type": "boolean"
}
},
"additionalProperties": false
}
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
Loading

0 comments on commit 49291f1

Please sign in to comment.