Skip to content

Commit d044e83

Browse files
committed
Export constants
1 parent ceb3aa7 commit d044e83

File tree

10 files changed

+68
-65
lines changed

10 files changed

+68
-65
lines changed

contracts/crypto-verify/Cargo.lock

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/crypto-verify/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ sha2 = "0.10"
4242
sha3 = "0.10"
4343

4444
[dev-dependencies]
45-
ark-ec = "0.4.2"
46-
ark-serialize = { version = "0.4.2", default-features = false }
4745
cosmwasm-vm = { path = "../../packages/vm", default-features = false, features = ["iterator"] }
4846
hex-literal = "0.4.1"

contracts/crypto-verify/tests/integration.rs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
//! 5. Anywhere you see query(deps.as_ref(), ...) you must replace it with query(&mut deps, ...)
2020
//! (Use cosmwasm_vm::testing::{init, execute, query}, instead of the contract variants).
2121
22-
use ark_bls12_381::G1Affine;
23-
use ark_ec::AffineRepr;
24-
use ark_serialize::CanonicalSerialize;
25-
use cosmwasm_std::{Binary, Response, Uint128};
22+
use cosmwasm_std::{Binary, Response, Uint128, BLS12_381_G1_GENERATOR_COMPRESSED};
2623
use cosmwasm_vm::testing::{
2724
instantiate, mock_env, mock_info, mock_instance, query, MockApi, MockQuerier, MockStorage,
2825
};
@@ -118,13 +115,8 @@ fn bls12_381_verifies() {
118115

119116
let msg = build_drand_message(round, &previous_signature);
120117

121-
let mut serialized = [0; 48];
122-
G1Affine::generator()
123-
.serialize_compressed(&mut serialized[..])
124-
.unwrap();
125-
126118
let verify_msg = QueryMsg::VerifyBls12PairingEquality {
127-
p: serialized.into(),
119+
p: BLS12_381_G1_GENERATOR_COMPRESSED.into(),
128120
q: signature.into(),
129121
r: PK_LEO_MAINNET.into(),
130122
msg: msg.into(),
@@ -149,13 +141,8 @@ fn bls12_381_errors() {
149141

150142
let msg = build_drand_message(round, &previous_signature);
151143

152-
let mut serialized = [0; 48];
153-
G1Affine::generator()
154-
.serialize_compressed(&mut serialized[..])
155-
.unwrap();
156-
157144
let verify_msg = QueryMsg::VerifyBls12PairingEquality {
158-
p: serialized.into(),
145+
p: BLS12_381_G1_GENERATOR_COMPRESSED.into(),
159146
q: signature.into(),
160147
r: PK_LEO_MAINNET.into(),
161148
msg: msg.into(),

packages/crypto/benches/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ use k256::ecdsa::SigningKey; // type alias
1616
use sha2::Sha256;
1717

1818
use cosmwasm_crypto::{
19-
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_g1_generator, bls12_381_hash_to_g1,
20-
bls12_381_hash_to_g2, bls12_381_pairing_equality, ed25519_batch_verify, ed25519_verify,
21-
secp256k1_recover_pubkey, secp256k1_verify, secp256r1_recover_pubkey, secp256r1_verify,
22-
HashFunction, BLS12_381_G1_POINT_LEN, BLS12_381_G2_POINT_LEN,
19+
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_hash_to_g1, bls12_381_hash_to_g2,
20+
bls12_381_pairing_equality, ed25519_batch_verify, ed25519_verify, secp256k1_recover_pubkey,
21+
secp256k1_verify, secp256r1_recover_pubkey, secp256r1_verify, HashFunction,
22+
BLS12_381_G1_GENERATOR_COMPRESSED, BLS12_381_G1_POINT_LEN, BLS12_381_G2_POINT_LEN,
2323
};
2424
use std::cmp::min;
2525

@@ -192,7 +192,7 @@ where
192192
let is_valid = black_box(bls12_381_pairing_equality(
193193
&serialized_pubkeys,
194194
&serialized_messages,
195-
&bls12_381_g1_generator(),
195+
&BLS12_381_G1_GENERATOR_COMPRESSED,
196196
&serialized_signature,
197197
))
198198
.unwrap();
@@ -224,7 +224,7 @@ where
224224
});
225225

226226
group.bench_function("bls12_381_verify", |b| {
227-
let generator = bls12_381_g1_generator();
227+
let generator = BLS12_381_G1_GENERATOR_COMPRESSED;
228228
let message = bls12_381_hash_to_g2(HashFunction::Sha256, &BLS_MESSAGE, BLS_DST);
229229

230230
b.iter(|| {
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,46 @@
1-
use ark_bls12_381::{G1Affine, G2Affine};
2-
use ark_ec::AffineRepr;
3-
use ark_serialize::CanonicalSerialize;
4-
5-
use crate::{BLS12_381_G1_POINT_LEN, BLS12_381_G2_POINT_LEN};
6-
7-
pub fn bls12_381_g1_generator() -> [u8; BLS12_381_G1_POINT_LEN] {
8-
let mut point = [0_u8; BLS12_381_G1_POINT_LEN];
9-
G1Affine::generator()
10-
.serialize_compressed(&mut point[..])
11-
.unwrap();
1+
#[cfg(test)]
2+
mod test {
3+
use ark_bls12_381::{G1Affine, G2Affine};
4+
use ark_ec::AffineRepr;
5+
use ark_serialize::CanonicalSerialize;
6+
use hex_literal::hex;
127

13-
point
14-
}
8+
use crate::{
9+
bls12_318::{BLS12_381_G1_GENERATOR_COMPRESSED, BLS12_381_G2_GENERATOR_COMPRESSED},
10+
BLS12_381_G1_POINT_LEN, BLS12_381_G2_POINT_LEN,
11+
};
1512

16-
pub fn bls12_381_g2_generator() -> [u8; BLS12_381_G2_POINT_LEN] {
17-
let mut point = [0_u8; BLS12_381_G2_POINT_LEN];
18-
G2Affine::generator()
19-
.serialize_compressed(&mut point[..])
20-
.unwrap();
13+
fn bls12_381_g1_generator() -> [u8; BLS12_381_G1_POINT_LEN] {
14+
let mut point = [0_u8; BLS12_381_G1_POINT_LEN];
15+
G1Affine::generator()
16+
.serialize_compressed(&mut point[..])
17+
.unwrap();
2118

22-
point
23-
}
19+
point
20+
}
2421

25-
#[cfg(test)]
26-
mod test {
27-
use hex_literal::hex;
22+
fn bls12_381_g2_generator() -> [u8; BLS12_381_G2_POINT_LEN] {
23+
let mut point = [0_u8; BLS12_381_G2_POINT_LEN];
24+
G2Affine::generator()
25+
.serialize_compressed(&mut point[..])
26+
.unwrap();
2827

29-
use crate::{bls12_381_g1_generator, bls12_381_g2_generator};
28+
point
29+
}
3030

3131
#[test]
3232
fn g1_generator_correct() {
3333
let mut generator = hex!("17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb");
3434
generator[0] |= 0b1000_0000;
3535
assert_eq!(generator, bls12_381_g1_generator());
36+
assert_eq!(bls12_381_g1_generator(), BLS12_381_G1_GENERATOR_COMPRESSED);
3637
}
3738

3839
#[test]
3940
fn g2_generator_correct() {
4041
let mut generator = hex!("13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8");
4142
generator[0] |= 0b1000_0000;
4243
assert_eq!(generator, bls12_381_g2_generator());
44+
assert_eq!(bls12_381_g2_generator(), BLS12_381_G2_GENERATOR_COMPRESSED);
4345
}
4446
}

packages/crypto/src/bls12_318/mod.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,22 @@ mod pairing;
55
mod points;
66

77
pub use aggregate::{bls12_381_aggregate_g1, bls12_381_aggregate_g2};
8-
pub use constants::{bls12_381_g1_generator, bls12_381_g2_generator};
98
pub use hash::{bls12_381_hash_to_g1, bls12_381_hash_to_g2, HashFunction};
109
pub use pairing::bls12_381_pairing_equality;
1110
pub use points::{bls12_381_g1_is_identity, bls12_381_g2_is_identity};
1211

1312
pub const BLS12_381_G1_POINT_LEN: usize = 48;
1413
pub const BLS12_381_G2_POINT_LEN: usize = 96;
14+
15+
pub const BLS12_381_G1_GENERATOR_COMPRESSED: [u8; BLS12_381_G1_POINT_LEN] = [
16+
151, 241, 211, 167, 49, 151, 215, 148, 38, 149, 99, 140, 79, 169, 172, 15, 195, 104, 140, 79,
17+
151, 116, 185, 5, 161, 78, 58, 63, 23, 27, 172, 88, 108, 85, 232, 63, 249, 122, 26, 239, 251,
18+
58, 240, 10, 219, 34, 198, 187,
19+
];
20+
pub const BLS12_381_G2_GENERATOR_COMPRESSED: [u8; BLS12_381_G2_POINT_LEN] = [
21+
147, 224, 43, 96, 82, 113, 159, 96, 125, 172, 211, 160, 136, 39, 79, 101, 89, 107, 208, 208,
22+
153, 32, 182, 26, 181, 218, 97, 187, 220, 127, 80, 73, 51, 76, 241, 18, 19, 148, 93, 87, 229,
23+
172, 125, 5, 93, 4, 43, 126, 2, 74, 162, 178, 240, 143, 10, 145, 38, 8, 5, 39, 45, 197, 16, 81,
24+
198, 228, 122, 212, 250, 64, 59, 2, 180, 81, 11, 100, 122, 227, 209, 119, 11, 172, 3, 38, 168,
25+
5, 187, 239, 212, 128, 86, 200, 193, 33, 189, 184,
26+
];

packages/crypto/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ mod secp256r1;
2323
#[cfg(feature = "std")]
2424
#[doc(hidden)]
2525
pub use crate::bls12_318::{
26-
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_g1_generator,
27-
bls12_381_g1_is_identity, bls12_381_g2_generator, bls12_381_g2_is_identity,
28-
bls12_381_hash_to_g1, bls12_381_hash_to_g2, bls12_381_pairing_equality, HashFunction,
29-
BLS12_381_G1_POINT_LEN, BLS12_381_G2_POINT_LEN,
26+
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_g1_is_identity,
27+
bls12_381_g2_is_identity, bls12_381_hash_to_g1, bls12_381_hash_to_g2,
28+
bls12_381_pairing_equality, HashFunction, BLS12_381_G1_GENERATOR_COMPRESSED,
29+
BLS12_381_G1_POINT_LEN, BLS12_381_G2_GENERATOR_COMPRESSED, BLS12_381_G2_POINT_LEN,
3030
};
3131
#[doc(hidden)]
3232
pub use crate::ecdsa::{ECDSA_PUBKEY_MAX_LEN, ECDSA_SIGNATURE_LEN, MESSAGE_HASH_MAX_LEN};

packages/crypto/tests/bls12_381.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
66
use base64::engine::general_purpose::STANDARD;
77
use base64_serde::base64_serde_type;
88
use cosmwasm_crypto::{
9-
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_g1_generator,
10-
bls12_381_g1_is_identity, bls12_381_g2_is_identity, bls12_381_hash_to_g2,
11-
bls12_381_pairing_equality, HashFunction, BLS12_381_G2_POINT_LEN,
9+
bls12_381_aggregate_g1, bls12_381_aggregate_g2, bls12_381_g1_is_identity,
10+
bls12_381_g2_is_identity, bls12_381_hash_to_g2, bls12_381_pairing_equality, HashFunction,
11+
BLS12_381_G1_GENERATOR_COMPRESSED, BLS12_381_G2_POINT_LEN,
1212
};
1313

1414
const PROOF_OF_POSSESSION_DST: &[u8] = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
@@ -261,7 +261,7 @@ fn bls12_381_verify_works() {
261261
let bool_result = bls12_381_pairing_equality(
262262
&pubkey,
263263
&message_point,
264-
&bls12_381_g1_generator(),
264+
&BLS12_381_G1_GENERATOR_COMPRESSED,
265265
&signature,
266266
)?;
267267

@@ -337,7 +337,7 @@ fn bls12_381_aggregate_verify_works() {
337337
let bool_result = bls12_381_pairing_equality(
338338
&pubkeys,
339339
&messages,
340-
&bls12_381_g1_generator(),
340+
&BLS12_381_G1_GENERATOR_COMPRESSED,
341341
&signature,
342342
)?;
343343

@@ -412,7 +412,7 @@ fn bls12_381_fast_aggregate_verify_works() {
412412
let bool_result = bls12_381_pairing_equality(
413413
&pubkey,
414414
&message_point,
415-
&bls12_381_g1_generator(),
415+
&BLS12_381_G1_GENERATOR_COMPRESSED,
416416
&signature,
417417
)?;
418418

packages/std/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,6 @@ pub use cosmwasm_core::{
122122
#[cfg(not(target_arch = "wasm32"))]
123123
pub use cosmwasm_core::assert_approx_eq;
124124

125+
pub use cosmwasm_crypto::{BLS12_381_G1_GENERATOR_COMPRESSED, BLS12_381_G2_GENERATOR_COMPRESSED};
126+
125127
pub use cosmwasm_derive::entry_point;

packages/std/src/testing/mock.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,7 @@ mod tests {
11681168
#[cfg(feature = "staking")]
11691169
use crate::{Decimal, Delegation};
11701170
use base64::{engine::general_purpose, Engine};
1171+
use cosmwasm_crypto::BLS12_381_G1_GENERATOR_COMPRESSED;
11711172
use hex_literal::hex;
11721173
use serde::Deserialize;
11731174

@@ -1361,7 +1362,6 @@ mod tests {
13611362
let api = MockApi::default();
13621363

13631364
let dst = b"BLS_SIG_BLS12381G2_XMD:SHA-256_SSWU_RO_POP_";
1364-
let g1_generator = cosmwasm_crypto::bls12_381_g1_generator();
13651365
let ps = hex!("a491d1b0ecd9bb917989f0e74f0dea0422eac4a873e5e2644f368dffb9a6e20fd6e10c1b77654d067c0618f6e5a7f79ab301803f8b5ac4a1133581fc676dfedc60d891dd5fa99028805e5ea5b08d3491af75d0707adab3b70c6a6a580217bf81b53d21a4cfd562c469cc81514d4ce5a6b577d8403d32a394dc265dd190b47fa9f829fdd7963afdf972e5e77854051f6f");
13661366
let qs: Vec<u8> = [
13671367
hex!("0000000000000000000000000000000000000000000000000000000000000000"),
@@ -1377,7 +1377,7 @@ mod tests {
13771377
let s = hex!("9104e74b9dfd3ad502f25d6a5ef57db0ed7d9a0e00f3500586d8ce44231212542fcfaf87840539b398bf07626705cf1105d246ca1062c6c2e1a53029a0f790ed5e3cb1f52f8234dc5144c45fc847c0cd37a92d68e7c5ba7c648a8a339f171244");
13781378

13791379
let is_valid = api
1380-
.bls12_381_pairing_equality(&ps, &qs, &g1_generator, &s)
1380+
.bls12_381_pairing_equality(&ps, &qs, &BLS12_381_G1_GENERATOR_COMPRESSED, &s)
13811381
.unwrap();
13821382
assert!(is_valid);
13831383
}
@@ -1437,9 +1437,13 @@ mod tests {
14371437
.bls12_381_hash_to_g2(HashFunction::Sha256, &msg, DOMAIN_HASH_TO_G2)
14381438
.unwrap();
14391439

1440-
let g1_generator = cosmwasm_crypto::bls12_381_g1_generator();
14411440
let is_valid = api
1442-
.bls12_381_pairing_equality(&g1_generator, &signature, &PK_LEO_MAINNET, &msg_point)
1441+
.bls12_381_pairing_equality(
1442+
&BLS12_381_G1_GENERATOR_COMPRESSED,
1443+
&signature,
1444+
&PK_LEO_MAINNET,
1445+
&msg_point,
1446+
)
14431447
.unwrap();
14441448

14451449
assert!(is_valid);

0 commit comments

Comments
 (0)