Files
solana/src/event.rs

149 lines
5.0 KiB
Rust
Raw Normal View History

//! The `log` crate provides the foundational data structures for Proof-of-History,
//! an ordered log of events in time.
/// Each log entry contains three pieces of data. The 'num_hashes' field is the number
2018-03-04 07:34:38 -07:00
/// of hashes performed since the previous entry. The 'id' field is the result
/// of hashing 'id' from the previous entry 'num_hashes' times. The 'event'
/// field points to an Event that took place shortly after 'id' was generated.
///
/// If you divide 'num_hashes' by the amount of time it takes to generate a new hash, you
/// get a duration estimate since the last event. Since processing power increases
/// over time, one should expect the duration 'num_hashes' represents to decrease proportionally.
/// Though processing power varies across nodes, the network gives priority to the
/// fastest processor. Duration should therefore be estimated by assuming that the hash
/// was generated by the fastest processor at the time the entry was logged.
use generic_array::GenericArray;
use generic_array::typenum::{U32, U64};
use ring::signature::Ed25519KeyPair;
2018-03-04 07:28:51 -07:00
use ring::{rand, signature};
use untrusted;
use serde::Serialize;
2018-03-04 07:28:51 -07:00
use bincode::serialize;
use log::Sha256Hash;
pub type PublicKey = GenericArray<u8, U32>;
pub type Signature = GenericArray<u8, U64>;
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Transfer<T> {
pub from: PublicKey,
pub to: PublicKey,
pub data: T,
pub last_id: Sha256Hash,
pub sig: Signature,
}
/// When 'event' is Tick, the event represents a simple clock tick, and exists for the
/// sole purpose of improving the performance of event log verification. A tick can
/// be generated in 'num_hashes' hashes and verified in 'num_hashes' hashes. By logging
2018-03-04 07:34:38 -07:00
/// a hash alongside the tick, each tick and be verified in parallel using the 'id'
/// of the preceding tick to seed its hashing.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Event<T> {
Tick,
Transaction(Transfer<T>),
}
impl<T> Event<T> {
pub fn new_claim(to: PublicKey, data: T, last_id: Sha256Hash, sig: Signature) -> Self {
let transfer = Transfer {
2018-03-03 20:41:05 -07:00
from: to,
to,
data,
last_id,
sig,
};
Event::Transaction(transfer)
}
}
/// Return a new ED25519 keypair
pub fn generate_keypair() -> Ed25519KeyPair {
let rng = rand::SystemRandom::new();
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap()
}
/// Return the public key for the given keypair
pub fn get_pubkey(keypair: &Ed25519KeyPair) -> PublicKey {
GenericArray::clone_from_slice(keypair.public_key_bytes())
}
/// Return a signature for the given data using the private key from the given keypair.
fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
let serialized = serialize(data).unwrap();
GenericArray::clone_from_slice(keypair.sign(&serialized).as_ref())
}
/// Return a signature for the given transaction data using the private key from the given keypair.
pub fn sign_transaction_data<T: Serialize>(
data: &T,
keypair: &Ed25519KeyPair,
to: &PublicKey,
last_id: &Sha256Hash,
) -> Signature {
2018-03-03 20:41:05 -07:00
let from = &get_pubkey(keypair);
sign_serialized(&(from, to, data, last_id), keypair)
}
/// Return a signature for the given data using the private key from the given keypair.
pub fn sign_claim_data<T: Serialize>(
data: &T,
keypair: &Ed25519KeyPair,
last_id: &Sha256Hash,
) -> Signature {
sign_transaction_data(data, keypair, &get_pubkey(keypair), last_id)
}
/// Verify a signed message with the given public key.
pub fn verify_signature(peer_public_key_bytes: &[u8], msg_bytes: &[u8], sig_bytes: &[u8]) -> bool {
let peer_public_key = untrusted::Input::from(peer_public_key_bytes);
let msg = untrusted::Input::from(msg_bytes);
let sig = untrusted::Input::from(sig_bytes);
signature::verify(&signature::ED25519, peer_public_key, msg, sig).is_ok()
}
pub fn get_signature<T>(event: &Event<T>) -> Option<Signature> {
match *event {
Event::Tick => None,
Event::Transaction(Transfer { sig, .. }) => Some(sig),
}
}
pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool {
if let Event::Transaction(Transfer {
from,
to,
ref data,
last_id,
sig,
}) = *event
{
let sign_data = serialize(&(&from, &to, &data, &last_id)).unwrap();
2018-03-03 20:41:05 -07:00
if !verify_signature(&from, &sign_data, &sig) {
return false;
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
use bincode::{deserialize, serialize};
#[test]
fn test_serialize_claim() {
let claim0 = Event::new_claim(
Default::default(),
0u8,
Default::default(),
Default::default(),
);
let buf = serialize(&claim0).unwrap();
let claim1: Event<u8> = deserialize(&buf).unwrap();
assert_eq!(claim1, claim0);
}
}