1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::BLAKE2B_HASH_SIZE;
use arrayvec::ArrayVec;
use core::borrow::Borrow;
use core::convert::TryFrom;
use ed25519_dalek::PublicKey;
use snafu::{NoneError, ResultExt};
#[cfg(feature = "std")]
use std::collections::HashMap;
use ed25519_dalek::Signature as DalekSignature;
#[cfg(feature = "std")]
use ed25519_dalek::verify_batch as verify_batch_dalek;
use crate::yamf_hash::YamfHash;
use super::verify_links_and_payload;
use super::Entry;
use rayon::prelude::*;
#[cfg(feature = "std")]
use blake2b_simd::blake2b;
use super::error::*;
#[cfg(feature = "std")]
pub fn verify_batch<E: AsRef<[u8]> + Sync, P: AsRef<[u8]> + Sync>(
entries_and_payloads: &[(E, Option<P>)],
) -> Result<()> {
verify_batch_links_and_payload(entries_and_payloads)?;
let bytes_iter = entries_and_payloads
.iter()
.map(|(bytes, _)| bytes.as_ref())
.collect::<Vec<_>>();
verify_batch_signatures(&bytes_iter)?;
Ok(())
}
#[cfg(feature = "std")]
pub fn verify_batch_links_and_payload<E: AsRef<[u8]> + Sync, P: AsRef<[u8]> + Sync>(
entries_and_payloads: &[(E, Option<P>)],
) -> Result<()> {
let hash_map = entries_and_payloads[..]
.par_iter()
.map(|(bytes, payload)| {
let entry = Entry::try_from(bytes.as_ref()).context(DecodeEntry)?;
let entry_hash = blake2b(bytes.as_ref());
let payload_and_hash = payload
.as_ref()
.map(|payload| (payload.as_ref(), blake2b(payload.as_ref())));
Ok((
entry.seq_num,
(bytes.as_ref(), entry, entry_hash, payload_and_hash),
))
})
.collect::<Result<HashMap<u64, (_, _, _, _)>>>()?;
hash_map
.par_iter()
.map(|(seq_num, (_, entry, _, payload_and_hash))| {
let backlink_and_hash = hash_map.get(&(seq_num - 1)).map(
|(bytes, _, entry_hash, _)| -> (_, YamfHash<ArrayVec<[u8; BLAKE2B_HASH_SIZE]>>) {
(*bytes, (*entry_hash).into())
},
);
let lipmaa_link_and_hash = hash_map.get(&(lipmaa_link::lipmaa(*seq_num))).map(
|(bytes, _, entry_hash, _)| -> (_, YamfHash<ArrayVec<[u8; BLAKE2B_HASH_SIZE]>>) {
(*bytes, (*entry_hash).into())
},
);
let payload_and_hash = payload_and_hash
.as_ref()
.map(|(payload, job)| (*payload, (*job).into()));
verify_links_and_payload(
entry,
payload_and_hash,
lipmaa_link_and_hash,
backlink_and_hash,
)
})
.collect()
}
#[cfg(feature = "std")]
pub fn verify_batch_signatures<'a, T: AsRef<[u8]>>(entries_bytes: &'a [T]) -> Result<()>
where
[T]: ParallelSlice<T>,
T: Sync,
{
entries_bytes
.as_parallel_slice()
.par_chunks(125)
.try_fold(
|| (),
|_, chunk| {
let entries = chunk
.into_iter()
.map(|bytes| Entry::try_from(bytes.as_ref()).context(DecodeEntry))
.collect::<Result<Vec<_>>>()?;
let unsigned_encoding_vecs = entries
.iter()
.map(|entry| {
let mut vec = Vec::with_capacity(entry.encoding_length());
entry
.encode_for_signing_write(&mut vec)
.context(EncodeEntryForSigning)?;
Ok(vec)
})
.collect::<Result<Vec<Vec<u8>>>>()?;
let unsigned_encodings = unsigned_encoding_vecs
.iter()
.map(|entry| entry.as_ref())
.collect::<Vec<_>>();
let signatures = entries
.iter()
.map(|entry| {
let ssb_sig =
DalekSignature::try_from(entry.sig.as_ref().unwrap().0.borrow())
.map_err(|_| NoneError)
.context(DecodeSigError)?;
Ok(ssb_sig)
})
.collect::<Result<Vec<DalekSignature>>>()?;
let pub_keys = entries
.iter()
.map(|entry| entry.author.clone())
.collect::<Vec<PublicKey>>();
verify_batch_dalek(&unsigned_encodings, &signatures, &pub_keys[..])
.map_err(|_| NoneError)
.context(InvalidSignature)?;
Ok(())
},
)
.try_reduce(|| (), |_, _| Ok(()))
}