Remove try_ methods.

master
Andreas Fackler 7 years ago committed by Andreas Fackler
parent ad11ceaed6
commit 214e5f81cf
  1. 84
      src/lib.rs
  2. 110
      src/poly.rs
  3. 8
      src/secret.rs

@ -230,18 +230,16 @@ pub struct SecretKey(Box<Fr>);
impl Default for SecretKey {
fn default() -> Self {
let mut fr = Fr::zero();
SecretKey::try_from_mut(&mut fr)
.unwrap_or_else(|e| panic!("Failed to create default `SecretKey`: {}", e))
SecretKey::from_mut(&mut fr)
}
}
/// Creates a random `SecretKey` from a given RNG. If you do not need to specify your own RNG, you
/// should use `SecretKey::random()` or `SecretKey::try_random()` as your constructor instead.
/// should use `SecretKey::random()` as your constructor instead.
impl Rand for SecretKey {
fn rand<R: Rng>(rng: &mut R) -> Self {
let mut fr = Fr::rand(rng);
SecretKey::try_from_mut(&mut fr)
.unwrap_or_else(|e| panic!("Failed to create random `SecretKey`: {}", e))
SecretKey::from_mut(&mut fr)
}
}
@ -249,16 +247,11 @@ impl Rand for SecretKey {
impl Clone for SecretKey {
fn clone(&self) -> Self {
let mut fr = *self.0;
SecretKey::try_from_mut(&mut fr)
.unwrap_or_else(|e| panic!("Failed to clone `SecretKey`: {}", e))
SecretKey::from_mut(&mut fr)
}
}
/// Zeroes out and unlocks the memory allocated from the `SecretKey`'s field element.
///
/// # Panics
///
/// Panics if we fail to unlock the memory containing the field element.
impl Drop for SecretKey {
fn drop(&mut self) {
self.zero_secret();
@ -288,54 +281,25 @@ impl SecretKey {
/// *WARNING* this constructor will overwrite the referenced `Fr` element with zeros after it
/// has been copied onto the heap.
pub fn from_mut(fr: &mut Fr) -> Self {
SecretKey::try_from_mut(fr)
.unwrap_or_else(|e| panic!("Falied to create `SecretKey`: {}", e))
}
/// Creates a new `SecretKey` from a mutable reference to a field element. This constructor
/// takes a reference to avoid any unnecessary stack copying/moving of secrets (i.e. the field
/// element). The field element is copied bytewise onto the heap, the resulting `Box` is
/// stored in the returned `SecretKey`.
///
/// This constructor is identical to `SecretKey::from_mut()` in every way except that this
/// constructor will return an `Err` where `SecretKey::from_mut()` would panic.
///
/// *WARNING* this constructor will overwrite the referenced `Fr` element with zeros after it
/// has been copied onto the heap.
pub fn try_from_mut(fr: &mut Fr) -> Result<Self> {
let fr_ptr = fr as *mut Fr;
let mut boxed_fr = Box::new(Fr::zero());
unsafe {
copy_nonoverlapping(fr_ptr, &mut *boxed_fr as *mut Fr, 1);
}
clear_fr(fr_ptr);
let sk = SecretKey(boxed_fr);
Ok(sk)
SecretKey(boxed_fr)
}
/// Creates a new random instance of `SecretKey`. If you want to use/define your own random
/// number generator, you should use the constructor: `SecretKey::rand()`. If you do not need
/// to specify your own RNG, you should use the `SecretKey::random()` and
/// `SecretKey::try_random()` constructors, which use
/// to specify your own RNG, you should use the `SecretKey::random()` constructor, which uses
/// [`rand::thead_rng()`](https://docs.rs/rand/0.4.3/rand/fn.thread_rng.html) internally as
/// their RNG.
/// its RNG.
pub fn random() -> Self {
let mut rng = rand::thread_rng();
SecretKey::rand(&mut rng)
}
/// Creates a new random instance of `SecretKey`. If you want to use/define your own random
/// number generator, you should use the constructor: `SecretKey::rand()`. If you do not need
/// to specify your own RNG, you should use the `SecretKey::random()` and
/// `SecretKey::try_random()` constructors, which use
/// [`rand::thead_rng()`](https://docs.rs/rand/0.4.3/rand/fn.thread_rng.html) internally as
/// their RNG.
pub fn try_random() -> Result<Self> {
let mut rng = rand::thread_rng();
let mut fr = Fr::rand(&mut rng);
SecretKey::try_from_mut(&mut fr)
}
/// Returns the matching public key.
pub fn public_key(&self) -> PublicKey {
PublicKey(G1Affine::one().mul(*self.0))
@ -390,21 +354,7 @@ impl SecretKeyShare {
/// *WARNING* this constructor will overwrite the pointed to `Fr` element with zeros once it
/// has been copied into a new `SecretKeyShare`.
pub fn from_mut(fr: &mut Fr) -> Self {
match SecretKey::try_from_mut(fr) {
Ok(sk) => SecretKeyShare(sk),
Err(e) => panic!(
"Failed to create `SecretKeyShare` from field element: {}",
e
),
}
}
/// Creates a new `SecretKeyShare` from a mutable reference to a field element. This
/// constructor takes a reference to avoid any unnecessary stack copying/moving of secrets
/// field elements. The field element will be copied bytewise onto the heap, the resulting
/// `Box` is stored in the `SecretKey` which is then wrapped in a `SecretKeyShare`.
pub fn try_from_mut(fr: &mut Fr) -> Result<Self> {
SecretKey::try_from_mut(fr).map(SecretKeyShare)
SecretKeyShare(SecretKey::from_mut(fr))
}
/// Returns the matching public key share.
@ -559,6 +509,10 @@ impl SecretKeySet {
/// Creates a set of secret key shares, where any `threshold + 1` of them can collaboratively
/// sign and decrypt. This constuctor is identical to the `SecretKey::try_random()` in every
/// way except that this constructor panics if the other returns an error.
///
/// # Panic
///
/// Panics if the `threshold` is too large for the coefficients to fit into a `Vec`.
pub fn random<R: Rng>(threshold: usize, rng: &mut R) -> Self {
SecretKeySet::try_random(threshold, rng)
.unwrap_or_else(|e| panic!("Failed to create random `SecretKeySet`: {}", e))
@ -577,20 +531,10 @@ impl SecretKeySet {
self.poly.degree()
}
/// Returns the `i`-th secret key share. This method is identical to the
/// `.try_secret_key_share()` in every way except that this method panics if
/// where `.try_secret_key_share()` would return an `Err`.
/// Returns the `i`-th secret key share.
pub fn secret_key_share<T: IntoFr>(&self, i: T) -> SecretKeyShare {
self.try_secret_key_share(i)
.unwrap_or_else(|e| panic!("Failed to create `SecretKeyShare`: {}", e))
}
/// Returns the `i`-th secret key share. This method is identical to the method
/// `.secret_key_share()` in every way except that this method returns an `Err` if
/// where `.secret_key_share()` would panic.
pub fn try_secret_key_share<T: IntoFr>(&self, i: T) -> Result<SecretKeyShare> {
let mut fr = self.poly.evaluate(into_fr_plus_1(i));
SecretKeyShare::try_from_mut(&mut fr)
SecretKeyShare::from_mut(&mut fr)
}
/// Returns the corresponding public key set. That information can be shared publicly.

@ -41,8 +41,7 @@ pub struct Poly {
/// Creates a new `Poly` with the same coefficients as another polynomial.
impl Clone for Poly {
fn clone(&self) -> Self {
Poly::try_from(self.coeff.clone())
.unwrap_or_else(|e| panic!("Failed to clone `Poly`: {}", e))
Poly::from(self.coeff.clone())
}
}
@ -177,8 +176,7 @@ impl<'a, B: Borrow<Poly>> ops::Mul<B> for &'a Poly {
coeffs[i + j].add_assign(&*tmp);
}
}
Poly::try_from(coeffs)
.unwrap_or_else(|e| panic!("Failed to create a new `Poly` during muliplication: {}", e))
Poly::from(coeffs)
}
}
@ -265,8 +263,8 @@ impl Drop for Poly {
/// Creates a new `Poly` instance from a vector of prime field elements representing the
/// coefficients of the polynomial.
impl From<Vec<Fr>> for Poly {
fn from(coeffs: Vec<Fr>) -> Self {
Poly::try_from(coeffs).unwrap_or_else(|e| panic!("Failed to create `Poly`: {}", e))
fn from(coeff: Vec<Fr>) -> Self {
Poly { coeff }
}
}
@ -279,16 +277,11 @@ impl ContainsSecret for Poly {
}
impl Poly {
/// Creates a new `Poly` instance from a vector of prime field elements representing the
/// coefficients of the polynomial.
pub fn try_from(coeff: Vec<Fr>) -> Result<Self> {
let poly = Poly { coeff };
Ok(poly)
}
/// Creates a random polynomial. This constructor is identical to the `Poly::try_random()`
/// constructor in every way except that this constructor will panic where `random` would
/// return an error.
/// Creates a random polynomial.
///
/// # Panics
///
/// Panics if the `degree` is too large for the coefficients to fit into a `Vec`.
pub fn random<R: Rng>(degree: usize, rng: &mut R) -> Self {
Poly::try_random(degree, rng)
.unwrap_or_else(|e| panic!("Failed to create random `Poly`: {}", e))
@ -302,7 +295,7 @@ impl Poly {
return Err(Error::DegreeTooHigh);
}
let coeff: Vec<Fr> = (0..=degree).map(|_| rng.gen()).collect();
Poly::try_from(coeff)
Ok(Poly::from(coeff))
}
/// Returns the polynomial with constant value `0`.
@ -315,19 +308,9 @@ impl Poly {
self.coeff.iter().all(|coeff| coeff.is_zero())
}
/// Returns the polynomial with constant value `1`. This constructor is identical to
/// `Poly::try_one()` in every way except that this constructor panics where `Poly::try_one()`
/// would return an `Err`.
/// Returns the polynomial with constant value `1`.
pub fn one() -> Self {
Poly::try_one()
.unwrap_or_else(|e| panic!("Failed to create constant `Poly` of value 1: {}", e))
}
/// Returns the polynomial with constant value `1`. This constructor is identical to
/// `Poly::one()` in every way except that this constructor returns `Err` if where `Poly::one()`
/// would panic.
pub fn try_one() -> Result<Self> {
Poly::try_constant(Fr::one())
Poly::constant(Fr::one())
}
/// Returns the polynomial with constant value `c`.
@ -336,69 +319,28 @@ impl Poly {
// overwrite that portion of memory with zeros once we have copied the element onto the
// heap as part of the vector of polynomial coefficients.
let fr_ptr = &c as *const Fr;
let poly = Poly::try_from(vec![c])
.unwrap_or_else(|e| panic!("Failed to create constant `Poly`: {}", e));
let poly = Poly::from(vec![c]);
clear_fr(fr_ptr);
poly
}
/// Returns the polynomial with constant value `c`. This constructor is identical to
/// `Poly::constant()` in every way except that this constructor returns an `Err` where
/// `constant` would panic.
pub fn try_constant(c: Fr) -> Result<Self> {
// We create a raw pointer to the field element within this method's stack frame so we can
// overwrite that portion of memory with zeros once we have copied the element onto the
// heap as part of polynomials `coeff` vector.
let fr_ptr = &c as *const Fr;
let res = Poly::try_from(vec![c]);
clear_fr(fr_ptr);
res
}
/// Returns the identity function, i.e. the polynomial "`x`".
pub fn identity() -> Self {
Poly::monomial(1)
}
/// Returns the identity function, i.e. the polynomial `x`.
pub fn try_identity() -> Result<Self> {
Poly::try_monomial(1)
}
/// Returns the (monic) monomial: `x.pow(degree)`.
pub fn monomial(degree: usize) -> Self {
Poly::try_monomial(degree).unwrap_or_else(|e| {
panic!(
"Failed to create monomial `Poly` of degree {}: {}",
degree, e
)
})
}
/// Returns the (monic) monomial: `x.pow(degree)`.
pub fn try_monomial(degree: usize) -> Result<Self> {
let coeff: Vec<Fr> = iter::repeat(Fr::zero())
.take(degree)
.chain(iter::once(Fr::one()))
.collect();
Poly::try_from(coeff)
Poly::from(coeff)
}
/// Returns the unique polynomial `f` of degree `samples.len() - 1` with the given values
/// `(x, f(x))`.
pub fn interpolate<T, U, I>(samples_repr: I) -> Self
where
I: IntoIterator<Item = (T, U)>,
T: IntoFr,
U: IntoFr,
{
Poly::try_interpolate(samples_repr)
.unwrap_or_else(|e| panic!("Failed to interpolate `Poly`: {}", e))
}
/// Returns the unique polynomial `f` of degree `samples.len() - 1` with the given values
/// `(x, f(x))`.
pub fn try_interpolate<T, U, I>(samples_repr: I) -> Result<Self>
where
I: IntoIterator<Item = (T, U)>,
T: IntoFr,
@ -445,16 +387,16 @@ impl Poly {
/// Returns the unique polynomial `f` of degree `samples.len() - 1` with the given values
/// `(x, f(x))`.
fn compute_interpolation(samples: &[(Fr, Fr)]) -> Result<Self> {
fn compute_interpolation(samples: &[(Fr, Fr)]) -> Self {
if samples.is_empty() {
return Ok(Poly::zero());
return Poly::zero();
}
// Interpolates on the first `i` samples.
let mut poly = Poly::try_constant(samples[0].1)?;
let mut poly = Poly::constant(samples[0].1);
let mut minus_s0 = samples[0].0;
minus_s0.negate();
// Is zero on the first `i` samples.
let mut base = Poly::try_from(vec![minus_s0, Fr::one()])?;
let mut base = Poly::from(vec![minus_s0, Fr::one()]);
// We update `base` so that it is always zero on all previous samples, and `poly` so that
// it has the correct values on the previous samples.
@ -471,9 +413,9 @@ impl Poly {
// Finally, multiply `base` by X - x, so that it is zero at `x`, too, now.
let mut minus_x = *x;
minus_x.negate();
base *= Poly::try_from(vec![minus_x, Fr::one()])?;
base *= Poly::from(vec![minus_x, Fr::one()]);
}
Ok(poly)
poly
}
/// Generates a non-redacted debug string. This method differs from
@ -603,6 +545,10 @@ impl ContainsSecret for BivarPoly {
}
impl BivarPoly {
/// Creates a random polynomial.
///
/// # Panics
///
/// Panics if the degree is too high for the coefficients to fit into a `Vec`.
pub fn random<R: Rng>(degree: usize, rng: &mut R) -> Self {
BivarPoly::try_random(degree, rng).unwrap_or_else(|e| {
panic!(
@ -649,12 +595,6 @@ impl BivarPoly {
/// Returns the `x`-th row, as a univariate polynomial.
pub fn row<T: IntoFr>(&self, x: T) -> Poly {
self.try_row(x)
.unwrap_or_else(|e| panic!("Failed to create `Poly` from row of `BivarPoly: {}`", e))
}
/// Returns the `x`-th row, as a univariate polynomial.
pub fn try_row<T: IntoFr>(&self, x: T) -> Result<Poly> {
let x_pow = self.powers(x);
let coeff: Vec<Fr> = (0..=self.degree)
.map(|i| {
@ -668,7 +608,7 @@ impl BivarPoly {
}
result
}).collect();
Poly::try_from(coeff)
Poly::from(coeff)
}
/// Returns the corresponding commitment. That information can be shared publicly.

@ -7,8 +7,6 @@ use std::ops::{Deref, DerefMut};
use memsec::memzero;
use Fr;
use error::Result;
lazy_static! {
/// The size in bytes of a single field element.
pub(crate) static ref FR_SIZE: usize = size_of::<Fr>();
@ -89,10 +87,6 @@ where
T: DerefMut,
{
pub(crate) fn new(x: T) -> Self {
Safe::try_new(x).unwrap_or_else(|e| panic!("Failed to create `Safe`: {}", e))
}
pub(crate) fn try_new(x: T) -> Result<Self> {
Ok(Safe(x))
Safe(x)
}
}

Loading…
Cancel
Save