Merging test runner implementation for monic and sqrt #20
4 changed files with 53 additions and 30 deletions
|
|
@ -9,7 +9,8 @@ use tasks01::{
|
|||
gfmul::gfmul_task,
|
||||
pad_oracle::padding_oracle,
|
||||
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,
|
||||
sea128::sea128,
|
||||
|
|
@ -132,6 +133,18 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
|
|||
|
||||
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!(
|
||||
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",
|
||||
|
|
|
|||
|
|
@ -81,6 +81,22 @@ pub fn gfpoly_sort(args: &Value) -> Result<Vec<Polynomial>> {
|
|||
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)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -58,23 +58,23 @@ impl FieldElement {
|
|||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if exponent & 1 == 1 {
|
||||
let temp = &self * &result;
|
||||
eprintln!("Mult");
|
||||
eprintln!("After mod: {:?}", temp);
|
||||
//eprintln!("Mult");
|
||||
//eprintln!("After mod: {:?}", temp);
|
||||
|
||||
result = temp
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
eprintln!("Square");
|
||||
// eprintln!("Square");
|
||||
|
||||
eprintln!("After squaring: {:?}", temp_square);
|
||||
// eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,21 +81,21 @@ impl Polynomial {
|
|||
//eprintln!("Current exponent: {:02X}", exponent);
|
||||
if exponent & 1 == 1 {
|
||||
let temp = &self * &result;
|
||||
eprintln!("Mult");
|
||||
eprintln!("After mod: {:?}", temp);
|
||||
//eprintln!("Mult");
|
||||
//eprintln!("After mod: {:?}", temp);
|
||||
|
||||
result = temp
|
||||
}
|
||||
let temp_square = &self * &self;
|
||||
eprintln!("Square");
|
||||
//eprintln!("Square");
|
||||
|
||||
eprintln!("After squaring: {:?}", temp_square);
|
||||
//eprintln!("After squaring: {:?}", temp_square);
|
||||
self = temp_square;
|
||||
//eprintln!("After mod: {:?}", self);
|
||||
exponent >>= 1;
|
||||
}
|
||||
|
||||
eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||
//eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||
|
||||
while !result.polynomial.is_empty()
|
||||
&& result
|
||||
|
|
@ -109,7 +109,7 @@ impl Polynomial {
|
|||
result.polynomial.pop();
|
||||
}
|
||||
|
||||
eprintln!("result in powmod after reduction: {:02X?}", result);
|
||||
//eprintln!("result in powmod after reduction: {:02X?}", result);
|
||||
|
||||
if result.is_empty() {
|
||||
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
||||
|
|
@ -256,7 +256,7 @@ impl Polynomial {
|
|||
true
|
||||
}
|
||||
|
||||
fn monic(mut self) -> Self {
|
||||
pub fn monic(&mut self) {
|
||||
let divident = self.polynomial.last().unwrap().clone();
|
||||
|
||||
for fieldelement in &mut self.polynomial.iter_mut() {
|
||||
|
|
@ -274,15 +274,9 @@ impl Polynomial {
|
|||
{
|
||||
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![];
|
||||
|
||||
for (position, element) in self.polynomial.iter().enumerate() {
|
||||
|
|
@ -1135,22 +1129,22 @@ mod tests {
|
|||
"1Ial5rAJGOucIdUe3zh5bw==",
|
||||
"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]
|
||||
fn test_poly_monic_poly_zero() {
|
||||
let json1 = 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]
|
||||
|
|
@ -1162,11 +1156,11 @@ mod tests {
|
|||
"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]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue