Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit bd84cf6

Browse files
authored
Merge pull request #15 from garious/add-historian
Demo proof-of-history and reordering attack
2 parents e57bba1 + 6e37f70 commit bd84cf6

File tree

6 files changed

+100
-33
lines changed

6 files changed

+100
-33
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ asm = ["sha2-asm"]
2424

2525
[dependencies]
2626
rayon = "1.0.0"
27-
itertools = "0.7.6"
2827
sha2 = "0.7.0"
2928
sha2-asm = {version="0.3", optional=true}
3029
digest = "0.7.2"

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::sync::mpsc::SendError;
3535
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
3636
hist.sender.send(Event::Tick)?;
3737
thread::sleep(time::Duration::new(0, 100_000));
38-
hist.sender.send(Event::UserDataKey(0xdeadbeef))?;
38+
hist.sender.send(Event::UserDataKey(Sha256Hash::default()))?;
3939
thread::sleep(time::Duration::new(0, 100_000));
4040
hist.sender.send(Event::Tick)?;
4141
Ok(())
@@ -50,6 +50,9 @@ fn main() {
5050
for entry in &entries {
5151
println!("{:?}", entry);
5252
}
53+
54+
// Proof-of-History: Verify the historian learned about the events
55+
// in the same order they appear in the vector.
5356
assert!(verify_slice(&entries, &seed));
5457
}
5558
```
@@ -62,6 +65,19 @@ Entry { num_hashes: 6, end_hash: [67, ...], event: UserDataKey(3735928559) }
6265
Entry { num_hashes: 5, end_hash: [123, ...], event: Tick }
6366
```
6467

68+
Proof-of-History
69+
---
70+
71+
Take note of the last line:
72+
73+
```rust
74+
assert!(verify_slice(&entries, &seed));
75+
```
76+
77+
[It's a proof!](https://en.wikipedia.org/wiki/Curry–Howard_correspondence) For each entry returned by the
78+
historian, we can verify that `end_hash` is the result of applying a sha256 hash to the previous `end_hash`
79+
exactly `num_hashes` times, and then hashing then event data on top of that. Because the event data is
80+
included in the hash, the events cannot be reordered without regenerating all the hashes.
6581

6682
# Developing
6783

src/bin/demo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::sync::mpsc::SendError;
88
fn create_log(hist: &Historian) -> Result<(), SendError<Event>> {
99
hist.sender.send(Event::Tick)?;
1010
thread::sleep(time::Duration::new(0, 100_000));
11-
hist.sender.send(Event::UserDataKey(0xdeadbeef))?;
11+
hist.sender.send(Event::UserDataKey(Sha256Hash::default()))?;
1212
thread::sleep(time::Duration::new(0, 100_000));
1313
hist.sender.send(Event::Tick)?;
1414
Ok(())

src/historian.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
use std::thread::JoinHandle;
99
use std::sync::mpsc::{Receiver, Sender};
10-
use log::{hash, Entry, Event, Sha256Hash};
10+
use log::{extend_and_hash, hash, Entry, Event, Sha256Hash};
1111

1212
pub struct Historian {
1313
pub sender: Sender<Event>,
@@ -24,31 +24,33 @@ pub enum ExitReason {
2424
fn log_events(
2525
receiver: &Receiver<Event>,
2626
sender: &Sender<Entry>,
27-
num_hashes: u64,
28-
end_hash: Sha256Hash,
29-
) -> Result<u64, (Entry, ExitReason)> {
27+
num_hashes: &mut u64,
28+
end_hash: &mut Sha256Hash,
29+
) -> Result<(), (Entry, ExitReason)> {
3030
use std::sync::mpsc::TryRecvError;
31-
let mut num_hashes = num_hashes;
3231
loop {
3332
match receiver.try_recv() {
3433
Ok(event) => {
34+
if let Event::UserDataKey(key) = event {
35+
*end_hash = extend_and_hash(end_hash, &key);
36+
}
3537
let entry = Entry {
36-
end_hash,
37-
num_hashes,
38+
end_hash: *end_hash,
39+
num_hashes: *num_hashes,
3840
event,
3941
};
4042
if let Err(_) = sender.send(entry.clone()) {
4143
return Err((entry, ExitReason::SendDisconnected));
4244
}
43-
num_hashes = 0;
45+
*num_hashes = 0;
4446
}
4547
Err(TryRecvError::Empty) => {
46-
return Ok(num_hashes);
48+
return Ok(());
4749
}
4850
Err(TryRecvError::Disconnected) => {
4951
let entry = Entry {
50-
end_hash,
51-
num_hashes,
52+
end_hash: *end_hash,
53+
num_hashes: *num_hashes,
5254
event: Event::Tick,
5355
};
5456
return Err((entry, ExitReason::RecvDisconnected));
@@ -69,9 +71,8 @@ pub fn create_logger(
6971
let mut end_hash = start_hash;
7072
let mut num_hashes = 0;
7173
loop {
72-
match log_events(&receiver, &sender, num_hashes, end_hash) {
73-
Ok(n) => num_hashes = n,
74-
Err(err) => return err,
74+
if let Err(err) = log_events(&receiver, &sender, &mut num_hashes, &mut end_hash) {
75+
return err;
7576
}
7677
end_hash = hash(&end_hash);
7778
num_hashes += 1;
@@ -108,7 +109,7 @@ mod tests {
108109

109110
hist.sender.send(Event::Tick).unwrap();
110111
sleep(Duration::new(0, 1_000_000));
111-
hist.sender.send(Event::UserDataKey(0xdeadbeef)).unwrap();
112+
hist.sender.send(Event::UserDataKey(zero)).unwrap();
112113
sleep(Duration::new(0, 1_000_000));
113114
hist.sender.send(Event::Tick).unwrap();
114115

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
pub mod log;
33
pub mod historian;
44
extern crate digest;
5-
extern crate itertools;
65
extern crate rayon;
76
extern crate sha2;

src/log.rs

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,24 @@ pub struct Entry {
3232
#[derive(Debug, PartialEq, Eq, Clone)]
3333
pub enum Event {
3434
Tick,
35-
UserDataKey(u64),
35+
UserDataKey(Sha256Hash),
3636
}
3737

3838
impl Entry {
3939
/// Creates a Entry from the number of hashes 'num_hashes' since the previous event
4040
/// and that resulting 'end_hash'.
4141
pub fn new_tick(num_hashes: u64, end_hash: &Sha256Hash) -> Self {
42-
let event = Event::Tick;
4342
Entry {
4443
num_hashes,
4544
end_hash: *end_hash,
46-
event,
45+
event: Event::Tick,
4746
}
4847
}
4948

5049
/// Verifies self.end_hash is the result of hashing a 'start_hash' 'self.num_hashes' times.
50+
/// If the event is a UserDataKey, then hash that as well.
5151
pub fn verify(self: &Self, start_hash: &Sha256Hash) -> bool {
52-
self.end_hash == next_tick(start_hash, self.num_hashes).end_hash
52+
self.end_hash == next_hash(start_hash, self.num_hashes, &self.event)
5353
}
5454
}
5555

@@ -60,13 +60,36 @@ pub fn hash(val: &[u8]) -> Sha256Hash {
6060
hasher.result()
6161
}
6262

63-
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
64-
pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry {
63+
/// Return the hash of the given hash extended with the given value.
64+
pub fn extend_and_hash(end_hash: &Sha256Hash, val: &[u8]) -> Sha256Hash {
65+
let mut hash_data = end_hash.to_vec();
66+
hash_data.extend_from_slice(val);
67+
hash(&hash_data)
68+
}
69+
70+
pub fn next_hash(start_hash: &Sha256Hash, num_hashes: u64, event: &Event) -> Sha256Hash {
6571
let mut end_hash = *start_hash;
6672
for _ in 0..num_hashes {
6773
end_hash = hash(&end_hash);
6874
}
69-
Entry::new_tick(num_hashes, &end_hash)
75+
if let Event::UserDataKey(key) = *event {
76+
return extend_and_hash(&end_hash, &key);
77+
}
78+
end_hash
79+
}
80+
81+
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
82+
pub fn next_entry(start_hash: &Sha256Hash, num_hashes: u64, event: Event) -> Entry {
83+
Entry {
84+
num_hashes,
85+
end_hash: next_hash(start_hash, num_hashes, &event),
86+
event,
87+
}
88+
}
89+
90+
/// Creates the next Tick Entry 'num_hashes' after 'start_hash'.
91+
pub fn next_tick(start_hash: &Sha256Hash, num_hashes: u64) -> Entry {
92+
next_entry(start_hash, num_hashes, Event::Tick)
7093
}
7194

7295
/// Verifies the hashes and counts of a slice of events are all consistent.
@@ -86,13 +109,16 @@ pub fn verify_slice_seq(events: &[Entry], start_hash: &Sha256Hash) -> bool {
86109

87110
/// Create a vector of Ticks of length 'len' from 'start_hash' hash and 'num_hashes'.
88111
pub fn create_ticks(start_hash: &Sha256Hash, num_hashes: u64, len: usize) -> Vec<Entry> {
89-
use itertools::unfold;
90-
let mut events = unfold(*start_hash, |state| {
91-
let event = next_tick(state, num_hashes);
92-
*state = event.end_hash;
93-
return Some(event);
94-
});
95-
events.by_ref().take(len).collect()
112+
use std::iter;
113+
let mut end_hash = *start_hash;
114+
iter::repeat(Event::Tick)
115+
.take(len)
116+
.map(|event| {
117+
let entry = next_entry(&end_hash, num_hashes, event);
118+
end_hash = entry.end_hash;
119+
entry
120+
})
121+
.collect()
96122
}
97123

98124
#[cfg(test)]
@@ -138,6 +164,32 @@ mod tests {
138164
verify_slice_generic(verify_slice_seq);
139165
}
140166

167+
#[test]
168+
fn test_reorder_attack() {
169+
let zero = Sha256Hash::default();
170+
let one = hash(&zero);
171+
172+
// First, verify UserData events
173+
let mut end_hash = zero;
174+
let events = [Event::UserDataKey(zero), Event::UserDataKey(one)];
175+
let mut entries: Vec<Entry> = events
176+
.iter()
177+
.map(|event| {
178+
let entry = next_entry(&end_hash, 0, event.clone());
179+
end_hash = entry.end_hash;
180+
entry
181+
})
182+
.collect();
183+
assert!(verify_slice(&entries, &zero)); // inductive step
184+
185+
// Next, swap only two UserData events and ensure verification fails.
186+
let event0 = entries[0].event.clone();
187+
let event1 = entries[1].event.clone();
188+
entries[0].event = event1;
189+
entries[1].event = event0;
190+
assert!(!verify_slice(&entries, &zero)); // inductive step
191+
}
192+
141193
}
142194

143195
#[cfg(all(feature = "unstable", test))]

0 commit comments

Comments
 (0)