Skip to content

Commit

Permalink
Arkworks Elliptic Curve utils overhaul (paritytech#1870)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
davxy authored Oct 16, 2023
1 parent aeda378 commit 31db70d
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 808 deletions.
2 changes: 1 addition & 1 deletion substrate/primitives/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sp-runtime-interface = { path = "../runtime-interface", default-features = false
# bls crypto
w3f-bls = { version = "0.1.3", default-features = false, optional = true}
# bandersnatch crypto
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "f4fe253", default-features = false, optional = true }
bandersnatch_vrfs = { git = "https://github.com/w3f/ring-vrf", rev = "4b09416", default-features = false, optional = true }

[dev-dependencies]
criterion = "0.4.0"
Expand Down
33 changes: 9 additions & 24 deletions substrate/primitives/core/src/bandersnatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ const PREOUT_SERIALIZED_LEN: usize = 33;
//
// This size is dependent on the ring domain size and the actual value
// is equal to the SCALE encoded size of the `KZG` backend.
//
// Some values:
// ring_size → ~serialized_size
// 512 → 74 KB
// 1024 → 147 KB
// 2048 → 295 KB
// NOTE: This is quite big but looks like there is an upcoming fix
// in the backend.
const RING_CONTEXT_SERIALIZED_LEN: usize = 147748;
const RING_CONTEXT_SERIALIZED_LEN: usize = 147716;

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

let thin_signature: ThinVrfSignature<N> =
self.secret.sign_thin_vrf(data.transcript.clone(), &ios);
Expand All @@ -567,7 +556,7 @@ pub mod vrf {
input: &VrfInput,
) -> [u8; N] {
let transcript = Transcript::new_labeled(context);
let inout = self.secret.vrf_inout(input.0.clone());
let inout = self.secret.vrf_inout(input.0);
inout.vrf_output_bytes(transcript)
}
}
Expand All @@ -583,7 +572,7 @@ pub mod vrf {
};

let preouts: [bandersnatch_vrfs::VrfPreOut; N] =
core::array::from_fn(|i| signature.outputs[i].0.clone());
core::array::from_fn(|i| signature.outputs[i].0);

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

let inputs = data.inputs.iter().map(|i| i.0.clone());
let inputs = data.inputs.iter().map(|i| i.0);

public.verify_thin_vrf(data.transcript.clone(), inputs, &signature).is_ok()
}
Expand All @@ -610,8 +599,7 @@ pub mod vrf {
input: &VrfInput,
) -> [u8; N] {
let transcript = Transcript::new_labeled(context);
let inout =
bandersnatch_vrfs::VrfInOut { input: input.0.clone(), preoutput: self.0.clone() };
let inout = bandersnatch_vrfs::VrfInOut { input: input.0, preoutput: self.0 };
inout.vrf_output_bytes(transcript)
}
}
Expand Down Expand Up @@ -733,10 +721,7 @@ pub mod ring_vrf {
data: &VrfSignData,
prover: &RingProver,
) -> RingVrfSignature {
let ios = core::array::from_fn(|i| {
let input = data.inputs[i].0.clone();
self.secret.vrf_inout(input)
});
let ios = core::array::from_fn(|i| self.secret.vrf_inout(data.inputs[i].0));

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

let preouts: [bandersnatch_vrfs::VrfPreOut; N] =
core::array::from_fn(|i| self.outputs[i].0.clone());
core::array::from_fn(|i| self.outputs[i].0);

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

let inputs = data.inputs.iter().map(|i| i.0.clone());
let inputs = data.inputs.iter().map(|i| i.0);

bandersnatch_vrfs::RingVerifier(verifier)
.verify_ring_vrf(data.transcript.clone(), inputs, &signature)
Expand Down
31 changes: 3 additions & 28 deletions substrate/primitives/crypto/ec-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "sp-crypto-ec-utils"
version = "0.4.0"
authors.workspace = true
description = "Host function interface for common elliptic curve operations in Substrate runtimes"
description = "Host functions for common Arkworks elliptic curve operations"
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
Expand All @@ -12,51 +12,26 @@ repository.workspace = true
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
ark-serialize = { version = "0.4.2", default-features = false }
ark-ff = { version = "0.4.2", default-features = false }
ark-ec = { version = "0.4.2", default-features = false }
ark-std = { version = "0.4.0", default-features = false }
ark-bls12-377 = { version = "0.4.0", features = ["curve"], default-features = false }
ark-bls12-381 = { version = "0.4.0", features = ["curve"], default-features = false }
ark-bw6-761 = { version = "0.4.0", default-features = false }
ark-ed-on-bls12-381-bandersnatch = { version = "0.4.0", default-features = false }
ark-ed-on-bls12-377 = { version = "0.4.0", default-features = false }
sp-std = { path = "../../std", default-features = false }
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false }
ark-scale = { version = "0.0.10", features = ["hazmat"], default-features = false }
ark-scale = { version = "0.0.11", features = ["hazmat"], default-features = false }
sp-runtime-interface = { path = "../../runtime-interface", default-features = false}

[dev-dependencies]
sp-io = { path = "../../io", default-features = false }
ark-algebra-test-templates = { version = "0.4.2", default-features = false }
sp-ark-models = { version = "0.4.1-beta", default-features = false }
sp-ark-bls12-377 = { version = "0.4.1-beta", default-features = false }
sp-ark-bls12-381 = { version = "0.4.1-beta", default-features = false }
sp-ark-bw6-761 = { version = "0.4.1-beta", default-features = false }
sp-ark-ed-on-bls12-377 = { version = "0.4.1-beta", default-features = false }
sp-ark-ed-on-bls12-381-bandersnatch = { version = "0.4.1-beta", default-features = false }
sp-std = { path = "../../std", default-features = false }

[features]
default = [ "std" ]
std = [
"ark-algebra-test-templates/std",
"ark-bls12-377/std",
"ark-bls12-381/std",
"ark-bw6-761/std",
"ark-ec/std",
"ark-ed-on-bls12-377/std",
"ark-ed-on-bls12-381-bandersnatch/std",
"ark-ff/std",
"ark-scale/std",
"ark-serialize/std",
"ark-std/std",
"codec/std",
"sp-ark-bls12-377/std",
"sp-ark-bls12-381/std",
"sp-ark-bw6-761/std",
"sp-ark-ed-on-bls12-377/std",
"sp-ark-ed-on-bls12-381-bandersnatch/std",
"sp-io/std",
"sp-runtime-interface/std",
"sp-std/std",
]
103 changes: 0 additions & 103 deletions substrate/primitives/crypto/ec-utils/src/bls12_377.rs

This file was deleted.

Loading

0 comments on commit 31db70d

Please sign in to comment.