fix: Add better handling of special cases to powmod
This commit is contained in:
parent
c5d3db27f4
commit
ca2067c04e
1 changed files with 110 additions and 22 deletions
|
|
@ -76,15 +76,19 @@ impl Polynomial {
|
||||||
)]);
|
)]);
|
||||||
|
|
||||||
if exponent == 1 {
|
if exponent == 1 {
|
||||||
return self;
|
eprintln!("special case 1: {:02X?}", self.clone().div(&modulus).1);
|
||||||
|
|
||||||
|
return self.div(&modulus).1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if exponent == 0 {
|
if exponent == 0 {
|
||||||
Polynomial::new(vec![FieldElement::new(
|
let inter = Polynomial::new(vec![FieldElement::new(
|
||||||
polynomial_2_block(vec![0], "gcm").unwrap(),
|
polynomial_2_block(vec![0], "gcm").unwrap(),
|
||||||
)])
|
)]);
|
||||||
.div(&modulus)
|
let result = inter.div(&modulus);
|
||||||
.1;
|
|
||||||
|
eprintln!("Returned value is: {:02X?}", result);
|
||||||
|
return result.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//eprintln!("Initial result: {:?}", result);
|
//eprintln!("Initial result: {:?}", result);
|
||||||
|
|
@ -103,6 +107,8 @@ impl Polynomial {
|
||||||
exponent >>= 1;
|
exponent >>= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eprintln!("result in powmod before reduction: {:02X?}", result);
|
||||||
|
|
||||||
while !result.polynomial.is_empty()
|
while !result.polynomial.is_empty()
|
||||||
&& result
|
&& result
|
||||||
.polynomial
|
.polynomial
|
||||||
|
|
@ -115,6 +121,8 @@ impl Polynomial {
|
||||||
result.polynomial.pop();
|
result.polynomial.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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])]);
|
||||||
}
|
}
|
||||||
|
|
@ -946,23 +954,6 @@ mod tests {
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//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 =
|
||||||
|
|
@ -1024,6 +1015,38 @@ mod tests {
|
||||||
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_div_eqdeg() {
|
||||||
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==", "wAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
|
let json2 = json!(["0AAAAAAAAAAAAAAAAAAAAA==", "IQAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let (result, remainder) = element2.div(&element1);
|
||||||
|
|
||||||
|
eprintln!("{:02X?}", (&result, &remainder));
|
||||||
|
|
||||||
|
assert!(!result.is_zero());
|
||||||
|
assert!(!remainder.is_zero());
|
||||||
|
//assert_eq!(BASE64_STANDARD.encode(product), "MoAAAAAAAAAAAAAAAAAAAA==");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_div_eqdeg_02() {
|
||||||
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==", "wAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
|
let json2 = json!(["KryptoanalyseAAAAAAAAA==", "DHBWMannheimAAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let element2: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let (result, remainder) = element2.div(&element1);
|
||||||
|
|
||||||
|
eprintln!("{:02X?}", (&result, &remainder));
|
||||||
|
|
||||||
|
assert!(!result.is_zero());
|
||||||
|
assert!(!remainder.is_zero());
|
||||||
|
//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!([
|
||||||
|
|
@ -1043,6 +1066,19 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_poly_powmod_k1() {
|
fn test_field_poly_powmod_k1() {
|
||||||
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
|
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(1, modulus);
|
||||||
|
|
||||||
|
eprintln!("Result is: {:02X?}", result);
|
||||||
|
assert_eq!(result.to_c_array(), vec!["JAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_powmod_k1_modulus_is_deg0() {
|
||||||
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==",]);
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
let json2 = json!(["KryptoanalyseAAAAAAAAA=="]);
|
let json2 = json!(["KryptoanalyseAAAAAAAAA=="]);
|
||||||
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
|
@ -1054,6 +1090,41 @@ mod tests {
|
||||||
assert_eq!(result.to_c_array(), vec!["JAAAAAAAAAAAAAAAAAAAAA=="]);
|
assert_eq!(result.to_c_array(), vec!["JAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_powmod_k1_eqdeg() {
|
||||||
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==", "JAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let json2 = json!(["KryptoanalyseAAAAAAAAA==", "KryptoanalyseAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let modulus: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let result = element1.pow_mod(1, modulus);
|
||||||
|
|
||||||
|
eprintln!("Result is: {:02X?}", result);
|
||||||
|
|
||||||
|
assert!(!(0 < 0));
|
||||||
|
assert_eq!(
|
||||||
|
result.to_c_array(),
|
||||||
|
vec!["JAAAAAAAAAAAAAAAAAAAAA==", "JAAAAAAAAAAAAAAAAAAAAA=="]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_field_poly_powmod_kn_eqdeg() {
|
||||||
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==", "JAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
|
let json2 = json!(["KryptoanalyseAAAAAAAAA==", "KryptoanalyseAAAAAAAAA=="]);
|
||||||
|
let element1: Polynomial = Polynomial::from_c_array(&json1);
|
||||||
|
let modulus: Polynomial = Polynomial::from_c_array(&json2);
|
||||||
|
|
||||||
|
let result = element1.pow_mod(100000, modulus);
|
||||||
|
|
||||||
|
eprintln!("Result is: {:02X?}", result);
|
||||||
|
|
||||||
|
assert!(!(0 < 0));
|
||||||
|
assert_eq!(
|
||||||
|
result.to_c_array(),
|
||||||
|
vec!["JAAAAAAAAAAAAAAAAAAAAA==", "JAAAAAAAAAAAAAAAAAAAAA=="]
|
||||||
|
);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_field_poly_powmod_k0() {
|
fn test_field_poly_powmod_k0() {
|
||||||
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==",]);
|
let json1 = json!(["JAAAAAAAAAAAAAAAAAAAAA==",]);
|
||||||
|
|
@ -1066,4 +1137,21 @@ mod tests {
|
||||||
eprintln!("Result is: {:02X?}", result);
|
eprintln!("Result is: {:02X?}", result);
|
||||||
assert_eq!(result.to_c_array(), vec!["gAAAAAAAAAAAAAAAAAAAAA=="]);
|
assert_eq!(result.to_c_array(), vec!["gAAAAAAAAAAAAAAAAAAAAA=="]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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==");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue