summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorOren Novotny <oren@novotny.org>2018-07-19 20:41:28 -0400
committerOren Novotny <oren@novotny.org>2018-07-19 20:41:28 -0400
commitad29383ed7a39f6d51779144a0dcf40c391981be (patch)
tree89ef3f2c8d1e40980f09fb80ee3cfcf2936c5cda /crypto
parentMerge from master (diff)
downloadBouncyCastle.NET-ed25519-ad29383ed7a39f6d51779144a0dcf40c391981be.tar.xz
Fix Synchronized for modern frameworks
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/math/ec/rfc8032/Ed25519.cs131
-rw-r--r--crypto/src/math/ec/rfc8032/Ed448.cs99
2 files changed, 118 insertions, 112 deletions
diff --git a/crypto/src/math/ec/rfc8032/Ed25519.cs b/crypto/src/math/ec/rfc8032/Ed25519.cs
index ff4587cb2..747b5b724 100644
--- a/crypto/src/math/ec/rfc8032/Ed25519.cs
+++ b/crypto/src/math/ec/rfc8032/Ed25519.cs
@@ -514,97 +514,100 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
             X25519Field.Zero(p.t);
         }
 
-        [MethodImpl(MethodImplOptions.Synchronized)]
+        //[MethodImpl(MethodImplOptions.Synchronized)]
         public static void Precompute()
         {
-            if (precompBase != null)
+            lock (typeof(Ed25519))
             {
-                return;
-            }
-
-            // Precomputed table for the base point in verification ladder
-            {
-                PointExt b = new PointExt();
-                X25519Field.Copy(B_x, 0, b.x, 0);
-                X25519Field.Copy(B_y, 0, b.y, 0);
-                PointExtendXY(b);
-
-                precompBaseTable = PointPrecompVar(b, 1 << (WnafWidthBase - 2));
-            }
+                if (precompBase != null)
+                {
+                    return;
+                }
 
-            PointAccum p = new PointAccum();
-            X25519Field.Copy(B_x, 0, p.x, 0);
-            X25519Field.Copy(B_y, 0, p.y, 0);
-            PointExtendXY(p);
+                // Precomputed table for the base point in verification ladder
+                {
+                    PointExt b = new PointExt();
+                    X25519Field.Copy(B_x, 0, b.x, 0);
+                    X25519Field.Copy(B_y, 0, b.y, 0);
+                    PointExtendXY(b);
 
-            precompBase = new int[PrecompBlocks * PrecompPoints * 3 * X25519Field.Size];
+                    precompBaseTable = PointPrecompVar(b, 1 << (WnafWidthBase - 2));
+                }
 
-            int off = 0;
-            for (int b = 0; b < PrecompBlocks; ++b)
-            {
-                PointExt[] ds = new PointExt[PrecompTeeth];
+                PointAccum p = new PointAccum();
+                X25519Field.Copy(B_x, 0, p.x, 0);
+                X25519Field.Copy(B_y, 0, p.y, 0);
+                PointExtendXY(p);
 
-                PointExt sum = new PointExt();
-                PointSetNeutral(sum);
+                precompBase = new int[PrecompBlocks * PrecompPoints * 3 * X25519Field.Size];
 
-                for (int t = 0; t < PrecompTeeth; ++t)
+                int off = 0;
+                for (int b = 0; b < PrecompBlocks; ++b)
                 {
-                    PointExt q = PointCopy(p);
-                    PointAddVar(true, sum, q, sum);
-                    PointDouble(p);
+                    PointExt[] ds = new PointExt[PrecompTeeth];
 
-                    ds[t] = PointCopy(p);
+                    PointExt sum = new PointExt();
+                    PointSetNeutral(sum);
 
-                    for (int s = 1; s < PrecompSpacing; ++s)
+                    for (int t = 0; t < PrecompTeeth; ++t)
                     {
+                        PointExt q = PointCopy(p);
+                        PointAddVar(true, sum, q, sum);
                         PointDouble(p);
+
+                        ds[t] = PointCopy(p);
+
+                        for (int s = 1; s < PrecompSpacing; ++s)
+                        {
+                            PointDouble(p);
+                        }
                     }
-                }
 
-                PointExt[] points = new PointExt[PrecompPoints];
-                int k = 0;
-                points[k++] = sum;
+                    PointExt[] points = new PointExt[PrecompPoints];
+                    int k = 0;
+                    points[k++] = sum;
 
-                for (int t = 0; t < (PrecompTeeth - 1); ++t)
-                {
-                    int size = 1 << t;
-                    for (int j = 0; j < size; ++j, ++k)
+                    for (int t = 0; t < (PrecompTeeth - 1); ++t)
                     {
-                        PointAddVar(false, points[k - size], ds[t], points[k] = new PointExt());
+                        int size = 1 << t;
+                        for (int j = 0; j < size; ++j, ++k)
+                        {
+                            PointAddVar(false, points[k - size], ds[t], points[k] = new PointExt());
+                        }
                     }
-                }
 
-                Debug.Assert(k == PrecompPoints);
+                    Debug.Assert(k == PrecompPoints);
 
-                for (int i = 0; i < PrecompPoints; ++i)
-                {
-                    PointExt q = points[i];
+                    for (int i = 0; i < PrecompPoints; ++i)
+                    {
+                        PointExt q = points[i];
 
-                    int[] x = X25519Field.Create();
-                    int[] y = X25519Field.Create();
+                        int[] x = X25519Field.Create();
+                        int[] y = X25519Field.Create();
 
-                    X25519Field.Add(q.z, q.z, x);
-                    // TODO[ed25519] Batch inversion
-                    X25519Field.Inv(x, y);
-                    X25519Field.Mul(q.x, y, x);
-                    X25519Field.Mul(q.y, y, y);
+                        X25519Field.Add(q.z, q.z, x);
+                        // TODO[ed25519] Batch inversion
+                        X25519Field.Inv(x, y);
+                        X25519Field.Mul(q.x, y, x);
+                        X25519Field.Mul(q.y, y, y);
 
-                    PointPrecomp r = new PointPrecomp();
-                    X25519Field.Apm(y, x, r.ypx_h, r.ymx_h);
-                    X25519Field.Mul(x, y, r.xyd);
-                    X25519Field.Mul(r.xyd, C_d4, r.xyd);
+                        PointPrecomp r = new PointPrecomp();
+                        X25519Field.Apm(y, x, r.ypx_h, r.ymx_h);
+                        X25519Field.Mul(x, y, r.xyd);
+                        X25519Field.Mul(r.xyd, C_d4, r.xyd);
 
-                    X25519Field.Normalize(r.ypx_h);
-                    X25519Field.Normalize(r.ymx_h);
-                    //X25519Field.Normalize(r.xyd);
+                        X25519Field.Normalize(r.ypx_h);
+                        X25519Field.Normalize(r.ymx_h);
+                        //X25519Field.Normalize(r.xyd);
 
-                    X25519Field.Copy(r.ypx_h, 0, precompBase, off); off += X25519Field.Size;
-                    X25519Field.Copy(r.ymx_h, 0, precompBase, off); off += X25519Field.Size;
-                    X25519Field.Copy(r.xyd, 0, precompBase, off);   off += X25519Field.Size;
+                        X25519Field.Copy(r.ypx_h, 0, precompBase, off); off += X25519Field.Size;
+                        X25519Field.Copy(r.ymx_h, 0, precompBase, off); off += X25519Field.Size;
+                        X25519Field.Copy(r.xyd, 0, precompBase, off); off += X25519Field.Size;
+                    }
                 }
-            }
 
-            Debug.Assert(off == precompBase.Length);
+                Debug.Assert(off == precompBase.Length);
+            }
         }
 
         private static void PruneScalar(byte[] n, int nOff, byte[] r)
diff --git a/crypto/src/math/ec/rfc8032/Ed448.cs b/crypto/src/math/ec/rfc8032/Ed448.cs
index 52c215160..58f583ddf 100644
--- a/crypto/src/math/ec/rfc8032/Ed448.cs
+++ b/crypto/src/math/ec/rfc8032/Ed448.cs
@@ -501,77 +501,80 @@ namespace Org.BouncyCastle.Math.EC.Rfc8032
             X448Field.One(p.z);
         }
 
-        [MethodImpl(MethodImplOptions.Synchronized)]
+        //[MethodImpl(MethodImplOptions.Synchronized)]
         public static void Precompute()
         {
-            if (precompBase != null)
+            lock (typeof(Ed448))
             {
-                return;
-            }
-
-            PointExt p = new PointExt();
-            X448Field.Copy(B_x, 0, p.x, 0);
-            X448Field.Copy(B_y, 0, p.y, 0);
-            PointExtendXY(p);
-
-            precompBaseTable = PointPrecompVar(p, 1 << (WnafWidthBase - 2));
+                if (precompBase != null)
+                {
+                    return;
+                }
 
-            precompBase = new uint[PrecompBlocks * PrecompPoints * 2 * X448Field.Size];
+                PointExt p = new PointExt();
+                X448Field.Copy(B_x, 0, p.x, 0);
+                X448Field.Copy(B_y, 0, p.y, 0);
+                PointExtendXY(p);
 
-            int off = 0;
-            for (int b = 0; b < PrecompBlocks; ++b)
-            {
-                PointExt[] ds = new PointExt[PrecompTeeth];
+                precompBaseTable = PointPrecompVar(p, 1 << (WnafWidthBase - 2));
 
-                PointExt sum = new PointExt();
-                PointSetNeutral(sum);
+                precompBase = new uint[PrecompBlocks * PrecompPoints * 2 * X448Field.Size];
 
-                for (int t = 0; t < PrecompTeeth; ++t)
+                int off = 0;
+                for (int b = 0; b < PrecompBlocks; ++b)
                 {
-                    PointAddVar(true, p, sum);
-                    PointDouble(p);
+                    PointExt[] ds = new PointExt[PrecompTeeth];
 
-                    ds[t] = PointCopy(p);
+                    PointExt sum = new PointExt();
+                    PointSetNeutral(sum);
 
-                    for (int s = 1; s < PrecompSpacing; ++s)
+                    for (int t = 0; t < PrecompTeeth; ++t)
                     {
+                        PointAddVar(true, p, sum);
                         PointDouble(p);
+
+                        ds[t] = PointCopy(p);
+
+                        for (int s = 1; s < PrecompSpacing; ++s)
+                        {
+                            PointDouble(p);
+                        }
                     }
-                }
 
-                PointExt[] points = new PointExt[PrecompPoints];
-                int k = 0;
-                points[k++] = sum;
+                    PointExt[] points = new PointExt[PrecompPoints];
+                    int k = 0;
+                    points[k++] = sum;
 
-                for (int t = 0; t < (PrecompTeeth - 1); ++t)
-                {
-                    int size = 1 << t;
-                    for (int j = 0; j < size; ++j, ++k)
+                    for (int t = 0; t < (PrecompTeeth - 1); ++t)
                     {
-                        points[k] = PointCopy(points[k - size]);
-                        PointAddVar(false, ds[t], points[k]);
+                        int size = 1 << t;
+                        for (int j = 0; j < size; ++j, ++k)
+                        {
+                            points[k] = PointCopy(points[k - size]);
+                            PointAddVar(false, ds[t], points[k]);
+                        }
                     }
-                }
 
-                Debug.Assert(k == PrecompPoints);
+                    Debug.Assert(k == PrecompPoints);
 
-                for (int i = 0; i < PrecompPoints; ++i)
-                {
-                    PointExt q = points[i];
-                    // TODO[ed448] Batch inversion
-                    X448Field.Inv(q.z, q.z);
-                    X448Field.Mul(q.x, q.z, q.x);
-                    X448Field.Mul(q.y, q.z, q.y);
+                    for (int i = 0; i < PrecompPoints; ++i)
+                    {
+                        PointExt q = points[i];
+                        // TODO[ed448] Batch inversion
+                        X448Field.Inv(q.z, q.z);
+                        X448Field.Mul(q.x, q.z, q.x);
+                        X448Field.Mul(q.y, q.z, q.y);
 
-    //                X448Field.Normalize(q.x);
-    //                X448Field.Normalize(q.y);
+                        //                X448Field.Normalize(q.x);
+                        //                X448Field.Normalize(q.y);
 
-                    X448Field.Copy(q.x, 0, precompBase, off);   off += X448Field.Size;
-                    X448Field.Copy(q.y, 0, precompBase, off);   off += X448Field.Size;
+                        X448Field.Copy(q.x, 0, precompBase, off); off += X448Field.Size;
+                        X448Field.Copy(q.y, 0, precompBase, off); off += X448Field.Size;
+                    }
                 }
-            }
 
-            Debug.Assert(off == precompBase.Length);
+                Debug.Assert(off == precompBase.Length);
+            }
         }
 
         private static void PruneScalar(byte[] n, int nOff, byte[] r)