commit
be4f8c9f14
9 changed files with 168 additions and 31 deletions
|
|
@ -9,8 +9,8 @@ use tasks01::{
|
||||||
gfmul::gfmul_task,
|
gfmul::gfmul_task,
|
||||||
pad_oracle::padding_oracle,
|
pad_oracle::padding_oracle,
|
||||||
pfmath::{
|
pfmath::{
|
||||||
gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_gcd, gfpoly_make_monic, gfpoly_mul,
|
gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_factor_sff, gfpoly_gcd,
|
||||||
gfpoly_pow, gfpoly_powmod, gfpoly_sort, gfpoly_sqrt,
|
gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod, gfpoly_sort, gfpoly_sqrt,
|
||||||
},
|
},
|
||||||
poly2block::poly2block,
|
poly2block::poly2block,
|
||||||
sea128::sea128,
|
sea128::sea128,
|
||||||
|
|
@ -157,6 +157,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
|
||||||
|
|
||||||
Ok(json)
|
Ok(json)
|
||||||
}
|
}
|
||||||
|
"gfpoly_factor_sff" => {
|
||||||
|
let result = gfpoly_factor_sff(args)?;
|
||||||
|
let json = json!({"factors" : result});
|
||||||
|
|
||||||
|
Ok(json)
|
||||||
|
}
|
||||||
|
|
||||||
_ => Err(anyhow!(
|
_ => Err(anyhow!(
|
||||||
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
|
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use serde_json::Value;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
field::FieldElement,
|
field::FieldElement,
|
||||||
poly::{gcd, Polynomial},
|
poly::{gcd, Polynomial},
|
||||||
|
sff::{sff, Factors},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn gfpoly_add(args: &Value) -> Result<Polynomial> {
|
pub fn gfpoly_add(args: &Value) -> Result<Polynomial> {
|
||||||
|
|
@ -112,7 +113,24 @@ pub fn gfpoly_gcd(args: &Value) -> Result<Polynomial> {
|
||||||
let poly_a = Polynomial::from_c_array(&args["A"].clone());
|
let poly_a = Polynomial::from_c_array(&args["A"].clone());
|
||||||
let poly_b = Polynomial::from_c_array(&args["B"].clone());
|
let poly_b = Polynomial::from_c_array(&args["B"].clone());
|
||||||
|
|
||||||
let result = gcd(poly_a.monic(), poly_b.monic());
|
let result = gcd(&poly_a.monic(), &poly_b.monic());
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gfpoly_factor_sff(arsg: &Value) -> Result<Vec<(Factors)>> {
|
||||||
|
let poly_f = Polynomial::from_c_array(&arsg["F"].clone());
|
||||||
|
|
||||||
|
let mut factors = sff(poly_f);
|
||||||
|
factors.sort();
|
||||||
|
let mut result: Vec<Factors> = vec![];
|
||||||
|
|
||||||
|
for (factor, exponent) in factors {
|
||||||
|
result.push(Factors {
|
||||||
|
factor: factor.to_c_array(),
|
||||||
|
exponent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Ok(result)
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/utils/dff.rs
Normal file
10
src/utils/dff.rs
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
use super::poly::Polynomial;
|
||||||
|
|
||||||
|
pub fn dff(f: Polynomial) {
|
||||||
|
let q = 2u128.pow(128);
|
||||||
|
let z: Vec<(Polynomial, u32)> = vec![];
|
||||||
|
let d = 1;
|
||||||
|
let f_start = f.clone();
|
||||||
|
|
||||||
|
while f_start.degree() >= 2 * d {}
|
||||||
|
}
|
||||||
0
src/utils/edf.rs
Normal file
0
src/utils/edf.rs
Normal file
|
|
@ -14,7 +14,7 @@ use super::{
|
||||||
poly::gfmul,
|
poly::gfmul,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct FieldElement {
|
pub struct FieldElement {
|
||||||
field_element: Vec<u8>,
|
field_element: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
pub mod ciphers;
|
pub mod ciphers;
|
||||||
|
pub mod dff;
|
||||||
|
pub mod edf;
|
||||||
pub mod field;
|
pub mod field;
|
||||||
pub mod math;
|
pub mod math;
|
||||||
pub mod net;
|
pub mod net;
|
||||||
pub mod parse;
|
pub mod parse;
|
||||||
pub mod poly;
|
pub mod poly;
|
||||||
|
pub mod sff;
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use serde_json::Value;
|
||||||
|
|
||||||
use super::field::FieldElement;
|
use super::field::FieldElement;
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
pub struct Polynomial {
|
pub struct Polynomial {
|
||||||
polynomial: Vec<FieldElement>,
|
polynomial: Vec<FieldElement>,
|
||||||
}
|
}
|
||||||
|
|
@ -22,6 +22,10 @@ impl Polynomial {
|
||||||
Self { polynomial }
|
Self { polynomial }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn degree(&self) -> usize {
|
||||||
|
self.polynomial.len()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_c_array(array: &Value) -> Self {
|
pub fn from_c_array(array: &Value) -> Self {
|
||||||
let mut polynomial: Vec<FieldElement> = vec![];
|
let mut polynomial: Vec<FieldElement> = vec![];
|
||||||
let c_array: Vec<String> = array
|
let c_array: Vec<String> = array
|
||||||
|
|
@ -178,13 +182,16 @@ impl Polynomial {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns (quotient, remainder)
|
// Returns (quotient, remainder)
|
||||||
pub fn div(self, rhs: &Self) -> (Self, Self) {
|
pub fn div(&self, rhs: &Self) -> (Self, Self) {
|
||||||
// Div by zero check ommitted since data is guaranteed to be non 0
|
// Div by zero check ommitted since data is guaranteed to be non 0
|
||||||
|
|
||||||
eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len());
|
eprintln!("{:?}, {:?}", self.polynomial.len(), rhs.polynomial.len());
|
||||||
|
|
||||||
if self.polynomial.len() < rhs.polynomial.len() {
|
if self.polynomial.len() < rhs.polynomial.len() {
|
||||||
return (Polynomial::new(vec![FieldElement::new(vec![0; 16])]), self);
|
return (
|
||||||
|
Polynomial::new(vec![FieldElement::new(vec![0; 16])]),
|
||||||
|
self.clone(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut remainder = self.clone();
|
let mut remainder = self.clone();
|
||||||
|
|
@ -483,12 +490,13 @@ impl Ord for Polynomial {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gcd(a: Polynomial, b: Polynomial) -> Polynomial {
|
pub fn gcd(a: &Polynomial, b: &Polynomial) -> Polynomial {
|
||||||
if a.is_zero() {
|
if a.is_zero() {
|
||||||
return b;
|
return b.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
return gcd(b.div(&a).1.monic(), a);
|
let monic_b = b.div(&a).1.monic();
|
||||||
|
return gcd(&monic_b, a);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sort_polynomial_array(mut polys: Vec<Polynomial>) -> Result<Vec<Polynomial>> {
|
pub fn sort_polynomial_array(mut polys: Vec<Polynomial>) -> Result<Vec<Polynomial>> {
|
||||||
|
|
@ -1300,7 +1308,7 @@ mod tests {
|
||||||
let a: Polynomial = Polynomial::from_c_array(&a);
|
let a: Polynomial = Polynomial::from_c_array(&a);
|
||||||
let b: Polynomial = Polynomial::from_c_array(&b);
|
let b: Polynomial = Polynomial::from_c_array(&b);
|
||||||
|
|
||||||
let result = gcd(a.monic(), b.monic());
|
let result = gcd(&a.monic(), &b.monic());
|
||||||
|
|
||||||
assert_eq!(json!(result.to_c_array()), expected);
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
}
|
}
|
||||||
|
|
@ -1314,7 +1322,7 @@ mod tests {
|
||||||
let a: Polynomial = Polynomial::from_c_array(&a);
|
let a: Polynomial = Polynomial::from_c_array(&a);
|
||||||
let b: Polynomial = Polynomial::from_c_array(&b);
|
let b: Polynomial = Polynomial::from_c_array(&b);
|
||||||
|
|
||||||
let result = gcd(a.monic(), b.monic());
|
let result = gcd(&a.monic(), &b.monic());
|
||||||
|
|
||||||
assert_eq!(json!(result.to_c_array()), expected);
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
92
src/utils/sff.rs
Normal file
92
src/utils/sff.rs
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::utils::{
|
||||||
|
field::FieldElement,
|
||||||
|
poly::{gcd, polynomial_2_block},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::poly::Polynomial;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
pub struct Factors {
|
||||||
|
pub factor: Vec<String>,
|
||||||
|
pub exponent: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sff(mut f: Polynomial) -> Vec<(Polynomial, u32)> {
|
||||||
|
let mut c = gcd(&f, &f.clone().diff());
|
||||||
|
f = f.div(&c).0;
|
||||||
|
let mut z: Vec<(Polynomial, u32)> = vec![];
|
||||||
|
let mut e: u32 = 1;
|
||||||
|
|
||||||
|
let one_element = Polynomial::new(vec![FieldElement::new(
|
||||||
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
|
)]);
|
||||||
|
|
||||||
|
while f != one_element {
|
||||||
|
let y = gcd(&f, &c);
|
||||||
|
if f != y {
|
||||||
|
z.push(((f.div(&y).0), e));
|
||||||
|
}
|
||||||
|
|
||||||
|
f = y.clone();
|
||||||
|
c = c.div(&y).0;
|
||||||
|
e += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if c != one_element {
|
||||||
|
let r = sff(c.sqrt());
|
||||||
|
for (f_star, e_star) in r {
|
||||||
|
z.push((f_star, 2 * e_star));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
z
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
|
||||||
|
use serde_json::json;
|
||||||
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn byte_indices_0x01() {
|
||||||
|
let json_f = json!([
|
||||||
|
"vL77UwAAAAAAAAAAAAAAAA==",
|
||||||
|
"mEHchYAAAAAAAAAAAAAAAA==",
|
||||||
|
"9WJa0MAAAAAAAAAAAAAAAA==",
|
||||||
|
"akHfwWAAAAAAAAAAAAAAAA==",
|
||||||
|
"E12o/QAAAAAAAAAAAAAAAA==",
|
||||||
|
"vKJ/FgAAAAAAAAAAAAAAAA==",
|
||||||
|
"yctWwAAAAAAAAAAAAAAAAA==",
|
||||||
|
"c1BXYAAAAAAAAAAAAAAAAA==",
|
||||||
|
"o0AtAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"AbP2AAAAAAAAAAAAAAAAAA==",
|
||||||
|
"k2YAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"vBYAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"dSAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"69gAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"VkAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"a4AAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"gAAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let poly_f = Polynomial::from_c_array(&json_f);
|
||||||
|
|
||||||
|
let mut factors = sff(poly_f);
|
||||||
|
factors.sort();
|
||||||
|
let mut result: Vec<Factors> = vec![];
|
||||||
|
|
||||||
|
for (factor, exponent) in factors {
|
||||||
|
result.push(Factors {
|
||||||
|
factor: factor.to_c_array(),
|
||||||
|
exponent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{:?}", result);
|
||||||
|
let bit_indices: Vec<u8> = vec![0];
|
||||||
|
assert!(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,26 +1,26 @@
|
||||||
{
|
{
|
||||||
"testcases": {
|
"testcases": {
|
||||||
"sandbox": {
|
"sandbox": {
|
||||||
"action": "gfpoly_gcd",
|
"action": "gfpoly_factor_sff",
|
||||||
"arguments": {
|
"arguments": {
|
||||||
"A": [
|
"F": [
|
||||||
"DNWpXnnY24XecPa7a8vrEA==",
|
"vL77UwAAAAAAAAAAAAAAAA==",
|
||||||
"I8uYpCbsiPaVvUznuv1IcA==",
|
"mEHchYAAAAAAAAAAAAAAAA==",
|
||||||
"wsbiU432ARWuO93He3vbvA==",
|
"9WJa0MAAAAAAAAAAAAAAAA==",
|
||||||
"zp0g3o8iNz7Y+8oUxw1vJw==",
|
"akHfwWAAAAAAAAAAAAAAAA==",
|
||||||
"J0GekE3uendpN6WUAuJ4AA==",
|
"E12o/QAAAAAAAAAAAAAAAA==",
|
||||||
"wACd0e6u1ii4AAAAAAAAAA==",
|
"vKJ/FgAAAAAAAAAAAAAAAA==",
|
||||||
"ACAAAAAAAAAAAAAAAAAAAA=="
|
"yctWwAAAAAAAAAAAAAAAAA==",
|
||||||
],
|
"c1BXYAAAAAAAAAAAAAAAAA==",
|
||||||
"B": [
|
"o0AtAAAAAAAAAAAAAAAAAA==",
|
||||||
"I20VjJmlSnRSe88gaDiLRQ==",
|
"AbP2AAAAAAAAAAAAAAAAAA==",
|
||||||
"0Cw5HxJm/pfybJoQDf7/4w==",
|
"k2YAAAAAAAAAAAAAAAAAAA==",
|
||||||
"8ByrMMf+vVj5r3YXUNCJ1g==",
|
"vBYAAAAAAAAAAAAAAAAAAA==",
|
||||||
"rEU/f2UZRXqmZ6V7EPKfBA==",
|
"dSAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"LfdALhvCrdhhGZWl9l9DSg==",
|
"69gAAAAAAAAAAAAAAAAAAA==",
|
||||||
"KSUKhN0n6/DZmHPozd1prw==",
|
"VkAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"DQrRkuA9Zx279wAAAAAAAA==",
|
"a4AAAAAAAAAAAAAAAAAAAA==",
|
||||||
"AhCEAAAAAAAAAAAAAAAAAA=="
|
"gAAAAAAAAAAAAAAAAAAAAA=="
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue