mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 13:31:10 +00:00
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:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user