Skip to content

Commit

Permalink
Implement {Add, Div, Mul, Sub}Assign for Gf256
Browse files Browse the repository at this point in the history
  • Loading branch information
psivesely authored and romac committed Mar 6, 2018
1 parent 3de1689 commit 9c123a9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/dss/thss/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) fn encode_secret_byte(m: u8, j: u8, poly: &Poly) -> u8 {
let mut acc = Gf256::from_byte(m);
for (l, &r) in poly.coeffs.iter().enumerate() {
let s = Gf256::from_byte(j).pow(l as u8 + 1);
acc = acc + r * s;
acc += r * s;
}
acc.to_byte()
}
28 changes: 27 additions & 1 deletion src/gf256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module provides the Gf256 type which is used to represent
//! elements of a finite field with 256 elements.

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

include!(concat!(env!("OUT_DIR"), "/nothinghardcoded.rs"));

Expand Down Expand Up @@ -74,6 +74,13 @@ impl Add<Gf256> for Gf256 {
}
}

impl AddAssign<Gf256> for Gf256 {
#[inline]
fn add_assign(&mut self, rhs: Gf256) {
*self = *self + rhs;
}
}

impl Sub<Gf256> for Gf256 {
type Output = Gf256;
#[inline]
Expand All @@ -82,6 +89,13 @@ impl Sub<Gf256> for Gf256 {
}
}

impl SubAssign<Gf256> for Gf256 {
#[inline]
fn sub_assign(&mut self, rhs: Gf256) {
*self = *self - rhs;
}
}

impl Mul<Gf256> for Gf256 {
type Output = Gf256;
fn mul(self, rhs: Gf256) -> Gf256 {
Expand All @@ -94,6 +108,12 @@ impl Mul<Gf256> for Gf256 {
}
}

impl MulAssign<Gf256> for Gf256 {
fn mul_assign(&mut self, rhs: Gf256) {
*self = *self * rhs;
}
}

impl Div<Gf256> for Gf256 {
type Output = Gf256;
fn div(self, rhs: Gf256) -> Gf256 {
Expand All @@ -107,6 +127,12 @@ impl Div<Gf256> for Gf256 {
}
}

impl DivAssign<Gf256> for Gf256 {
fn div_assign(&mut self, rhs: Gf256) {
*self = *self / rhs;
}
}

impl Neg for Gf256 {
type Output = Gf256;
fn neg(self) -> Gf256 {
Expand Down
6 changes: 3 additions & 3 deletions src/lagrange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub(crate) fn interpolate_at(points: &[(u8, u8)]) -> u8 {
let xj = Gf256::from_byte(raw_xj);
let delta = xi - xj;
assert_ne!(delta.poly, 0, "Duplicate shares");
prod = prod * xj / delta;
prod *= xj / delta;
}
}
sum = sum + prod * yi;
sum += prod * yi;
}
sum.to_byte()
}
Expand All @@ -37,7 +37,7 @@ pub(crate) fn interpolate(points: &[(Gf256, Gf256)]) -> Poly {
let mut prod = Gf256::one();
for &(x1, _) in points {
if x != x1 {
prod = prod * (x - x1);
prod *= x - x1;

let mut prec = Gf256::zero();
coeffs = coeffs
Expand Down
2 changes: 1 addition & 1 deletion src/poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Poly {
let mut result = Gf256::zero();

for (i, c) in self.coeffs.iter().enumerate() {
result = result + *c * x.pow(i as u8);
result += *c * x.pow(i as u8);
}

result
Expand Down
4 changes: 2 additions & 2 deletions src/sss/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub(crate) fn encode_secret_byte<W: Write>(src: &[u8], n: u8, w: &mut W) -> io::
let mut fac = Gf256::one();
let mut acc = Gf256::zero();
for &coeff in src.iter() {
acc = acc + fac * Gf256::from_byte(coeff);
fac = fac * x;
acc += fac * Gf256::from_byte(coeff);
fac *= x;
}
w.write_all(&[acc.to_byte()])?;
}
Expand Down

0 comments on commit 9c123a9

Please sign in to comment.