plexcryptool/src/math/modred.rs

47 lines
1.4 KiB
Rust
Raw Normal View History

2023-05-16 13:59:15 +02:00
#![allow(dead_code)]
/// modular reduction
///
/// Implements automatic modular reduction in a field specified by a given relation.
///
/// Basically, any binary number can be written as a polynomial. This polynomial can be reduced by
/// the relation that defines a field. In that field. This is what we call modular reduction.
///
/// Author: Christoph J. Scherr <software@cscherr.de>
/// License: MIT
/// Source: <https://git.cscherr.de/PlexSheep/plexcryptool/>
2023-05-17 11:13:11 +02:00
use crate::cplex::printing::seperator;
2023-05-16 15:54:24 +02:00
2023-05-16 13:59:15 +02:00
#[test]
fn test_modred() {
let rel: u64 = 0x1053;
let pol0: u64 = 0x100001;
assert_eq!(modred(pol0, rel, false).unwrap(), 0x21e);
}
pub fn modred(mut poly: u64, relation: u64, verbose: bool) -> Result<u64, String> {
let mut diffrence: u32;
let mut index: usize = 0;
if verbose {
2023-05-16 15:54:24 +02:00
println!("relation:\t{:#x}\t", relation);
println!("polynomial:\t{:#x}\t", poly);
2023-05-17 11:13:11 +02:00
seperator();
2023-05-16 13:59:15 +02:00
}
2023-05-16 14:23:05 +02:00
if relation > poly {
if verbose {
println!("relation is longer than polynom, nothing to do.");
}
return Ok(poly);
}
while poly > relation {
2023-05-16 13:59:15 +02:00
diffrence = relation.leading_zeros() - poly.leading_zeros();
poly = poly ^ (relation << diffrence);
if verbose {
2023-05-16 15:54:24 +02:00
println!("{index}:\tpoly: {:#x}\t {:#064b}", poly, poly);
2023-05-16 13:59:15 +02:00
}
index += 1;
}
return Ok(poly);
}