summary refs log tree commit diff
path: root/crypto/src/math/raw
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-20 17:41:10 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-20 17:41:10 +0700
commitcd1564f37a3af1a7abc5092eb55040d337e79ae2 (patch)
treee75c0812208876eccfa3f15cf8fda10ecf3fb4e8 /crypto/src/math/raw
parentMicrosoft.NET.Test.Sdk 17.3.2 (diff)
downloadBouncyCastle.NET-ed25519-cd1564f37a3af1a7abc5092eb55040d337e79ae2.tar.xz
Add Inverse64 method
Diffstat (limited to 'crypto/src/math/raw')
-rw-r--r--crypto/src/math/raw/Mod.cs17
1 files changed, 16 insertions, 1 deletions
diff --git a/crypto/src/math/raw/Mod.cs b/crypto/src/math/raw/Mod.cs
index ea61bdd83..acbb1d91f 100644
--- a/crypto/src/math/raw/Mod.cs
+++ b/crypto/src/math/raw/Mod.cs
@@ -41,7 +41,7 @@ namespace Org.BouncyCastle.Math.Raw
 
         public static uint Inverse32(uint d)
         {
-            Debug.Assert((d & 1) == 1);
+            Debug.Assert((d & 1U) == 1U);
 
             //int x = d + (((d + 1) & 4) << 1);   // d.x == 1 mod 2**4
             uint x = d;                         // d.x == 1 mod 2**3
@@ -53,6 +53,21 @@ namespace Org.BouncyCastle.Math.Raw
             return x;
         }
 
+        public static ulong Inverse64(ulong d)
+        {
+            Debug.Assert((d & 1UL) == 1UL);
+
+            //ulong x = d + (((d + 1) & 4) << 1);   // d.x == 1 mod 2**4
+            ulong x = d;                            // d.x == 1 mod 2**3
+            x *= 2 - d * x;                         // d.x == 1 mod 2**6
+            x *= 2 - d * x;                         // d.x == 1 mod 2**12
+            x *= 2 - d * x;                         // d.x == 1 mod 2**24
+            x *= 2 - d * x;                         // d.x == 1 mod 2**48
+            x *= 2 - d * x;                         // d.x == 1 mod 2**96
+            Debug.Assert(d * x == 1UL);
+            return x;
+        }
+
         public static uint ModOddInverse(uint[] m, uint[] x, uint[] z)
         {
 #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER