summary refs log tree commit diff
path: root/crypto/test/src/math/ec/test/ECPointPerformanceTest.cs
blob: 69d6823e196f26da1cd1599638694692ece12707 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections;
using System.Text;

using NUnit.Framework;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Sec;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.EC;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;
using Org.BouncyCastle.Utilities.Date;

namespace Org.BouncyCastle.Math.EC.Tests
{
    /**
    * Compares the performance of the the window NAF point multiplication against
    * conventional point multiplication.
    */
    [TestFixture, Explicit]
    public class ECPointPerformanceTest
    {
        public const int PRE_ROUNDS = 100;
        public const int NUM_ROUNDS = 1000;

        private static string[] COORD_NAMES = new string[]{ "AFFINE", "HOMOGENEOUS", "JACOBIAN", "JACOBIAN-CHUDNOVSKY",
            "JACOBIAN-MODIFIED", "LAMBDA-AFFINE", "LAMBDA-PROJECTIVE", "SKEWED" };

        private void RandMult(string curveName)
        {
            X9ECParameters spec = ECNamedCurveTable.GetByName(curveName);
            if (spec != null)
            {
                RandMult(curveName, spec);
            }

            spec = CustomNamedCurves.GetByName(curveName);
            if (spec != null)
            {
                RandMult(curveName + " (custom)", spec);
            }
        }

        private void RandMult(string label, X9ECParameters spec)
        {
            ECCurve C = spec.Curve;
            ECPoint G = (ECPoint)spec.G;
            BigInteger n = spec.N;

            SecureRandom random = new SecureRandom();
            random.SetSeed(DateTimeUtilities.CurrentUnixMs());

            Console.WriteLine(label);

            int[] coords = ECCurve.GetAllCoordinateSystems();
            for (int i = 0; i < coords.Length; ++i)
            {
                int coord = coords[i];
                if (C.SupportsCoordinateSystem(coord))
                {
                    ECCurve c = C;
                    ECPoint g = G;

                    if (c.CoordinateSystem != coord)
                    {
                        c = C.Configure().SetCoordinateSystem(coord).Create();
                        g = c.ImportPoint(G);
                    }

                    double avgDuration = RandMult(random, g, n);
                    string coordName = COORD_NAMES[coord];
                    StringBuilder sb = new StringBuilder();
                    sb.Append("  ");
                    sb.Append(coordName);
                    for (int j = coordName.Length; j < 30; ++j)
                    {
                        sb.Append(' ');
                    }
                    sb.Append(": ");
                    sb.Append(avgDuration);
                    sb.Append("ms");
                    Console.WriteLine(sb.ToString());
                }
            }
        }

        private double RandMult(SecureRandom random, ECPoint g, BigInteger n)
        {
            BigInteger[] ks = new BigInteger[128];
            for (int i = 0; i < ks.Length; ++i)
            {
                ks[i] = new BigInteger(n.BitLength - 1, random);
            }

            int ki = 0;
            ECPoint p = g;
            for (int i = 1; i <= PRE_ROUNDS; i++)
            {
                BigInteger k = ks[ki];
                p = g.Multiply(k);
                if (++ki == ks.Length)
                {
                    ki = 0;
                    g = p;
                }
            }
            long startTime = DateTimeUtilities.CurrentUnixMs();
            for (int i = 1; i <= NUM_ROUNDS; i++)
            {
                BigInteger k = ks[ki];
                p = g.Multiply(k);
                if (++ki == ks.Length)
                {
                    ki = 0;
                    g = p;
                }
            }
            long endTime = DateTimeUtilities.CurrentUnixMs();

            return (double)(endTime - startTime) / NUM_ROUNDS;
        }

        [Test]
        public void TestMultiply()
        {
            ArrayList nameList = new ArrayList();
            CollectionUtilities.AddRange(nameList, ECNamedCurveTable.Names);
            string[] names = (string[])nameList.ToArray(typeof(string));
            Array.Sort(names);
            ISet oids = new HashSet();
            foreach (string name in names)
            {
                DerObjectIdentifier oid = ECNamedCurveTable.GetOid(name);
                if (!oids.Contains(oid))
                {
                    oids.Add(oid);
                    RandMult(name);
                }
            }
        }

        public static void Main(string[] args)
        {
            new ECPointPerformanceTest().TestMultiply();
        }
    }
}