Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ECPointPerformanceTest.java « test « ec « math « bouncycastle « org « java « test « src « core - gitlab.com/quite/humla-spongycastle.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6db88a00a859e13d1486cddb3792c7047662f662 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package org.bouncycastle.math.ec.test;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import junit.framework.TestCase;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.x9.ECNamedCurveTable;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.ec.CustomNamedCurves;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
import org.bouncycastle.util.Times;

/**
 * Compares the performance of the the window NAF point multiplication against conventional point
 * multiplication.
 */
public class ECPointPerformanceTest extends TestCase
{
    static final int MILLIS_PER_ROUND = 200;
    static final int MILLIS_WARMUP = 1000;

    static final int MULTS_PER_CHECK = 16;
    static final int NUM_ROUNDS = 10;

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

    private void randMult(String curveName) throws Exception
    {
        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) throws Exception
    {
        ECCurve C = spec.getCurve();
        ECPoint G = (ECPoint)spec.getG();
        BigInteger n = spec.getN();

        SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
        random.setSeed(System.currentTimeMillis());

        System.out.println(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.getCoordinateSystem() != coord)
                {
                    c = C.configure().setCoordinateSystem(coord).create();
                    g = c.importPoint(G);
                }

                double avgRate = randMult(random, g, n);
                String coordName = COORD_NAMES[coord];
                StringBuffer sb = new StringBuffer();
                sb.append("  ");
                sb.append(coordName);
                for (int j = sb.length(); j < 28; ++j)
                {
                    sb.append(' ');
                }
                sb.append(": ");
                sb.append(avgRate);
                sb.append(" mults/sec");
                for (int j = sb.length(); j < 64; ++j)
                {
                    sb.append(' ');
                }
                sb.append('(');
                sb.append(1000.0 / avgRate);
                sb.append(" millis/mult)");
                System.out.println(sb.toString());
            }
        }
    }

    private double randMult(SecureRandom random, ECPoint g, BigInteger n) throws Exception
    {
        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;

        {
            long startTime = Times.nanoTime();
            long goalTime = startTime + 1000000L * MILLIS_WARMUP;

            do
            {
                BigInteger k = ks[ki];
                p = g.multiply(k);
                if ((ki & 1) != 0)
                {
                    g = p;
                }
                if (++ki == ks.length)
                {
                    ki = 0;
                }
            }
            while (Times.nanoTime() < goalTime);
        }

        double minRate = Double.MAX_VALUE, maxRate = Double.MIN_VALUE, totalRate = 0.0;

        for (int i = 1; i <= NUM_ROUNDS; i++)
        {
            long startTime = Times.nanoTime();
            long goalTime = startTime + 1000000L * MILLIS_PER_ROUND;
            long count = 0, endTime;

            do
            {
                ++count;

                for (int j = 0; j < MULTS_PER_CHECK; ++j)
                {
                    BigInteger k = ks[ki];
                    p = g.multiply(k);
                    if ((ki & 1) != 0)
                    {
                        g = p;
                    }
                    if (++ki == ks.length)
                    {
                        ki = 0;
                    }
                }

                endTime = Times.nanoTime();
            }
            while (endTime < goalTime);

            double roundElapsed = (double)(endTime - startTime);
            double roundRate = count * MULTS_PER_CHECK * 1000000000L / roundElapsed;

            minRate = Math.min(minRate, roundRate);
            maxRate = Math.max(maxRate, roundRate);
            totalRate += roundRate;
        }

        return (totalRate - minRate - maxRate) / (NUM_ROUNDS - 2);
    }

    public void testMultiply() throws Exception
    {
        SortedSet names = new TreeSet(enumToList(ECNamedCurveTable.getNames()));
        names.addAll(enumToList(CustomNamedCurves.getNames()));

        Set oids = new HashSet();

        Iterator it = names.iterator();
        while (it.hasNext())
        {
            String name = (String)it.next();
            ASN1ObjectIdentifier oid = ECNamedCurveTable.getOID(name);
            if (oid == null)
            {
                oid = CustomNamedCurves.getOID(name);
            }
            if (oid != null && !oids.add(oid))
            {
                continue;
            }

            randMult(name);
        }
    }

    private List enumToList(Enumeration en)
    {
        List rv = new ArrayList();

        while (en.hasMoreElements())
        {
            rv.add(en.nextElement());
        }

        return rv;
    }
}