sp_consensus_sassafras/
vrf.rs1use crate::{Randomness, TicketBody, TicketId};
21#[cfg(not(feature = "std"))]
22use alloc::vec::Vec;
23use codec::Encode;
24use sp_consensus_slots::Slot;
25
26pub use sp_core::bandersnatch::{
27 ring_vrf::{RingProver, RingVerifier, RingVerifierKey, RingVrfSignature},
28 vrf::{VrfInput, VrfPreOutput, VrfSignData, VrfSignature},
29};
30
31pub const RING_SIZE: usize = 1024;
33
34pub type RingContext = sp_core::bandersnatch::ring_vrf::RingContext<RING_SIZE>;
36
37pub fn slot_claim_input(randomness: &Randomness, slot: Slot, epoch: u64) -> VrfInput {
39 let v = [b"sassafras-ticket", randomness.as_slice(), &slot.to_le_bytes(), &epoch.to_le_bytes()]
40 .concat();
41 VrfInput::new(&v[..])
42}
43
44pub fn slot_claim_sign_data(randomness: &Randomness, slot: Slot, epoch: u64) -> VrfSignData {
46 let v = [b"sassafras-ticket", randomness.as_slice(), &slot.to_le_bytes(), &epoch.to_le_bytes()]
47 .concat();
48 VrfSignData::new(&v[..], &[])
49}
50
51pub fn ticket_id_input(randomness: &Randomness, attempt: u32, epoch: u64) -> VrfInput {
53 let v =
54 [b"sassafras-ticket", randomness.as_slice(), &attempt.to_le_bytes(), &epoch.to_le_bytes()]
55 .concat();
56 VrfInput::new(&v[..])
57}
58
59pub fn ticket_body_sign_data(ticket_body: &TicketBody, ticket_id_input: VrfInput) -> VrfSignData {
61 VrfSignData { vrf_input: ticket_id_input, aux_data: ticket_body.encode() }
62}
63
64pub fn make_ticket_id(preout: &VrfPreOutput) -> TicketId {
69 let bytes: [u8; 16] = preout.make_bytes()[..16].try_into().unwrap();
70 u128::from_le_bytes(bytes)
71}