mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 20:17:57 +00:00
VRF refactory (#13889)
* First iteration to encapsulate schnorrkel and merlin usage * Remove schnorkel direct dependency from BABE pallet * Remove schnorrkel direct dependency from BABE client * Trivial renaming for VrfTranscript data and value * Better errors * Expose a function to get a schnorrkel friendly transcript * Keep the vrf signature stuff together (preventing some clones around) * Fix tests * Remove vrf agnostic transcript and define it as an associated type for VrfSigner and VrfVerifier * Fix babe pallet mock * Inner types are required to be public for polkadot * Update client/consensus/babe/src/verification.rs Co-authored-by: Koute <koute@users.noreply.github.com> * Nit * Remove Deref implementations * make_bytes as a method * Trigger CI --------- Co-authored-by: Koute <koute@users.noreply.github.com>
This commit is contained in:
@@ -19,14 +19,15 @@
|
||||
|
||||
use super::{
|
||||
AllowedSlots, AuthorityId, AuthorityIndex, AuthoritySignature, BabeAuthorityWeight,
|
||||
BabeEpochConfiguration, Slot, BABE_ENGINE_ID,
|
||||
BabeEpochConfiguration, Randomness, Slot, BABE_ENGINE_ID,
|
||||
};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use scale_info::TypeInfo;
|
||||
|
||||
use sp_core::sr25519::vrf::VrfSignature;
|
||||
use sp_runtime::{DigestItem, RuntimeDebug};
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
use sp_consensus_vrf::schnorrkel::{Randomness, VRFOutput, VRFProof};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use scale_info::TypeInfo;
|
||||
|
||||
/// Raw BABE primary slot assignment pre-digest.
|
||||
#[derive(Clone, RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo)]
|
||||
@@ -35,10 +36,8 @@ pub struct PrimaryPreDigest {
|
||||
pub authority_index: super::AuthorityIndex,
|
||||
/// Slot
|
||||
pub slot: Slot,
|
||||
/// VRF output
|
||||
pub vrf_output: VRFOutput,
|
||||
/// VRF proof
|
||||
pub vrf_proof: VRFProof,
|
||||
/// VRF signature
|
||||
pub vrf_signature: VrfSignature,
|
||||
}
|
||||
|
||||
/// BABE secondary slot assignment pre-digest.
|
||||
@@ -62,10 +61,8 @@ pub struct SecondaryVRFPreDigest {
|
||||
pub authority_index: super::AuthorityIndex,
|
||||
/// Slot
|
||||
pub slot: Slot,
|
||||
/// VRF output
|
||||
pub vrf_output: VRFOutput,
|
||||
/// VRF proof
|
||||
pub vrf_proof: VRFProof,
|
||||
/// VRF signature
|
||||
pub vrf_signature: VrfSignature,
|
||||
}
|
||||
|
||||
/// A BABE pre-runtime digest. This contains all data required to validate a
|
||||
@@ -118,11 +115,10 @@ impl PreDigest {
|
||||
}
|
||||
|
||||
/// Returns the VRF output and proof, if they exist.
|
||||
pub fn vrf(&self) -> Option<(&VRFOutput, &VRFProof)> {
|
||||
pub fn vrf_signature(&self) -> Option<&VrfSignature> {
|
||||
match self {
|
||||
PreDigest::Primary(primary) => Some((&primary.vrf_output, &primary.vrf_proof)),
|
||||
PreDigest::SecondaryVRF(secondary) =>
|
||||
Some((&secondary.vrf_output, &secondary.vrf_proof)),
|
||||
PreDigest::Primary(primary) => Some(&primary.vrf_signature),
|
||||
PreDigest::SecondaryVRF(secondary) => Some(&secondary.vrf_signature),
|
||||
PreDigest::SecondaryPlain(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,22 +23,17 @@
|
||||
pub mod digests;
|
||||
pub mod inherents;
|
||||
|
||||
pub use merlin::Transcript;
|
||||
pub use sp_consensus_vrf::schnorrkel::{
|
||||
Randomness, RANDOMNESS_LENGTH, VRF_OUTPUT_LENGTH, VRF_PROOF_LENGTH,
|
||||
};
|
||||
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use scale_info::TypeInfo;
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
#[cfg(feature = "std")]
|
||||
use sp_keystore::vrf::{VRFTranscriptData, VRFTranscriptValue};
|
||||
use sp_runtime::{traits::Header, ConsensusEngineId, RuntimeDebug};
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
use crate::digests::{NextConfigDescriptor, NextEpochDescriptor};
|
||||
|
||||
pub use sp_core::sr25519::vrf::{VrfOutput, VrfProof, VrfSignature, VrfTranscript};
|
||||
|
||||
/// Key type for BABE module.
|
||||
pub const KEY_TYPE: sp_core::crypto::KeyTypeId = sp_application_crypto::key_types::BABE;
|
||||
|
||||
@@ -47,11 +42,14 @@ mod app {
|
||||
app_crypto!(sr25519, BABE);
|
||||
}
|
||||
|
||||
/// The prefix used by BABE for its VRF keys.
|
||||
pub const BABE_VRF_PREFIX: &[u8] = b"substrate-babe-vrf";
|
||||
/// VRF context used for per-slot randomness generation.
|
||||
pub const RANDOMNESS_VRF_CONTEXT: &[u8] = b"BabeVRFInOutContext";
|
||||
|
||||
/// BABE VRFInOut context.
|
||||
pub static BABE_VRF_INOUT_CONTEXT: &[u8] = b"BabeVRFInOutContext";
|
||||
/// VRF output length for per-slot randomness.
|
||||
pub const RANDOMNESS_LENGTH: usize = 32;
|
||||
|
||||
/// Randomness type required by BABE operations.
|
||||
pub type Randomness = [u8; RANDOMNESS_LENGTH];
|
||||
|
||||
/// A Babe authority keypair. Necessarily equivalent to the schnorrkel public key used in
|
||||
/// the main Babe module. If that ever changes, then this must, too.
|
||||
@@ -96,26 +94,16 @@ pub type BabeAuthorityWeight = u64;
|
||||
/// of 0 (regardless of whether they are plain or vrf secondary blocks).
|
||||
pub type BabeBlockWeight = u32;
|
||||
|
||||
/// Make a VRF transcript from given randomness, slot number and epoch.
|
||||
pub fn make_transcript(randomness: &Randomness, slot: Slot, epoch: u64) -> Transcript {
|
||||
let mut transcript = Transcript::new(&BABE_ENGINE_ID);
|
||||
transcript.append_u64(b"slot number", *slot);
|
||||
transcript.append_u64(b"current epoch", epoch);
|
||||
transcript.append_message(b"chain randomness", &randomness[..]);
|
||||
transcript
|
||||
}
|
||||
|
||||
/// Make a VRF transcript data container
|
||||
#[cfg(feature = "std")]
|
||||
pub fn make_transcript_data(randomness: &Randomness, slot: Slot, epoch: u64) -> VRFTranscriptData {
|
||||
VRFTranscriptData {
|
||||
label: &BABE_ENGINE_ID,
|
||||
items: vec![
|
||||
("slot number", VRFTranscriptValue::U64(*slot)),
|
||||
("current epoch", VRFTranscriptValue::U64(epoch)),
|
||||
("chain randomness", VRFTranscriptValue::Bytes(randomness.to_vec())),
|
||||
pub fn make_transcript(randomness: &Randomness, slot: Slot, epoch: u64) -> VrfTranscript {
|
||||
VrfTranscript::new(
|
||||
&BABE_ENGINE_ID,
|
||||
&[
|
||||
(b"slot number", &slot.to_le_bytes()),
|
||||
(b"current epoch", &epoch.to_le_bytes()),
|
||||
(b"chain randomness", randomness),
|
||||
],
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/// An consensus log item for BABE.
|
||||
@@ -355,7 +343,7 @@ pub struct Epoch {
|
||||
/// The authorities and their weights.
|
||||
pub authorities: Vec<(AuthorityId, BabeAuthorityWeight)>,
|
||||
/// Randomness for this epoch.
|
||||
pub randomness: [u8; VRF_OUTPUT_LENGTH],
|
||||
pub randomness: Randomness,
|
||||
/// Configuration of the epoch.
|
||||
pub config: BabeEpochConfiguration,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user