Refinements to VRF types (#14036)

* Allow extra signing data

* Fix tests after renaming

* Rename VrfSecret/VrfVerifier to VrfSecret/VrfPublic

* Further encrapsulation of 'transcript' type to the sr25519 implementation

* Keystore sr25519 pre-output

* Leave additional custom input field hidden in the associated VrfInput type

* Fix test

* More ergonomic output_bytes

* Trigger pipeline

* Define a separated type for vrf signature data

* Fix docs

* Fix doc

* Remove annotation

* Directly use dleq_proove and dleq_verify in sr25519

* Trigger CI

* Remove cruft before merge
This commit is contained in:
Davide Galassi
2023-05-04 15:41:59 +02:00
committed by GitHub
parent 93165bc4d2
commit 3a90728de0
12 changed files with 384 additions and 138 deletions
+60 -11
View File
@@ -20,7 +20,7 @@
use crate::{Error, Keystore, KeystorePtr};
use sp_core::{
crypto::{ByteArray, KeyTypeId, Pair, VrfSigner},
crypto::{ByteArray, KeyTypeId, Pair, VrfSecret},
ecdsa, ed25519, sr25519,
};
@@ -99,15 +99,25 @@ impl MemoryKeystore {
Ok(sig)
}
fn vrf_sign<T: Pair + VrfSigner>(
fn vrf_sign<T: Pair + VrfSecret>(
&self,
key_type: KeyTypeId,
public: &T::Public,
transcript: &T::VrfInput,
data: &T::VrfSignData,
) -> Result<Option<T::VrfSignature>, Error> {
let sig = self.pair::<T>(key_type, public).map(|pair| pair.vrf_sign(transcript));
let sig = self.pair::<T>(key_type, public).map(|pair| pair.vrf_sign(data));
Ok(sig)
}
fn vrf_output<T: Pair + VrfSecret>(
&self,
key_type: KeyTypeId,
public: &T::Public,
input: &T::VrfInput,
) -> Result<Option<T::VrfOutput>, Error> {
let preout = self.pair::<T>(key_type, public).map(|pair| pair.vrf_output(input));
Ok(preout)
}
}
impl Keystore for MemoryKeystore {
@@ -136,9 +146,18 @@ impl Keystore for MemoryKeystore {
&self,
key_type: KeyTypeId,
public: &sr25519::Public,
transcript: &sr25519::vrf::VrfTranscript,
data: &sr25519::vrf::VrfSignData,
) -> Result<Option<sr25519::vrf::VrfSignature>, Error> {
self.vrf_sign::<sr25519::Pair>(key_type, public, transcript)
self.vrf_sign::<sr25519::Pair>(key_type, public, data)
}
fn sr25519_vrf_output(
&self,
key_type: KeyTypeId,
public: &sr25519::Public,
input: &sr25519::vrf::VrfInput,
) -> Result<Option<sr25519::vrf::VrfOutput>, Error> {
self.vrf_output::<sr25519::Pair>(key_type, public, input)
}
fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
@@ -267,7 +286,36 @@ mod tests {
let secret_uri = "//Alice";
let key_pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
let transcript = sr25519::vrf::VrfTranscript::new(
let data = sr25519::vrf::VrfInput::new(
b"Test",
&[
(b"one", &1_u64.to_le_bytes()),
(b"two", &2_u64.to_le_bytes()),
(b"three", "test".as_bytes()),
],
)
.into_sign_data();
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &data);
assert!(result.unwrap().is_none());
store
.insert(SR25519, secret_uri, key_pair.public().as_ref())
.expect("Inserts unknown key");
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &data);
assert!(result.unwrap().is_some());
}
#[test]
fn vrf_output() {
let store = MemoryKeystore::new();
let secret_uri = "//Alice";
let pair = sr25519::Pair::from_string(secret_uri, None).expect("Generates key pair");
let input = sr25519::vrf::VrfInput::new(
b"Test",
&[
(b"one", &1_u64.to_le_bytes()),
@@ -276,16 +324,17 @@ mod tests {
],
);
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &transcript);
let result = store.sr25519_vrf_output(SR25519, &pair.public(), &input);
assert!(result.unwrap().is_none());
store
.insert(SR25519, secret_uri, key_pair.public().as_ref())
.insert(SR25519, secret_uri, pair.public().as_ref())
.expect("Inserts unknown key");
let result = store.sr25519_vrf_sign(SR25519, &key_pair.public(), &transcript);
let preout = store.sr25519_vrf_output(SR25519, &pair.public(), &input).unwrap().unwrap();
assert!(result.unwrap().is_some());
let result = preout.make_bytes::<32>(b"rand", &input, &pair.public());
assert!(result.is_ok());
}
#[test]