summary refs log tree commit diff
path: root/crypto/src/math/ec/ECCurve.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/ECCurve.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/ECCurve.cs')
-rw-r--r--crypto/src/math/ec/ECCurve.cs58
1 files changed, 49 insertions, 9 deletions
diff --git a/crypto/src/math/ec/ECCurve.cs b/crypto/src/math/ec/ECCurve.cs
index 5b9b39c10..60fbc887a 100644
--- a/crypto/src/math/ec/ECCurve.cs
+++ b/crypto/src/math/ec/ECCurve.cs
@@ -521,7 +521,7 @@ namespace Org.BouncyCastle.Math.EC
         }
 
         private class DefaultLookupTable
-            : ECLookupTable
+            : AbstractECLookupTable
         {
             private readonly ECCurve m_outer;
             private readonly byte[] m_table;
@@ -534,12 +534,12 @@ namespace Org.BouncyCastle.Math.EC
                 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)
             {
                 int FE_BYTES = (m_outer.FieldSize + 7) / 8;
                 byte[] x = new byte[FE_BYTES], y = new byte[FE_BYTES];
@@ -558,6 +558,26 @@ namespace Org.BouncyCastle.Math.EC
                     pos += (FE_BYTES * 2);
                 }
 
+                return CreatePoint(x, y);
+            }
+
+            public override ECPoint LookupVar(int index)
+            {
+                int FE_BYTES = (m_outer.FieldSize + 7) / 8;
+                byte[] x = new byte[FE_BYTES], y = new byte[FE_BYTES];
+                int pos = index * FE_BYTES * 2;
+
+                for (int j = 0; j < FE_BYTES; ++j)
+                {
+                    x[j] = m_table[pos + j];
+                    y[j] = m_table[pos + FE_BYTES + j];
+                }
+
+                return CreatePoint(x, y);
+            }
+
+            private ECPoint CreatePoint(byte[] x, byte[] y)
+            {
                 ECFieldElement X = m_outer.FromBigInteger(new BigInteger(1, x));
                 ECFieldElement Y = m_outer.FromBigInteger(new BigInteger(1, y));
                 return m_outer.CreateRawPoint(X, Y, false);
@@ -1251,7 +1271,7 @@ namespace Org.BouncyCastle.Math.EC
         }
 
         private class DefaultF2mLookupTable
-            : ECLookupTable
+            : AbstractECLookupTable
         {
             private readonly F2mCurve m_outer;
             private readonly long[] m_table;
@@ -1264,16 +1284,13 @@ namespace Org.BouncyCastle.Math.EC
                 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)
             {
-                int m = m_outer.m;
-                int[] ks = m_outer.IsTrinomial() ? new int[]{ m_outer.k1 } : new int[]{ m_outer.k1, m_outer.k2, m_outer.k3 }; 
-
                 int FE_LONGS = (m_outer.m + 63) / 64;
                 long[] x = new long[FE_LONGS], y = new long[FE_LONGS];
                 int pos = 0;
@@ -1291,6 +1308,29 @@ namespace Org.BouncyCastle.Math.EC
                     pos += (FE_LONGS * 2);
                 }
 
+                return CreatePoint(x, y);
+            }
+
+            public override ECPoint LookupVar(int index)
+            {
+                int FE_LONGS = (m_outer.m + 63) / 64;
+                long[] x = new long[FE_LONGS], y = new long[FE_LONGS];
+                int pos = index * FE_LONGS * 2;
+
+                for (int j = 0; j < FE_LONGS; ++j)
+                {
+                    x[j] = m_table[pos + j];
+                    y[j] = m_table[pos + FE_LONGS + j];
+                }
+
+                return CreatePoint(x, y);
+            }
+
+            private ECPoint CreatePoint(long[] x, long[] y)
+            {
+                int m = m_outer.m;
+                int[] ks = m_outer.IsTrinomial() ? new int[] { m_outer.k1 } : new int[] { m_outer.k1, m_outer.k2, m_outer.k3 }; 
+
                 ECFieldElement X = new F2mFieldElement(m, ks, new LongArray(x));
                 ECFieldElement Y = new F2mFieldElement(m, ks, new LongArray(y));
                 return m_outer.CreateRawPoint(X, Y, false);