summary refs log tree commit diff
path: root/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2019-08-09 17:08:48 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2019-08-09 17:08:48 +0700
commitac5ab976832d3d6e107502acd318f9fe3b12e547 (patch)
treed594b09d80bd74705d23e011a43f8fc8fda87904 /crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
parentASN.1 updates from bc-java (diff)
downloadBouncyCastle.NET-ed25519-ac5ab976832d3d6e107502acd318f9fe3b12e547.tar.xz
Add non-constant-time variant to ECLookupTable
Diffstat (limited to 'crypto/src/math/ec/custom/sec/SecP256K1Curve.cs')
-rw-r--r--crypto/src/math/ec/custom/sec/SecP256K1Curve.cs28
1 files changed, 24 insertions, 4 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
index b3a5dd646..bdda5a1b9 100644
--- a/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
+++ b/crypto/src/math/ec/custom/sec/SecP256K1Curve.cs
@@ -13,6 +13,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
 
         private const int SECP256K1_DEFAULT_COORDS = COORD_JACOBIAN;
         private const int SECP256K1_FE_INTS = 8;
+        private static readonly ECFieldElement[] SECP256K1_AFFINE_ZS = new ECFieldElement[] { new SecP256K1FieldElement(BigInteger.One) };
 
         protected readonly SecP256K1Point m_infinity;
 
@@ -91,7 +92,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         }
 
         private class SecP256K1LookupTable
-            : ECLookupTable
+            : AbstractECLookupTable
         {
             private readonly SecP256K1Curve m_outer;
             private readonly uint[] m_table;
@@ -104,12 +105,12 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                 this.m_size = size;
             }
 
-            public virtual int Size
+            public override int Size
             {
                 get { return m_size; }
             }
 
-            public virtual ECPoint Lookup(int index)
+            public override ECPoint Lookup(int index)
             {
                 uint[] x = Nat256.Create(), y = Nat256.Create();
                 int pos = 0;
@@ -127,7 +128,26 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
                     pos += (SECP256K1_FE_INTS * 2);
                 }
 
-                return m_outer.CreateRawPoint(new SecP256K1FieldElement(x), new SecP256K1FieldElement(y), false);
+                return CreatePoint(x, y);
+            }
+
+            public override ECPoint LookupVar(int index)
+            {
+                uint[] x = Nat256.Create(), y = Nat256.Create();
+                int pos = index * SECP256K1_FE_INTS * 2;
+
+                for (int j = 0; j < SECP256K1_FE_INTS; ++j)
+                {
+                    x[j] = m_table[pos + j];
+                    y[j] = m_table[pos + SECP256K1_FE_INTS + j];
+                }
+
+                return CreatePoint(x, y);
+            }
+
+            private ECPoint CreatePoint(uint[] x, uint[] y)
+            {
+                return m_outer.CreateRawPoint(new SecP256K1FieldElement(x), new SecP256K1FieldElement(y), SECP256K1_AFFINE_ZS, false);
             }
         }
     }