From 20dae028d8a37e14e87f3e73e670a100a9069f7a Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Tue, 4 May 2021 17:53:45 +0200 Subject: [PATCH] fix subtraction --- src/field/crandall_field.rs | 2 +- src/field/field_testing.rs | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/field/crandall_field.rs b/src/field/crandall_field.rs index 91b230af..95d80eed 100644 --- a/src/field/crandall_field.rs +++ b/src/field/crandall_field.rs @@ -268,7 +268,7 @@ impl Sub for CrandallField { #[inline] #[allow(clippy::suspicious_arithmetic_impl)] fn sub(self, rhs: Self) -> Self { - let (diff, under) = self.0.overflowing_sub(rhs.0); + let (diff, under) = self.0.overflowing_sub(rhs.to_canonical_u64()); Self(diff.overflowing_add((under as u64) * Self::ORDER).0) } } diff --git a/src/field/field_testing.rs b/src/field/field_testing.rs index bcb190e8..77b23ea3 100644 --- a/src/field/field_testing.rs +++ b/src/field/field_testing.rs @@ -305,6 +305,16 @@ macro_rules! test_arithmetic { } } } + + #[test] + fn subtraction() { + type F = $field; + + let (a, b) = (F::from_canonical_u64((F::ORDER + 1) / 2), F::TWO); + let x = a * b; + assert_eq!(x, F::ONE); + assert_eq!(F::ZERO - x, F::NEG_ONE); + } } }; }