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
154
155
156
157
158
159
160
161
162
163
164
165
#![cfg(not(target_arch = "bpf"))]
use {
curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar, traits::Identity},
serde::{Deserialize, Serialize},
std::collections::HashMap,
};
const TWO15: u32 = 32768;
const TWO14: u32 = 16384;
const TWO18: u32 = 262144;
#[derive(Serialize, Deserialize, Copy, Clone, Debug, Eq, PartialEq)]
pub struct DiscreteLog {
pub generator: RistrettoPoint,
pub target: RistrettoPoint,
}
#[derive(Serialize, Deserialize, Default)]
pub struct DecodeU32Precomputation(HashMap<[u8; 32], u32>);
fn decode_u32_precomputation(generator: RistrettoPoint) -> DecodeU32Precomputation {
let mut hashmap = HashMap::new();
let two12_scalar = Scalar::from(TWO14);
let identity = RistrettoPoint::identity();
let generator = two12_scalar * generator;
let ristretto_iter = RistrettoIterator::new(identity, generator);
let mut steps_for_breakpoint = 0;
ristretto_iter.zip(0..TWO18).for_each(|(elem, x_hi)| {
let key = elem.compress().to_bytes();
hashmap.insert(key, x_hi);
if x_hi % TWO15 == 0 {
println!(" [{:?}/8] completed", steps_for_breakpoint);
steps_for_breakpoint += 1;
}
});
println!(" [8/8] completed");
DecodeU32Precomputation(hashmap)
}
lazy_static::lazy_static! {
pub static ref DECODE_U32_PRECOMPUTATION_FOR_G: DecodeU32Precomputation = {
static DECODE_U32_PRECOMPUTATION_FOR_G_BINCODE: &[u8] =
include_bytes!("decode_u32_precomputation_for_G.bincode");
bincode::deserialize(DECODE_U32_PRECOMPUTATION_FOR_G_BINCODE).unwrap_or_default()
};
}
impl DiscreteLog {
pub(crate) fn decode_u32(self) -> Option<u32> {
self.decode_u32_online(&decode_u32_precomputation(self.generator))
}
pub fn decode_u32_online(self, hashmap: &DecodeU32Precomputation) -> Option<u32> {
let ristretto_iter = RistrettoIterator::new(self.target, -self.generator);
let mut decoded = None;
ristretto_iter.zip(0..TWO14).for_each(|(elem, x_lo)| {
let key = elem.compress().to_bytes();
if hashmap.0.contains_key(&key) {
let x_hi = hashmap.0[&key];
decoded = Some(x_lo + TWO14 * x_hi);
}
});
decoded
}
}
struct RistrettoIterator {
pub curr: RistrettoPoint,
pub step: RistrettoPoint,
}
impl RistrettoIterator {
fn new(curr: RistrettoPoint, step: RistrettoPoint) -> Self {
RistrettoIterator { curr, step }
}
}
impl Iterator for RistrettoIterator {
type Item = RistrettoPoint;
fn next(&mut self) -> Option<Self::Item> {
let r = self.curr;
self.curr += self.step;
Some(r)
}
}
#[cfg(test)]
mod tests {
use {
super::*, curve25519_dalek::constants::RISTRETTO_BASEPOINT_POINT as G, std::time::Instant,
};
#[test]
#[allow(non_snake_case)]
fn test_serialize_decode_u32_precomputation_for_G() {
let decode_u32_precomputation_for_G = decode_u32_precomputation(G);
if decode_u32_precomputation_for_G.0 != DECODE_U32_PRECOMPUTATION_FOR_G.0 {
use std::{fs::File, io::Write, path::PathBuf};
let mut f = File::create(PathBuf::from(
"src/encryption/decode_u32_precomputation_for_G.bincode",
))
.unwrap();
f.write_all(&bincode::serialize(&decode_u32_precomputation_for_G).unwrap())
.unwrap();
panic!("Rebuild and run this test again");
}
}
#[test]
fn test_decode_correctness() {
let amount: u32 = 65545;
let instance = DiscreteLog {
generator: G,
target: Scalar::from(amount) * G,
};
let start_precomputation = Instant::now();
let precomputed_hashmap = decode_u32_precomputation(G);
let precomputation_secs = start_precomputation.elapsed().as_secs_f64();
let start_online = Instant::now();
let computed_amount = instance.decode_u32_online(&precomputed_hashmap).unwrap();
let online_secs = start_online.elapsed().as_secs_f64();
assert_eq!(amount, computed_amount);
println!("16/16 Split precomputation: {:?} sec", precomputation_secs);
println!("16/16 Split online computation: {:?} sec", online_secs);
}
}