Merge poly diff functionality #21

Merged
0xalivecow merged 2 commits from dev into main 2024-11-23 09:29:04 +00:00
5 changed files with 69 additions and 22 deletions
Showing only changes of commit 4b1bca8ee0 - Show all commits

View file

@ -9,8 +9,8 @@ use tasks01::{
gfmul::gfmul_task,
pad_oracle::padding_oracle,
pfmath::{
gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow, gfpoly_powmod,
gfpoly_sort, gfpoly_sqrt,
gfdiv, gfpoly_add, gfpoly_diff, gfpoly_divmod, gfpoly_make_monic, gfpoly_mul, gfpoly_pow,
gfpoly_powmod, gfpoly_sort, gfpoly_sqrt,
},
poly2block::poly2block,
sea128::sea128,
@ -145,6 +145,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result<Value> {
Ok(json)
}
"gfpoly_diff" => {
let result = gfpoly_diff(args)?;
let json = json!({"F'" : result.to_c_array()});
Ok(json)
}
_ => Err(anyhow!(
"Fatal. No compatible action found. Json data was {:?}. Arguments were; {:?}",

View file

@ -97,6 +97,14 @@ pub fn gfpoly_sqrt(args: &Value) -> Result<Polynomial> {
Ok(result)
}
pub fn gfpoly_diff(args: &Value) -> Result<Polynomial> {
let poly_f = Polynomial::from_c_array(&args["F"].clone());
let result = poly_f.diff();
Ok(result)
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -24,6 +24,10 @@ impl FieldElement {
87, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01,
];
pub fn zero(self) -> Self {
FieldElement::new(vec![0])
}
pub const fn new(field_element: Vec<u8>) -> Self {
Self { field_element }
}

View file

@ -292,6 +292,19 @@ impl Polynomial {
Polynomial::new(result)
}
pub fn diff(mut self) -> Self {
// Pop first element
self.polynomial.remove(0);
for (position, element) in self.polynomial.iter_mut().enumerate() {
if position % 2 == 1 {
*element = FieldElement::new(vec![0; 16]);
}
}
self
}
}
impl Clone for Polynomial {
@ -645,26 +658,6 @@ mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
/*
* TODO: Consider removing
#[test]
fn coefficients_to_byte_arr_xex_test1() {
let coefficients: Vec<u8> = vec![0];
let byte_array = vec![
01, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
#[test]
fn coefficients_to_byte_arr_xex_test2() {
let coefficients: Vec<u8> = vec![127, 12, 9, 0];
let byte_array = vec![
01, 12, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 80,
];
assert_eq!(coefficients_to_byte_arr_xex(coefficients), byte_array)
}
*/
#[test]
fn byte_indices_0x01() {
let byte: u8 = 0x01;
@ -1192,4 +1185,25 @@ mod tests {
assert_eq!(json!(result.to_c_array()), expected);
}
#[test]
fn test_poly_diff() {
let json1 = json!([
"IJustWannaTellYouAAAAA==",
"HowImFeelingAAAAAAAAAA==",
"GottaMakeYouAAAAAAAAAA==",
"UnderstaaaaaaaaaaaaanQ=="
]);
let expected = json!([
"HowImFeelingAAAAAAAAAA==",
"AAAAAAAAAAAAAAAAAAAAAA==",
"UnderstaaaaaaaaaaaaanQ=="
]);
let element1: Polynomial = Polynomial::from_c_array(&json1);
eprintln!("Starting poly sqrt");
let result = element1.diff();
assert_eq!(json!(result.to_c_array()), expected);
}
}

15
test_json/sandbox.json Normal file
View file

@ -0,0 +1,15 @@
{
"testcases": {
"sandbox": {
"action": "gfpoly_diff",
"arguments": {
"F": [
"IJustWannaTellYouAAAAA==",
"HowImFeelingAAAAAAAAAA==",
"GottaMakeYouAAAAAAAAAA==",
"UnderstaaaaaaaaaaaaanQ=="
]
}
}
}
}