Skip to content

Commit

Permalink
sp-core: Rename VrfOutput to VrfPreOutput (#2534)
Browse files Browse the repository at this point in the history
This will make more sense after
#2524 since the
schnorrkel type for VRF outputs is also renamed in the latest version.
Can be reviewed independently though.

Can be merged after #1577
so that there is less pain for @davxy.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
  • Loading branch information
andresilva and bkchr authored Dec 6, 2023
1 parent 91cbe08 commit 1f023de
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use super::*;
use crate::backend::Backend;
use polkadot_node_primitives::approval::v1::{
AssignmentCert, AssignmentCertKind, VrfOutput, VrfProof, VrfSignature, RELAY_VRF_MODULO_CONTEXT,
AssignmentCert, AssignmentCertKind, VrfPreOutput, VrfProof, VrfSignature,
RELAY_VRF_MODULO_CONTEXT,
};
use polkadot_node_subsystem_util::database::Database;
use sp_application_crypto::sp_core::H256;
Expand All @@ -30,9 +31,12 @@ fn dummy_assignment_cert(kind: AssignmentCertKind) -> AssignmentCert {
let mut prng = rand_core::OsRng;
let keypair = schnorrkel::Keypair::generate_with(&mut prng);
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();
let preout = inout.to_output();

AssignmentCert { kind, vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) } }
AssignmentCert {
kind,
vrf: VrfSignature { pre_output: VrfPreOutput(preout), proof: VrfProof(proof) },
}
}

fn make_block_entry_v1(
Expand Down
18 changes: 10 additions & 8 deletions polkadot/node/core/approval-voting/src/criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use parity_scale_codec::{Decode, Encode};
use polkadot_node_primitives::approval::{
self as approval_types,
v1::{AssignmentCert, AssignmentCertKind, DelayTranche, RelayVRFStory},
v2::{AssignmentCertKindV2, AssignmentCertV2, CoreBitfield, VrfOutput, VrfProof, VrfSignature},
v2::{
AssignmentCertKindV2, AssignmentCertV2, CoreBitfield, VrfPreOutput, VrfProof, VrfSignature,
},
};
use polkadot_primitives::{
AssignmentId, AssignmentPair, CandidateHash, CoreIndex, GroupIndex, IndexedVec, SessionInfo,
Expand Down Expand Up @@ -459,7 +461,7 @@ fn compute_relay_vrf_modulo_assignments_v1(
let cert = AssignmentCert {
kind: AssignmentCertKind::RelayVRFModulo { sample: rvm_sample },
vrf: VrfSignature {
output: VrfOutput(vrf_in_out.to_output()),
pre_output: VrfPreOutput(vrf_in_out.to_output()),
proof: VrfProof(vrf_proof),
},
};
Expand Down Expand Up @@ -539,7 +541,7 @@ fn compute_relay_vrf_modulo_assignments_v2(
core_bitfield: assignment_bitfield.clone(),
},
vrf: VrfSignature {
output: VrfOutput(vrf_in_out.to_output()),
pre_output: VrfPreOutput(vrf_in_out.to_output()),
proof: VrfProof(vrf_proof),
},
};
Expand Down Expand Up @@ -574,7 +576,7 @@ fn compute_relay_vrf_delay_assignments(
let cert = AssignmentCertV2 {
kind: AssignmentCertKindV2::RelayVRFDelay { core_index: core },
vrf: VrfSignature {
output: VrfOutput(vrf_in_out.to_output()),
pre_output: VrfPreOutput(vrf_in_out.to_output()),
proof: VrfProof(vrf_proof),
},
};
Expand Down Expand Up @@ -689,7 +691,7 @@ pub(crate) fn check_assignment_cert(
}
}

let vrf_output = &assignment.vrf.output;
let vrf_pre_output = &assignment.vrf.pre_output;
let vrf_proof = &assignment.vrf.proof;
let first_claimed_core_index =
claimed_core_indices.first_one().expect("Checked above; qed") as u32;
Expand All @@ -704,7 +706,7 @@ pub(crate) fn check_assignment_cert(
let (vrf_in_out, _) = public
.vrf_verify_extra(
relay_vrf_modulo_transcript_v2(relay_vrf_story),
&vrf_output.0,
&vrf_pre_output.0,
&vrf_proof.0,
assigned_cores_transcript(core_bitfield),
)
Expand Down Expand Up @@ -753,7 +755,7 @@ pub(crate) fn check_assignment_cert(
let (vrf_in_out, _) = public
.vrf_verify_extra(
relay_vrf_modulo_transcript_v1(relay_vrf_story, *sample),
&vrf_output.0,
&vrf_pre_output.0,
&vrf_proof.0,
assigned_core_transcript(CoreIndex(first_claimed_core_index)),
)
Expand Down Expand Up @@ -791,7 +793,7 @@ pub(crate) fn check_assignment_cert(
let (vrf_in_out, _) = public
.vrf_verify(
relay_vrf_delay_transcript(relay_vrf_story, *core_index),
&vrf_output.0,
&vrf_pre_output.0,
&vrf_proof.0,
)
.map_err(|_| InvalidAssignment(Reason::VRFDelayOutputMismatch))?;
Expand Down
16 changes: 11 additions & 5 deletions polkadot/node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::backend::V1ReadBackend;
use polkadot_node_primitives::{
approval::{
v1::{
AssignmentCert, AssignmentCertKind, DelayTranche, VrfOutput, VrfProof, VrfSignature,
AssignmentCert, AssignmentCertKind, DelayTranche, VrfPreOutput, VrfProof, VrfSignature,
RELAY_VRF_MODULO_CONTEXT,
},
v2::{AssignmentCertKindV2, AssignmentCertV2},
Expand Down Expand Up @@ -415,9 +415,12 @@ fn garbage_assignment_cert(kind: AssignmentCertKind) -> AssignmentCert {
let mut prng = rand_core::OsRng;
let keypair = schnorrkel::Keypair::generate_with(&mut prng);
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();
let preout = inout.to_output();

AssignmentCert { kind, vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) } }
AssignmentCert {
kind,
vrf: VrfSignature { pre_output: VrfPreOutput(preout), proof: VrfProof(proof) },
}
}

fn garbage_assignment_cert_v2(kind: AssignmentCertKindV2) -> AssignmentCertV2 {
Expand All @@ -426,9 +429,12 @@ fn garbage_assignment_cert_v2(kind: AssignmentCertKindV2) -> AssignmentCertV2 {
let mut prng = rand_core::OsRng;
let keypair = schnorrkel::Keypair::generate_with(&mut prng);
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();
let preout = inout.to_output();

AssignmentCertV2 { kind, vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) } }
AssignmentCertV2 {
kind,
vrf: VrfSignature { pre_output: VrfPreOutput(preout), proof: VrfProof(proof) },
}
}

fn sign_approval(
Expand Down
10 changes: 5 additions & 5 deletions polkadot/node/network/approval-distribution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use polkadot_node_network_protocol::{
};
use polkadot_node_primitives::approval::{
v1::{
AssignmentCert, AssignmentCertKind, IndirectAssignmentCert, VrfOutput, VrfProof,
AssignmentCert, AssignmentCertKind, IndirectAssignmentCert, VrfPreOutput, VrfProof,
VrfSignature,
},
v2::{
Expand Down Expand Up @@ -298,14 +298,14 @@ fn fake_assignment_cert(block_hash: Hash, validator: ValidatorIndex) -> Indirect
let mut prng = rand_core::OsRng;
let keypair = schnorrkel::Keypair::generate_with(&mut prng);
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();
let preout = inout.to_output();

IndirectAssignmentCert {
block_hash,
validator,
cert: AssignmentCert {
kind: AssignmentCertKind::RelayVRFModulo { sample: 1 },
vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) },
vrf: VrfSignature { pre_output: VrfPreOutput(preout), proof: VrfProof(proof) },
},
}
}
Expand All @@ -320,14 +320,14 @@ fn fake_assignment_cert_v2(
let mut prng = rand_core::OsRng;
let keypair = schnorrkel::Keypair::generate_with(&mut prng);
let (inout, proof, _) = keypair.vrf_sign(ctx.bytes(msg));
let out = inout.to_output();
let preout = inout.to_output();

IndirectAssignmentCertV2 {
block_hash,
validator,
cert: AssignmentCertV2 {
kind: AssignmentCertKindV2::RelayVRFModuloCompact { core_bitfield },
vrf: VrfSignature { output: VrfOutput(out), proof: VrfProof(proof) },
vrf: VrfSignature { pre_output: VrfPreOutput(preout), proof: VrfProof(proof) },
},
}
}
Expand Down
20 changes: 10 additions & 10 deletions polkadot/node/primitives/src/approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
pub mod v1 {
use sp_consensus_babe as babe_primitives;
pub use sp_consensus_babe::{
Randomness, Slot, VrfOutput, VrfProof, VrfSignature, VrfTranscript,
Randomness, Slot, VrfPreOutput, VrfProof, VrfSignature, VrfTranscript,
};

use parity_scale_codec::{Decode, Encode};
Expand Down Expand Up @@ -145,14 +145,14 @@ pub mod v1 {
AuthorityOutOfBounds(usize),
}

/// An unsafe VRF output. Provide BABE Epoch info to create a `RelayVRFStory`.
pub struct UnsafeVRFOutput {
vrf_output: VrfOutput,
/// An unsafe VRF pre-output. Provide BABE Epoch info to create a `RelayVRFStory`.
pub struct UnsafeVRFPreOutput {
vrf_pre_output: VrfPreOutput,
slot: Slot,
authority_index: u32,
}

impl UnsafeVRFOutput {
impl UnsafeVRFPreOutput {
/// Get the slot.
pub fn slot(&self) -> Slot {
self.slot
Expand All @@ -177,7 +177,7 @@ pub mod v1 {
sp_consensus_babe::make_vrf_transcript(randomness, self.slot, epoch_index);

let inout = self
.vrf_output
.vrf_pre_output
.0
.attach_input_hash(&pubkey, transcript.0)
.map_err(ApprovalError::SchnorrkelSignature)?;
Expand All @@ -190,16 +190,16 @@ pub mod v1 {
/// This fails if either there is no BABE `PreRuntime` digest or
/// the digest has type `SecondaryPlain`, which Substrate nodes do
/// not produce or accept anymore.
pub fn babe_unsafe_vrf_info(header: &Header) -> Option<UnsafeVRFOutput> {
pub fn babe_unsafe_vrf_info(header: &Header) -> Option<UnsafeVRFPreOutput> {
use babe_primitives::digests::CompatibleDigestItem;

for digest in &header.digest.logs {
if let Some(pre) = digest.as_babe_pre_digest() {
let slot = pre.slot();
let authority_index = pre.authority_index();

return pre.vrf_signature().map(|sig| UnsafeVRFOutput {
vrf_output: sig.output.clone(),
return pre.vrf_signature().map(|sig| UnsafeVRFPreOutput {
vrf_pre_output: sig.pre_output.clone(),
slot,
authority_index,
})
Expand All @@ -214,7 +214,7 @@ pub mod v1 {
pub mod v2 {
use parity_scale_codec::{Decode, Encode};
pub use sp_consensus_babe::{
Randomness, Slot, VrfOutput, VrfProof, VrfSignature, VrfTranscript,
Randomness, Slot, VrfPreOutput, VrfProof, VrfSignature, VrfTranscript,
};
use std::ops::BitOr;

Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/babe/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn claim_primary_slot(
.make_bytes::<AUTHORING_SCORE_LENGTH>(
AUTHORING_SCORE_VRF_CONTEXT,
&data.as_ref(),
&vrf_signature.output,
&vrf_signature.pre_output,
)
.map(|bytes| u128::from_le_bytes(bytes) < threshold)
.unwrap_or_default();
Expand Down
8 changes: 4 additions & 4 deletions substrate/client/consensus/babe/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ fn claim_vrf_check() {
};
let data = make_vrf_sign_data(&epoch.randomness.clone(), 0.into(), epoch.epoch_index);
let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
assert_eq!(pre_digest.vrf_signature.output, sign.output);
assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);

// We expect a SecondaryVRF claim for slot 1
let pre_digest = match claim_slot(1.into(), &epoch, &keystore).unwrap().0 {
Expand All @@ -589,7 +589,7 @@ fn claim_vrf_check() {
};
let data = make_vrf_sign_data(&epoch.randomness.clone(), 1.into(), epoch.epoch_index);
let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
assert_eq!(pre_digest.vrf_signature.output, sign.output);
assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);

// Check that correct epoch index has been used if epochs are skipped (primary VRF)
let slot = Slot::from(103);
Expand All @@ -601,7 +601,7 @@ fn claim_vrf_check() {
let data = make_vrf_sign_data(&epoch.randomness.clone(), slot, fixed_epoch.epoch_index);
let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
assert_eq!(fixed_epoch.epoch_index, 11);
assert_eq!(claim.vrf_signature.output, sign.output);
assert_eq!(claim.vrf_signature.pre_output, sign.pre_output);

// Check that correct epoch index has been used if epochs are skipped (secondary VRF)
let slot = Slot::from(100);
Expand All @@ -613,7 +613,7 @@ fn claim_vrf_check() {
let data = make_vrf_sign_data(&epoch.randomness.clone(), slot, fixed_epoch.epoch_index);
let sign = keystore.sr25519_vrf_sign(AuthorityId::ID, &public, &data).unwrap().unwrap();
assert_eq!(fixed_epoch.epoch_index, 11);
assert_eq!(pre_digest.vrf_signature.output, sign.output);
assert_eq!(pre_digest.vrf_signature.pre_output, sign.pre_output);
}

// Propose and import a new BABE block on top of the given parent.
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/babe/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn check_primary_header<B: BlockT + Sized>(
.make_bytes::<AUTHORING_SCORE_LENGTH>(
AUTHORING_SCORE_VRF_CONTEXT,
&data.as_ref(),
&pre_digest.vrf_signature.output,
&pre_digest.vrf_signature.pre_output,
)
.map(u128::from_le_bytes)
.map_err(|_| babe_err(Error::VrfVerificationFailed))?;
Expand Down
22 changes: 11 additions & 11 deletions substrate/client/keystore/src/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ impl LocalKeystore {
Ok(sig)
}

fn vrf_output<T: CorePair + VrfSecret>(
fn vrf_pre_output<T: CorePair + VrfSecret>(
&self,
key_type: KeyTypeId,
public: &T::Public,
input: &T::VrfInput,
) -> std::result::Result<Option<T::VrfOutput>, TraitError> {
let preout = self
) -> std::result::Result<Option<T::VrfPreOutput>, TraitError> {
let pre_output = self
.0
.read()
.key_pair_by_type::<T>(public, key_type)?
.map(|pair| pair.vrf_output(input));
Ok(preout)
.map(|pair| pair.vrf_pre_output(input));
Ok(pre_output)
}
}

Expand Down Expand Up @@ -188,13 +188,13 @@ impl Keystore for LocalKeystore {
self.vrf_sign::<sr25519::Pair>(key_type, public, data)
}

fn sr25519_vrf_output(
fn sr25519_vrf_pre_output(
&self,
key_type: KeyTypeId,
public: &sr25519::Public,
input: &sr25519::vrf::VrfInput,
) -> std::result::Result<Option<sr25519::vrf::VrfOutput>, TraitError> {
self.vrf_output::<sr25519::Pair>(key_type, public, input)
) -> std::result::Result<Option<sr25519::vrf::VrfPreOutput>, TraitError> {
self.vrf_pre_output::<sr25519::Pair>(key_type, public, input)
}

fn ed25519_public_keys(&self, key_type: KeyTypeId) -> Vec<ed25519::Public> {
Expand Down Expand Up @@ -293,13 +293,13 @@ impl Keystore for LocalKeystore {
self.vrf_sign::<bandersnatch::Pair>(key_type, public, data)
}

fn bandersnatch_vrf_output(
fn bandersnatch_vrf_pre_output(
&self,
key_type: KeyTypeId,
public: &bandersnatch::Public,
input: &bandersnatch::vrf::VrfInput,
) -> std::result::Result<Option<bandersnatch::vrf::VrfOutput>, TraitError> {
self.vrf_output::<bandersnatch::Pair>(key_type, public, input)
) -> std::result::Result<Option<bandersnatch::vrf::VrfPreOutput>, TraitError> {
self.vrf_pre_output::<bandersnatch::Pair>(key_type, public, input)
}

fn bandersnatch_ring_vrf_sign(
Expand Down
6 changes: 5 additions & 1 deletion substrate/frame/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,11 @@ pub mod pallet {
});

public
.make_bytes(RANDOMNESS_VRF_CONTEXT, &transcript, &signature.output)
.make_bytes(
RANDOMNESS_VRF_CONTEXT,
&transcript,
&signature.pre_output,
)
.ok()
});

Expand Down
Loading

0 comments on commit 1f023de

Please sign in to comment.