summaryrefslogtreecommitdiffstats
path: root/vendor/crypto-bigint/src/uint/neg_mod.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 12:41:41 +0000
commit10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87 (patch)
treebdffd5d80c26cf4a7a518281a204be1ace85b4c1 /vendor/crypto-bigint/src/uint/neg_mod.rs
parentReleasing progress-linux version 1.70.0+dfsg1-9~progress7.99u1. (diff)
downloadrustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.tar.xz
rustc-10ee2acdd26a7f1298c6f6d6b7af9b469fe29b87.zip
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/crypto-bigint/src/uint/neg_mod.rs')
-rw-r--r--vendor/crypto-bigint/src/uint/neg_mod.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/vendor/crypto-bigint/src/uint/neg_mod.rs b/vendor/crypto-bigint/src/uint/neg_mod.rs
new file mode 100644
index 000000000..0a1dc033a
--- /dev/null
+++ b/vendor/crypto-bigint/src/uint/neg_mod.rs
@@ -0,0 +1,68 @@
+//! [`UInt`] negation modulus operations.
+
+use crate::{Limb, NegMod, UInt};
+
+impl<const LIMBS: usize> UInt<LIMBS> {
+ /// Computes `-a mod p` in constant time.
+ /// Assumes `self` is in `[0, p)`.
+ pub const fn neg_mod(&self, p: &Self) -> Self {
+ let z = self.ct_is_nonzero();
+ let mut ret = p.sbb(self, Limb::ZERO).0;
+ let mut i = 0;
+ while i < LIMBS {
+ // Set ret to 0 if the original value was 0, in which
+ // case ret would be p.
+ ret.limbs[i].0 &= z;
+ i += 1;
+ }
+ ret
+ }
+
+ /// Computes `-a mod p` in constant time for the special modulus
+ /// `p = MAX+1-c` where `c` is small enough to fit in a single [`Limb`].
+ pub const fn neg_mod_special(&self, c: Limb) -> Self {
+ Self::ZERO.sub_mod_special(self, c)
+ }
+}
+
+impl<const LIMBS: usize> NegMod for UInt<LIMBS> {
+ type Output = Self;
+
+ fn neg_mod(&self, p: &Self) -> Self {
+ debug_assert!(self < p);
+ self.neg_mod(p)
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::U256;
+
+ #[test]
+ fn neg_mod_random() {
+ let x =
+ U256::from_be_hex("8d16e171674b4e6d8529edba4593802bf30b8cb161dd30aa8e550d41380007c2");
+ let p =
+ U256::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
+
+ let actual = x.neg_mod(&p);
+ let expected =
+ U256::from_be_hex("056c53337d72b9d666f86c9256ce5f08cabc1b63b207864ce0d6ecf010e2d9f3");
+
+ assert_eq!(expected, actual);
+ }
+
+ #[test]
+ fn neg_mod_zero() {
+ let x =
+ U256::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
+ let p =
+ U256::from_be_hex("928334a4e4be0843ec225a4c9c61df34bdc7a81513e4b6f76f2bfa3148e2e1b5");
+
+ let actual = x.neg_mod(&p);
+ let expected =
+ U256::from_be_hex("0000000000000000000000000000000000000000000000000000000000000000");
+
+ assert_eq!(expected, actual);
+ }
+}