2018-02-18 09:59:15 -07:00
|
|
|
//! The `log` crate provides the foundational data structures for Proof-of-History,
|
|
|
|
//! an ordered log of events in time.
|
2018-02-15 10:13:56 -07:00
|
|
|
|
2018-02-18 09:59:15 -07:00
|
|
|
/// Each log entry contains three pieces of data. The 'num_hashes' field is the number
|
|
|
|
/// of hashes performed since the previous entry. The 'end_hash' field is the result
|
|
|
|
/// of hashing 'end_hash' from the previous entry 'num_hashes' times. The 'event'
|
|
|
|
/// field points to an Event that took place shortly after 'end_hash' was generated.
|
2018-02-15 10:13:56 -07:00
|
|
|
///
|
2018-02-15 10:48:30 -07:00
|
|
|
/// If you divide 'num_hashes' by the amount of time it takes to generate a new hash, you
|
2018-02-15 10:13:56 -07:00
|
|
|
/// get a duration estimate since the last event. Since processing power increases
|
2018-02-15 10:48:30 -07:00
|
|
|
/// over time, one should expect the duration 'num_hashes' represents to decrease proportionally.
|
2018-02-15 10:13:56 -07:00
|
|
|
/// 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.
|
2018-02-19 16:17:13 -07:00
|
|
|
|
2018-02-20 16:26:11 -07:00
|
|
|
use generic_array::GenericArray;
|
2018-02-24 06:53:36 -07:00
|
|
|
use generic_array::typenum::{U32, U64};
|
|
|
|
use ring::signature::Ed25519KeyPair;
|
2018-02-26 16:42:31 -07:00
|
|
|
use serde::Serialize;
|
|
|
|
|
2018-02-19 16:17:13 -07:00
|
|
|
pub type Sha256Hash = GenericArray<u8, U32>;
|
2018-02-24 06:53:36 -07:00
|
|
|
pub type PublicKey = GenericArray<u8, U32>;
|
|
|
|
pub type Signature = GenericArray<u8, U64>;
|
2018-02-19 16:17:13 -07:00
|
|
|
|
2018-02-20 16:26:11 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-02-26 15:37:33 -07:00
|
|
|
pub struct Entry<T> {
|
2018-02-15 10:57:32 -07:00
|
|
|
pub num_hashes: u64,
|
2018-02-19 16:17:13 -07:00
|
|
|
pub end_hash: Sha256Hash,
|
2018-02-26 15:37:33 -07:00
|
|
|
pub event: Event<T>,
|
2018-02-15 10:57:32 -07:00
|
|
|
}
|
|
|
|
|
2018-02-18 09:53:38 -07:00
|
|
|
/// When 'event' is Tick, the event represents a simple clock tick, and exists for the
|
2018-02-15 10:13:56 -07:00
|
|
|
/// sole purpose of improving the performance of event log verification. A tick can
|
2018-02-16 09:56:10 -07:00
|
|
|
/// be generated in 'num_hashes' hashes and verified in 'num_hashes' hashes. By logging
|
|
|
|
/// a hash alongside the tick, each tick and be verified in parallel using the 'end_hash'
|
|
|
|
/// of the preceding tick to seed its hashing.
|
2018-02-20 16:26:11 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
|
2018-02-26 15:31:01 -07:00
|
|
|
pub enum Event<T> {
|
2018-02-15 10:57:32 -07:00
|
|
|
Tick,
|
2018-02-24 06:53:36 -07:00
|
|
|
Claim {
|
|
|
|
key: PublicKey,
|
2018-02-26 15:31:01 -07:00
|
|
|
data: T,
|
2018-02-24 06:53:36 -07:00
|
|
|
sig: Signature,
|
|
|
|
},
|
2018-02-26 11:01:19 -07:00
|
|
|
Transaction {
|
|
|
|
from: PublicKey,
|
|
|
|
to: PublicKey,
|
2018-02-26 15:31:01 -07:00
|
|
|
data: T,
|
2018-02-26 11:01:19 -07:00
|
|
|
sig: Signature,
|
|
|
|
},
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:37:33 -07:00
|
|
|
impl<T> Entry<T> {
|
2018-02-18 09:53:38 -07:00
|
|
|
/// Creates a Entry from the number of hashes 'num_hashes' since the previous event
|
2018-02-15 10:48:30 -07:00
|
|
|
/// and that resulting 'end_hash'.
|
2018-02-19 16:17:13 -07:00
|
|
|
pub fn new_tick(num_hashes: u64, end_hash: &Sha256Hash) -> Self {
|
2018-02-18 09:53:38 -07:00
|
|
|
Entry {
|
2018-02-15 10:48:30 -07:00
|
|
|
num_hashes,
|
2018-02-19 16:17:13 -07:00
|
|
|
end_hash: *end_hash,
|
2018-02-20 13:07:54 -07:00
|
|
|
event: Event::Tick,
|
2018-02-15 10:48:30 -07:00
|
|
|
}
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
2018-02-15 11:45:04 -07:00
|
|
|
}
|
|
|
|
|
2018-02-28 10:07:54 -07:00
|
|
|
/// Return a new ED25519 keypair
|
2018-02-26 11:01:19 -07:00
|
|
|
pub fn generate_keypair() -> Ed25519KeyPair {
|
|
|
|
use ring::{rand, signature};
|
|
|
|
use untrusted;
|
|
|
|
let rng = rand::SystemRandom::new();
|
|
|
|
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
|
|
|
|
signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-02-28 10:07:54 -07:00
|
|
|
/// Return the public key for the given keypair
|
|
|
|
pub fn get_pubkey(keypair: &Ed25519KeyPair) -> PublicKey {
|
|
|
|
GenericArray::clone_from_slice(keypair.public_key_bytes())
|
2018-02-24 06:53:36 -07:00
|
|
|
}
|
|
|
|
|
2018-02-28 10:07:54 -07:00
|
|
|
/// Return a signature for the given data using the private key from the given keypair.
|
|
|
|
pub fn sign_serialized<T: Serialize>(data: &T, keypair: &Ed25519KeyPair) -> Signature {
|
2018-02-26 16:42:31 -07:00
|
|
|
use bincode::serialize;
|
2018-02-28 10:07:54 -07:00
|
|
|
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,
|
|
|
|
) -> Signature {
|
|
|
|
sign_serialized(&(data, to), keypair)
|
2018-02-26 11:01:19 -07:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:53:36 -07:00
|
|
|
/// Return a Sha256 hash for the given data.
|
2018-02-19 16:17:13 -07:00
|
|
|
pub fn hash(val: &[u8]) -> Sha256Hash {
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
let mut hasher = Sha256::default();
|
|
|
|
hasher.input(val);
|
|
|
|
hasher.result()
|
2018-02-19 12:09:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-20 13:07:54 -07:00
|
|
|
/// Return the hash of the given hash extended with the given value.
|
2018-03-01 17:44:10 -07:00
|
|
|
pub fn extend_and_hash(end_hash: &Sha256Hash, val: &[u8]) -> Sha256Hash {
|
2018-02-20 13:07:54 -07:00
|
|
|
let mut hash_data = end_hash.to_vec();
|
|
|
|
hash_data.extend_from_slice(val);
|
|
|
|
hash(&hash_data)
|
|
|
|
}
|
|
|
|
|
2018-03-01 17:44:10 -07:00
|
|
|
pub fn get_signature<T>(event: &Event<T>) -> Option<Signature> {
|
2018-02-24 06:53:36 -07:00
|
|
|
match *event {
|
2018-03-01 17:44:10 -07:00
|
|
|
Event::Tick => None,
|
|
|
|
Event::Claim { sig, .. } => Some(sig),
|
|
|
|
Event::Transaction { sig, .. } => Some(sig),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_event<T>(end_hash: &Sha256Hash, event: &Event<T>) -> Sha256Hash {
|
|
|
|
match get_signature(event) {
|
|
|
|
None => *end_hash,
|
|
|
|
Some(sig) => extend_and_hash(end_hash, &sig),
|
2018-02-24 06:53:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:42:31 -07:00
|
|
|
/// Creates the hash 'num_hashes' after start_hash, plus an additional hash for any event data.
|
|
|
|
pub fn next_hash<T: Serialize>(
|
2018-02-26 15:31:01 -07:00
|
|
|
start_hash: &Sha256Hash,
|
|
|
|
num_hashes: u64,
|
2018-02-26 16:42:31 -07:00
|
|
|
event: &Event<T>,
|
2018-02-26 15:31:01 -07:00
|
|
|
) -> Sha256Hash {
|
2018-02-19 16:17:13 -07:00
|
|
|
let mut end_hash = *start_hash;
|
2018-02-15 11:45:04 -07:00
|
|
|
for _ in 0..num_hashes {
|
2018-02-19 16:17:13 -07:00
|
|
|
end_hash = hash(&end_hash);
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
2018-02-24 06:53:36 -07:00
|
|
|
hash_event(&end_hash, event)
|
2018-02-20 13:07:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
2018-02-26 16:42:31 -07:00
|
|
|
pub fn next_entry<T: Serialize>(
|
2018-02-26 15:37:33 -07:00
|
|
|
start_hash: &Sha256Hash,
|
|
|
|
num_hashes: u64,
|
2018-02-26 16:42:31 -07:00
|
|
|
event: Event<T>,
|
|
|
|
) -> Entry<T> {
|
2018-02-20 13:07:54 -07:00
|
|
|
Entry {
|
|
|
|
num_hashes,
|
|
|
|
end_hash: next_hash(start_hash, num_hashes, &event),
|
|
|
|
event,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:42:31 -07:00
|
|
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
|
|
|
pub fn next_entry_mut<T: Serialize>(
|
2018-02-26 15:31:01 -07:00
|
|
|
start_hash: &mut Sha256Hash,
|
|
|
|
num_hashes: u64,
|
2018-02-26 16:42:31 -07:00
|
|
|
event: Event<T>,
|
|
|
|
) -> Entry<T> {
|
2018-02-26 11:01:19 -07:00
|
|
|
let entry = next_entry(start_hash, num_hashes, event);
|
|
|
|
*start_hash = entry.end_hash;
|
|
|
|
entry
|
|
|
|
}
|
|
|
|
|
2018-02-20 13:07:54 -07:00
|
|
|
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
|
2018-02-26 16:42:31 -07:00
|
|
|
pub fn next_tick<T: Serialize>(start_hash: &Sha256Hash, num_hashes: u64) -> Entry<T> {
|
2018-02-20 13:07:54 -07:00
|
|
|
next_entry(start_hash, num_hashes, Event::Tick)
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-28 10:23:01 -07:00
|
|
|
pub fn verify_event<T: Serialize>(event: &Event<T>) -> bool {
|
2018-02-26 16:42:31 -07:00
|
|
|
use bincode::serialize;
|
2018-02-28 10:23:01 -07:00
|
|
|
if let Event::Claim { key, ref data, sig } = *event {
|
2018-02-26 16:42:31 -07:00
|
|
|
let mut claim_data = serialize(&data).unwrap();
|
|
|
|
if !verify_signature(&key, &claim_data, &sig) {
|
2018-02-26 14:39:01 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Event::Transaction {
|
|
|
|
from,
|
|
|
|
to,
|
2018-02-26 16:42:31 -07:00
|
|
|
ref data,
|
2018-02-26 14:39:01 -07:00
|
|
|
sig,
|
2018-02-28 10:23:01 -07:00
|
|
|
} = *event
|
2018-02-26 14:39:01 -07:00
|
|
|
{
|
2018-02-28 10:07:54 -07:00
|
|
|
let sign_data = serialize(&(&data, &to)).unwrap();
|
2018-02-26 14:39:01 -07:00
|
|
|
if !verify_signature(&from, &sign_data, &sig) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 10:23:01 -07:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Verifies self.end_hash is the result of hashing a 'start_hash' 'self.num_hashes' times.
|
|
|
|
/// If the event is not a Tick, then hash that as well.
|
|
|
|
pub fn verify_entry<T: Serialize>(entry: &Entry<T>, start_hash: &Sha256Hash) -> bool {
|
|
|
|
if !verify_event(&entry.event) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-26 14:39:01 -07:00
|
|
|
entry.end_hash == next_hash(start_hash, entry.num_hashes, &entry.event)
|
|
|
|
}
|
|
|
|
|
2018-02-15 10:13:56 -07:00
|
|
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
2018-02-26 15:37:33 -07:00
|
|
|
pub fn verify_slice(events: &[Entry<Sha256Hash>], start_hash: &Sha256Hash) -> bool {
|
2018-02-15 10:13:56 -07:00
|
|
|
use rayon::prelude::*;
|
2018-02-19 16:17:13 -07:00
|
|
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
2018-02-15 10:13:56 -07:00
|
|
|
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
2018-02-26 14:39:01 -07:00
|
|
|
event_pairs.all(|(x0, x1)| verify_entry(&x1, &x0.end_hash))
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-28 18:04:35 -07:00
|
|
|
/// Verifies the hashes and counts of a slice of events are all consistent.
|
|
|
|
pub fn verify_slice_u64(events: &[Entry<u64>], start_hash: &Sha256Hash) -> bool {
|
|
|
|
use rayon::prelude::*;
|
|
|
|
let genesis = [Entry::new_tick(Default::default(), start_hash)];
|
|
|
|
let event_pairs = genesis.par_iter().chain(events).zip(events);
|
|
|
|
event_pairs.all(|(x0, x1)| verify_entry(&x1, &x0.end_hash))
|
|
|
|
}
|
|
|
|
|
2018-02-15 10:13:56 -07:00
|
|
|
/// Verifies the hashes and events serially. Exists only for reference.
|
2018-02-26 16:42:31 -07:00
|
|
|
pub fn verify_slice_seq<T: Serialize>(events: &[Entry<T>], start_hash: &Sha256Hash) -> bool {
|
2018-02-18 09:53:38 -07:00
|
|
|
let genesis = [Entry::new_tick(0, start_hash)];
|
2018-02-15 16:00:05 -07:00
|
|
|
let mut event_pairs = genesis.iter().chain(events).zip(events);
|
2018-02-26 14:39:01 -07:00
|
|
|
event_pairs.all(|(x0, x1)| verify_entry(&x1, &x0.end_hash))
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:53:36 -07:00
|
|
|
/// 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 {
|
|
|
|
use untrusted;
|
|
|
|
use ring::signature;
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-02-26 16:42:31 -07:00
|
|
|
pub fn create_entries<T: Serialize>(
|
2018-02-26 15:31:01 -07:00
|
|
|
start_hash: &Sha256Hash,
|
|
|
|
num_hashes: u64,
|
2018-02-26 16:42:31 -07:00
|
|
|
events: Vec<Event<T>>,
|
|
|
|
) -> Vec<Entry<T>> {
|
2018-02-26 11:01:19 -07:00
|
|
|
let mut end_hash = *start_hash;
|
|
|
|
events
|
2018-02-26 16:42:31 -07:00
|
|
|
.into_iter()
|
|
|
|
.map(|event| next_entry_mut(&mut end_hash, num_hashes, event))
|
2018-02-26 11:01:19 -07:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:50:48 -07:00
|
|
|
/// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'.
|
2018-02-26 15:37:33 -07:00
|
|
|
pub fn create_ticks(
|
|
|
|
start_hash: &Sha256Hash,
|
|
|
|
num_hashes: u64,
|
|
|
|
len: usize,
|
|
|
|
) -> Vec<Entry<Sha256Hash>> {
|
2018-02-20 13:07:54 -07:00
|
|
|
use std::iter;
|
|
|
|
let mut end_hash = *start_hash;
|
|
|
|
iter::repeat(Event::Tick)
|
|
|
|
.take(len)
|
2018-02-26 11:01:19 -07:00
|
|
|
.map(|event| next_entry_mut(&mut end_hash, num_hashes, event))
|
2018-02-20 13:07:54 -07:00
|
|
|
.collect()
|
2018-02-15 10:13:56 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:47:05 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_event_verify() {
|
2018-02-19 16:17:13 -07:00
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let one = hash(&zero);
|
2018-02-26 16:42:31 -07:00
|
|
|
assert!(verify_entry::<u8>(&Entry::new_tick(0, &zero), &zero)); // base case
|
|
|
|
assert!(!verify_entry::<u8>(&Entry::new_tick(0, &zero), &one)); // base case, bad
|
|
|
|
assert!(verify_entry::<u8>(&next_tick(&zero, 1), &zero)); // inductive step
|
|
|
|
assert!(!verify_entry::<u8>(&next_tick(&zero, 1), &one)); // inductive step, bad
|
2018-02-15 17:47:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_next_tick() {
|
2018-02-19 16:17:13 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
assert_eq!(next_tick::<Sha256Hash>(&zero, 1).num_hashes, 1)
|
2018-02-15 17:47:05 -07:00
|
|
|
}
|
|
|
|
|
2018-02-26 15:37:33 -07:00
|
|
|
fn verify_slice_generic(verify_slice: fn(&[Entry<Sha256Hash>], &Sha256Hash) -> bool) {
|
2018-02-19 16:17:13 -07:00
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let one = hash(&zero);
|
|
|
|
assert!(verify_slice(&vec![], &zero)); // base case
|
|
|
|
assert!(verify_slice(&vec![Entry::new_tick(0, &zero)], &zero)); // singleton case 1
|
|
|
|
assert!(!verify_slice(&vec![Entry::new_tick(0, &zero)], &one)); // singleton case 2, bad
|
|
|
|
assert!(verify_slice(&create_ticks(&zero, 0, 2), &zero)); // inductive step
|
|
|
|
|
|
|
|
let mut bad_ticks = create_ticks(&zero, 0, 2);
|
|
|
|
bad_ticks[1].end_hash = one;
|
|
|
|
assert!(!verify_slice(&bad_ticks, &zero)); // inductive step, bad
|
2018-02-16 09:14:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verify_slice() {
|
|
|
|
verify_slice_generic(verify_slice);
|
2018-02-15 17:47:05 -07:00
|
|
|
}
|
2018-02-16 09:14:42 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_verify_slice_seq() {
|
2018-02-26 16:42:31 -07:00
|
|
|
verify_slice_generic(verify_slice_seq::<Sha256Hash>);
|
2018-02-16 09:14:42 -07:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:46:36 -07:00
|
|
|
#[test]
|
|
|
|
fn test_reorder_attack() {
|
|
|
|
let zero = Sha256Hash::default();
|
|
|
|
let one = hash(&zero);
|
|
|
|
|
2018-03-01 17:01:55 -07:00
|
|
|
// First, verify Claim events
|
|
|
|
let keypair = generate_keypair();
|
|
|
|
let event0 = Event::Claim {
|
|
|
|
key: get_pubkey(&keypair),
|
|
|
|
data: zero,
|
|
|
|
sig: sign_serialized(&zero, &keypair),
|
|
|
|
};
|
|
|
|
|
|
|
|
let event1 = Event::Claim {
|
|
|
|
key: get_pubkey(&keypair),
|
|
|
|
data: one,
|
|
|
|
sig: sign_serialized(&one, &keypair),
|
|
|
|
};
|
|
|
|
let events = vec![event0, event1];
|
2018-02-26 16:42:31 -07:00
|
|
|
let mut entries = create_entries(&zero, 0, events);
|
2018-02-24 06:53:36 -07:00
|
|
|
assert!(verify_slice(&entries, &zero));
|
2018-02-20 14:46:36 -07:00
|
|
|
|
2018-03-01 17:01:55 -07:00
|
|
|
// Next, swap two Claim events and ensure verification fails.
|
2018-02-20 14:46:36 -07:00
|
|
|
let event0 = entries[0].event.clone();
|
|
|
|
let event1 = entries[1].event.clone();
|
|
|
|
entries[0].event = event1;
|
|
|
|
entries[1].event = event0;
|
2018-02-24 06:53:36 -07:00
|
|
|
assert!(!verify_slice(&entries, &zero));
|
2018-02-20 14:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-02-24 06:53:36 -07:00
|
|
|
#[test]
|
2018-02-26 11:01:19 -07:00
|
|
|
fn test_claim() {
|
|
|
|
let keypair = generate_keypair();
|
2018-02-28 10:07:54 -07:00
|
|
|
let data = hash(b"hello, world");
|
|
|
|
let event0 = Event::Claim {
|
|
|
|
key: get_pubkey(&keypair),
|
|
|
|
data,
|
|
|
|
sig: sign_serialized(&data, &keypair),
|
|
|
|
};
|
2018-02-24 06:53:36 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
let entries = create_entries(&zero, 0, vec![event0]);
|
2018-02-24 06:53:36 -07:00
|
|
|
assert!(verify_slice(&entries, &zero));
|
|
|
|
}
|
2018-02-24 10:27:51 -07:00
|
|
|
|
|
|
|
#[test]
|
2018-02-26 11:01:19 -07:00
|
|
|
fn test_wrong_data_claim_attack() {
|
|
|
|
let keypair = generate_keypair();
|
2018-02-28 10:07:54 -07:00
|
|
|
let event0 = Event::Claim {
|
|
|
|
key: get_pubkey(&keypair),
|
|
|
|
data: hash(b"goodbye cruel world"),
|
|
|
|
sig: sign_serialized(&hash(b"hello, world"), &keypair),
|
|
|
|
};
|
2018-02-24 10:27:51 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
let entries = create_entries(&zero, 0, vec![event0]);
|
2018-02-26 11:01:19 -07:00
|
|
|
assert!(!verify_slice(&entries, &zero));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer() {
|
|
|
|
let keypair0 = generate_keypair();
|
|
|
|
let keypair1 = generate_keypair();
|
2018-02-28 10:07:54 -07:00
|
|
|
let pubkey1 = get_pubkey(&keypair1);
|
|
|
|
let data = hash(b"hello, world");
|
|
|
|
let event0 = Event::Transaction {
|
|
|
|
from: get_pubkey(&keypair0),
|
|
|
|
to: pubkey1,
|
|
|
|
data,
|
|
|
|
sig: sign_transaction_data(&data, &keypair0, &pubkey1),
|
|
|
|
};
|
2018-02-26 11:01:19 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
let entries = create_entries(&zero, 0, vec![event0]);
|
2018-02-26 11:01:19 -07:00
|
|
|
assert!(verify_slice(&entries, &zero));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_wrong_data_transfer_attack() {
|
|
|
|
let keypair0 = generate_keypair();
|
|
|
|
let keypair1 = generate_keypair();
|
2018-02-28 10:07:54 -07:00
|
|
|
let pubkey1 = get_pubkey(&keypair1);
|
|
|
|
let data = hash(b"hello, world");
|
|
|
|
let event0 = Event::Transaction {
|
|
|
|
from: get_pubkey(&keypair0),
|
|
|
|
to: pubkey1,
|
|
|
|
data: hash(b"goodbye cruel world"), // <-- attack!
|
|
|
|
sig: sign_transaction_data(&data, &keypair0, &pubkey1),
|
|
|
|
};
|
2018-02-26 11:01:19 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
let entries = create_entries(&zero, 0, vec![event0]);
|
2018-02-26 11:01:19 -07:00
|
|
|
assert!(!verify_slice(&entries, &zero));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_transfer_hijack_attack() {
|
|
|
|
let keypair0 = generate_keypair();
|
|
|
|
let keypair1 = generate_keypair();
|
2018-02-28 10:07:54 -07:00
|
|
|
let thief_keypair = generate_keypair();
|
|
|
|
let pubkey1 = get_pubkey(&keypair1);
|
|
|
|
let data = hash(b"hello, world");
|
|
|
|
let event0 = Event::Transaction {
|
|
|
|
from: get_pubkey(&keypair0),
|
|
|
|
to: get_pubkey(&thief_keypair), // <-- attack!
|
|
|
|
data: hash(b"goodbye cruel world"),
|
|
|
|
sig: sign_transaction_data(&data, &keypair0, &pubkey1),
|
|
|
|
};
|
2018-02-26 11:01:19 -07:00
|
|
|
let zero = Sha256Hash::default();
|
2018-02-26 16:42:31 -07:00
|
|
|
let entries = create_entries(&zero, 0, vec![event0]);
|
2018-02-24 10:27:51 -07:00
|
|
|
assert!(!verify_slice(&entries, &zero));
|
|
|
|
}
|
2018-02-15 17:47:05 -07:00
|
|
|
}
|
|
|
|
|
2018-02-15 10:13:56 -07:00
|
|
|
#[cfg(all(feature = "unstable", test))]
|
|
|
|
mod bench {
|
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2018-02-18 09:59:15 -07:00
|
|
|
use log::*;
|
2018-02-15 10:13:56 -07:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn event_bench(bencher: &mut Bencher) {
|
2018-02-19 16:17:13 -07:00
|
|
|
let start_hash = Default::default();
|
2018-02-19 16:51:32 -07:00
|
|
|
let events = create_ticks(&start_hash, 10_000, 8);
|
2018-02-16 09:38:12 -08:00
|
|
|
bencher.iter(|| {
|
2018-02-19 16:17:13 -07:00
|
|
|
assert!(verify_slice(&events, &start_hash));
|
2018-02-16 09:38:12 -08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn event_bench_seq(bencher: &mut Bencher) {
|
2018-02-19 16:17:13 -07:00
|
|
|
let start_hash = Default::default();
|
2018-02-19 16:51:32 -07:00
|
|
|
let events = create_ticks(&start_hash, 10_000, 8);
|
2018-02-15 10:13:56 -07:00
|
|
|
bencher.iter(|| {
|
2018-02-26 17:03:50 -07:00
|
|
|
assert!(verify_slice_seq(&events, &start_hash));
|
2018-02-15 10:13:56 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|