Bandersnatch VRF (#14412)

* Introduce bandersnatch vrf

* Some documentation

* Fix tests

* Fix docs refs

* Some more docs

* Comments about key derivation

* Make clippy happy

* Fix ring context enc/dec test

* Fix docs

* Switch to upstream ring-vrf

* Use sub-domains to construct VrfInput

* Bandersnatch VRF experimental feature

* Restore upstream dep

* Fix feature flags

* Apply typo fix

Co-authored-by: Anton <anton.kalyaev@gmail.com>

* Bump bandersnatch-vrfs

* Weiestrass form has been selected

* Rename bandersnatch testing app crypto id

* Support for seed recovery

* Clarified domain size <-> key size relationship

* cargo fmt

* Trigger CI

* Some required tweaks to crypto types

* Remove leftovers from Cargo.toml

* Remove some TODO notes

* Simplification of structs construction

* Trigger CI

* Apply review suggestion

Co-authored-by: Koute <koute@users.noreply.github.com>

* Docs typo

* Fix keystore tests

* Consistence

* Add ref to git rependency

* Static check of MAX_VRF_IOS value

* Clarify behavior for out of ring keys signatures

* Add test for ring-vrf to the keystore

* Fix docs

---------

Co-authored-by: Anton <anton.kalyaev@gmail.com>
Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
Davide Galassi
2023-08-09 17:09:47 +02:00
committed by GitHub
parent 8321cee4f5
commit 314109d87b
23 changed files with 1900 additions and 59 deletions
+155 -6
View File
@@ -19,6 +19,8 @@
pub mod testing;
#[cfg(feature = "bandersnatch-experimental")]
use sp_core::bandersnatch;
#[cfg(feature = "bls-experimental")]
use sp_core::{bls377, bls381};
use sp_core::{
@@ -174,6 +176,91 @@ pub trait Keystore: Send + Sync {
msg: &[u8; 32],
) -> Result<Option<ecdsa::Signature>, Error>;
/// Returns all the bandersnatch public keys for the given key type.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public>;
/// Generate a new bandersnatch key pair for the given key type and an optional seed.
///
/// Returns an `bandersnatch::Public` key of the generated key pair or an `Err` if
/// something failed during key generation.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_generate_new(
&self,
key_type: KeyTypeId,
seed: Option<&str>,
) -> Result<bandersnatch::Public, Error>;
/// Generate an bandersnatch signature for a given message.
///
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
/// them to a private key that exists in the keystore.
///
/// Returns an [`bandersnatch::Signature`] or `None` in case the given `key_type`
/// and `public` combination doesn't exist in the keystore.
/// An `Err` will be returned if generating the signature itself failed.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
msg: &[u8],
) -> Result<Option<bandersnatch::Signature>, Error>;
/// Generate a bandersnatch VRF signature for the given data.
///
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
/// them to a private key that exists in the keystore.
///
/// Returns `None` if the given `key_type` and `public` combination doesn't
/// exist in the keystore or an `Err` when something failed.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfSignData,
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error>;
/// Generate a bandersnatch VRF (pre)output for a given input data.
///
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
/// them to a private key that exists in the keystore.
///
/// Returns `None` if the given `key_type` and `public` combination doesn't
/// exist in the keystore or an `Err` when something failed.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_output(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfInput,
) -> Result<Option<bandersnatch::vrf::VrfOutput>, Error>;
/// Generate a bandersnatch ring-VRF signature for the given data.
///
/// Receives [`KeyTypeId`] and an [`bandersnatch::Public`] key to be able to map
/// them to a private key that exists in the keystore.
///
/// Also takes a [`bandersnatch::ring_vrf::RingProver`] instance obtained from
/// a valid [`bandersnatch::ring_vrf::RingContext`].
///
/// The ring signature is verifiable if the public key corresponding to the
/// signing [`bandersnatch::Pair`] is part of the ring from which the
/// [`bandersnatch::ring_vrf::RingProver`] has been constructed.
/// If not, the produced signature is just useless.
///
/// Returns `None` if the given `key_type` and `public` combination doesn't
/// exist in the keystore or an `Err` when something failed.
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_ring_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfSignData,
prover: &bandersnatch::ring_vrf::RingProver,
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error>;
/// Returns all bls12-381 public keys for the given key type.
#[cfg(feature = "bls-experimental")]
fn bls381_public_keys(&self, id: KeyTypeId) -> Vec<bls381::Public>;
@@ -258,6 +345,7 @@ pub trait Keystore: Send + Sync {
/// - sr25519
/// - ed25519
/// - ecdsa
/// - bandersnatch
/// - bls381
/// - bls377
///
@@ -291,6 +379,12 @@ pub trait Keystore: Send + Sync {
self.ecdsa_sign(id, &public, msg)?.map(|s| s.encode())
},
#[cfg(feature = "bandersnatch-experimental")]
bandersnatch::CRYPTO_ID => {
let public = bandersnatch::Public::from_slice(public)
.map_err(|_| Error::ValidationError("Invalid public key format".into()))?;
self.bandersnatch_sign(id, &public, msg)?.map(|s| s.encode())
},
#[cfg(feature = "bls-experimental")]
bls381::CRYPTO_ID => {
let public = bls381::Public::from_slice(public)
@@ -400,16 +494,59 @@ impl<T: Keystore + ?Sized> Keystore for Arc<T> {
(**self).ecdsa_sign_prehashed(key_type, public, msg)
}
fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
(**self).insert(key_type, suri, public)
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
(**self).bandersnatch_public_keys(key_type)
}
fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
(**self).keys(key_type)
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_generate_new(
&self,
key_type: KeyTypeId,
seed: Option<&str>,
) -> Result<bandersnatch::Public, Error> {
(**self).bandersnatch_generate_new(key_type, seed)
}
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
(**self).has_keys(public_keys)
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
msg: &[u8],
) -> Result<Option<bandersnatch::Signature>, Error> {
(**self).bandersnatch_sign(key_type, public, msg)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfSignData,
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
(**self).bandersnatch_vrf_sign(key_type, public, input)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_output(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfInput,
) -> Result<Option<bandersnatch::vrf::VrfOutput>, Error> {
(**self).bandersnatch_vrf_output(key_type, public, input)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_ring_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfSignData,
prover: &bandersnatch::ring_vrf::RingProver,
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
(**self).bandersnatch_ring_vrf_sign(key_type, public, input, prover)
}
#[cfg(feature = "bls-experimental")]
@@ -459,6 +596,18 @@ impl<T: Keystore + ?Sized> Keystore for Arc<T> {
) -> Result<Option<bls377::Signature>, Error> {
(**self).bls377_sign(key_type, public, msg)
}
fn insert(&self, key_type: KeyTypeId, suri: &str, public: &[u8]) -> Result<(), ()> {
(**self).insert(key_type, suri, public)
}
fn keys(&self, key_type: KeyTypeId) -> Result<Vec<Vec<u8>>, Error> {
(**self).keys(key_type)
}
fn has_keys(&self, public_keys: &[(Vec<u8>, KeyTypeId)]) -> bool {
(**self).has_keys(public_keys)
}
}
/// A shared pointer to a keystore implementation.