Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Babe VRF Signing in keystore #6225

Merged
merged 36 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6a6b33f
Introduce trait
Jun 2, 2020
363bb02
Implement VRFSigner in keystore
Jun 2, 2020
45c7582
Use vrf_sign from keystore
Jun 2, 2020
4fcf607
Convert output to VRFInOut
Jun 2, 2020
3c148b7
Simplify conversion
Jun 2, 2020
3ca49f6
vrf_sign secondary slot using keystore
Jun 2, 2020
f292623
Fix RPC call to claim_slot
Jun 2, 2020
df6063d
Use Public instead of Pair
Jun 2, 2020
76bcb2c
Check primary threshold in signer
Jun 4, 2020
81e31c5
Fix interface to return error
Jun 4, 2020
1d64e56
Move vrf_sign to BareCryptoStore
Jun 4, 2020
95a7b93
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 4, 2020
f62b1d7
Fix authorship_works test
Jun 4, 2020
04ec073
Fix BABE logic leaks
Jun 7, 2020
6c6f380
Acquire a read lock once
Jun 7, 2020
c572d13
Also fix RPC acquiring the read lock once
Jun 7, 2020
b5415f6
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 7, 2020
e4db9bf
Implement a generic way to construct VRF Transcript
Jun 8, 2020
b581c6c
Use make_transcript_data to call sr25519_vrf_sign
Jun 8, 2020
022b706
Make sure VRFTranscriptData is serializable
Jun 8, 2020
0c23a48
Cleanup
Jun 8, 2020
c3f17de
Move VRF to it's own module
Jun 8, 2020
a691f24
Implement & test VRF signing in testing module
Jun 8, 2020
a5821f0
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 8, 2020
b5322c3
Remove leftover
Jun 8, 2020
53bed39
Fix feature requirements
Jun 8, 2020
43596d7
Revert removing vec macro
Jun 8, 2020
61e3b8d
Drop keystore pointer to prevent deadlock
Jun 8, 2020
383bf44
Nitpicks
Jun 8, 2020
2508594
Add test to make sure make_transcript works
Jun 8, 2020
1fd6936
Fix mismatch in VRF transcript
Jun 10, 2020
b45a1ca
Add a test to verify transcripts match in babe
Jun 10, 2020
df7dd65
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 10, 2020
67b1c03
Return VRFOutput and VRFProof from keystore
Jun 10, 2020
e46467b
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 10, 2020
ecd7cf1
Merge remote-tracking branch 'upstream/master' into vrf-signing
Jun 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 36 additions & 27 deletions client/consensus/babe/src/authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
//! BABE authority selection and slot claiming.

use sp_consensus_babe::{
make_transcript, AuthorityId, BabeAuthorityWeight, BABE_VRF_PREFIX,
BABE_ENGINE_ID, BABE_VRF_PREFIX,
AuthorityId, BabeAuthorityWeight,
SlotNumber, AuthorityPair,
make_transcript,
};
use sp_consensus_babe::digests::{
PreDigest, PrimaryPreDigest, SecondaryPlainPreDigest, SecondaryVRFPreDigest,
};
use sp_consensus_vrf::schnorrkel::{VRFOutput, VRFProof};
use sp_core::{U256, blake2_256};
use sp_core::{U256, blake2_256, traits::VRFSigner};
use codec::Encode;
use schnorrkel::vrf::VRFInOut;
use sp_core::Pair;
Expand Down Expand Up @@ -125,6 +127,7 @@ fn claim_secondary_slot(
slot_number: SlotNumber,
epoch: &Epoch,
key_pairs: &[(AuthorityPair, usize)],
keystore: &KeyStorePtr,
author_secondary_vrf: bool,
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, epoch_index, .. } = epoch;
Expand All @@ -142,18 +145,16 @@ fn claim_secondary_slot(
for (pair, authority_index) in key_pairs {
if pair.public() == *expected_author {
let pre_digest = if author_secondary_vrf {
let transcript = super::authorship::make_transcript(
randomness,
slot_number,
*epoch_index,
let (output, proof) = keystore.read().vrf_sign(
pair.as_ref(), &BABE_ENGINE_ID, randomness, slot_number, *epoch_index,
);

let s = get_keypair(&pair).vrf_sign(transcript);
let proof = schnorrkel::vrf::VRFProof::from_bytes(&proof).ok()?;
let output = schnorrkel::vrf::VRFOutput::from_bytes(&output).ok()?;

PreDigest::SecondaryVRF(SecondaryVRFPreDigest {
slot_number,
vrf_output: VRFOutput(s.0.to_output()),
vrf_proof: VRFProof(s.1),
vrf_output: VRFOutput(output),
vrf_proof: VRFProof(proof),
authority_index: *authority_index as u32,
})
} else {
Expand Down Expand Up @@ -188,17 +189,18 @@ pub fn claim_slot(
})
.collect::<Vec<_>>()
};
claim_slot_using_key_pairs(slot_number, epoch, &key_pairs)
claim_slot_using_key_pairs(slot_number, epoch, keystore, &key_pairs)
}

/// Like `claim_slot`, but allows passing an explicit set of key pairs. Useful if we intend
/// to make repeated calls for different slots using the same key pairs.
pub fn claim_slot_using_key_pairs(
slot_number: SlotNumber,
epoch: &Epoch,
keystore: &KeyStorePtr,
key_pairs: &[(AuthorityPair, usize)],
) -> Option<(PreDigest, AuthorityId)> {
claim_primary_slot(slot_number, epoch, epoch.config.c, &key_pairs)
claim_primary_slot(slot_number, epoch, epoch.config.c, keystore, &key_pairs)
.or_else(|| {
if epoch.config.allowed_slots.is_secondary_plain_slots_allowed() ||
epoch.config.allowed_slots.is_secondary_vrf_slots_allowed()
Expand All @@ -207,6 +209,7 @@ pub fn claim_slot_using_key_pairs(
slot_number,
&epoch,
&key_pairs,
keystore,
epoch.config.allowed_slots.is_secondary_vrf_slots_allowed(),
)
} else {
Expand All @@ -215,11 +218,6 @@ pub fn claim_slot_using_key_pairs(
})
}

fn get_keypair(q: &AuthorityPair) -> &schnorrkel::Keypair {
use sp_core::crypto::IsWrappedBy;
sp_core::sr25519::Pair::from_ref(q).as_ref()
}

/// Claim a primary slot if it is our turn. Returns `None` if it is not our turn.
/// This hashes the slot number, epoch, genesis hash, and chain randomness into
/// the VRF. If the VRF produces a value less than `threshold`, it is our turn,
Expand All @@ -228,6 +226,7 @@ fn claim_primary_slot(
slot_number: SlotNumber,
epoch: &Epoch,
c: (u64, u64),
keystore: &KeyStorePtr,
key_pairs: &[(AuthorityPair, usize)],
) -> Option<(PreDigest, AuthorityId)> {
let Epoch { authorities, randomness, epoch_index, .. } = epoch;
Expand All @@ -241,16 +240,26 @@ fn claim_primary_slot(
// be empty. Therefore, this division in `calculate_threshold` is safe.
let threshold = super::authorship::calculate_primary_threshold(c, authorities, *authority_index);

let pre_digest = get_keypair(pair)
.vrf_sign_after_check(transcript, |inout| super::authorship::check_primary_threshold(inout, threshold))
.map(|s| {
PreDigest::Primary(PrimaryPreDigest {
slot_number,
vrf_output: VRFOutput(s.0.to_output()),
vrf_proof: VRFProof(s.1),
authority_index: *authority_index as u32,
})
});
let (output, proof) = keystore.read().vrf_sign(
pair.as_ref(), &BABE_ENGINE_ID, randomness, slot_number, *epoch_index,
);
let proof = schnorrkel::vrf::VRFProof::from_bytes(&proof).ok()?;
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
let output = schnorrkel::vrf::VRFOutput::from_bytes(&output).ok()?;
let inout = output.attach_input_hash(
&pair.as_ref().as_ref().public,
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
transcript
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
).ok()?;

let pre_digest = if super::authorship::check_primary_threshold(&inout, threshold) {
Some(PreDigest::Primary(PrimaryPreDigest {
slot_number,
vrf_output: VRFOutput(output),
vrf_proof: VRFProof(proof),
authority_index: *authority_index as u32,
}))
} else {
None
};

// early exit on first successful claim
if let Some(pre_digest) = pre_digest {
Expand Down
4 changes: 3 additions & 1 deletion client/keystore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ derive_more = "0.99.2"
sp-core = { version = "2.0.0-rc2", path = "../../primitives/core" }
sp-application-crypto = { version = "2.0.0-rc2", path = "../../primitives/application-crypto" }
hex = "0.4.0"
merlin = { version = "2.0", default-features = false }
parking_lot = "0.10.0"
rand = "0.7.2"
serde_json = "1.0.41"
subtle = "2.1.1"
parking_lot = "0.10.0"
schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated", "u64_backend"], default-features = false}

[dev-dependencies]
tempfile = "3.1.0"
34 changes: 33 additions & 1 deletion client/keystore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
use std::{collections::{HashMap, HashSet}, path::PathBuf, fs::{self, File}, io::{self, Write}, sync::Arc};
use sp_core::{
crypto::{IsWrappedBy, CryptoTypePublicPair, KeyTypeId, Pair as PairT, Protected, Public},
traits::{BareCryptoStore, BareCryptoStoreError as TraitError},
traits::{BareCryptoStore, BareCryptoStoreError as TraitError, VRFSigner},
sr25519::Pair as Sr25519Pair,
Encode,
};
use sp_application_crypto::{AppKey, AppPublic, AppPair, ed25519, sr25519, ecdsa};
use parking_lot::RwLock;
use merlin::Transcript;

/// Keystore pointer
pub type KeyStorePtr = Arc<RwLock<Store>>;
Expand Down Expand Up @@ -296,6 +298,20 @@ impl Store {

Ok(public_keys)
}

fn make_vrf_transcript(
&self,
label: &'static [u8],
randomness: &[u8],
slot_number: u64,
epoch: u64
) -> Transcript {
let mut transcript = Transcript::new(label.clone());
transcript.append_u64(b"slot number", slot_number);
transcript.append_u64(b"current epoch", epoch);
transcript.append_message(b"chain randomness", &randomness[..]);
transcript
}
}

impl BareCryptoStore for Store {
Expand Down Expand Up @@ -440,6 +456,22 @@ impl BareCryptoStore for Store {
}
}

impl VRFSigner for Store {
fn vrf_sign(
&self,
pair: &Sr25519Pair,
label: &'static [u8],
randomness: &[u8],
slot_number: u64,
epoch: u64
) -> (Vec<u8>, Vec<u8>) {
let transcript = self.make_vrf_transcript(label, randomness, slot_number, epoch);
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
let pair = pair.as_ref();
let (inout, proof, _) = pair.vrf_sign(transcript);
(inout.to_output().to_bytes().to_vec(), proof.to_bytes().to_vec())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
13 changes: 13 additions & 0 deletions primitives/core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ pub trait BareCryptoStore: Send + Sync {
}
}

/// VRF-signing capability.
pub trait VRFSigner {
rakanalh marked this conversation as resolved.
Show resolved Hide resolved
/// VRF-sign round
fn vrf_sign(
&self,
pair: &sr25519::Pair,
label: &'static [u8],
randomness: &[u8],
slot_number: u64,
epoch: u64
) -> (Vec<u8>, Vec<u8>);
}

/// A pointer to the key store.
pub type BareCryptoStorePtr = Arc<parking_lot::RwLock<dyn BareCryptoStore>>;

Expand Down