use super::super::Zq;
use crate::{
error::MathError,
integer::Z,
macros::arithmetics::{
arithmetic_assign_between_types, arithmetic_assign_trait_borrowed_to_owned,
arithmetic_between_types_zq, arithmetic_trait_borrowed_to_owned,
arithmetic_trait_mixed_borrowed_owned,
},
traits::CompareBase,
};
use flint_sys::{
fmpz::fmpz,
fmpz_mod::{fmpz_mod_sub, fmpz_mod_sub_fmpz, fmpz_mod_sub_si, fmpz_mod_sub_ui},
};
use std::ops::{Sub, SubAssign};
impl SubAssign<&Zq> for Zq {
fn sub_assign(&mut self, other: &Self) {
if !self.compare_base(other) {
panic!("{}", self.call_compare_base_error(other).unwrap());
}
unsafe {
fmpz_mod_sub(
&mut self.value.value,
&self.value.value,
&other.value.value,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
}
}
impl SubAssign<i64> for Zq {
fn sub_assign(&mut self, other: i64) {
unsafe {
fmpz_mod_sub_si(
&mut self.value.value,
&self.value.value,
other,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
}
}
impl SubAssign<u64> for Zq {
fn sub_assign(&mut self, other: u64) {
unsafe {
fmpz_mod_sub_ui(
&mut self.value.value,
&self.value.value,
other,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
}
}
impl SubAssign<&Z> for Zq {
fn sub_assign(&mut self, other: &Z) {
unsafe {
fmpz_mod_sub_fmpz(
&mut self.value.value,
&self.value.value,
&other.value,
self.modulus.get_fmpz_mod_ctx_struct(),
)
};
}
}
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, Zq, Zq);
arithmetic_assign_trait_borrowed_to_owned!(SubAssign, sub_assign, Zq, Z);
arithmetic_assign_between_types!(SubAssign, sub_assign, Zq, i64, i32 i16 i8);
arithmetic_assign_between_types!(SubAssign, sub_assign, Zq, u64, u32 u16 u8);
impl Sub for &Zq {
type Output = Zq;
fn sub(self, other: Self) -> Self::Output {
self.sub_safe(other).unwrap()
}
}
impl Zq {
pub fn sub_safe(&self, other: &Self) -> Result<Zq, MathError> {
if !self.compare_base(other) {
return Err(self.call_compare_base_error(other).unwrap());
}
let mut out_z = Z::ZERO;
unsafe {
fmpz_mod_sub(
&mut out_z.value,
&self.value.value,
&other.value.value,
self.modulus.get_fmpz_mod_ctx_struct(),
);
}
Ok(Self {
value: out_z,
modulus: self.modulus.clone(),
})
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, Zq, Zq, Zq);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, Zq, Zq, Zq);
arithmetic_between_types_zq!(Sub, sub, Zq, i64 i32 i16 i8 u64 u32 u16 u8);
impl Sub<&Z> for &Zq {
type Output = Zq;
fn sub(self, other: &Z) -> Self::Output {
let mut out = fmpz(0);
unsafe {
fmpz_mod_sub_fmpz(
&mut out,
&self.value.value,
&other.value,
self.modulus.get_fmpz_mod_ctx_struct(),
);
}
Zq {
modulus: self.modulus.clone(),
value: Z { value: out },
}
}
}
arithmetic_trait_borrowed_to_owned!(Sub, sub, Zq, Z, Zq);
arithmetic_trait_mixed_borrowed_owned!(Sub, sub, Zq, Z, Zq);
#[cfg(test)]
mod test_sub_assign {
use crate::{integer::Z, integer_mod_q::Zq};
#[test]
fn correct_small() {
let mut a: Zq = Zq::from((-1, 7));
let b = Zq::from((1, 7));
let c = Zq::from((-1, 7));
a -= &b;
assert_eq!(Zq::from((5, 7)), a);
a -= &c;
assert_eq!(Zq::from((6, 7)), a);
a -= &c;
assert_eq!(Zq::from((0, 7)), a);
a -= &c;
assert_eq!(Zq::from((1, 7)), a);
a -= &c;
assert_eq!(Zq::from((2, 7)), a);
a -= 2 * b;
assert_eq!(Zq::from((0, 7)), a);
}
#[test]
fn correct_large() {
let mut a = Zq::from((i64::MAX, u64::MAX));
let b = Zq::from((i64::MAX - 1, u64::MAX));
a -= b;
assert_eq!(Zq::from((1, u64::MAX)), a);
}
#[test]
fn availability() {
let mut a = Zq::from((3, 7));
let b = Zq::from((1, 7));
let c = Z::from(1);
a -= &b;
a -= b;
a -= &c;
a -= c;
a -= 1_u8;
a -= 1_u16;
a -= 1_u32;
a -= 1_u64;
a -= 1_i8;
a -= 1_i16;
a -= 1_i32;
a -= 1_i64;
}
#[test]
#[should_panic]
fn mismatching_moduli() {
let mut a = Zq::from((3, 7));
let b = Zq::from((1, 8));
a -= b;
}
}
#[cfg(test)]
mod test_sub {
use super::Zq;
#[test]
fn sub() {
let a: Zq = Zq::from((11, 17));
let b: Zq = Zq::from((12, 17));
let c: Zq = a - b;
assert_eq!(c, Zq::from((16, 17)));
}
#[test]
fn sub_borrow() {
let a: Zq = Zq::from((10, 11));
let b: Zq = Zq::from((1, 11));
let c: Zq = &a - &b;
assert_eq!(c, Zq::from((9, 11)));
}
#[test]
fn sub_first_borrowed() {
let a: Zq = Zq::from((2, 11));
let b: Zq = Zq::from((5, 11));
let c: Zq = &a - b;
assert_eq!(c, Zq::from((-3, 11)));
}
#[test]
fn sub_second_borrowed() {
let a: Zq = Zq::from((12, 11));
let b: Zq = Zq::from((10, 11));
let c: Zq = a - &b;
assert_eq!(c, Zq::from((2, 11)));
}
#[test]
fn sub_large_numbers() {
let a: Zq = Zq::from((u32::MAX, u32::MAX - 58));
let b: Zq = Zq::from((i32::MAX, u32::MAX - 58));
let c: Zq = a - b;
assert_eq!(c, Zq::from((u32::MAX - (u32::MAX - 1) / 2, u32::MAX - 58)));
}
#[test]
#[should_panic]
fn sub_mismatching_modulus() {
let a: Zq = Zq::from((4, 11));
let b: Zq = Zq::from((1, 3));
let _c: Zq = a - b;
}
#[test]
fn sub_safe_is_err() {
let a: Zq = Zq::from((4, 11));
let b: Zq = Zq::from((1, 3));
assert!(&a.sub_safe(&b).is_err());
}
}
#[cfg(test)]
mod test_sub_between_z_and_zq {
use super::Z;
use crate::integer_mod_q::Zq;
#[test]
fn sub() {
let a: Zq = Zq::from((4, 11));
let b: Z = Z::from(9);
let c: Zq = a - b;
assert_eq!(c, Zq::from((6, 11)));
}
#[test]
fn sub_borrow() {
let a: Zq = Zq::from((4, 11));
let b: Z = Z::from(9);
let c: Zq = &a - &b;
assert_eq!(c, Zq::from((6, 11)));
}
#[test]
fn sub_first_borrowed() {
let a: Zq = Zq::from((4, 11));
let b: Z = Z::from(9);
let c: Zq = &a - b;
assert_eq!(c, Zq::from((6, 11)));
}
#[test]
fn sub_second_borrowed() {
let a: Zq = Zq::from((4, 11));
let b: Z = Z::from(9);
let c: Zq = a - &b;
assert_eq!(c, Zq::from((6, 11)));
}
#[test]
fn sub_large_numbers() {
let a: Zq = Zq::from((i64::MAX, u64::MAX - 58));
let b: Zq = Zq::from((i64::MAX - 1, i64::MAX));
let c: Z = Z::from(u64::MAX);
let d: Zq = a - &c;
let e: Zq = b - c;
assert_eq!(d, Zq::from(((u64::MAX - 1) / 2 - 58, u64::MAX - 58)));
assert_eq!(e, Zq::from((-2, i64::MAX)));
}
}
#[cfg(test)]
mod test_sub_between_types {
use crate::integer_mod_q::Zq;
#[test]
#[allow(clippy::op_ref)]
fn sub() {
let a: Zq = Zq::from((4, 11));
let b: u64 = 1;
let c: u32 = 1;
let d: u16 = 1;
let e: u8 = 1;
let f: i64 = 1;
let g: i32 = 1;
let h: i16 = 1;
let i: i8 = 1;
let _: Zq = &a - &b;
let _: Zq = &a - &c;
let _: Zq = &a - &d;
let _: Zq = &a - &e;
let _: Zq = &a - &f;
let _: Zq = &a - &g;
let _: Zq = &a - &h;
let _: Zq = &a - &i;
let _: Zq = &b - &a;
let _: Zq = &c - &a;
let _: Zq = &d - &a;
let _: Zq = &e - &a;
let _: Zq = &f - &a;
let _: Zq = &g - &a;
let _: Zq = &h - &a;
let _: Zq = &i - &a;
let _: Zq = &a - b;
let _: Zq = &a - c;
let _: Zq = &a - d;
let _: Zq = &a - e;
let _: Zq = &a - f;
let _: Zq = &a - g;
let _: Zq = &a - h;
let _: Zq = &a - i;
let _: Zq = &b - Zq::from((4, 11));
let _: Zq = &c - Zq::from((4, 11));
let _: Zq = &d - Zq::from((4, 11));
let _: Zq = &e - Zq::from((4, 11));
let _: Zq = &f - Zq::from((4, 11));
let _: Zq = &g - Zq::from((4, 11));
let _: Zq = &h - Zq::from((4, 11));
let _: Zq = &i - Zq::from((4, 11));
let _: Zq = Zq::from((4, 11)) - &b;
let _: Zq = Zq::from((4, 11)) - &c;
let _: Zq = Zq::from((4, 11)) - &d;
let _: Zq = Zq::from((4, 11)) - &e;
let _: Zq = Zq::from((4, 11)) - &f;
let _: Zq = Zq::from((4, 11)) - &g;
let _: Zq = Zq::from((4, 11)) - &h;
let _: Zq = Zq::from((4, 11)) - &i;
let _: Zq = b - &a;
let _: Zq = c - &a;
let _: Zq = d - &a;
let _: Zq = e - &a;
let _: Zq = f - &a;
let _: Zq = g - &a;
let _: Zq = h - &a;
let _: Zq = i - &a;
let _: Zq = Zq::from((4, 11)) - b;
let _: Zq = Zq::from((4, 11)) - c;
let _: Zq = Zq::from((4, 11)) - d;
let _: Zq = Zq::from((4, 11)) - e;
let _: Zq = Zq::from((4, 11)) - f;
let _: Zq = Zq::from((4, 11)) - g;
let _: Zq = Zq::from((4, 11)) - h;
let _: Zq = Zq::from((4, 11)) - i;
let _: Zq = b - Zq::from((4, 11));
let _: Zq = c - Zq::from((4, 11));
let _: Zq = d - Zq::from((4, 11));
let _: Zq = e - Zq::from((4, 11));
let _: Zq = f - Zq::from((4, 11));
let _: Zq = g - Zq::from((4, 11));
let _: Zq = h - Zq::from((4, 11));
let _: Zq = i - Zq::from((4, 11));
}
}