Skip to content

Commit ddc51e5

Browse files
committed
merge
2 parents c8facf4 + c92519e commit ddc51e5

File tree

14 files changed

+202
-356
lines changed

14 files changed

+202
-356
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ sp-crypto-ec-utils = { git = "https://github.com/paritytech/polkadot-sdk.git", t
197197
getrandom = { version = "0.2.15", features = ["custom"], default-features = false }
198198
sp-keystore = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2409", default-features = false }
199199
w3f-bls = { version = "=0.1.3", default-features = false }
200-
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ "r1cs", "snark" ] }
200+
ark-crypto-primitives = { version = "0.4.0", default-features = false, features = [ "r1cs", "snark" ] }
201201
ark-scale = { version = "0.0.11", default-features = false, features = ["hazmat"] }
202202
sp-ark-bls12-381 = { git = "https://github.com/paritytech/substrate-curves", default-features = false }
203203
ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false }
@@ -207,8 +207,8 @@ ark-ec = { version = "0.4.0", default-features = false }
207207
ark-std = { version = "0.4.0", default-features = false }
208208
anyhow = "1.0.81"
209209
sha2 = { version = "0.10.8", default-features = false }
210-
tle = { git = "https://github.com/ideal-lab5/tle", rev = "6226d8a73b6c0dcc5d4f3671193d58ada21ce88f", default-features = false }
211210
rand_chacha = { version = "0.3.1", default-features = false }
211+
tle = { git = "https://github.com/ideal-lab5/timelock", rev = "5416406cfd32799e31e1795393d4916894de4468", default-features = false }
212212

213213
frame-metadata = "16"
214214

node/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub type FullClient = sc_service::TFullClient<Block, RuntimeApi, RuntimeExecutor
1212
/// `runtime-benchmarks`.
1313
pub type HostFunctions = (
1414
sp_io::SubstrateHostFunctions,
15-
sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions,
1615
frame_benchmarking::benchmarking::HostFunctions,
16+
sp_crypto_ec_utils::bls12_381::host_calls::HostFunctions,
1717
);
1818
pub type RuntimeExecutor = WasmExecutor<HostFunctions>;

node/src/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,11 @@ where
314314
metrics,
315315
})?;
316316

317-
if config.offchain_worker.enabled {
317+
if config.offchain_worker.enabled && config.role.is_authority() {
318318
match sp_keystore::Keystore::sr25519_generate_new(
319319
&*keystore_container.keystore(),
320320
pallet_drand::KEY_TYPE,
321-
Some("//Alice"),
321+
None,
322322
) {
323323
Ok(_) => {
324324
task_manager.spawn_handle().spawn(

pallets/drand/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ ark-scale = { workspace = true, default-features = false, features = ["hazmat"]
4343
w3f-bls = { workspace = true, default-features = false }
4444
sp-keyring = { workspace = true, default-features = false }
4545
subtensor-macros.workspace = true
46+
tle = { workspace = true, default-features = false }
4647

4748
[dev-dependencies]
4849
sp-keystore = { workspace = true, default-features = false }
@@ -74,6 +75,7 @@ std = [
7475
"ark-crypto-primitives/std",
7576
"ark-scale/std",
7677
"w3f-bls/std",
78+
"tle/std",
7779
]
7880
runtime-benchmarks = [
7981
"frame-benchmarking/runtime-benchmarks",
@@ -86,6 +88,3 @@ try-runtime = [
8688
"frame-system/try-runtime",
8789
"sp-runtime/try-runtime",
8890
]
89-
90-
# use the drand mainnet configuration
91-
mainnet = []

pallets/drand/src/benchmarking.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,9 @@ use crate::Pallet as Drand;
2222
use frame_benchmarking::v2::*;
2323
use frame_system::RawOrigin;
2424

25-
#[cfg(not(feature = "mainnet"))]
2625
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"fe290beca10872ef2fb164d2aa4442de4566183ec51c56ff3cd603d930e54fdd\",\"signature\":\"b44679b9a59af2ec876b1a6b1ad52ea9b1615fc3982b19576350f93447cb1125e342b73a8dd2bacbe47e4b6b63ed5e39\"}";
27-
#[cfg(not(feature = "mainnet"))]
2826
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a\",\"period\":3,\"genesis_time\":1692803367,\"hash\":\"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971\",\"groupHash\":\"f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e\",\"schemeID\":\"bls-unchained-g1-rfc9380\",\"metadata\":{\"beaconID\":\"quicknet\"}}";
2927

30-
// mainnet parameters
31-
#[cfg(feature = "mainnet")]
32-
pub const DRAND_PULSE: &str = "{\"round\":1000,\"randomness\":\"a40d3e0e7e3c71f28b7da2fd339f47f0bcf10910309f5253d7c323ec8cea3212\",\"signature\":\"99bf96de133c3d3937293cfca10c8152b18ab2d034ccecf115658db324d2edc00a16a2044cd04a8a38e2a307e5ecff3511315be8d282079faf24098f283e0ed2c199663b334d2e84c55c032fe469b212c5c2087ebb83a5b25155c3283f5b79ac\",\"previous_signature\":\"af0d93299a363735fe847f5ea241442c65843dc1bd3a7b79646b3b10072e908bf034d35cd69d378e3341f139100cd4cd03030399864ef8803a5a4f5e64fccc20bbae36d1ca22a6ddc43d2630c41105e90598fab11e5c7456df3925d4b577b113\"}";
33-
#[cfg(feature = "mainnet")]
34-
pub const DRAND_INFO_RESPONSE: &str = "{\"public_key\":\"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31\",\"period\":30,\"genesis_time\":1595431050,\"hash\":\"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce\",\"groupHash\":\"176f93498eac9ca337150b46d21dd58673ea4e3581185f869672e59fa4cb390a\",\"schemeID\":\"pedersen-bls-chained\",\"metadata\":{\"beaconID\":\"default\"}}";
35-
3628
#[benchmarks(
3729
where
3830
T::Public: From<sp_core::sr25519::Public>,
@@ -54,8 +46,9 @@ mod benchmarks {
5446
};
5547

5648
#[extrinsic_call]
57-
set_beacon_config(RawOrigin::None, config_payload.clone(), None);
58-
assert_eq!(BeaconConfig::<T>::get(), Some(config));
49+
set_beacon_config(RawOrigin::Root, config_payload.clone(), None);
50+
51+
assert_eq!(BeaconConfig::<T>::get(), config);
5952
}
6053

6154
#[benchmark]

pallets/drand/src/bls12_381.rs

Lines changed: 27 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -14,80 +14,34 @@
1414
* limitations under the License.
1515
*/
1616

17-
use crate::utils::ScalarFieldFor;
18-
use ark_bls12_381::{G1Affine, G1Projective, G2Affine, G2Projective};
19-
use ark_ec::{pairing::Pairing, short_weierstrass::SWCurveConfig, AffineRepr};
17+
use ark_ec::pairing::Pairing;
18+
use ark_std::{ops::Neg, Zero};
2019
use sp_ark_bls12_381::{
21-
Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G1Projective as G1ProjectiveOpt,
22-
G2Affine as G2AffineOpt, G2Projective as G2ProjectiveOpt,
20+
Bls12_381 as Bls12_381Opt, G1Affine as G1AffineOpt, G2Affine as G2AffineOpt,
2321
};
2422

25-
#[inline]
26-
pub fn pairing(a: G1Affine, b: G2Affine) {
27-
let _out = ark_bls12_381::Bls12_381::multi_pairing([a], [b]);
28-
}
29-
30-
#[inline]
31-
pub fn pairing_opt(a: G1AffineOpt, b: G2AffineOpt) {
32-
let _out = Bls12_381Opt::multi_pairing([a], [b]);
33-
}
34-
35-
#[inline]
36-
pub fn msm_g1(bases: &[G1Affine], scalars: &[ScalarFieldFor<G1Affine>]) {
37-
let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::msm(bases, scalars);
38-
}
39-
40-
#[inline]
41-
pub fn msm_g1_opt(bases: &[G1AffineOpt], scalars: &[<G1AffineOpt as AffineRepr>::ScalarField]) {
42-
let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::msm(bases, scalars);
43-
}
44-
45-
#[inline]
46-
pub fn msm_g2(bases: &[G2Affine], scalars: &[ScalarFieldFor<G2Affine>]) {
47-
let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::msm(bases, scalars);
48-
}
49-
50-
#[inline]
51-
pub fn msm_g2_opt(bases: &[G2AffineOpt], scalars: &[ScalarFieldFor<G2AffineOpt>]) {
52-
let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::msm(bases, scalars);
53-
}
54-
55-
#[inline]
56-
pub fn mul_projective_g1(base: &G1Projective, scalar: &[u64]) {
57-
let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::mul_projective(base, scalar);
58-
}
59-
60-
#[inline]
61-
pub fn mul_projective_g1_opt(base: &G1ProjectiveOpt, scalar: &[u64]) {
62-
let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::mul_projective(base, scalar);
63-
}
64-
65-
#[inline]
66-
pub fn mul_affine_g1(base: &G1Affine, scalar: &[u64]) {
67-
let _out = <ark_bls12_381::g1::Config as SWCurveConfig>::mul_affine(base, scalar);
68-
}
69-
70-
#[inline]
71-
pub fn mul_affine_g1_opt(base: &G1AffineOpt, scalar: &[u64]) {
72-
let _out = <sp_ark_bls12_381::g1::Config as SWCurveConfig>::mul_affine(base, scalar);
73-
}
74-
75-
#[inline]
76-
pub fn mul_projective_g2(base: &G2Projective, scalar: &[u64]) {
77-
let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::mul_projective(base, scalar);
78-
}
79-
80-
#[inline]
81-
pub fn mul_projective_g2_opt(base: &G2ProjectiveOpt, scalar: &[u64]) {
82-
let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::mul_projective(base, scalar);
83-
}
84-
85-
#[inline]
86-
pub fn mul_affine_g2(base: &G2Affine, scalar: &[u64]) {
87-
let _out = <ark_bls12_381::g2::Config as SWCurveConfig>::mul_affine(base, scalar);
88-
}
89-
90-
#[inline]
91-
pub fn mul_affine_g2_opt(base: &G2AffineOpt, scalar: &[u64]) {
92-
let _out = <sp_ark_bls12_381::g2::Config as SWCurveConfig>::mul_affine(base, scalar);
23+
/// An optimized way to verify Drand pulses from quicket
24+
/// Instead of computing two pairings and comparing them, we instead compute a multi miller loop,
25+
/// and then take the final exponentiation, saving a lot of computational cost.
26+
///
27+
/// This function is also inlined as a way to optimize performance.
28+
///
29+
/// * `signature`:
30+
/// * `q`:
31+
/// * `msg_on_curve`: The message signed by Drand, hashed to G1
32+
/// * `p_pub`: The beacon public key
33+
#[inline]
34+
pub fn fast_pairing_opt(
35+
signature: G1AffineOpt,
36+
q: G2AffineOpt,
37+
r: G1AffineOpt,
38+
s: G2AffineOpt,
39+
) -> bool {
40+
let looped = Bls12_381Opt::multi_miller_loop([signature.neg(), r], [q, s]);
41+
let exp = Bls12_381Opt::final_exponentiation(looped);
42+
43+
match exp {
44+
Some(e) => e.is_zero(),
45+
None => false,
46+
}
9347
}

0 commit comments

Comments
 (0)