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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! ## Shamir's secret-sharing scheme
//!
//! [Shamir's secret-sharing scheme][shamir-wiki] is an algorithm for splitting a
//! secret into some number of shares `n`, such that you need at minimum some number
//! of shares `k` to reconstruct the original secret.
//!
//! ``` rust
//! use gf256::shamir::shamir;
//!
//! // generate shares
//! let shares = shamir::generate(b"secret secret secret!", 5, 4);
//!
//! // <4 can't reconstruct secret
//! assert_ne!(shamir::reconstruct(&shares[..1]), b"secret secret secret!");
//! assert_ne!(shamir::reconstruct(&shares[..2]), b"secret secret secret!");
//! assert_ne!(shamir::reconstruct(&shares[..3]), b"secret secret secret!");
//!
//! // >=4 can reconstruct secret
//! assert_eq!(shamir::reconstruct(&shares[..4]), b"secret secret secret!");
//! assert_eq!(shamir::reconstruct(&shares[..5]), b"secret secret secret!");
//! ```
//!
//! Note this module requires feature `shamir`. You may also want to enable the
//! feature `thread-rng`, which is required for the default rng.
//!
//! A fully featured implementation of Shamir's secret sharing can be found in
//! [`examples/shamir.rs`][shamir-example]:
//!
//! ``` bash
//! $ RUSTFLAGS="-Ctarget-cpu=native" cargo +nightly run --features nightly,thread-rng,lfsr,crc,shamir,raid,rs --example shamir
//!
//! testing shamir("Hello World!")
//! generate share1 => .....uT4.z.O. 019ddb829d755434f77ae84ffd
//! generate share2 => .Y4..Nq...... 025934c8a74e711c05f4aeb7d0
//! generate share3 => ...H7.....?v. 03c2be4837b908b4ad113f7607
//! generate share4 => .y......]..x. 0479b0bbf09cb8e85de497780f
//! generate share5 => ...,Z...e.m.. 0515b62c5ad2e20b65b86d15e9
//! reconstruct 1 shares => ....uT4.z.O. 9ddb829d755434f77ae84ffd
//! reconstruct 2 shares => *uO...,R.!.. 2a754f8b97bc2c520021ece6
//! reconstruct 3 shares => .Q...-._.y.* 0651020d822d9c5f9f798e2a
//! reconstruct 4 shares => Hello World! 48656c6c6f20576f726c6421
//! reconstruct 5 shares => Hello World! 48656c6c6f20576f726c6421
//! ```
//!
//! ## How does Shamir's secret sharing scheme work?
//!
//! The underlying theory of Shamir's secret sharing is actually relatively easy
//! to visualize.
//!
//! Consider some 2-degree polynomial:
//!
//! ``` text
//! .
//! . ......
//! ..' ''.
//! ' . '.
//! .' . '
//! . . '.
//!. . . . . : . . . . . . . . . . .
//! ' .
//! ' .
//! ' .
//! ' .
//! . .
//! .
//! ```
//!
//! Because our polynomial is 2-degree, we need at minimum 3 points to uniquely
//! define the polynomial. If we only have 2 points:
//!
//! ``` text
//! .
//! .
//! .
//! . o
//! o .
//! .
//! . . . . . . . . . . . . . . . . .
//! .
//! .
//! .
//! .
//! .
//! .
//! ```
//!
//! There are any number of polynomials that intersect these 2 points! With only
//! 2 points, it's impossible to figure out the original polynomial.
//!
//! ``` text
//! '. .
//! . ' . ...... . .
//! '. ..' ''. .'
//! '. ' ' . 'o'
//! ''o' . ..' '
//! . '''......''' ' '.
//! . . . . . : .'. . . . . . . . . .
//! ' ' . .
//! ' .. .
//! ' : .
//! ' .'....'
//! . .
//! .
//! ```
//!
//! But with 3 points, there is only one 2-degree polynomial that hits all three:
//!
//! ``` text
//! .
//! . .o....
//! ..' ''.
//! ' . 'o
//! o' . '
//! . . '.
//!. . . . . : . . . . . . . . . . .
//! ' .
//! ' .
//! ' .
//! ' .
//! . .
//! .
//! ```
//!
//! We can store a secret value on a polynomial by creating a polynomial where
//! the intersection at some arbitrary x-coordinate give us our secret value.
//! Choosing the arbitrary coordinate `x=0` is convenient because creating the
//! secret polynomial is as easy as choosing random values for the non-constant
//! coefficients. Say we wanted to store the secret value [4][xkcd-4]:
//!
//! ``` text
//! f(x) = 4 + 32x + 12x^2
//! ^ \----+----/
//! | '-- random coefficients
//! '----------- our secret value
//! ```
//!
//! We can then create any number of shares by evaluating the secret polynomial
//! at arbitrary coordinates (except zero!):
//!
//! ``` rust
//! // our random polynomial
//! let f = |x: f64| { 4.0 + 32.0*x + 12.0*x.powf(2.0) };
//!
//! // generate 4 shares
//! assert_eq!(f(1.0), 48.0);
//! assert_eq!(f(2.0), 116.0);
//! assert_eq!(f(3.0), 208.0);
//! assert_eq!(f(4.0), 324.0);
//! ```
//!
//! So our shares would be (`x=1`, `y=48`), (`x=2`, `y=116`), (`x=3`, `y=208`),
//! and (`x=4`, `y=324`).
//!
//! Since we used a 2-degree polynomial, we need at minimum any 3 of the shares to
//! find the original polynomial. Any fewer and finding the original polynomial
//! would be impossible. If we wanted a different threshold, say `k` shares, we would
//! just need to use a `k-1` degree polynomial.
//!
//! If we have at least 3 of the shares, we can find the original secret using
//! a technique called [Lagrange interpolation][lagrange-interpolation] (Wikipedia
//! will going to do a better job of explaining the math than I can):
//!
//! ``` rust
//! // we need >= 3 shares
//! let shares = [
//! (1.0, 48.0),
//! (2.0, 116.0),
//! (4.0, 324.0)
//! ];
//!
//! // find f(0) using Lagrange interpolation
//! let mut y = 0.0;
//! for (i, (x0, y0)) in shares.iter().enumerate() {
//! let mut li = 1.0;
//! for (j, (x1, _y1)) in shares.iter().enumerate() {
//! if i != j {
//! li *= x1 / (x1-x0);
//! }
//! }
//!
//! y += li*y0;
//! }
//!
//! // y should now equal our secret value!
//! assert_eq!(y, 4.0);
//! ```
//!
//! That sure is great, but using floats everywhere sure is annoying. Fortunately
//! this math works perfectly fine in finite field!
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! // our random polynomial
//! let f = |x: gf256| { gf256(4) + gf256(32)*x + gf256(12)*x.pow(2) };
//!
//! // generate 4 shares
//! assert_eq!(f(gf256(1)), gf256(40));
//! assert_eq!(f(gf256(2)), gf256(116));
//! assert_eq!(f(gf256(3)), gf256(88));
//! assert_eq!(f(gf256(4)), gf256(68));
//! ```
//!
//! ``` rust
//! # use ::gf256::*;
//! #
//! // we need >= 3 shares
//! let shares = [
//! (gf256(1), gf256(40)),
//! (gf256(2), gf256(116)),
//! (gf256(4), gf256(68)),
//! ];
//!
//! // find f(0) using Lagrange interpolation
//! let mut y = gf256(0);
//! for (i, (x0, y0)) in shares.iter().enumerate() {
//! let mut li = gf256(1);
//! for (j, (x1, _y1)) in shares.iter().enumerate() {
//! if i != j {
//! li *= x1 / (x1-x0);
//! }
//! }
//!
//! y += li*y0;
//! }
//!
//! // y should now equal our secret value!
//! assert_eq!(y, gf256(4));
//! ```
//!
//! And this is how our Shamir's secret sharing scheme works:
//!
//! ``` rust
//! use gf256::shamir::shamir;
//!
//! let shares = [[1, 40], [2, 116], [4, 68]];
//! assert_eq!(shamir::reconstruct(&shares), &[4]);
//! ```
//!
//! Of course, we usually want to distribute secrets that are more
//! than a byte large. We can expand this scheme to any number of bytes
//! by choosing a different random polynomial for each byte in the secret
//! value. Though we can at least share the same x-coordinate for all generated
//! points in a given share.
//!
//! ``` rust
//! # // using a fixed-rng (bad!) so this example is reproducible/testable
//! # pub use ::gf256::*; // these imports are a hack around doctest namespacing issues
//! # pub use ::gf256::gf;
//! # #[::gf256::shamir::shamir(rng=gf256::lfsr::Lfsr64::new(0x123456789abcdef1))]
//! # mod shamir {}
//! #
//! # fn main() {
//! // generate shares
//! let shares = shamir::generate(b"secret secret secret!", 4, 3);
//!
//! fn hex(xs: &[u8]) -> String {
//! xs.iter()
//! .map(|x| format!("{:02x}", x))
//! .collect()
//! }
//!
//! assert_eq!(hex(&shares[0]), "01fb3cdc338aed9bc436218f52788f5768e1d282042a");
//! assert_eq!(hex(&shares[1]), "0264be77c1902132faa6661c7c7f9c8b00ec15d89fd7");
//! assert_eq!(hex(&shares[2]), "03ece7c8807fb8894df524e14b7333af0d6eb53fefdc");
//! assert_eq!(hex(&shares[3]), "0435778acd4a2bfdb37757b0962e9e644e0254a79377");
//! // ^\-------------------+--------------------/
//! // | |
//! // arbitrary x-coordinate y-coordinates
//!
//! // reconstruct our secret
//! assert_eq!(shamir::reconstruct(&shares), b"secret secret secret!");
//! # }
//! ```
//!
//! Note that using a different polynomial for each byte is quite important.
//! Shamir's secret sharing scheme is a generalization of a [one-time pad][one-time-pad],
//! and sharing a polynomial for all bytes reduces the one-time pad into a simple
//! substitution cipher, opening the scheme up to attacks.
//!
//! ## Limitations
//!
//! It may be a surprise, but it turns out that finite-fields are finite. This means
//! there are only a finite number of elements to choose from when choosing our
//! the arbitrary x-coordinates for our shares.
//!
//! Because of this, Shamir's secret sharing scheme is limited to the number of non-zero
//! elements in our field. In the case of `GF(256)`, this limits us to 255 shares.
//!
//! ## Constant-time
//!
//! The default Shamir's secret-sharing implementation internally uses a custom
//! Galois-field type in `barret` mode and should be constant-time.
//!
//! ## Security notes
//!
//! It's worth emphasizing that the gf256 was implemented primarily as an
//! educational project. I would not suggest using this library for security-related
//! applications without first evaluating externally. You use this library at your
//! own risk.
//!
//!
//! [shamir-wiki]: https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing
//! [xkcd-4]: https://xkcd.com/221/
//! [lagrange-interpolation]: https://en.wikipedia.org/wiki/Lagrange_polynomial
//! [one-time-pad]: https://en.wikipedia.org/wiki/One-time_pad
//! [shamir-example]:
// macro for creating Shamir secret-sharing implementations
pub use shamir;
// Shamir secret-sharing functions
//
// Note we can only provide a default if we have ThreadRng available,
// otherwise we can only provide the shamir macro which accepts a
// custom Rng type
//