Skip to content

Commit

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

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

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
  • Loading branch information
andresilva and bkchr committed Dec 6, 2023
1 parent 24d089f commit a6b46ab
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 141 deletions.
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
22 changes: 11 additions & 11 deletions substrate/frame/sassafras/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ pub mod pallet {
#[pallet::storage]
pub type RingVerifierData<T: Config> = StorageValue<_, vrf::RingVerifierData>;

/// Slot claim vrf-preoutput used to generate per-slot randomness.
/// Slot claim VRF pre-output used to generate per-slot randomness.
///
/// The value is ephemeral and is cleared on block finalization.
#[pallet::storage]
pub(crate) type ClaimTemporaryData<T> = StorageValue<_, vrf::VrfOutput>;
pub(crate) type ClaimTemporaryData<T> = StorageValue<_, vrf::VrfPreOutput>;

/// Genesis configuration for Sassafras protocol.
#[pallet::genesis_config]
Expand Down Expand Up @@ -324,12 +324,12 @@ pub mod pallet {
Self::post_genesis_initialize(claim.slot);
}

let randomness_output = claim
let randomness_pre_output = claim
.vrf_signature
.outputs
.pre_outputs
.get(0)
.expect("Valid claim must have vrf signature; qed");
ClaimTemporaryData::<T>::put(randomness_output);
.expect("Valid claim must have VRF signature; qed");
ClaimTemporaryData::<T>::put(randomness_pre_output);

let trigger_weight = T::EpochChangeTrigger::trigger::<T>(block_num);

Expand All @@ -346,9 +346,9 @@ pub mod pallet {
CurrentSlot::<T>::get(),
EpochIndex::<T>::get(),
);
let randomness_output = ClaimTemporaryData::<T>::take()
let randomness_pre_output = ClaimTemporaryData::<T>::take()
.expect("Unconditionally populated in `on_initialize`; `on_finalize` is always called after; qed");
let randomness = randomness_output
let randomness = randomness_pre_output
.make_bytes::<RANDOMNESS_LENGTH>(RANDOMNESS_VRF_CONTEXT, &randomness_input);
Self::deposit_slot_randomness(&randomness);

Expand Down Expand Up @@ -422,15 +422,15 @@ pub mod pallet {
for ticket in tickets {
debug!(target: LOG_TARGET, "Checking ring proof");

let Some(ticket_id_output) = ticket.signature.outputs.get(0) else {
debug!(target: LOG_TARGET, "Missing ticket vrf output from ring signature");
let Some(ticket_id_pre_output) = ticket.signature.pre_outputs.get(0) else {
debug!(target: LOG_TARGET, "Missing ticket VRF pre-output from ring signature");
continue
};
let ticket_id_input =
vrf::ticket_id_input(&randomness, ticket.body.attempt_idx, epoch_idx);

// Check threshold constraint
let ticket_id = vrf::make_ticket_id(&ticket_id_input, &ticket_id_output);
let ticket_id = vrf::make_ticket_id(&ticket_id_input, &ticket_id_pre_output);
if ticket_id >= ticket_threshold {
debug!(target: LOG_TARGET, "Ignoring ticket over threshold ({:032x} >= {:032x})", ticket_id, ticket_threshold);
continue
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/sassafras/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ pub fn make_ticket_body(attempt_idx: u32, pair: &AuthorityPair) -> (TicketId, Ti
let randomness = Sassafras::next_randomness();

let ticket_id_input = vrf::ticket_id_input(&randomness, attempt_idx, epoch);
let ticket_id_output = pair.as_inner_ref().vrf_output(&ticket_id_input);
let ticket_id_pre_output = pair.as_inner_ref().vrf_pre_output(&ticket_id_input);

let id = vrf::make_ticket_id(&ticket_id_input, &ticket_id_output);
let id = vrf::make_ticket_id(&ticket_id_input, &ticket_id_pre_output);

// Make a dummy ephemeral public that hopefully is unique within one test instance.
// In the tests, the values within the erased public are just used to compare
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/consensus/babe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use sp_std::vec::Vec;
use crate::digests::{NextConfigDescriptor, NextEpochDescriptor};

pub use sp_core::sr25519::vrf::{
VrfInput, VrfOutput, VrfProof, VrfSignData, VrfSignature, VrfTranscript,
VrfInput, VrfPreOutput, VrfProof, VrfSignData, VrfSignature, VrfTranscript,
};

/// Key type for BABE module.
Expand Down
24 changes: 12 additions & 12 deletions substrate/primitives/consensus/sassafras/src/vrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Utilities related to VRF input, output and signatures.
//! Utilities related to VRF input, pre-output and signatures.

use crate::{Randomness, TicketBody, TicketId};
use scale_codec::Encode;
Expand All @@ -24,7 +24,7 @@ use sp_std::vec::Vec;

pub use sp_core::bandersnatch::{
ring_vrf::{RingProver, RingVerifier, RingVerifierData, RingVrfSignature},
vrf::{VrfInput, VrfOutput, VrfSignData, VrfSignature},
vrf::{VrfInput, VrfPreOutput, VrfSignData, VrfSignature},
};

/// Ring VRF domain size for Sassafras consensus.
Expand Down Expand Up @@ -90,21 +90,21 @@ pub fn ticket_body_sign_data(ticket_body: &TicketBody, ticket_id_input: VrfInput
)
}

/// Make ticket-id from the given VRF input and output.
/// Make ticket-id from the given VRF input and pre-output.
///
/// Input should have been obtained via [`ticket_id_input`].
/// Output should have been obtained from the input directly using the vrf secret key
/// or from the vrf signature outputs.
pub fn make_ticket_id(input: &VrfInput, output: &VrfOutput) -> TicketId {
let bytes = output.make_bytes::<16>(b"ticket-id", input);
/// Pre-output should have been obtained from the input directly using the vrf
/// secret key or from the vrf signature pre-outputs.
pub fn make_ticket_id(input: &VrfInput, pre_output: &VrfPreOutput) -> TicketId {
let bytes = pre_output.make_bytes::<16>(b"ticket-id", input);
u128::from_le_bytes(bytes)
}

/// Make revealed key seed from a given VRF input and ouput.
/// Make revealed key seed from a given VRF input and pre-ouput.
///
/// Input should have been obtained via [`revealed_key_input`].
/// Output should have been obtained from the input directly using the vrf secret key
/// or from the vrf signature outputs.
pub fn make_revealed_key_seed(input: &VrfInput, output: &VrfOutput) -> [u8; 32] {
output.make_bytes::<32>(b"revealed-seed", input)
/// Pre-output should have been obtained from the input directly using the vrf
/// secret key or from the vrf signature pre-outputs.
pub fn make_revealed_key_seed(input: &VrfInput, pre_output: &VrfPreOutput) -> [u8; 32] {
pre_output.make_bytes::<32>(b"revealed-seed", input)
}
Loading

0 comments on commit a6b46ab

Please sign in to comment.