mirror of
https://github.com/mii443/RustySecrets.git
synced 2025-08-22 16:25:32 +00:00
Implement {Add, Div, Mul, Sub}Assign for Gf256
This commit is contained in:
committed by
Romain Ruetschi
parent
3de16890a4
commit
9c123a900d
@ -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
src/gf256.rs
28
src/gf256.rs
@ -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"));
|
||||
|
||||
@ -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]
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
@ -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 {
|
||||
|
@ -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()
|
||||
}
|
||||
@ -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
|
||||
|
@ -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
|
||||
|
@ -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()])?;
|
||||
}
|
||||
|
Reference in New Issue
Block a user