#![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 /// License: MIT /// Source: #[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 { let mut diffrence: u32; let mut index: usize = 0; if verbose { println!("relation:\t0x{:x}\t", relation); println!("polynomial:\t0x{:x}\t", poly); println!("======================================================================="); } if relation > poly { if verbose { println!("relation is longer than polynom, nothing to do."); } return Ok(poly); } while poly > relation { diffrence = relation.leading_zeros() - poly.leading_zeros(); poly = poly ^ (relation << diffrence); if verbose { println!("{index}:\tpoly: 0x{:x}\t 0b{:b}", poly, poly); } index += 1; } return Ok(poly); }