Skip to content

Commit 97cfbbb

Browse files
committed
Refactor contract tests
1 parent 902f399 commit 97cfbbb

File tree

8 files changed

+260
-57
lines changed

8 files changed

+260
-57
lines changed

contracts/crypto-verify/schema/crypto-verify.json

Lines changed: 72 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,20 +388,19 @@
388388
"additionalProperties": false
389389
},
390390
{
391-
"description": "BLS12-381 pairing equality verification",
391+
"description": "BLS12-381 pairing equality verification (where the key is an element of G1)",
392392
"type": "object",
393393
"required": [
394-
"verify_bls12_pairing_equality"
394+
"verify_bls12_pairing_equality_g1"
395395
],
396396
"properties": {
397-
"verify_bls12_pairing_equality": {
397+
"verify_bls12_pairing_equality_g1": {
398398
"type": "object",
399399
"required": [
400400
"dst",
401401
"msg",
402-
"p",
403-
"q",
404-
"r"
402+
"pubkey",
403+
"signature"
405404
],
406405
"properties": {
407406
"dst": {
@@ -420,24 +419,70 @@
420419
}
421420
]
422421
},
423-
"p": {
424-
"description": "The point $p$ in its compressed format (element of G1)",
422+
"pubkey": {
423+
"description": "The public key point in its compressed format (element of G1)",
425424
"allOf": [
426425
{
427426
"$ref": "#/definitions/Binary"
428427
}
429428
]
430429
},
431-
"q": {
432-
"description": "The point $q$ in its compressed format (element of G2)",
430+
"signature": {
431+
"description": "The signature point in its compressed format (element of G2)",
432+
"allOf": [
433+
{
434+
"$ref": "#/definitions/Binary"
435+
}
436+
]
437+
}
438+
},
439+
"additionalProperties": false
440+
}
441+
},
442+
"additionalProperties": false
443+
},
444+
{
445+
"description": "BLS12-381 pairing equality verification (where the key is an element of G2)",
446+
"type": "object",
447+
"required": [
448+
"verify_bls12_pairing_equality_g2"
449+
],
450+
"properties": {
451+
"verify_bls12_pairing_equality_g2": {
452+
"type": "object",
453+
"required": [
454+
"dst",
455+
"msg",
456+
"pubkey",
457+
"signature"
458+
],
459+
"properties": {
460+
"dst": {
461+
"description": "The `dst` component used to hash the message to the curve",
433462
"allOf": [
434463
{
435464
"$ref": "#/definitions/Binary"
436465
}
437466
]
438467
},
439-
"r": {
440-
"description": "The point $r$ in its compressed format (element of G1)",
468+
"msg": {
469+
"description": "The message that should be verified",
470+
"allOf": [
471+
{
472+
"$ref": "#/definitions/Binary"
473+
}
474+
]
475+
},
476+
"pubkey": {
477+
"description": "The public key point in its compressed format (element of G2)",
478+
"allOf": [
479+
{
480+
"$ref": "#/definitions/Binary"
481+
}
482+
]
483+
},
484+
"signature": {
485+
"description": "The signature point in its compressed format (element of G1)",
441486
"allOf": [
442487
{
443488
"$ref": "#/definitions/Binary"
@@ -482,7 +527,21 @@
482527
},
483528
"additionalProperties": false
484529
},
485-
"verify_bls12_pairing_equality": {
530+
"verify_bls12_pairing_equality_g1": {
531+
"$schema": "http://json-schema.org/draft-07/schema#",
532+
"title": "VerifyResponse",
533+
"type": "object",
534+
"required": [
535+
"verifies"
536+
],
537+
"properties": {
538+
"verifies": {
539+
"type": "boolean"
540+
}
541+
},
542+
"additionalProperties": false
543+
},
544+
"verify_bls12_pairing_equality_g2": {
486545
"$schema": "http://json-schema.org/draft-07/schema#",
487546
"title": "VerifyResponse",
488547
"type": "object",

contracts/crypto-verify/schema/raw/query.json

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,20 +377,19 @@
377377
"additionalProperties": false
378378
},
379379
{
380-
"description": "BLS12-381 pairing equality verification",
380+
"description": "BLS12-381 pairing equality verification (where the key is an element of G1)",
381381
"type": "object",
382382
"required": [
383-
"verify_bls12_pairing_equality"
383+
"verify_bls12_pairing_equality_g1"
384384
],
385385
"properties": {
386-
"verify_bls12_pairing_equality": {
386+
"verify_bls12_pairing_equality_g1": {
387387
"type": "object",
388388
"required": [
389389
"dst",
390390
"msg",
391-
"p",
392-
"q",
393-
"r"
391+
"pubkey",
392+
"signature"
394393
],
395394
"properties": {
396395
"dst": {
@@ -409,24 +408,70 @@
409408
}
410409
]
411410
},
412-
"p": {
413-
"description": "The point $p$ in its compressed format (element of G1)",
411+
"pubkey": {
412+
"description": "The public key point in its compressed format (element of G1)",
413+
"allOf": [
414+
{
415+
"$ref": "#/definitions/Binary"
416+
}
417+
]
418+
},
419+
"signature": {
420+
"description": "The signature point in its compressed format (element of G2)",
421+
"allOf": [
422+
{
423+
"$ref": "#/definitions/Binary"
424+
}
425+
]
426+
}
427+
},
428+
"additionalProperties": false
429+
}
430+
},
431+
"additionalProperties": false
432+
},
433+
{
434+
"description": "BLS12-381 pairing equality verification (where the key is an element of G2)",
435+
"type": "object",
436+
"required": [
437+
"verify_bls12_pairing_equality_g2"
438+
],
439+
"properties": {
440+
"verify_bls12_pairing_equality_g2": {
441+
"type": "object",
442+
"required": [
443+
"dst",
444+
"msg",
445+
"pubkey",
446+
"signature"
447+
],
448+
"properties": {
449+
"dst": {
450+
"description": "The `dst` component used to hash the message to the curve",
451+
"allOf": [
452+
{
453+
"$ref": "#/definitions/Binary"
454+
}
455+
]
456+
},
457+
"msg": {
458+
"description": "The message that should be verified",
414459
"allOf": [
415460
{
416461
"$ref": "#/definitions/Binary"
417462
}
418463
]
419464
},
420-
"q": {
421-
"description": "The point $q$ in its compressed format (element of G2)",
465+
"pubkey": {
466+
"description": "The public key point in its compressed format (element of G2)",
422467
"allOf": [
423468
{
424469
"$ref": "#/definitions/Binary"
425470
}
426471
]
427472
},
428-
"r": {
429-
"description": "The point $r$ in its compressed format (element of G1)",
473+
"signature": {
474+
"description": "The signature point in its compressed format (element of G1)",
430475
"allOf": [
431476
{
432477
"$ref": "#/definitions/Binary"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "VerifyResponse",
4+
"type": "object",
5+
"required": [
6+
"verifies"
7+
],
8+
"properties": {
9+
"verifies": {
10+
"type": "boolean"
11+
}
12+
},
13+
"additionalProperties": false
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "VerifyResponse",
4+
"type": "object",
5+
"required": [
6+
"verifies"
7+
],
8+
"properties": {
9+
"verifies": {
10+
"type": "boolean"
11+
}
12+
},
13+
"additionalProperties": false
14+
}
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
use cosmwasm_std::{Api, HashFunction, StdResult};
1+
use cosmwasm_std::{
2+
Api, HashFunction, StdResult, BLS12_381_G1_GENERATOR_COMPRESSED,
3+
BLS12_381_G2_GENERATOR_COMPRESSED,
4+
};
25

3-
pub fn verify(
6+
pub fn verify_g1(
47
api: &dyn Api,
5-
p: &[u8],
6-
q: &[u8],
7-
r: &[u8],
8+
signature: &[u8],
9+
pubkey: &[u8],
810
msg: &[u8],
911
dst: &[u8],
1012
) -> StdResult<bool> {
1113
let s = api.bls12_381_hash_to_g2(HashFunction::Sha256, msg, dst)?;
12-
api.bls12_381_pairing_equality(p, q, r, &s)
14+
api.bls12_381_pairing_equality(&BLS12_381_G1_GENERATOR_COMPRESSED, signature, pubkey, &s)
15+
.map_err(Into::into)
16+
}
17+
18+
pub fn verify_g2(
19+
api: &dyn Api,
20+
signature: &[u8],
21+
pubkey: &[u8],
22+
msg: &[u8],
23+
dst: &[u8],
24+
) -> StdResult<bool> {
25+
let s = api.bls12_381_hash_to_g1(HashFunction::Sha256, msg, dst)?;
26+
api.bls12_381_pairing_equality(signature, &BLS12_381_G2_GENERATOR_COMPRESSED, &s, pubkey)
1327
.map_err(Into::into)
1428
}

contracts/crypto-verify/src/contract.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,22 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
112112
&r,
113113
&s,
114114
)?),
115-
QueryMsg::VerifyBls12PairingEquality { p, q, r, msg, dst } => {
116-
to_json_binary(&query_verify_bls12_pairing(deps, &p, &q, &r, &msg, &dst)?)
117-
}
115+
QueryMsg::VerifyBls12PairingEqualityG1 {
116+
signature,
117+
pubkey,
118+
msg,
119+
dst,
120+
} => to_json_binary(&query_verify_bls12_pairing_g1(
121+
deps, &signature, &pubkey, &msg, &dst,
122+
)?),
123+
QueryMsg::VerifyBls12PairingEqualityG2 {
124+
signature,
125+
pubkey,
126+
msg,
127+
dst,
128+
} => to_json_binary(&query_verify_bls12_pairing_g2(
129+
deps, &signature, &pubkey, &msg, &dst,
130+
)?),
118131
}
119132
}
120133

@@ -285,15 +298,25 @@ pub fn query_list_verifications(deps: Deps) -> StdResult<ListVerificationsRespon
285298
})
286299
}
287300

288-
pub fn query_verify_bls12_pairing(
301+
pub fn query_verify_bls12_pairing_g1(
289302
deps: Deps,
290-
p: &[u8],
291-
q: &[u8],
292-
r: &[u8],
303+
signature: &[u8],
304+
pubkey: &[u8],
305+
msg: &[u8],
306+
dst: &[u8],
307+
) -> StdResult<VerifyResponse> {
308+
let verifies = crate::bls12_381::verify_g1(deps.api, signature, pubkey, msg, dst)?;
309+
Ok(VerifyResponse { verifies })
310+
}
311+
312+
pub fn query_verify_bls12_pairing_g2(
313+
deps: Deps,
314+
signature: &[u8],
315+
pubkey: &[u8],
293316
msg: &[u8],
294317
dst: &[u8],
295318
) -> StdResult<VerifyResponse> {
296-
let verifies = crate::bls12_381::verify(deps.api, p, q, r, msg, dst)?;
319+
let verifies = crate::bls12_381::verify_g2(deps.api, signature, pubkey, msg, dst)?;
297320
Ok(VerifyResponse { verifies })
298321
}
299322

contracts/crypto-verify/src/msg.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,25 @@ pub enum QueryMsg {
108108
/// The representation of this component is a big-endian encoded 256bit integer
109109
s: Binary,
110110
},
111-
/// BLS12-381 pairing equality verification
111+
/// BLS12-381 pairing equality verification (where the key is an element of G1)
112112
#[returns(VerifyResponse)]
113-
VerifyBls12PairingEquality {
114-
/// The point $p$ in its compressed format (element of G1)
115-
p: Binary,
116-
/// The point $q$ in its compressed format (element of G2)
117-
q: Binary,
118-
/// The point $r$ in its compressed format (element of G1)
119-
r: Binary,
113+
VerifyBls12PairingEqualityG1 {
114+
/// The signature point in its compressed format (element of G2)
115+
signature: Binary,
116+
/// The public key point in its compressed format (element of G1)
117+
pubkey: Binary,
118+
/// The message that should be verified
119+
msg: Binary,
120+
/// The `dst` component used to hash the message to the curve
121+
dst: Binary,
122+
},
123+
/// BLS12-381 pairing equality verification (where the key is an element of G2)
124+
#[returns(VerifyResponse)]
125+
VerifyBls12PairingEqualityG2 {
126+
/// The signature point in its compressed format (element of G1)
127+
signature: Binary,
128+
/// The public key point in its compressed format (element of G2)
129+
pubkey: Binary,
120130
/// The message that should be verified
121131
msg: Binary,
122132
/// The `dst` component used to hash the message to the curve

0 commit comments

Comments
 (0)