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.
+127 -2
View File
@@ -19,6 +19,8 @@
use crate::{Error, Keystore, KeystorePtr};
#[cfg(feature = "bandersnatch-experimental")]
use sp_core::bandersnatch;
#[cfg(feature = "bls-experimental")]
use sp_core::{bls377, bls381};
use sp_core::{
@@ -214,6 +216,64 @@ impl Keystore for MemoryKeystore {
Ok(sig)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_public_keys(&self, key_type: KeyTypeId) -> Vec<bandersnatch::Public> {
self.public_keys::<bandersnatch::Pair>(key_type)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_generate_new(
&self,
key_type: KeyTypeId,
seed: Option<&str>,
) -> Result<bandersnatch::Public, Error> {
self.generate_new::<bandersnatch::Pair>(key_type, seed)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
msg: &[u8],
) -> Result<Option<bandersnatch::Signature>, Error> {
self.sign::<bandersnatch::Pair>(key_type, public, msg)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
data: &bandersnatch::vrf::VrfSignData,
) -> Result<Option<bandersnatch::vrf::VrfSignature>, Error> {
self.vrf_sign::<bandersnatch::Pair>(key_type, public, data)
}
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_ring_vrf_sign(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
data: &bandersnatch::vrf::VrfSignData,
prover: &bandersnatch::ring_vrf::RingProver,
) -> Result<Option<bandersnatch::ring_vrf::RingVrfSignature>, Error> {
let sig = self
.pair::<bandersnatch::Pair>(key_type, public)
.map(|pair| pair.ring_vrf_sign(data, prover));
Ok(sig)
}
#[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.vrf_output::<bandersnatch::Pair>(key_type, public, input)
}
#[cfg(feature = "bls-experimental")]
fn bls381_public_keys(&self, key_type: KeyTypeId) -> Vec<bls381::Public> {
self.public_keys::<bls381::Pair>(key_type)
@@ -330,7 +390,7 @@ mod tests {
}
#[test]
fn vrf_sign() {
fn sr25519_vrf_sign() {
let store = MemoryKeystore::new();
let secret_uri = "//Alice";
@@ -359,7 +419,7 @@ mod tests {
}
#[test]
fn vrf_output() {
fn sr25519_vrf_output() {
let store = MemoryKeystore::new();
let secret_uri = "//Alice";
@@ -406,4 +466,69 @@ mod tests {
let res = store.ecdsa_sign_prehashed(ECDSA, &pair.public(), &msg).unwrap();
assert!(res.is_some());
}
#[test]
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_vrf_sign() {
use sp_core::testing::BANDERSNATCH;
let store = MemoryKeystore::new();
let secret_uri = "//Alice";
let key_pair =
bandersnatch::Pair::from_string(secret_uri, None).expect("Generates key pair");
let in1 = bandersnatch::vrf::VrfInput::new("in", "foo");
let sign_data =
bandersnatch::vrf::VrfSignData::new_unchecked(b"Test", Some("m1"), Some(in1));
let result = store.bandersnatch_vrf_sign(BANDERSNATCH, &key_pair.public(), &sign_data);
assert!(result.unwrap().is_none());
store
.insert(BANDERSNATCH, secret_uri, key_pair.public().as_ref())
.expect("Inserts unknown key");
let result = store.bandersnatch_vrf_sign(BANDERSNATCH, &key_pair.public(), &sign_data);
assert!(result.unwrap().is_some());
}
#[test]
#[cfg(feature = "bandersnatch-experimental")]
fn bandersnatch_ring_vrf_sign() {
use sp_core::testing::BANDERSNATCH;
let store = MemoryKeystore::new();
let ring_ctx = bandersnatch::ring_vrf::RingContext::new_testing();
let mut pks: Vec<_> = (0..16)
.map(|i| bandersnatch::Pair::from_seed(&[i as u8; 32]).public())
.collect();
let prover_idx = 3;
let prover = ring_ctx.prover(&pks, prover_idx).unwrap();
let secret_uri = "//Alice";
let pair = bandersnatch::Pair::from_string(secret_uri, None).expect("Generates key pair");
pks[prover_idx] = pair.public();
let in1 = bandersnatch::vrf::VrfInput::new("in1", "foo");
let sign_data =
bandersnatch::vrf::VrfSignData::new_unchecked(b"Test", &["m1", "m2"], [in1]);
let result =
store.bandersnatch_ring_vrf_sign(BANDERSNATCH, &pair.public(), &sign_data, &prover);
assert!(result.unwrap().is_none());
store
.insert(BANDERSNATCH, secret_uri, pair.public().as_ref())
.expect("Inserts unknown key");
let result =
store.bandersnatch_ring_vrf_sign(BANDERSNATCH, &pair.public(), &sign_data, &prover);
assert!(result.unwrap().is_some());
}
}