use core::ops::{Sub, SubAssign};
use super::BigMontyForm;
use crate::monty::sub_mod_assign;
impl<'a> SubAssign<&BigMontyForm<'a>> for BigMontyForm<'a> {
fn sub_assign(&mut self, rhs: &BigMontyForm<'a>) {
self.assert_same_modulus(rhs);
sub_mod_assign(&mut self.value, &rhs.value, self.params.modulus.as_limbs());
}
}
impl<'a> SubAssign<BigMontyForm<'a>> for BigMontyForm<'a> {
fn sub_assign(&mut self, rhs: BigMontyForm<'a>) {
*self -= &rhs;
}
}
impl<'a> Sub<&BigMontyForm<'a>> for BigMontyForm<'a> {
type Output = BigMontyForm<'a>;
fn sub(mut self, rhs: &BigMontyForm<'a>) -> BigMontyForm<'a> {
self -= rhs;
self
}
}
impl<'a> Sub<BigMontyForm<'a>> for BigMontyForm<'a> {
type Output = BigMontyForm<'a>;
fn sub(mut self, rhs: BigMontyForm<'a>) -> BigMontyForm<'a> {
self -= &rhs;
self
}
}
impl<'a> Sub<BigMontyForm<'a>> for &BigMontyForm<'a> {
type Output = BigMontyForm<'a>;
fn sub(self, rhs: BigMontyForm<'a>) -> BigMontyForm<'a> {
self.clone() - &rhs
}
}
impl<'a> Sub<&BigMontyForm<'a>> for &BigMontyForm<'a> {
type Output = BigMontyForm<'a>;
fn sub(self, rhs: &BigMontyForm<'a>) -> BigMontyForm<'a> {
self.clone() - rhs
}
}
#[cfg(test)]
mod tests {
use super::super::testing::{MODULI, VALUES, form, int, params};
fn sub_mod(a: u64, b: u64, m: u64) -> u64 {
((u128::from(a % m) + u128::from(m) - u128::from(b % m)) % u128::from(m)) as u64
}
#[test]
fn the_difference_is_the_primitive_difference_reduced_by_the_modulus() {
for m in MODULI {
let params = params(m);
for a in VALUES {
for b in VALUES {
let difference = (form(a, ¶ms) - form(b, ¶ms)).retrieve();
assert_eq!(difference, int(sub_mod(a, b, m)), "{a} {b} {m}");
}
}
}
}
}