feat: ready test runner for monic and sqrt tasks

This commit is contained in:
Alivecow 2024-11-22 21:16:53 +01:00
parent f75e7de733
commit 5bb9bcebff
4 changed files with 53 additions and 30 deletions

View file

@ -9,7 +9,8 @@ use tasks01::{
gfmul::gfmul_task, gfmul::gfmul_task,
pad_oracle::padding_oracle, pad_oracle::padding_oracle,
pfmath::{ pfmath::{
gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_mul, gfpoly_pow, gfpoly_powmod, gfpoly_sort, gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod,
gfpoly_sort, gfpoly_sqrt,
}, },
poly2block::poly2block, poly2block::poly2block,
sea128::sea128, sea128::sea128,
@ -132,6 +133,18 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
Ok(json) Ok(json)
} }
"gfpoly_make_monic" => {
let result = gfpoly_make_monic(args)?;
let json = json!({"S" : result.to_c_array()});
Ok(json)
}
"gfpoly_sqrt" => {
let result = gfpoly_sqrt(args)?;
let json = json!({"S" : result.to_c_array()});
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; {:?}",

View file

@ -81,6 +81,22 @@ pub fn gfpoly_sort(args: &Value) -> Result<Vec<Polynomial>> {
Ok(polys) Ok(polys)
} }
pub fn gfpoly_make_monic(args: &Value) -> Result<Polynomial> {
let mut poly_a = Polynomial::from_c_array(&args["A"].clone());
poly_a.monic();
Ok(poly_a)
}
pub fn gfpoly_sqrt(args: &Value) -> Result<Polynomial> {
let poly_a = Polynomial::from_c_array(&args["Q"].clone());
let result = poly_a.sqrt();
Ok(result)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -58,23 +58,23 @@ impl FieldElement {
//eprintln!("Current exponent: {:02X}", exponent); //eprintln!("Current exponent: {:02X}", exponent);
if exponent & 1 == 1 { if exponent & 1 == 1 {
let temp = &self * &result; let temp = &self * &result;
eprintln!("Mult"); //eprintln!("Mult");
eprintln!("After mod: {:?}", temp); //eprintln!("After mod: {:?}", temp);
result = temp result = temp
} }
let temp_square = &self * &self; let temp_square = &self * &self;
eprintln!("Square"); // eprintln!("Square");
eprintln!("After squaring: {:?}", temp_square); // eprintln!("After squaring: {:?}", temp_square);
self = temp_square; self = temp_square;
//eprintln!("After mod: {:?}", self); //eprintln!("After mod: {:?}", self);
exponent >>= 1; exponent >>= 1;
} }
eprintln!("result in powmod before reduction: {:02X?}", result); // eprintln!("result in powmod before reduction: {:02X?}", result);
eprintln!("result in powmod after reduction: {:02X?}", result); // eprintln!("result in powmod after reduction: {:02X?}", result);
result result
} }

View file

@ -81,21 +81,21 @@ impl Polynomial {
//eprintln!("Current exponent: {:02X}", exponent); //eprintln!("Current exponent: {:02X}", exponent);
if exponent & 1 == 1 { if exponent & 1 == 1 {
let temp = &self * &result; let temp = &self * &result;
eprintln!("Mult"); //eprintln!("Mult");
eprintln!("After mod: {:?}", temp); //eprintln!("After mod: {:?}", temp);
result = temp result = temp
} }
let temp_square = &self * &self; let temp_square = &self * &self;
eprintln!("Square"); //eprintln!("Square");
eprintln!("After squaring: {:?}", temp_square); //eprintln!("After squaring: {:?}", temp_square);
self = temp_square; self = temp_square;
//eprintln!("After mod: {:?}", self); //eprintln!("After mod: {:?}", self);
exponent >>= 1; exponent >>= 1;
} }
eprintln!("result in powmod before reduction: {:02X?}", result); //eprintln!("result in powmod before reduction: {:02X?}", result);
while !result.polynomial.is_empty() while !result.polynomial.is_empty()
&& result && result
@ -109,7 +109,7 @@ impl Polynomial {
result.polynomial.pop(); result.polynomial.pop();
} }
eprintln!("result in powmod after reduction: {:02X?}", result); //eprintln!("result in powmod after reduction: {:02X?}", result);
if result.is_empty() { if result.is_empty() {
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]); result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
@ -256,7 +256,7 @@ impl Polynomial {
true true
} }
fn monic(mut self) -> Self { pub fn monic(&mut self) {
let divident = self.polynomial.last().unwrap().clone(); let divident = self.polynomial.last().unwrap().clone();
for fieldelement in &mut self.polynomial.iter_mut() { for fieldelement in &mut self.polynomial.iter_mut() {
@ -274,15 +274,9 @@ impl Polynomial {
{ {
self.polynomial.pop(); self.polynomial.pop();
} }
if self.is_empty() {
self = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
}
self
} }
fn sqrt(self) -> Self { pub fn sqrt(self) -> Self {
let mut result = vec![]; let mut result = vec![];
for (position, element) in self.polynomial.iter().enumerate() { for (position, element) in self.polynomial.iter().enumerate() {
@ -1135,22 +1129,22 @@ mod tests {
"1Ial5rAJGOucIdUe3zh5bw==", "1Ial5rAJGOucIdUe3zh5bw==",
"gAAAAAAAAAAAAAAAAAAAAA==" "gAAAAAAAAAAAAAAAAAAAAA=="
]); ]);
let element1: Polynomial = Polynomial::from_c_array(&json1); let mut element1: Polynomial = Polynomial::from_c_array(&json1);
let result = element1.monic(); element1.monic();
assert_eq!(json!(result.to_c_array()), expected); assert_eq!(json!(element1.to_c_array()), expected);
} }
#[test] #[test]
fn test_poly_monic_poly_zero() { fn test_poly_monic_poly_zero() {
let json1 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]); let json1 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]); let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
let element1: Polynomial = Polynomial::from_c_array(&json1); let mut element1: Polynomial = Polynomial::from_c_array(&json1);
let result = element1.monic(); element1.monic();
assert_eq!(json!(result.to_c_array()), expected); assert_eq!(json!(element1.to_c_array()), expected);
} }
#[test] #[test]
@ -1162,11 +1156,11 @@ mod tests {
"AAAAAAAAAAAAAAAAAAAAAA==" "AAAAAAAAAAAAAAAAAAAAAA=="
]); ]);
let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]); let expected = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
let element1: Polynomial = Polynomial::from_c_array(&json1); let mut element1: Polynomial = Polynomial::from_c_array(&json1);
let result = element1.monic(); element1.monic();
assert_eq!(json!(result.to_c_array()), expected); assert_eq!(json!(element1.to_c_array()), expected);
} }
#[test] #[test]