Skip to content

Commit 31db70d

Browse files
authored
Arkworks Elliptic Curve utils overhaul (paritytech#1870)
- Removal of Arkworks unit tests. These tests were just testing the arkworks upstream implementation which should be assumed correct. This is not the place to test well known dependencies. - Removal of some over-engineering. We just store the calls to Arkworks in one file. Per-curve sources are not required. - Docs formatting --- I also took the opportunity to bump the `bandersnatch-vrfs` crate revision internally providing some new shiny stuff.
1 parent aeda378 commit 31db70d

14 files changed

+209
-808
lines changed

substrate/primitives/core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ sp-runtime-interface = { path = "../runtime-interface", default-features = false
5757
# bls crypto
5858
w3f-bls = { version = "0.1.3", default-features = false, optional = true}
5959
# bandersnatch crypto
60-
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "f4fe253", default-features = false, optional = true }
60+
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "4b09416", default-features = false, optional = true }
6161

6262
[dev-dependencies]
6363
criterion = "0.4.0"

substrate/primitives/core/src/bandersnatch.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,7 @@ const PREOUT_SERIALIZED_LEN: usize = 33;
6060
//
6161
// This size is dependent on the ring domain size and the actual value
6262
// is equal to the SCALE encoded size of the `KZG` backend.
63-
//
64-
// Some values:
65-
// ring_size → ~serialized_size
66-
// 512 → 74 KB
67-
// 1024 → 147 KB
68-
// 2048 → 295 KB
69-
// NOTE: This is quite big but looks like there is an upcoming fix
70-
// in the backend.
71-
const RING_CONTEXT_SERIALIZED_LEN: usize = 147748;
63+
const RING_CONTEXT_SERIALIZED_LEN: usize = 147716;
7264

7365
/// Bandersnatch public key.
7466
#[cfg_attr(feature = "full_crypto", derive(Hash))]
@@ -538,10 +530,7 @@ pub mod vrf {
538530
#[cfg(feature = "full_crypto")]
539531
impl Pair {
540532
fn vrf_sign_gen<const N: usize>(&self, data: &VrfSignData) -> VrfSignature {
541-
let ios = core::array::from_fn(|i| {
542-
let input = data.inputs[i].0.clone();
543-
self.secret.vrf_inout(input)
544-
});
533+
let ios = core::array::from_fn(|i| self.secret.vrf_inout(data.inputs[i].0));
545534

546535
let thin_signature: ThinVrfSignature<N> =
547536
self.secret.sign_thin_vrf(data.transcript.clone(), &ios);
@@ -567,7 +556,7 @@ pub mod vrf {
567556
input: &VrfInput,
568557
) -> [u8; N] {
569558
let transcript = Transcript::new_labeled(context);
570-
let inout = self.secret.vrf_inout(input.0.clone());
559+
let inout = self.secret.vrf_inout(input.0);
571560
inout.vrf_output_bytes(transcript)
572561
}
573562
}
@@ -583,7 +572,7 @@ pub mod vrf {
583572
};
584573

585574
let preouts: [bandersnatch_vrfs::VrfPreOut; N] =
586-
core::array::from_fn(|i| signature.outputs[i].0.clone());
575+
core::array::from_fn(|i| signature.outputs[i].0);
587576

588577
// Deserialize only the proof, the rest has already been deserialized
589578
// This is another hack used because backend signature type is generic over
@@ -596,7 +585,7 @@ pub mod vrf {
596585
};
597586
let signature = ThinVrfSignature { proof, preouts };
598587

599-
let inputs = data.inputs.iter().map(|i| i.0.clone());
588+
let inputs = data.inputs.iter().map(|i| i.0);
600589

601590
public.verify_thin_vrf(data.transcript.clone(), inputs, &signature).is_ok()
602591
}
@@ -610,8 +599,7 @@ pub mod vrf {
610599
input: &VrfInput,
611600
) -> [u8; N] {
612601
let transcript = Transcript::new_labeled(context);
613-
let inout =
614-
bandersnatch_vrfs::VrfInOut { input: input.0.clone(), preoutput: self.0.clone() };
602+
let inout = bandersnatch_vrfs::VrfInOut { input: input.0, preoutput: self.0 };
615603
inout.vrf_output_bytes(transcript)
616604
}
617605
}
@@ -733,10 +721,7 @@ pub mod ring_vrf {
733721
data: &VrfSignData,
734722
prover: &RingProver,
735723
) -> RingVrfSignature {
736-
let ios = core::array::from_fn(|i| {
737-
let input = data.inputs[i].0.clone();
738-
self.secret.vrf_inout(input)
739-
});
724+
let ios = core::array::from_fn(|i| self.secret.vrf_inout(data.inputs[i].0));
740725

741726
let ring_signature: bandersnatch_vrfs::RingVrfSignature<N> =
742727
bandersnatch_vrfs::RingProver { ring_prover: prover, secret: &self.secret }
@@ -792,12 +777,12 @@ pub mod ring_vrf {
792777
};
793778

794779
let preouts: [bandersnatch_vrfs::VrfPreOut; N] =
795-
core::array::from_fn(|i| self.outputs[i].0.clone());
780+
core::array::from_fn(|i| self.outputs[i].0);
796781

797782
let signature =
798783
bandersnatch_vrfs::RingVrfSignature { proof: vrf_signature.proof, preouts };
799784

800-
let inputs = data.inputs.iter().map(|i| i.0.clone());
785+
let inputs = data.inputs.iter().map(|i| i.0);
801786

802787
bandersnatch_vrfs::RingVerifier(verifier)
803788
.verify_ring_vrf(data.transcript.clone(), inputs, &signature)

substrate/primitives/crypto/ec-utils/Cargo.toml

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "sp-crypto-ec-utils"
33
version = "0.4.0"
44
authors.workspace = true
5-
description = "Host function interface for common elliptic curve operations in Substrate runtimes"
5+
description = "Host functions for common Arkworks elliptic curve operations"
66
edition.workspace = true
77
license = "Apache-2.0"
88
homepage = "https://substrate.io"
@@ -12,51 +12,26 @@ repository.workspace = true
1212
targets = ["x86_64-unknown-linux-gnu"]
1313

1414
[dependencies]
15-
ark-serialize = { version = "0.4.2", default-features = false }
16-
ark-ff = { version = "0.4.2", default-features = false }
1715
ark-ec = { version = "0.4.2", default-features = false }
18-
ark-std = { version = "0.4.0", default-features = false }
1916
ark-bls12-377 = { version = "0.4.0", features = ["curve"], default-features = false }
2017
ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false }
2118
ark-bw6-761 = { version = "0.4.0", default-features = false }
2219
ark-ed-on-bls12-381-bandersnatch = { version = "0.4.0", default-features = false }
2320
ark-ed-on-bls12-377 = { version = "0.4.0", default-features = false }
24-
sp-std = { path = "../../std", default-features = false }
25-
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
26-
ark-scale = { version = "0.0.10", features = ["hazmat"], default-features = false }
21+
ark-scale = { version = "0.0.11", features = ["hazmat"], default-features = false }
2722
sp-runtime-interface = { path = "../../runtime-interface", default-features = false}
28-
29-
[dev-dependencies]
30-
sp-io = { path = "../../io", default-features = false }
31-
ark-algebra-test-templates = { version = "0.4.2", default-features = false }
32-
sp-ark-models = { version = "0.4.1-beta", default-features = false }
33-
sp-ark-bls12-377 = { version = "0.4.1-beta", default-features = false }
34-
sp-ark-bls12-381 = { version = "0.4.1-beta", default-features = false }
35-
sp-ark-bw6-761 = { version = "0.4.1-beta", default-features = false }
36-
sp-ark-ed-on-bls12-377 = { version = "0.4.1-beta", default-features = false }
37-
sp-ark-ed-on-bls12-381-bandersnatch = { version = "0.4.1-beta", default-features = false }
23+
sp-std = { path = "../../std", default-features = false }
3824

3925
[features]
4026
default = [ "std" ]
4127
std = [
42-
"ark-algebra-test-templates/std",
4328
"ark-bls12-377/std",
4429
"ark-bls12-381/std",
4530
"ark-bw6-761/std",
4631
"ark-ec/std",
4732
"ark-ed-on-bls12-377/std",
4833
"ark-ed-on-bls12-381-bandersnatch/std",
49-
"ark-ff/std",
5034
"ark-scale/std",
51-
"ark-serialize/std",
52-
"ark-std/std",
53-
"codec/std",
54-
"sp-ark-bls12-377/std",
55-
"sp-ark-bls12-381/std",
56-
"sp-ark-bw6-761/std",
57-
"sp-ark-ed-on-bls12-377/std",
58-
"sp-ark-ed-on-bls12-381-bandersnatch/std",
59-
"sp-io/std",
6035
"sp-runtime-interface/std",
6136
"sp-std/std",
6237
]

substrate/primitives/crypto/ec-utils/src/bls12_377.rs

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)