Merging test runner implementation for monic and sqrt #20
4 changed files with 161 additions and 32 deletions
|
|
@ -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; {:?}",
|
||||||
|
|
|
||||||
|
|
@ -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::*;
|
||||||
|
|
|
||||||
|
|
@ -36,21 +36,46 @@ impl FieldElement {
|
||||||
BASE64_STANDARD.encode(&self.field_element)
|
BASE64_STANDARD.encode(&self.field_element)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pow(&self, mut exponent: u128) -> FieldElement {
|
pub fn pow(mut self, mut exponent: u128) -> FieldElement {
|
||||||
|
let mut result: FieldElement =
|
||||||
|
FieldElement::new(polynomial_2_block(vec![0], "gcm").unwrap());
|
||||||
|
|
||||||
|
if exponent == 1 {
|
||||||
|
eprintln!("special case 1: {:02X?}", self.clone());
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
if exponent == 0 {
|
if exponent == 0 {
|
||||||
// Return polynomial with coefficient 1
|
let result = FieldElement::new(polynomial_2_block(vec![0], "gcm").unwrap());
|
||||||
return FieldElement::new(vec![1]);
|
|
||||||
|
eprintln!("Returned value is: {:02X?}", result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
let base = self.clone();
|
//eprintln!("Initial result: {:?}", result);
|
||||||
let mut result = base.clone();
|
|
||||||
exponent -= 1; // Subtract 1 because we already set result to base
|
|
||||||
|
|
||||||
while exponent > 0 {
|
while exponent > 0 {
|
||||||
result = result * base.clone();
|
//eprintln!("Current exponent: {:02X}", exponent);
|
||||||
exponent -= 1;
|
if exponent & 1 == 1 {
|
||||||
|
let temp = &self * &result;
|
||||||
|
//eprintln!("Mult");
|
||||||
|
//eprintln!("After mod: {:?}", temp);
|
||||||
|
|
||||||
|
result = temp
|
||||||
|
}
|
||||||
|
let temp_square = &self * &self;
|
||||||
|
// eprintln!("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 after reduction: {:02X?}", result);
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -56,19 +56,63 @@ impl Polynomial {
|
||||||
output
|
output
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pow(&self, mut exponent: u128) -> Polynomial {
|
pub fn pow(mut self, mut exponent: u128) -> Polynomial {
|
||||||
if exponent == 0 {
|
let mut result: Polynomial = Polynomial::new(vec![FieldElement::new(
|
||||||
return Polynomial::new(vec![FieldElement::new(
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
polynomial_2_block(vec![0], "gcm").unwrap(),
|
)]);
|
||||||
)]);
|
|
||||||
|
if exponent == 1 {
|
||||||
|
eprintln!("special case 1: {:02X?}", self.clone());
|
||||||
|
|
||||||
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
let base = self.clone();
|
if exponent == 0 {
|
||||||
let mut result = base.clone();
|
let result = Polynomial::new(vec![FieldElement::new(
|
||||||
exponent -= 1;
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
|
)]);
|
||||||
|
|
||||||
|
eprintln!("Returned value is: {:02X?}", result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//eprintln!("Initial result: {:?}", result);
|
||||||
while exponent > 0 {
|
while exponent > 0 {
|
||||||
result = result * base.clone();
|
//eprintln!("Current exponent: {:02X}", exponent);
|
||||||
exponent -= 1;
|
if exponent & 1 == 1 {
|
||||||
|
let temp = &self * &result;
|
||||||
|
//eprintln!("Mult");
|
||||||
|
//eprintln!("After mod: {:?}", temp);
|
||||||
|
|
||||||
|
result = temp
|
||||||
|
}
|
||||||
|
let temp_square = &self * &self;
|
||||||
|
//eprintln!("Square");
|
||||||
|
|
||||||
|
//eprintln!("After squaring: {:?}", temp_square);
|
||||||
|
self = temp_square;
|
||||||
|
//eprintln!("After mod: {:?}", self);
|
||||||
|
exponent >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||||
|
|
||||||
|
while !result.polynomial.is_empty()
|
||||||
|
&& result
|
||||||
|
.polynomial
|
||||||
|
.last()
|
||||||
|
.unwrap()
|
||||||
|
.as_ref()
|
||||||
|
.iter()
|
||||||
|
.all(|&x| x == 0)
|
||||||
|
{
|
||||||
|
result.polynomial.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
//eprintln!("result in powmod after reduction: {:02X?}", result);
|
||||||
|
|
||||||
|
if result.is_empty() {
|
||||||
|
result = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
@ -212,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() {
|
||||||
|
|
@ -230,12 +274,18 @@ impl Polynomial {
|
||||||
{
|
{
|
||||||
self.polynomial.pop();
|
self.polynomial.pop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if self.is_empty() {
|
pub fn sqrt(self) -> Self {
|
||||||
self = Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
let mut result = vec![];
|
||||||
|
|
||||||
|
for (position, element) in self.polynomial.iter().enumerate() {
|
||||||
|
if position % 2 == 0 {
|
||||||
|
result.push(element.clone().pow(2u128.pow(127)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self
|
Polynomial::new(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1079,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]
|
||||||
|
|
@ -1106,9 +1156,34 @@ 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!(element1.to_c_array()), expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_poly_sqrt() {
|
||||||
|
let json1 = json!([
|
||||||
|
"5TxUxLHO1lHE/rSFquKIAg==",
|
||||||
|
"AAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"0DEUJYdHlmd4X7nzzIdcCA==",
|
||||||
|
"AAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"PKUa1+JHTxHE8y3LbuKIIA==",
|
||||||
|
"AAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"Ds96KiAKKoigKoiKiiKAiA=="
|
||||||
|
]);
|
||||||
|
let expected = json!([
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
eprintln!("Starting poly sqrt");
|
||||||
|
|
||||||
|
let result = element1.sqrt();
|
||||||
|
|
||||||
assert_eq!(json!(result.to_c_array()), expected);
|
assert_eq!(json!(result.to_c_array()), expected);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue