summaryrefslogtreecommitdiff
path: root/repackaged/bcprov/src/test/java/com/android/org/bouncycastle/math/ec/test/ECAlgorithmsTest.java
blob: ba944d774470b54c743d21197ec0420ff49edcdb (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
/* GENERATED SOURCE. DO NOT MODIFY. */
package com.android.org.bouncycastle.math.ec.test;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.android.org.bouncycastle.asn1.x9.ECNamedCurveTable;
import com.android.org.bouncycastle.asn1.x9.X9ECParameters;
import com.android.org.bouncycastle.asn1.x9.X9ECPoint;
import com.android.org.bouncycastle.crypto.ec.CustomNamedCurves;
import com.android.org.bouncycastle.math.ec.ECAlgorithms;
import com.android.org.bouncycastle.math.ec.ECCurve;
import com.android.org.bouncycastle.math.ec.ECPoint;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import android.platform.test.annotations.LargeTest;

/**
 * @hide This class is not part of the Android public SDK API
 */
public class ECAlgorithmsTest extends TestCase
{
    private static final int SCALE = 4;
    private static final SecureRandom RND = new SecureRandom();

    public void testSumOfMultiplies()
    {
        X9ECParameters x9 = CustomNamedCurves.getByName("secp256r1");
        assertNotNull(x9);
        doTestSumOfMultiplies(x9);
    }

    // TODO Ideally, mark this test not to run by default
    @LargeTest
    public void testSumOfMultipliesComplete()
    {
        ArrayList x9s = getTestCurves();
        Iterator it = x9s.iterator();
        while (it.hasNext())
        {
            X9ECParameters x9 = (X9ECParameters)it.next();
            doTestSumOfMultiplies(x9);
        }
    }

    public void testSumOfTwoMultiplies()
    {
        X9ECParameters x9 = CustomNamedCurves.getByName("secp256r1");
        assertNotNull(x9);
        doTestSumOfTwoMultiplies(x9);
    }

    // TODO Ideally, mark this test not to run by default
    @LargeTest
    public void testSumOfTwoMultipliesComplete()
    {
        ArrayList x9s = getTestCurves();
        Iterator it = x9s.iterator();
        while (it.hasNext())
        {
            X9ECParameters x9 = (X9ECParameters)it.next();
            doTestSumOfTwoMultiplies(x9);
        }
    }

    private void doTestSumOfMultiplies(X9ECParameters x9)
    {
        ECPoint[] points = new ECPoint[SCALE];
        BigInteger[] scalars = new BigInteger[SCALE];
        for (int i = 0; i < SCALE; ++i)
        {
            points[i] = getRandomPoint(x9);
            scalars[i] = getRandomScalar(x9);
        }

        ECPoint u = x9.getCurve().getInfinity();
        for (int i = 0; i < SCALE; ++i)
        {
            u = u.add(points[i].multiply(scalars[i]));

            ECPoint v = ECAlgorithms.sumOfMultiplies(copyPoints(points, i + 1), copyScalars(scalars, i + 1));

            ECPoint[] results = new ECPoint[]{ u, v };
            x9.getCurve().normalizeAll(results);

            assertPointsEqual("ECAlgorithms.sumOfMultiplies is incorrect", results[0], results[1]);
        }
    }

    private void doTestSumOfTwoMultiplies(X9ECParameters x9)
    {
        ECPoint p = getRandomPoint(x9);
        BigInteger a = getRandomScalar(x9);

        for (int i = 0; i < SCALE; ++i)
        {
            ECPoint q = getRandomPoint(x9);
            BigInteger b = getRandomScalar(x9);
            
            ECPoint u = p.multiply(a).add(q.multiply(b));
            ECPoint v = ECAlgorithms.shamirsTrick(p, a, q, b);
            ECPoint w = ECAlgorithms.sumOfTwoMultiplies(p, a, q, b);

            ECPoint[] results = new ECPoint[]{ u, v, w };
            x9.getCurve().normalizeAll(results);

            assertPointsEqual("ECAlgorithms.shamirsTrick is incorrect", results[0], results[1]);
            assertPointsEqual("ECAlgorithms.sumOfTwoMultiplies is incorrect", results[0], results[2]);

            p = q;
            a = b;
        }
    }

    private void assertPointsEqual(String message, ECPoint a, ECPoint b)
    {
        assertEquals(message, a, b);
    }

    private ECPoint[] copyPoints(ECPoint[] ps, int len)
    {
        ECPoint[] result = new ECPoint[len];
        System.arraycopy(ps, 0, result, 0, len);
        return result;
    }

    private BigInteger[] copyScalars(BigInteger[] ks, int len)
    {
        BigInteger[] result = new BigInteger[len];
        System.arraycopy(ks, 0, result, 0, len);
        return result;
    }

    private ECPoint getRandomPoint(X9ECParameters x9)
    {
        return x9.getG().multiply(getRandomScalar(x9));
    }

    private BigInteger getRandomScalar(X9ECParameters x9)
    {
        return new BigInteger(x9.getN().bitLength(), RND);
    }

    private ArrayList getTestCurves()
    {
        ArrayList x9s = new ArrayList();
        Set names = new HashSet(AllTests.enumToList(ECNamedCurveTable.getNames()));
        names.addAll(AllTests.enumToList(CustomNamedCurves.getNames()));

        Iterator it = names.iterator();
        while (it.hasNext())
        {
            String name = (String)it.next();

            X9ECParameters x9 = ECNamedCurveTable.getByName(name);
            if (x9 != null)
            {
                addTestCurves(x9s, x9);
            }

            x9 = CustomNamedCurves.getByName(name);
            if (x9 != null)
            {
                addTestCurves(x9s, x9);
            }
        }
        return x9s;
    }

    private void addTestCurves(ArrayList x9s, X9ECParameters x9)
    {
        ECCurve curve = x9.getCurve();

        int[] coords = ECCurve.getAllCoordinateSystems();
        for (int i = 0; i < coords.length; ++i)
        {
            int coord = coords[i];
            if (curve.getCoordinateSystem() == coord)
            {
                x9s.add(x9);
            }
            else if (curve.supportsCoordinateSystem(coord))
            {
                ECCurve c = curve.configure().setCoordinateSystem(coord).create();
                x9s.add(new X9ECParameters(c, new X9ECPoint(c.importPoint(x9.getG()), false), x9.getN(), x9.getH()));
            }
        }
    }

    public static Test suite()
    {
        return new TestSuite(ECAlgorithmsTest.class);
    }

}