feat/refactor: Change unneded vec initialisations and start on possible new gfmul
This commit is contained in:
parent
270abdb7b7
commit
5b27a4ad9c
2 changed files with 81 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use crate::utils::field::ByteArray;
|
||||
use base64::alphabet::BIN_HEX;
|
||||
use base64::prelude::*;
|
||||
use num::traits::FromBytes;
|
||||
use num::{BigInt, BigUint, One, Zero};
|
||||
use std::{str::FromStr, u128, u8, usize};
|
||||
|
||||
|
|
@ -13,6 +14,7 @@ use anyhow::{anyhow, Ok, Result};
|
|||
use serde_json::Value;
|
||||
|
||||
use super::field::FieldElement;
|
||||
use super::math::reverse_bits_in_bytevec;
|
||||
|
||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Polynomial {
|
||||
|
|
@ -291,10 +293,7 @@ impl Polynomial {
|
|||
}
|
||||
|
||||
let mut quotient_coeffs =
|
||||
vec![
|
||||
FieldElement::new(polynomial_2_block(vec![0; 16], "gcm").unwrap());
|
||||
dividend_deg - divisor_deg + 1
|
||||
];
|
||||
vec![FieldElement::new(vec![0; 16]); dividend_deg - divisor_deg + 1];
|
||||
|
||||
while remainder.polynomial.len() >= divisor.polynomial.len() {
|
||||
let deg_diff = remainder.polynomial.len() - divisor.polynomial.len();
|
||||
|
|
@ -305,8 +304,7 @@ impl Polynomial {
|
|||
|
||||
quotient_coeffs[deg_diff] = quot_coeff.clone();
|
||||
|
||||
let mut subtrahend =
|
||||
vec![FieldElement::new(polynomial_2_block(vec![0; 16], "gcm").unwrap()); deg_diff];
|
||||
let mut subtrahend = vec![FieldElement::new(vec![0; 16]); deg_diff];
|
||||
subtrahend.extend(
|
||||
divisor
|
||||
.polynomial
|
||||
|
|
@ -658,6 +656,53 @@ pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u
|
|||
Ok(result.0)
|
||||
}
|
||||
|
||||
pub fn bgfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
|
||||
//TODO: Implement gfmul with bigint
|
||||
let red_poly_bytes: BigUint = BigUint::from_slice(&[
|
||||
0x87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0x01,
|
||||
]);
|
||||
|
||||
let mut poly1: BigUint = BigUint::from_le_bytes(&reverse_bits_in_bytevec(poly_a.to_owned()));
|
||||
|
||||
let mut poly2: BigUint = BigUint::from_le_bytes(&reverse_bits_in_bytevec(poly_b.to_owned()));
|
||||
|
||||
/*
|
||||
if semantic == "gcm" {
|
||||
poly1.re;
|
||||
poly2.reverse_bits_in_bytevec();
|
||||
}
|
||||
*/
|
||||
|
||||
let mut result: BigUint = BigUint::zero();
|
||||
|
||||
if (&poly2 & (BigUint::one() << 127)) == BigUint::one() {
|
||||
result = &result ^ &poly1;
|
||||
}
|
||||
poly2 = &poly2 >> 1;
|
||||
|
||||
while &poly2 != &BigUint::zero() {
|
||||
poly1 = &poly1 << 1;
|
||||
|
||||
if (&poly1 & (BigUint::one() << 127)) == BigUint::one() {
|
||||
poly1 = &poly1 ^ &red_poly_bytes;
|
||||
}
|
||||
|
||||
if &poly2 & BigUint::one() == BigUint::one() {
|
||||
result = &result ^ &poly1;
|
||||
}
|
||||
|
||||
poly2 = &poly2 >> 1;
|
||||
}
|
||||
|
||||
/*
|
||||
if semantic == "gcm" {
|
||||
result.reverse_bits_in_bytevec();
|
||||
}
|
||||
*/
|
||||
|
||||
Ok(reverse_bits_in_bytevec(result.to_bytes_le()))
|
||||
}
|
||||
|
||||
pub fn convert_gcm_to_xex(gcm_poly: Vec<u8>) -> Result<Vec<u8>> {
|
||||
let xex_poly = gcm_poly
|
||||
.into_iter()
|
||||
|
|
@ -795,7 +840,7 @@ pub fn coefficient_to_binary(coefficients: Vec<u8>) -> u128 {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::utils::poly::b64_2_num;
|
||||
use crate::utils::poly::{b64_2_num, gcd};
|
||||
use anyhow::Result;
|
||||
use serde_json::json;
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
|
|
@ -1429,4 +1474,17 @@ mod tests {
|
|||
|
||||
assert_eq!(json!(result.to_c_array()), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_poly_gcd_stress() {
|
||||
eprintln!("{:?}", Polynomial::one());
|
||||
|
||||
let poly1 = Polynomial::rand(&(500 as usize));
|
||||
let poly2 = Polynomial::rand(&(500 as usize));
|
||||
|
||||
let result = gcd(&poly1.monic(), &poly2.monic());
|
||||
|
||||
eprintln!("{:02X?}", result.to_c_array());
|
||||
assert!(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue