Skip to content

Commit

Permalink
Arkworks Elliptic Curve utils overhaul (#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 19f38ca commit 38ef04e
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 973 deletions.
174 changes: 9 additions & 165 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading

0 comments on commit 38ef04e

Please sign in to comment.