Merge pull request #14 from 0xalivecow/dev
Merge fixes for pfmath functions
This commit is contained in:
commit
c1bcb768ba
12 changed files with 208 additions and 51 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::{self, args},
|
env::{self},
|
||||||
fs,
|
fs,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,8 @@
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
|
|
||||||
use std::{collections::HashMap, env::args};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::utils::{
|
use crate::utils::parse::{Responses, Testcase, Testcases};
|
||||||
ciphers::gcm_encrypt_aes,
|
|
||||||
parse::{Responses, Testcase, Testcases},
|
|
||||||
};
|
|
||||||
use tasks01::{
|
use tasks01::{
|
||||||
block2poly::block2poly,
|
block2poly::block2poly,
|
||||||
gcm::{gcm_decrypt, gcm_encrypt},
|
gcm::{gcm_decrypt, gcm_encrypt},
|
||||||
|
|
@ -14,7 +11,7 @@ use tasks01::{
|
||||||
pfmath::{gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_mul, gfpoly_pow, gfpoly_powmod},
|
pfmath::{gfdiv, gfpoly_add, gfpoly_divmod, gfpoly_mul, gfpoly_pow, gfpoly_powmod},
|
||||||
poly2block::poly2block,
|
poly2block::poly2block,
|
||||||
sea128::sea128,
|
sea128::sea128,
|
||||||
xex::{self, fde_xex},
|
xex::{fde_xex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::utils::poly::{b64_2_num, block_2_polynomial, get_coefficients};
|
use crate::utils::poly::block_2_polynomial;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,4 @@
|
||||||
use crate::utils::{
|
use crate::utils::poly::gfmul;
|
||||||
field::ByteArray,
|
|
||||||
poly::{b64_2_num, coefficient_to_binary, gfmul},
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ use base64::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::Instant;
|
||||||
use std::{thread, usize};
|
use std::usize;
|
||||||
|
|
||||||
pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
||||||
let hostname: String = serde_json::from_value(args["hostname"].clone())?;
|
let hostname: String = serde_json::from_value(args["hostname"].clone())?;
|
||||||
|
|
@ -29,8 +29,6 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
||||||
let mut chunk_counter = 0;
|
let mut chunk_counter = 0;
|
||||||
|
|
||||||
for chunk in &cipher_chunks {
|
for chunk in &cipher_chunks {
|
||||||
let start = Instant::now();
|
|
||||||
|
|
||||||
let mut stream = TcpStream::connect(format!("{}:{}", hostname, port))?;
|
let mut stream = TcpStream::connect(format!("{}:{}", hostname, port))?;
|
||||||
stream.set_nonblocking(false)?;
|
stream.set_nonblocking(false)?;
|
||||||
|
|
||||||
|
|
@ -111,12 +109,12 @@ pub fn padding_oracle(args: &Value) -> Result<Vec<u8>> {
|
||||||
} else {
|
} else {
|
||||||
//eprintln!("Invalid padding");
|
//eprintln!("Invalid padding");
|
||||||
// Search for second hit
|
// Search for second hit
|
||||||
let valid_val = (255
|
let valid_val = 255
|
||||||
- server_q_resp
|
- server_q_resp
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.rev()
|
||||||
.position(|&r| r == 0x01)
|
.position(|&r| r == 0x01)
|
||||||
.unwrap_or(0x00) as u8);
|
.unwrap_or(0x00) as u8;
|
||||||
if valid_val == 0x00 {
|
if valid_val == 0x00 {
|
||||||
eprintln!("No valid found");
|
eprintln!("No valid found");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::utils::poly::{self, polynomial_2_block};
|
use crate::utils::poly::{polynomial_2_block};
|
||||||
use anyhow::{Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use base64::prelude::*;
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
pub fn poly2block(args: &Value) -> Result<Vec<u8>> {
|
pub fn poly2block(args: &Value) -> Result<Vec<u8>> {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ pub fn sea128(args: &Value) -> Result<String> {
|
||||||
let input_string: String = serde_json::from_value(args["input"].clone())?;
|
let input_string: String = serde_json::from_value(args["input"].clone())?;
|
||||||
//let plaintexts: &[u8] = &b64_2_num(plaintexts_string)?.to_ne_bytes();
|
//let plaintexts: &[u8] = &b64_2_num(plaintexts_string)?.to_ne_bytes();
|
||||||
let input = BASE64_STANDARD.decode(input_string)?;
|
let input = BASE64_STANDARD.decode(input_string)?;
|
||||||
let xor_val: u128 = 0xc0ffeec0ffeec0ffeec0ffeec0ffee11;
|
|
||||||
|
|
||||||
let mode: String = serde_json::from_value(args["mode"].clone())?;
|
let mode: String = serde_json::from_value(args["mode"].clone())?;
|
||||||
match mode.as_str() {
|
match mode.as_str() {
|
||||||
|
|
@ -34,7 +33,6 @@ pub fn sea128(args: &Value) -> Result<String> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
use std::{io::BufRead, process::Output};
|
use crate::utils::{field::ByteArray, poly::gfmul};
|
||||||
|
|
||||||
use crate::utils::{field::ByteArray, math::reverse_bits_in_bytevec, poly::gfmul};
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use openssl::symm::{Cipher, Crypter, Mode};
|
use openssl::symm::{Cipher, Crypter, Mode};
|
||||||
|
|
@ -40,7 +38,6 @@ pub fn aes_128_decrypt(key: &Vec<u8>, input: &Vec<u8>) -> Result<Vec<u8>> {
|
||||||
|
|
||||||
let mut bytes: [u8; 16] = [0u8; 16];
|
let mut bytes: [u8; 16] = [0u8; 16];
|
||||||
bytes.copy_from_slice(&plaintext);
|
bytes.copy_from_slice(&plaintext);
|
||||||
let number: u128 = <u128>::from_be_bytes(bytes);
|
|
||||||
|
|
||||||
Ok(plaintext)
|
Ok(plaintext)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,10 @@
|
||||||
use std::{
|
use std::ops::{Add, BitXor, Div, Mul, Sub};
|
||||||
env::args,
|
|
||||||
ops::{Add, BitXor, Div, Mul, Rem, Sub},
|
|
||||||
result,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::{anyhow, Ok, Result};
|
use anyhow::{anyhow, Ok, Result};
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::{tasks::tasks01::poly2block::poly2block, utils::poly::polynomial_2_block};
|
use crate::utils::poly::polynomial_2_block;
|
||||||
|
|
||||||
use super::{math::xor_bytes, poly::gfmul};
|
use super::{math::xor_bytes, poly::gfmul};
|
||||||
|
|
||||||
|
|
@ -58,7 +54,9 @@ impl Polynomial {
|
||||||
|
|
||||||
pub fn pow(&self, mut exponent: u128) -> Polynomial {
|
pub fn pow(&self, mut exponent: u128) -> Polynomial {
|
||||||
if exponent == 0 {
|
if exponent == 0 {
|
||||||
return Polynomial::new(vec![FieldElement::new(vec![0])]);
|
return Polynomial::new(vec![FieldElement::new(
|
||||||
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
|
)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let base = self.clone();
|
let base = self.clone();
|
||||||
|
|
@ -73,6 +71,12 @@ impl Polynomial {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pow_mod(mut self, mut exponent: u128, modulus: Polynomial) -> Polynomial {
|
pub fn pow_mod(mut self, mut exponent: u128, modulus: Polynomial) -> Polynomial {
|
||||||
|
if exponent == 0 {
|
||||||
|
return Polynomial::new(vec![FieldElement::new(
|
||||||
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
|
)]);
|
||||||
|
}
|
||||||
|
|
||||||
let mut result: Polynomial = Polynomial::new(vec![FieldElement::new(
|
let mut result: Polynomial = Polynomial::new(vec![FieldElement::new(
|
||||||
polynomial_2_block(vec![0], "gcm").unwrap(),
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
)]);
|
)]);
|
||||||
|
|
@ -99,6 +103,12 @@ impl Polynomial {
|
||||||
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());
|
||||||
|
|
||||||
|
if self.polynomial.len() < rhs.polynomial.len() {
|
||||||
|
return (Polynomial::new(vec![FieldElement::new(vec![0; 16])]), self);
|
||||||
|
}
|
||||||
|
|
||||||
let mut remainder = self.clone();
|
let mut remainder = self.clone();
|
||||||
let divisor = rhs;
|
let divisor = rhs;
|
||||||
let dividend_deg = remainder.polynomial.len() - 1;
|
let dividend_deg = remainder.polynomial.len() - 1;
|
||||||
|
|
@ -155,6 +165,15 @@ impl Polynomial {
|
||||||
|
|
||||||
(Polynomial::new(quotient_coeffs), remainder)
|
(Polynomial::new(quotient_coeffs), remainder)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_zero(&self) -> bool {
|
||||||
|
for field_element in &self.polynomial {
|
||||||
|
if !field_element.is_zero() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Polynomial {
|
impl Clone for Polynomial {
|
||||||
|
|
@ -167,8 +186,10 @@ impl Clone for Polynomial {
|
||||||
|
|
||||||
impl Mul for Polynomial {
|
impl Mul for Polynomial {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
|
|
||||||
fn mul(self, rhs: Self) -> Self::Output {
|
fn mul(self, rhs: Self) -> Self::Output {
|
||||||
|
if self.is_zero() || rhs.is_zero() {
|
||||||
|
return Polynomial::new(vec![FieldElement::new(vec![0; 16])]);
|
||||||
|
}
|
||||||
let mut polynomial: Vec<FieldElement> =
|
let mut polynomial: Vec<FieldElement> =
|
||||||
vec![FieldElement::new(vec![0; 16]); self.polynomial.len() + rhs.polynomial.len() - 1];
|
vec![FieldElement::new(vec![0; 16]); self.polynomial.len() + rhs.polynomial.len() - 1];
|
||||||
for i in 0..self.polynomial.len() {
|
for i in 0..self.polynomial.len() {
|
||||||
|
|
@ -184,6 +205,9 @@ impl Mul for Polynomial {
|
||||||
impl Mul for &Polynomial {
|
impl Mul for &Polynomial {
|
||||||
type Output = Polynomial;
|
type Output = Polynomial;
|
||||||
fn mul(self, rhs: Self) -> Self::Output {
|
fn mul(self, rhs: Self) -> Self::Output {
|
||||||
|
if self.is_zero() || rhs.is_zero() {
|
||||||
|
return Polynomial::new(vec![FieldElement::new(vec![0])]);
|
||||||
|
}
|
||||||
let mut polynomial: Vec<FieldElement> =
|
let mut polynomial: Vec<FieldElement> =
|
||||||
vec![FieldElement::new(vec![0; 16]); self.polynomial.len() + rhs.polynomial.len() - 1];
|
vec![FieldElement::new(vec![0; 16]); self.polynomial.len() + rhs.polynomial.len() - 1];
|
||||||
for i in 0..self.polynomial.len() {
|
for i in 0..self.polynomial.len() {
|
||||||
|
|
@ -300,6 +324,10 @@ impl FieldElement {
|
||||||
//eprintln!("Inverse rhs {:?}", inverse);
|
//eprintln!("Inverse rhs {:?}", inverse);
|
||||||
FieldElement::new(inverse)
|
FieldElement::new(inverse)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_zero(&self) -> bool {
|
||||||
|
self.field_element.iter().all(|&x| x == 0x00)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Mul for FieldElement {
|
impl Mul for FieldElement {
|
||||||
|
|
@ -522,15 +550,13 @@ impl ByteArray {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use base64::prelude::*;
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_byte_array_shift1() {
|
fn test_byte_array_shift1() {
|
||||||
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]);
|
let mut byte_array: ByteArray = ByteArray(vec![0x00, 0x01]);
|
||||||
let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]);
|
let shifted_array: ByteArray = ByteArray(vec![0x00, 0x02]);
|
||||||
byte_array.left_shift("xex");
|
byte_array.left_shift("xex").unwrap();
|
||||||
|
|
||||||
assert_eq!(byte_array.0, shifted_array.0);
|
assert_eq!(byte_array.0, shifted_array.0);
|
||||||
}
|
}
|
||||||
|
|
@ -539,7 +565,7 @@ mod tests {
|
||||||
fn test_byte_array_shift2() {
|
fn test_byte_array_shift2() {
|
||||||
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
||||||
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]);
|
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x01]);
|
||||||
byte_array.left_shift("xex");
|
byte_array.left_shift("xex").unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
byte_array.0, shifted_array.0,
|
byte_array.0, shifted_array.0,
|
||||||
|
|
@ -552,7 +578,7 @@ mod tests {
|
||||||
fn test_byte_array_shift1_gcm() {
|
fn test_byte_array_shift1_gcm() {
|
||||||
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
||||||
let shifted_array: ByteArray = ByteArray(vec![0x7F, 0x80]);
|
let shifted_array: ByteArray = ByteArray(vec![0x7F, 0x80]);
|
||||||
byte_array.left_shift("gcm");
|
byte_array.left_shift("gcm").unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
byte_array.0, shifted_array.0,
|
byte_array.0, shifted_array.0,
|
||||||
|
|
@ -565,7 +591,7 @@ mod tests {
|
||||||
fn test_byte_array_shift1_right_gcm() {
|
fn test_byte_array_shift1_right_gcm() {
|
||||||
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
let mut byte_array: ByteArray = ByteArray(vec![0xFF, 0x00]);
|
||||||
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x00]);
|
let shifted_array: ByteArray = ByteArray(vec![0xFE, 0x00]);
|
||||||
byte_array.right_shift("gcm");
|
byte_array.right_shift("gcm").unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
byte_array.0, shifted_array.0,
|
byte_array.0, shifted_array.0,
|
||||||
|
|
@ -578,7 +604,7 @@ mod tests {
|
||||||
fn test_byte_array_shift_right() {
|
fn test_byte_array_shift_right() {
|
||||||
let mut byte_array: ByteArray = ByteArray(vec![0x02]);
|
let mut byte_array: ByteArray = ByteArray(vec![0x02]);
|
||||||
let shifted_array: ByteArray = ByteArray(vec![0x01]);
|
let shifted_array: ByteArray = ByteArray(vec![0x01]);
|
||||||
byte_array.right_shift("xex");
|
byte_array.right_shift("xex").unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
byte_array.0, shifted_array.0,
|
byte_array.0, shifted_array.0,
|
||||||
|
|
@ -666,6 +692,68 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_add_zero() {
|
||||||
|
let json1 = json!([
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let json2 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let sum = element2 + element1;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
sum.to_c_array(),
|
||||||
|
vec![
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_add_zero_to_zero() {
|
||||||
|
let json1 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let json2 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let sum = element2 + element1;
|
||||||
|
|
||||||
|
assert_eq!(sum.to_c_array(), vec!["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_add_short_to_long() {
|
||||||
|
let json1 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let json2 = json!([
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let sum = element2 + element1;
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
sum.to_c_array(),
|
||||||
|
vec![
|
||||||
|
"NeverGonnaGiveYouUpAAA==",
|
||||||
|
"NeverGonnaLetYouDownAA==",
|
||||||
|
"NeverGonnaRunAroundAAA==",
|
||||||
|
"AndDesertYouAAAAAAAAAA=="
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_mul_01() {
|
fn test_field_mul_01() {
|
||||||
let json1 = json!([
|
let json1 = json!([
|
||||||
|
|
@ -694,7 +782,26 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_pow_01() {
|
fn test_poly_mul_with_zero() {
|
||||||
|
let json1 = json!([
|
||||||
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"ACAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let json2 = json!(["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
//eprintln!("{:?}", element1);
|
||||||
|
|
||||||
|
let result = element1 * element2;
|
||||||
|
|
||||||
|
assert_eq!(result.to_c_array(), vec!["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_pow_01() {
|
||||||
let json1 = json!([
|
let json1 = json!([
|
||||||
"JAAAAAAAAAAAAAAAAAAAAA==",
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
"wAAAAAAAAAAAAAAAAAAAAA==",
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
|
@ -719,6 +826,21 @@ mod tests {
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_poly_pow_with_zero() {
|
||||||
|
let json1 = json!([
|
||||||
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"ACAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
|
||||||
|
let result = element1.pow(0);
|
||||||
|
|
||||||
|
assert_eq!(result.to_c_array(), vec!["gAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_pow_mod_01() {
|
fn test_field_pow_mod_01() {
|
||||||
let json1 = json!([
|
let json1 = json!([
|
||||||
|
|
@ -745,6 +867,38 @@ mod tests {
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_pow_mod_with_zero() {
|
||||||
|
let json1 = json!([
|
||||||
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"ACAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
|
||||||
|
let result = element1.pow(0);
|
||||||
|
|
||||||
|
assert_eq!(result.to_c_array(), vec!["gAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_pow_mod_10mill() {
|
||||||
|
let json1 = json!([
|
||||||
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"ACAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let json2 = json!(["KryptoanalyseAAAAAAAAA==", "DHBWMannheimAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let modulus: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let result = element1.pow_mod(10000000, modulus);
|
||||||
|
|
||||||
|
assert!(!result.is_zero())
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_poly_div_01() {
|
fn test_poly_div_01() {
|
||||||
let element1 =
|
let element1 =
|
||||||
|
|
@ -782,6 +936,29 @@ mod tests {
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_div_larger_div() {
|
||||||
|
let json1 = json!([
|
||||||
|
"JAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"wAAAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"ACAAAAAAAAAAAAAAAAAAAA=="
|
||||||
|
]);
|
||||||
|
let json2 = json!(["0AAAAAAAAAAAAAAAAAAAAA==", "IQAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
//eprintln!("{:?}", element1);
|
||||||
|
|
||||||
|
println!("Beginning the new division");
|
||||||
|
let (result, remainder) = element2.div(&element1);
|
||||||
|
|
||||||
|
assert_eq!(result.to_c_array(), vec!["AAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
assert_eq!(
|
||||||
|
remainder.to_c_array(),
|
||||||
|
vec!["0AAAAAAAAAAAAAAAAAAAAA==", "IQAAAAAAAAAAAAAAAAAAAA=="]
|
||||||
|
);
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_poly_powmod_01() {
|
fn test_field_poly_powmod_01() {
|
||||||
let json1 = json!([
|
let json1 = json!([
|
||||||
|
|
@ -796,6 +973,6 @@ mod tests {
|
||||||
let result = element1.pow_mod(1000, modulus);
|
let result = element1.pow_mod(1000, modulus);
|
||||||
|
|
||||||
eprintln!("Result is: {:02X?}", result);
|
eprintln!("Result is: {:02X?}", result);
|
||||||
assert_eq!(result.to_c_array(), vec!["XrEhmKuat+Glt5zZWtMo6g=="]);
|
assert_eq!(result.to_c_array(), vec!["oNXl5P8xq2WpUTP92u25zg=="]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
||||||
use anyhow::{anyhow, Ok, Result};
|
use anyhow::{Ok, Result};
|
||||||
use base64::Engine;
|
|
||||||
|
|
||||||
use super::poly::gfmul;
|
|
||||||
|
|
||||||
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
|
pub fn xor_bytes(vec1: &Vec<u8>, mut vec2: Vec<u8>) -> Result<Vec<u8>> {
|
||||||
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
|
for (byte1, byte2) in vec1.iter().zip(vec2.iter_mut()) {
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,6 @@ pub fn parse_json(json: String) -> Result<Testcases> {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
use crate::utils::field::ByteArray;
|
use crate::utils::field::ByteArray;
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use serde_json::Value;
|
|
||||||
use std::{str::FromStr, u128, u8, usize};
|
use std::{str::FromStr, u128, u8, usize};
|
||||||
|
|
||||||
use super::{field, math::reverse_bits_in_bytevec};
|
|
||||||
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000;
|
pub const RED_POLY: u128 = 0x87000000_00000000_00000000_00000000;
|
||||||
|
|
||||||
pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
|
pub fn gfmul(poly_a: &Vec<u8>, poly_b: &Vec<u8>, semantic: &str) -> Result<Vec<u8>> {
|
||||||
|
|
@ -172,7 +170,7 @@ pub fn coefficients_to_byte_arr_xex(coeffs: Vec<u8>) -> Vec<u8> {
|
||||||
let mut byte_array: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
let mut byte_array: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
||||||
for coeff in coeffs {
|
for coeff in coeffs {
|
||||||
let block_num = coeff / 8;
|
let block_num = coeff / 8;
|
||||||
byte_array[usize::from(block_num)] |= (1 << (coeff % 7));
|
byte_array[usize::from(block_num)] |= 1 << (coeff % 7);
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_array
|
byte_array
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue