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
//! libss is a Rust libary for secret sharing
//!
//! The shamir module contains an implementation of [Shamir Secret Sharing](https://en.wikipedia.org/wiki/Shamir%27s_Secret_Sharing) over GF(2**8)
//!
//! gf256 is a module which contains a representation of field elements in GF(2**8).
#[cfg(test)]
#[macro_use]
extern crate quickcheck;
extern crate rand;
extern crate subtle;

use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

/// gf256 is a module for field elements over the field GF(2**8) with irreducible polynomial x^8+x^4+x^3+x+1
///
/// *WARNING* this library was not audited by an expert in this area and does not guarantee constant-time cryptographic implmentation
/// But, This module uses the Rust crate `subtle` to move towards this goal and one day hopes to acheive these guarantees
///
/// # Examples
///
/// All elements are their own additive inverse
/// GF256::zero() is the additive identity
///
/// ```
/// use libss::gf256::GF256;
/// use libss::Field;
///
/// let x = GF256(80);
/// let x_plus_x = x + x;
///
/// assert_eq!(x_plus_x, GF256::zero());
/// ```
///
/// All elements except zero have inverses
///
/// ```
/// use libss::gf256::GF256;
/// use libss::Field;
///
/// let x = GF256(80);
/// let x_mul_x = x.inverse().unwrap() * x;
///
/// assert_eq!(x_mul_x, GF256::one());
/// ```
pub mod gf256;
mod interpolate;
mod polynomial;

/// Shamir is a module for shamir secret sharing.
///
/// # Example
///
/// The following code splits a secret (a slice of bytes) into n shares
/// of which k are required to recover the secret.
///
/// ```
/// extern crate rand;
///
/// use rand::{thread_rng,Rng};
/// use libss::shamir::Shamir;
///
/// let k = 3;
/// let n = 5;
/// let size = 32;
///
/// // Generate a random secret
/// let mut random_secret = Vec::with_capacity(size);
/// (0..size).for_each(|_| random_secret.push(thread_rng().gen::<u8>()));
///
/// // Split this random secret into n shares, of which k are needed to recover the secret
/// let shares = Shamir::split(&random_secret, k, n).unwrap();
///
/// // Combine the shares to recover the secret
/// let combined = Shamir::combine(&shares);
/// assert_eq!(combined, random_secret);
/// ```
///
pub mod shamir;

/// The Field trait is taken from [zkcrypto/ff](https://github.com/zkcrypto/ff/blob/master/src/lib.rs)
/// This trait represents an element of a field.
pub trait Field:
    Sized
    + Eq
    + Copy
    + Clone
    + Send
    + Sync
    + std::fmt::Debug
    + std::fmt::Display
    + 'static
    + rand::Rand
    + Add<Output = Self>
    + AddAssign
    + Div<Output = Self>
    + DivAssign
    + Mul<Output = Self>
    + MulAssign
    + Neg<Output = Self>
    + Sub<Output = Self>
    + SubAssign
{
    /// Returns the zero element of the field, the additive identity.
    fn zero() -> Self;

    /// Returns the one element of the field, the multiplicative identity.
    fn one() -> Self;

    /// Returns true iff this element is zero.
    fn is_zero(&self) -> bool;

    /// Computes the multiplicative inverse of this element, if nonzero.
    fn inverse(&self) -> Option<Self>;

    /// Squares this element.
    fn square(&mut self);
}