summary refs log tree commit diff
path: root/crypto/src/math/ec/endo/EndoUtilities.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/math/ec/endo/EndoUtilities.cs')
-rw-r--r--crypto/src/math/ec/endo/EndoUtilities.cs34
1 files changed, 34 insertions, 0 deletions
diff --git a/crypto/src/math/ec/endo/EndoUtilities.cs b/crypto/src/math/ec/endo/EndoUtilities.cs
new file mode 100644
index 000000000..16916e632
--- /dev/null
+++ b/crypto/src/math/ec/endo/EndoUtilities.cs
@@ -0,0 +1,34 @@
+using System;
+
+using Org.BouncyCastle.Math;
+
+namespace Org.BouncyCastle.Math.EC.Endo
+{
+    public abstract class EndoUtilities
+    {
+        public static BigInteger[] DecomposeScalar(ScalarSplitParameters p, BigInteger k)
+        {
+            int bits = p.Bits;
+            BigInteger b1 = CalculateB(k, p.G1, bits);
+            BigInteger b2 = CalculateB(k, p.G2, bits);
+
+            BigInteger a = k.Subtract((b1.Multiply(p.V1A)).Add(b2.Multiply(p.V2A)));
+            BigInteger b = (b1.Multiply(p.V1B)).Add(b2.Multiply(p.V2B)).Negate();
+
+            return new BigInteger[]{ a, b };
+        }
+
+        private static BigInteger CalculateB(BigInteger k, BigInteger g, int t)
+        {
+            bool negative = (g.SignValue < 0);
+            BigInteger b = k.Multiply(g.Abs());
+            bool extra = b.TestBit(t - 1);
+            b = b.ShiftRight(t);
+            if (extra)
+            {
+                b = b.Add(BigInteger.One);
+            }
+            return negative ? b.Negate() : b;
+        }
+    }
+}