refactor: fix broken gfmil algo
This commit is contained in:
parent
ccf0b03ec0
commit
2e22bd5789
3 changed files with 73 additions and 9 deletions
|
|
@ -48,4 +48,64 @@ mod tests {
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gfmul_task02() -> Result<()> {
|
||||||
|
let args: Value = json!({"a": "AwEAAAAAAAAAAAAAAAAAgA==", "b": "gBAAAAAAAAAAAAAAAAAAAA=="});
|
||||||
|
|
||||||
|
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
|
||||||
|
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
|
||||||
|
|
||||||
|
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
|
||||||
|
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
|
||||||
|
|
||||||
|
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result, "QKgUAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"Failure. Calulated result was: {}",
|
||||||
|
result
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gfmul_task03() -> Result<()> {
|
||||||
|
let args: Value = json!({"a": "AwEAAAAAAAAAAAAAAAAAgA==", "b": "oBAAAAAAAAAAAAAAAAAAAA=="});
|
||||||
|
|
||||||
|
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
|
||||||
|
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
|
||||||
|
|
||||||
|
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
|
||||||
|
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
|
||||||
|
|
||||||
|
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result, "UIAUAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"Failure. Calulated result was: {}",
|
||||||
|
result
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn gfmul_task04() -> Result<()> {
|
||||||
|
let args: Value = json!({"a": "ARIAAAAAAAAAAAAAAAAAgA==", "b": "AgAAAAAAAAAAAAAAAAAAAA=="});
|
||||||
|
|
||||||
|
let poly1_text: String = serde_json::from_value(args["a"].clone())?;
|
||||||
|
let poly_a = BASE64_STANDARD.decode(poly1_text)?;
|
||||||
|
|
||||||
|
let poly2_text: String = serde_json::from_value(args["b"].clone())?;
|
||||||
|
let poly_b = BASE64_STANDARD.decode(poly2_text)?;
|
||||||
|
|
||||||
|
let result = BASE64_STANDARD.encode(gfmul(poly_a, poly_b, "xex")?);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
result, "hSQAAAAAAAAAAAAAAAAAAA==",
|
||||||
|
"Failure. Calulated result was: {}",
|
||||||
|
result
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,10 @@ impl ByteArray {
|
||||||
(self.0.first().unwrap() & 1) == 1
|
(self.0.first().unwrap() & 1) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn msb_is_one(&self) -> bool {
|
||||||
|
(self.0.last().unwrap() & 1) == 1
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
for i in self.0.iter() {
|
for i in self.0.iter() {
|
||||||
if *i != 0 {
|
if *i != 0 {
|
||||||
|
|
|
||||||
|
|
@ -19,20 +19,20 @@ pub fn gfmul(poly_a: Vec<u8>, poly_b: Vec<u8>, semantic: &str) -> Result<Vec<u8>
|
||||||
|
|
||||||
if poly2.LSB_is_one() {
|
if poly2.LSB_is_one() {
|
||||||
result.xor_byte_arrays(&poly1);
|
result.xor_byte_arrays(&poly1);
|
||||||
poly2.right_shift(semantic)?;
|
|
||||||
} else {
|
|
||||||
poly2.right_shift(semantic)?;
|
|
||||||
}
|
}
|
||||||
|
poly2.right_shift(semantic)?;
|
||||||
|
|
||||||
while !poly2.is_empty() {
|
while !poly2.is_empty() {
|
||||||
if poly2.LSB_is_one() {
|
|
||||||
poly1.left_shift(semantic)?;
|
|
||||||
poly1.xor_byte_arrays(&red_poly_bytes);
|
|
||||||
result.xor_byte_arrays(&poly1);
|
|
||||||
} else {
|
|
||||||
poly1.left_shift(semantic)?;
|
poly1.left_shift(semantic)?;
|
||||||
|
|
||||||
|
if poly1.msb_is_one() {
|
||||||
poly1.xor_byte_arrays(&red_poly_bytes);
|
poly1.xor_byte_arrays(&red_poly_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if poly2.LSB_is_one() {
|
||||||
|
result.xor_byte_arrays(&poly1);
|
||||||
|
}
|
||||||
|
|
||||||
poly2.right_shift(semantic)?;
|
poly2.right_shift(semantic)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue