Skip to content

Commit 25544c9

Browse files
rflumeic0ns
authored andcommitted
Added JUnit tests for 'ECComputer.dbl' and 'ECComputer.add' (#393)
* Added JUnit tests for methods 'ECComputer.dbl' and 'ECComputer.add'
1 parent 767b688 commit 25544c9

File tree

1 file changed

+219
-0
lines changed

1 file changed

+219
-0
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/**
2+
* TLS-Attacker - A Modular Penetration Testing Framework for TLS
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.tlsattacker.core.crypto.ec;
10+
11+
import static org.junit.Assert.assertTrue;
12+
import java.math.BigInteger;
13+
import org.junit.Before;
14+
import org.junit.Test;
15+
16+
/**
17+
* Test values from for curve P192: http://point-at-infinity.org/ecc/nisttv
18+
* Curve: P192 ------------- k = 1 x =
19+
* 188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012 y =
20+
* 07192B95FFC8DA78631011ED6B24CDD573F977A11E794811
21+
*
22+
* k = 2 x = DAFEBF5828783F2AD35534631588A3F629A70FB16982A888 y =
23+
* DD6BDA0D993DA0FA46B27BBC141B868F59331AFA5C7E93AB
24+
*
25+
* k = 3 x = 76E32A2557599E6EDCD283201FB2B9AADFD0D359CBB263DA y =
26+
* 782C37E372BA4520AA62E0FED121D49EF3B543660CFD05FD
27+
*
28+
* k = 4 x = 35433907297CC378B0015703374729D7A4FE46647084E4BA y =
29+
* A2649984F2135C301EA3ACB0776CD4F125389B311DB3BE32
30+
*
31+
* k = 5 x = 10BB8E9840049B183E078D9C300E1605590118EBDD7FF590 y =
32+
* 31361008476F917BADC9F836E62762BE312B72543CCEAEA1
33+
*/
34+
35+
public class ECComputerTest {
36+
37+
public ECComputerTest() {
38+
}
39+
40+
@Before
41+
public void setUp() {
42+
}
43+
44+
// Testing dbl method with P(x,y)
45+
@Test
46+
public void dblTest() {
47+
// init secp192r1 curve
48+
Curve c = new Curve("secp192r1", new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
49+
new BigInteger("6277101735386680763835789423207666416083908700390324961276"), new BigInteger(
50+
"2455155546008943817740293915197451784769108058161191238065"), 192);
51+
52+
// init ECComputer
53+
ECComputer ecc = new ECComputer(c, null);
54+
55+
// Values for P (= 1P)
56+
BigInteger x1 = new BigInteger("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", 16);
57+
BigInteger y1 = new BigInteger("07192B95FFC8DA78631011ED6B24CDD573F977A11E794811", 16);
58+
Point p1 = new Point(x1, y1);
59+
60+
// Values for 2P (= P doubled)
61+
BigInteger x2 = new BigInteger("DAFEBF5828783F2AD35534631588A3F629A70FB16982A888", 16);
62+
BigInteger y2 = new BigInteger("DD6BDA0D993DA0FA46B27BBC141B868F59331AFA5C7E93AB", 16);
63+
Point p2 = new Point(x2, y2);
64+
65+
// double P with method ECCompuer.dbl
66+
Point p1_dbl = new Point();
67+
try {
68+
p1_dbl = ecc.dbl(p1);
69+
} catch (DivisionException e) {
70+
System.out.println("DivisionException thrown in ECComputerTest.dblTest");
71+
assertTrue(false);
72+
}
73+
assertTrue(p1_dbl.equals(p2));
74+
}
75+
76+
// Testing dbl method with Infinitycheck
77+
@Test
78+
public void dblInfTest() {
79+
// init secp192r1 curve
80+
Curve c = new Curve("secp192r1", new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
81+
new BigInteger("6277101735386680763835789423207666416083908700390324961276"), new BigInteger(
82+
"2455155546008943817740293915197451784769108058161191238065"), 192);
83+
84+
// init ECComputer
85+
ECComputer ecc = new ECComputer(c, null);
86+
87+
// Values for P (= 1P)
88+
BigInteger x1 = new BigInteger("188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", 16);
89+
BigInteger y1 = new BigInteger("07192B95FFC8DA78631011ED6B24CDD573F977A11E794811", 16);
90+
Point p1 = new Point(x1, y1);
91+
92+
System.out.println("P set: \n" + p1.toString());
93+
94+
// set p1 to infinity
95+
p1.setInfinity(true);
96+
97+
// double P with method ECCompuer.dbl with infinity check
98+
Point p1_dbl = new Point();
99+
try {
100+
p1_dbl = ecc.dbl(p1, true);
101+
} catch (DivisionException e) {
102+
System.out.println("DivisionException thrown in ECComputerTest.dblInfTest");
103+
assertTrue(false);
104+
}
105+
// Print Values
106+
assertTrue(p1_dbl.equals(p1));
107+
108+
// Testing add method with P(x,0)
109+
p1.setY(new BigInteger("0", 10));
110+
111+
try {
112+
p1_dbl = ecc.dbl(p1, true); // double
113+
} catch (DivisionException e) {
114+
System.out.println("DivisionException thrown in ECComputerTest.dblInfTest");
115+
assertTrue(false);
116+
}
117+
// Print Values
118+
assertTrue(p1_dbl.isInfinity());
119+
120+
// if dblTest() passed and this test passed until here, method
121+
// ECComputer.dbl(p, true) works properly, as
122+
// ECComputer.dbl(p) is called and was already tested.
123+
}
124+
125+
// Testing add method with 2P + 2P =? 5P
126+
@Test
127+
public void addTest() {
128+
// init secp192r1 curve
129+
Curve c = new Curve("secp192r1", new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
130+
new BigInteger("6277101735386680763835789423207666416083908700390324961276"), new BigInteger(
131+
"2455155546008943817740293915197451784769108058161191238065"), 192);
132+
133+
// init ECComputer
134+
ECComputer ecc = new ECComputer(c, null);
135+
136+
// Values for 2P
137+
BigInteger x2 = new BigInteger("DAFEBF5828783F2AD35534631588A3F629A70FB16982A888", 16);
138+
BigInteger y2 = new BigInteger("DD6BDA0D993DA0FA46B27BBC141B868F59331AFA5C7E93AB", 16);
139+
Point p2 = new Point(x2, y2);
140+
141+
// Values for 3P
142+
BigInteger x3 = new BigInteger("76E32A2557599E6EDCD283201FB2B9AADFD0D359CBB263DA", 16);
143+
BigInteger y3 = new BigInteger("782C37E372BA4520AA62E0FED121D49EF3B543660CFD05FD", 16);
144+
Point p3 = new Point(x3, y3);
145+
146+
// Values for 5P
147+
BigInteger x5 = new BigInteger("10BB8E9840049B183E078D9C300E1605590118EBDD7FF590", 16);
148+
BigInteger y5 = new BigInteger("31361008476F917BADC9F836E62762BE312B72543CCEAEA1", 16);
149+
Point p5 = new Point(x5, y5);
150+
151+
// calc 2P + 3P with ECComputer.add
152+
Point p5_add = new Point();
153+
try {
154+
p5_add = ecc.add(p2, p3);
155+
} catch (DivisionException e) {
156+
System.out.println("DivisionException thrown in ECComputerTest.addTest");
157+
assertTrue(false);
158+
}
159+
assertTrue(p5_add.equals(p5));
160+
}
161+
162+
// Testing add method with Infinitycheck
163+
@Test
164+
public void addInfTest() {
165+
// init secp192r1 curve
166+
Curve c = new Curve("secp192r1", new BigInteger("6277101735386680763835789423207666416083908700390324961279"),
167+
new BigInteger("6277101735386680763835789423207666416083908700390324961276"), new BigInteger(
168+
"2455155546008943817740293915197451784769108058161191238065"), 192);
169+
170+
// init ECComputer
171+
ECComputer ecc = new ECComputer(c, null);
172+
173+
// calc 2P + 3P with ECComputer.add
174+
Point p5_add = new Point();
175+
// Values for 2P
176+
BigInteger x2 = new BigInteger("DAFEBF5828783F2AD35534631588A3F629A70FB16982A888", 16);
177+
BigInteger y2 = new BigInteger("DD6BDA0D993DA0FA46B27BBC141B868F59331AFA5C7E93AB", 16);
178+
Point p2 = new Point(x2, y2);
179+
180+
// Values for 3P
181+
BigInteger x3 = new BigInteger("76E32A2557599E6EDCD283201FB2B9AADFD0D359CBB263DA", 16);
182+
BigInteger y3 = new BigInteger("782C37E372BA4520AA62E0FED121D49EF3B543660CFD05FD", 16);
183+
Point p3 = new Point(x3, y3);
184+
185+
// instanciate Point object
186+
Point p5 = new Point();
187+
188+
try {
189+
// test with p2 = infinity
190+
assertTrue(ecc.add(null, p3, true).equals(p3));
191+
p2.setInfinity(true);
192+
assertTrue(ecc.add(p2, p3, true).equals(p3));
193+
194+
// reset p2
195+
p2.setInfinity(false);
196+
197+
// test with p3 = infinity
198+
assertTrue(ecc.add(p2, null, true).equals(p2));
199+
p3.setInfinity(true);
200+
assertTrue(ecc.add(p2, p3, true).equals(p2));
201+
202+
// reset p3
203+
p3.setInfinity(false);
204+
205+
// test equal coordinate behavour
206+
// 2P + 2P =? 4P (doubled)
207+
assertTrue(ecc.add(p2, p2, true).equals(ecc.dbl(p2, true)));
208+
// 2P + 3P =? infinity if x_p2 = x_p3
209+
p2.setX(p3.getX());
210+
assertTrue(ecc.add(p2, p3, true).isInfinity());
211+
} catch (DivisionException e) {
212+
System.out.println("DivisionException thrown in ECComputerTest.addInfTest");
213+
assertTrue(false);
214+
}
215+
// if addTest() passed and this test passed until here, method
216+
// ECComputer.add(p, q, true) works properly, as
217+
// ECComputer.add(p, q) is called and was already tested.
218+
}
219+
}

0 commit comments

Comments
 (0)