Added table-driven inverse for GF(2^8)
This commit is contained in:
@@ -18,4 +18,8 @@ serde_cbor = "0.9.0"
|
||||
serde_json = "1.0.111"
|
||||
bendy = "0.3.3"
|
||||
generic-array = "1.0.0"
|
||||
anyhow = "1.0.79"
|
||||
anyhow = "1.0.79"
|
||||
|
||||
[build-dependencies]
|
||||
rand = "0.6.5"
|
||||
generic-array = "1.0.0"
|
||||
|
||||
29
build.rs
Normal file
29
build.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::path::Path;
|
||||
use crate::gf256::GF256;
|
||||
|
||||
#[path="src/gf.rs"]
|
||||
mod gf;
|
||||
#[path="src/gf256.rs"]
|
||||
mod gf256;
|
||||
|
||||
fn write_tbl(path: impl AsRef<Path>, name: &str, tbl: &[u8; 256]) {
|
||||
let mut res = format!("const INV_TBL: [{name}; {tbl_len}] = [\n", tbl_len=tbl.len());
|
||||
for v in tbl {
|
||||
res += &format!("\t{name}({v}),\n");
|
||||
}
|
||||
res += "];\n";
|
||||
|
||||
std::fs::write(path, res).unwrap();
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
let mut gf256_inv_tbl = [0;256];
|
||||
for i in 0..256 {
|
||||
gf256_inv_tbl[i] = u8::from(GF256::from(i as u8).minv_slow());
|
||||
}
|
||||
let mut out_file = std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
|
||||
out_file.push("gf256_inv.rs");
|
||||
write_tbl(&out_file, "GF256", &gf256_inv_tbl);
|
||||
|
||||
println!("cargo::rustc-cfg=rssss_have_codegen")
|
||||
}
|
||||
42
src/gf256.rs
42
src/gf256.rs
@@ -3,9 +3,11 @@ use std::fmt::{Debug, Display, Formatter};
|
||||
use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign, Div, DivAssign};
|
||||
use generic_array::GenericArray;
|
||||
use generic_array::typenum::U1;
|
||||
use rand::distributions::{Standard};
|
||||
use rand::distributions::Standard;
|
||||
use rand::prelude::Distribution;
|
||||
use rand::Rng;
|
||||
|
||||
#[cfg(rssss_have_codegen)]
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use crate::gf::GF;
|
||||
|
||||
@@ -15,9 +17,12 @@ use crate::gf::GF;
|
||||
///
|
||||
/// Multiplication is performed modulo $x^8 + x^4 + x^3 + x + 1$, or `{11B}`
|
||||
///
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(rssss_have_codegen, derive(Serialize, Deserialize))]
|
||||
pub struct GF256(u8);
|
||||
|
||||
#[cfg(rssss_have_codegen)]
|
||||
include!(concat!(env!("OUT_DIR"), "/gf256_inv.rs"));
|
||||
|
||||
impl GF256 {
|
||||
|
||||
/// Multiply by x, reducing modulo 14E
|
||||
@@ -27,6 +32,19 @@ impl GF256 {
|
||||
GF256(poly << 1 ^ (0x1B & mask))
|
||||
}
|
||||
|
||||
pub(crate) fn minv_slow(self) -> Self {
|
||||
let mut res = self;
|
||||
let mut factor = 1;
|
||||
for _ in 0..6 {
|
||||
res = res * res * self;
|
||||
factor = factor * 2 + 1;
|
||||
}
|
||||
res = res * res;
|
||||
factor = factor*2;
|
||||
//assert_eq!(factor, 254);
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl GF for GF256 {
|
||||
@@ -38,19 +56,13 @@ impl GF for GF256 {
|
||||
|
||||
/// Multiplicative inverse
|
||||
/// Note that this is an expensive operation
|
||||
#[cfg(rssss_have_codegen)]
|
||||
fn minv(self) -> Self {
|
||||
// For constant time evaluation, we compute self**254
|
||||
let mut res = self;
|
||||
let mut factor = 1;
|
||||
for _ in 0..6 {
|
||||
res = res * res * self;
|
||||
factor = factor * 2 + 1;
|
||||
}
|
||||
res = res * res;
|
||||
factor = factor*2;
|
||||
assert_eq!(factor, 254);
|
||||
|
||||
res
|
||||
INV_TBL[self.0 as usize]
|
||||
}
|
||||
#[cfg(not(rssss_have_codegen))]
|
||||
fn minv(self) -> Self {
|
||||
self.minv_slow()
|
||||
}
|
||||
|
||||
fn decode(chunk: GenericArray<u8, Self::ChunkSize>) -> Self {
|
||||
@@ -154,7 +166,7 @@ impl TryFrom<usize> for GF256 {
|
||||
|
||||
// Utility functions
|
||||
/// Unsigned negate
|
||||
fn uneg(v: u8) -> u8 {
|
||||
const fn uneg(v: u8) -> u8 {
|
||||
(-(v as i8)) as u8
|
||||
}
|
||||
|
||||
|
||||
@@ -165,7 +165,7 @@ impl Command for GenShare {
|
||||
let mut rng = rand::rngs::OsRng::new()?;
|
||||
|
||||
let share = rssss::s4::Share::new((self.share_no as u8).into(), poly.as_slice());
|
||||
eprintln!("{share:?}");
|
||||
// eprintln!("{share:?}");
|
||||
share.write_to(io::stdout())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user