shamirss 0.1.2

Hobbist implementation of Shamirs Secret Sharing algorithm.
Documentation
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
use crate::errors::SSSError;
use base64::{engine::general_purpose::STANDARD, Engine as _};
use openssl::bn::{BigNum, BigNumContextRef};

/// Default prime used for mod calculations.
///
pub(crate) const DEFAULT_PRIME: &str =
    "115792089237316195423570985008687907853269984665640564039457584007913129639747";

/// Maximum initial size of big int is set to 32 bytes to protect against value overflow.
///
pub(crate) const U8S_TO_BIG_INT_INITIAL: usize = 32;

/// Calculates is the bytes slice divisor is initial big int size.
///
#[inline(always)]
pub(crate) fn is_proper_size(v: &[u8]) -> bool {
    v.len() % U8S_TO_BIG_INT_INITIAL == 0
}

/// Returns random number between 0 and DEFAULT_PRIME - 1.
///
#[inline(always)]
pub(crate) fn random(upper_limit: &BigNum) -> Result<BigNum, SSSError> {
    let mut result = BigNum::new()?;
    upper_limit.rand_range(&mut result)?;
    Ok(result)
}

/// Maps bytes to big nums.
///
#[inline(always)]
pub(crate) fn bytes_to_big_nums(bytes: &[u8]) -> Result<Vec<BigNum>, SSSError> {
    let mut slice =
        Vec::with_capacity((bytes.len() as f64 / U8S_TO_BIG_INT_INITIAL as f64).ceil() as usize);
    for (start, _) in bytes.iter().step_by(U8S_TO_BIG_INT_INITIAL).enumerate() {
        let start = start * U8S_TO_BIG_INT_INITIAL;
        let end = if start + U8S_TO_BIG_INT_INITIAL > bytes.len() {
            bytes.len()
        } else {
            start + U8S_TO_BIG_INT_INITIAL
        };
        let num = BigNum::from_slice(&bytes[start..end])?;
        slice.push(num);
    }

    Ok(slice)
}

/// Maps big nums to bytes.
///
#[inline(always)]
pub(crate) fn big_nums_to_bytes(slice: &[BigNum]) -> Vec<u8> {
    let mut result = Vec::with_capacity(slice.len() * U8S_TO_BIG_INT_INITIAL);
    for big_int in slice.iter() {
        let bytes = big_int.to_vec();
        if bytes.len() < U8S_TO_BIG_INT_INITIAL {
            result.extend(vec![0; U8S_TO_BIG_INT_INITIAL - bytes.len()].iter());
            result.extend(bytes);
        } else {
            result.extend(bytes);
        }
    }
    result
}

/// Evaluates polynomial slice.
///
#[inline(always)]
pub(crate) fn evaluate(
    ctx: &mut BigNumContextRef,
    slice: &[BigNum],
    value: &BigNum,
    prime: &BigNum,
) -> Result<BigNum, SSSError> {
    let mut result = BigNum::new()?;

    for i in (0..slice.len()).rev() {
        let mut temp = BigNum::new()?;
        temp.checked_mul(result.as_ref(), value, ctx)?;
        result.checked_add(temp.as_ref(), &slice[i])?;
        temp.nnmod(result.as_ref(), prime, ctx)?;
        result = temp
    }

    Ok(result)
}

/// Decodes hex to bytes.
///
#[inline(always)]
pub(crate) fn secret_hex_to_bytes(s: &str) -> Result<Vec<u8>, SSSError> {
    Ok(hex::decode(s)?)
}

/// Decodes hex shares slice to slices of bytes slices.
///
#[inline(always)]
pub(crate) fn shares_hex_to_bytes(s: &[String]) -> Result<Vec<Vec<u8>>, SSSError> {
    let mut err = None;
    let result = s
        .iter()
        .map(hex::decode)
        .filter_map(|r| {
            r.map_err(|e| {
                if err.is_none() {
                    err = Some(e)
                }
            })
            .ok()
        })
        .collect();
    if let Some(e) = err {
        return Err(SSSError::FromHex(e));
    }

    Ok(result)
}

/// Encodes secret bytes to hex.
///
#[inline(always)]
pub(crate) fn secret_bytes_to_hex(h: &[u8]) -> String {
    hex::encode(h)
}

/// Encodes shares slices of bytes to hex slices.
///
#[inline(always)]
pub(crate) fn shares_bytes_to_hex(h: Vec<Vec<u8>>) -> Vec<String> {
    h.iter().map(hex::encode).collect::<Vec<String>>()
}

/// Decodes base64 to bytes.
///
#[inline(always)]
pub(crate) fn secret_base64_to_bytes(s: &str) -> Result<Vec<u8>, SSSError> {
    Ok(STANDARD.decode(s)?)
}

/// Decodes base64 shares slice to slices of bytes slices.
///
#[inline(always)]
pub(crate) fn shares_base64_to_bytes(s: &[String]) -> Result<Vec<Vec<u8>>, SSSError> {
    let mut err = None;
    let result = s
        .iter()
        .map(|b| STANDARD.decode(b))
        .filter_map(|r| {
            r.map_err(|e| {
                if err.is_none() {
                    err = Some(e)
                }
            })
            .ok()
        })
        .collect();
    if let Some(e) = err {
        return Err(SSSError::FromBase64(e));
    }

    Ok(result)
}

/// Encodes secret bytes to base64.
///
#[inline(always)]
pub(crate) fn secret_bytes_to_base64(h: &[u8]) -> String {
    STANDARD.encode(h)
}

/// Encodes secret bytes to base64.
///
#[inline(always)]
pub(crate) fn shares_bytes_to_base64(h: Vec<Vec<u8>>) -> Vec<String> {
    h.iter()
        .map(|s| STANDARD.encode(s))
        .collect::<Vec<String>>()
}

#[cfg(test)]
mod tests {
    use openssl::bn::BigNumContext;

    use super::*;
    use std::collections::HashSet;
    use std::time::Instant;

    const ITER_COUNT: usize = 1_000_000;
    const BENCH_ITTER: usize = 100_000;

    #[test]
    fn it_shall_create_unique_random_big_int_each_time() -> Result<(), SSSError> {
        let prime = BigNum::from_dec_str(DEFAULT_PRIME).unwrap();
        let mut set = HashSet::new();
        for _ in 0..ITER_COUNT {
            assert!(set.insert(random(&prime)?.to_vec()));
        }

        Ok(())
    }

    #[test]
    fn it_should_benchmark_random_big_int() -> Result<(), SSSError> {
        let prime = BigNum::from_dec_str(DEFAULT_PRIME)?;
        let now = Instant::now();
        for _ in 0..BENCH_ITTER {
            random(&prime)?;
        }

        println!(
            "Time to calculate random big int took on average {} [ nanoseconds ]",
            (now.elapsed().as_nanos() as f64 / BENCH_ITTER as f64) as usize
        );

        Ok(())
    }

    #[test]
    fn it_should_convert_bytes_to_slice_of_big_ints() -> Result<(), SSSError> {
        let bytes_2_bytes: &[u8; 2] = &[10, 33];
        let expected_big_int_from_2_bytes: &[BigNum; 1] = &[BigNum::from_dec_str("2593")?];

        let bytes_16_bytes: &[u8; 16] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_16_bytes: &[BigNum; 1] = &[BigNum::from_dec_str(
            "13468799629078354179291325055507502481",
        )?];

        let bytes_32_bytes: &[u8; 32] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_32_bytes: &[BigNum; 1] = &[BigNum::from_dec_str(
            "4583195017366640394614729638310681267193810345231665377656985560666360124817",
        )?];

        let bytes_64_bytes: &[u8; 64] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145,
        ];
        let expected_big_int_from_64_bytes: &[BigNum; 1] = &[BigNum::from_dec_str(
            "4583195017366640394614729638310681267193810345231665377656985560666360124817",
        )?];

        let bytes_128_bytes: &[u8; 128] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_128_bytes: &[BigNum; 2] = &[
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
        ];

        let bytes_256_bytes: &[u8; 256] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_256_bytes: &[BigNum; 4] = &[
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
        ];

        let bytes_512_bytes: &[u8; 512] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_512_bytes: &[BigNum; 8] = &[
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
        ];

        let bytes_496_bytes: &[u8; 496] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let expected_big_int_from_496_bytes: &[BigNum; 8] = &[
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
            BigNum::from_dec_str(
                "4583195017366640394614729638310681267193810345231665377656985560666360124817",
            )?,
        ];

        struct TestCase<'a> {
            name: &'a str,
            buff: &'a [u8],
            expected: &'a [BigNum],
        }

        for c in vec![
            TestCase {
                name: "bytes_2_bytes",
                buff: bytes_2_bytes,
                expected: expected_big_int_from_2_bytes,
            },
            TestCase {
                name: "bytes_16_bytes",
                buff: bytes_16_bytes,
                expected: expected_big_int_from_16_bytes,
            },
            TestCase {
                name: "bytes_32_bytes",
                buff: bytes_32_bytes,
                expected: expected_big_int_from_32_bytes,
            },
            TestCase {
                name: "bytes_64_bytes",
                buff: bytes_64_bytes,
                expected: expected_big_int_from_64_bytes,
            },
            TestCase {
                name: "bytes_128_bytes",
                buff: bytes_128_bytes,
                expected: expected_big_int_from_128_bytes,
            },
            TestCase {
                name: "bytes_256_bytes",
                buff: bytes_256_bytes,
                expected: expected_big_int_from_256_bytes,
            },
            TestCase {
                name: "bytes_512_bytes",
                buff: bytes_512_bytes,
                expected: expected_big_int_from_512_bytes,
            },
            TestCase {
                name: "bytes_496_bytes",
                buff: bytes_496_bytes,
                expected: expected_big_int_from_496_bytes,
            },
        ]
        .iter()
        {
            let polynomial = bytes_to_big_nums(c.buff)?;
            println!("Testing {}", c.name);
            for (x, y) in polynomial.iter().zip(c.expected.iter()) {
                assert_eq!(*x, *y);
            }
        }

        Ok(())
    }

    #[test]
    fn it_shall_benchmark_convert_bytes_to_slice_of_big_ints() -> Result<(), SSSError> {
        let bytes_512_bytes: &[u8; 512] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];
        let now = Instant::now();
        for _ in 0..BENCH_ITTER {
            let _ = bytes_to_big_nums(bytes_512_bytes)?;
        }

        println!(
            "Time to transform 512 bytes to big int slice took on average {} [ nanoseconds ]",
            (now.elapsed().as_nanos() as f64 / BENCH_ITTER as f64) as usize
        );

        Ok(())
    }

    #[test]
    fn it_should_convert_bytes_to_polynomial_and_back() -> Result<(), SSSError> {
        let bytes_512_bytes: &[u8; 512] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];

        let polynomial = bytes_to_big_nums(bytes_512_bytes)?;
        let bytes_converted_back: Vec<u8> = big_nums_to_bytes(&polynomial);

        for (x, y) in bytes_512_bytes.iter().zip(bytes_converted_back.iter()) {
            assert_eq!(*x, *y);
        }

        Ok(())
    }

    #[test]
    fn it_should_benchmark_convert_big_int_to_bytes() -> Result<(), SSSError> {
        let bytes_512_bytes: &[u8; 512] = &[
            10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23,
            123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99,
            0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22,
            233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6,
            99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160,
            22, 233, 222, 21, 145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21,
            145, 10, 33, 255, 23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145, 10, 33, 255,
            23, 123, 111, 6, 99, 0, 158, 160, 22, 233, 222, 21, 145,
        ];

        let polynomial = bytes_to_big_nums(bytes_512_bytes)?;

        let now = Instant::now();
        for _ in 0..BENCH_ITTER {
            let _: Vec<u8> = big_nums_to_bytes(&polynomial);
        }

        println!(
            "Time to transform {} big ints to bytes took on average {} [ nanoseconds ]",
            polynomial.len(),
            (now.elapsed().as_nanos() as f64 / BENCH_ITTER as f64) as usize
        );

        Ok(())
    }

    #[test]
    fn it_should_evaluate_the_polynomial() -> Result<(), SSSError> {
        struct TestCase<'a> {
            name: &'a str,
            slice: &'a [BigNum],
            value: BigNum,
            actual: BigNum,
        }

        let test_cases: &[TestCase; 3] = &[
            TestCase {
                name: "actual 20",
                slice: &[
                    BigNum::from_dec_str("20")?,
                    BigNum::from_dec_str("21")?,
                    BigNum::from_dec_str("42")?,
                ],
                value: BigNum::from_dec_str("0")?,
                actual: BigNum::from_dec_str("20")?,
            },
            TestCase {
                name: "actual 0",
                slice: &[
                    BigNum::from_dec_str("0")?,
                    BigNum::from_dec_str("0")?,
                    BigNum::from_dec_str("0")?,
                ],
                value: BigNum::from_dec_str("4")?,
                actual: BigNum::from_dec_str("0")?,
            },
            TestCase {
                name: "actual 54321",
                slice: &[
                    BigNum::from_dec_str("1")?,
                    BigNum::from_dec_str("2")?,
                    BigNum::from_dec_str("3")?,
                    BigNum::from_dec_str("4")?,
                    BigNum::from_dec_str("5")?,
                ],
                value: BigNum::from_dec_str("10")?,
                actual: BigNum::from_dec_str("54321")?,
            },
        ];

        let mut ctx = BigNumContext::new().unwrap();
        let prime = BigNum::from_dec_str(DEFAULT_PRIME).unwrap();

        for c in test_cases.iter() {
            println!("Testing {}", c.name);
            let actual = evaluate(&mut ctx, c.slice, &c.value, &prime)?;
            assert!(actual.eq(&c.actual));
        }

        Ok(())
    }

    #[test]
    fn it_should_benchmark_evaluate_the_polynomial() -> Result<(), SSSError> {
        let slice: &[BigNum; 12] = &[
            BigNum::from_dec_str("1")?,
            BigNum::from_dec_str("22")?,
            BigNum::from_dec_str("322")?,
            BigNum::from_dec_str("42222")?,
            BigNum::from_dec_str("234235")?,
            BigNum::from_dec_str("242424241")?,
            BigNum::from_dec_str("2242234242")?,
            BigNum::from_dec_str("3424242435687")?,
            BigNum::from_dec_str("43456345635777")?,
            BigNum::from_dec_str("5256767346724562")?,
            BigNum::from_dec_str("414514351432434")?,
            BigNum::from_dec_str("5134578587467844567")?,
        ];
        let value = BigNum::from_dec_str("0")?;
        let mut ctx = BigNumContext::new()?;
        let prime = BigNum::from_dec_str(DEFAULT_PRIME)?;
        let now = Instant::now();

        for _ in 0..BENCH_ITTER {
            let _ = evaluate(&mut ctx, slice, &value, &prime);
        }

        println!(
            "Time to evaluate polynomial of {} parameters to bytes took on average {} [ nanoseconds ]",
            slice.len(),
            (now.elapsed().as_nanos() as f64 / BENCH_ITTER as f64) as usize
        );

        Ok(())
    }
}