-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABArithmeticFunctionCodeTests.java
More file actions
314 lines (253 loc) · 10.4 KB
/
Copy pathABArithmeticFunctionCodeTests.java
File metadata and controls
314 lines (253 loc) · 10.4 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package org.ciyam.at;
import static org.junit.Assert.*;
import org.ciyam.at.test.ExecutableTest;
import org.ciyam.at.test.TestUtils;
import org.junit.Before;
import org.junit.Test;
/**
* Tests for the 256-bit A/B arithmetic function codes (0x0140 - 0x0147).
* <p>
* A1..A4 / B1..B4 are treated as 256-bit unsigned integers, least significant register first.
* <p>
* These function codes require AT creation version 3, so tests here run under a version 3 header,
* apart from the explicit version 2 fault tests.
*/
public class ABArithmeticFunctionCodeTests extends ExecutableTest {
private static final int A_SOURCE_ADDRESS = 0;
private static final int B_SOURCE_ADDRESS = 4;
private static final int A_RESULT_ADDRESS = 8;
private static final int B_RESULT_ADDRESS = 12;
private static final long ALL_ONES = 0xffffffffffffffffL;
private static final long TOP_BIT = 0x8000000000000000L;
@Before
public void useVersion3Header() {
headerBytes = TestUtils.V3_HEADER_BYTES;
}
@Test
public void testAddAToB() throws ExecutionException {
// B = B + A
executeFunction(FunctionCode.ADD_A_TO_B,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertA("A should be unmodified", 3L, 0L, 0L, 0L);
assertB("B should hold sum", 8L, 0L, 0L, 0L);
}
@Test
public void testAddBToA() throws ExecutionException {
// A = A + B
executeFunction(FunctionCode.ADD_B_TO_A,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertA("A should hold sum", 8L, 0L, 0L, 0L);
assertB("B should be unmodified", 5L, 0L, 0L, 0L);
}
@Test
public void testAddCarriesAcrossAllLimbs() throws ExecutionException {
// B is 2^192 - 1 so adding 1 carries through limbs 1, 2 and 3 into limb 4
executeFunction(FunctionCode.ADD_A_TO_B,
new long[] { 1L, 0L, 0L, 0L },
new long[] { ALL_ONES, ALL_ONES, ALL_ONES, 0L });
assertB("carry should propagate to top limb", 0L, 0L, 0L, 1L);
}
@Test
public void testAddWrapsModulo2To256() throws ExecutionException {
// B is 2^256 - 1 so adding 5 wraps to 4
executeFunction(FunctionCode.ADD_A_TO_B,
new long[] { 5L, 0L, 0L, 0L },
new long[] { ALL_ONES, ALL_ONES, ALL_ONES, ALL_ONES });
assertB("sum should wrap modulo 2^256", 4L, 0L, 0L, 0L);
}
@Test
public void testSubAFromB() throws ExecutionException {
// B = B - A
executeFunction(FunctionCode.SUB_A_FROM_B,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertA("A should be unmodified", 3L, 0L, 0L, 0L);
assertB("B should hold difference", 2L, 0L, 0L, 0L);
}
@Test
public void testSubBFromA() throws ExecutionException {
// A = A - B
executeFunction(FunctionCode.SUB_B_FROM_A,
new long[] { 5L, 0L, 0L, 0L },
new long[] { 3L, 0L, 0L, 0L });
assertA("A should hold difference", 2L, 0L, 0L, 0L);
assertB("B should be unmodified", 3L, 0L, 0L, 0L);
}
@Test
public void testSubBorrowsAcrossAllLimbs() throws ExecutionException {
// B is 2^192 so subtracting 1 borrows through limbs 3, 2 and 1
executeFunction(FunctionCode.SUB_A_FROM_B,
new long[] { 1L, 0L, 0L, 0L },
new long[] { 0L, 0L, 0L, 1L });
assertB("borrow should propagate from top limb", ALL_ONES, ALL_ONES, ALL_ONES, 0L);
}
@Test
public void testSubWrapsModulo2To256() throws ExecutionException {
// 0 - 1 should wrap to 2^256 - 1
executeFunction(FunctionCode.SUB_A_FROM_B,
new long[] { 1L, 0L, 0L, 0L },
new long[] { 0L, 0L, 0L, 0L });
assertB("difference should wrap modulo 2^256", ALL_ONES, ALL_ONES, ALL_ONES, ALL_ONES);
}
@Test
public void testMulAByB() throws ExecutionException {
// B = A * B
executeFunction(FunctionCode.MUL_A_BY_B,
new long[] { 6L, 0L, 0L, 0L },
new long[] { 7L, 0L, 0L, 0L });
assertA("A should be unmodified", 6L, 0L, 0L, 0L);
assertB("B should hold product", 42L, 0L, 0L, 0L);
}
@Test
public void testMulBByA() throws ExecutionException {
// A = A * B
executeFunction(FunctionCode.MUL_B_BY_A,
new long[] { 6L, 0L, 0L, 0L },
new long[] { 7L, 0L, 0L, 0L });
assertA("A should hold product", 42L, 0L, 0L, 0L);
assertB("B should be unmodified", 7L, 0L, 0L, 0L);
}
@Test
public void testMulCarriesAcrossLimbs() throws ExecutionException {
// 2^64 * 2^64 = 2^128, i.e. limb 3
executeFunction(FunctionCode.MUL_A_BY_B,
new long[] { 0L, 1L, 0L, 0L },
new long[] { 0L, 1L, 0L, 0L });
assertB("product should reach limb 3", 0L, 0L, 1L, 0L);
}
@Test
public void testMulWrapsModulo2To256() throws ExecutionException {
// 3 * 2^255 = 2^256 + 2^255, and only the low 256 bits (2^255) are kept
executeFunction(FunctionCode.MUL_A_BY_B,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 0L, 0L, 0L, TOP_BIT });
assertB("product should wrap modulo 2^256", 0L, 0L, 0L, TOP_BIT);
}
@Test
public void testDivAByB() throws ExecutionException {
// B = A / B, with unsigned truncating division
executeFunction(FunctionCode.DIV_A_BY_B,
new long[] { 7L, 0L, 0L, 0L },
new long[] { 2L, 0L, 0L, 0L });
assertA("A should be unmodified", 7L, 0L, 0L, 0L);
assertB("B should hold truncated quotient", 3L, 0L, 0L, 0L);
}
@Test
public void testDivBByA() throws ExecutionException {
// A = B / A, with unsigned truncating division
executeFunction(FunctionCode.DIV_B_BY_A,
new long[] { 2L, 0L, 0L, 0L },
new long[] { 7L, 0L, 0L, 0L });
assertA("A should hold truncated quotient", 3L, 0L, 0L, 0L);
assertB("B should be unmodified", 7L, 0L, 0L, 0L);
}
@Test
public void testDivQuotientSpansLimbs() throws ExecutionException {
// 2^192 / 2 = 2^191, i.e. top bit of limb 3
executeFunction(FunctionCode.DIV_A_BY_B,
new long[] { 0L, 0L, 0L, 1L },
new long[] { 2L, 0L, 0L, 0L });
assertB("quotient should span limb boundary", 0L, 0L, TOP_BIT, 0L);
}
@Test
public void testDivIsUnsigned() throws ExecutionException {
// A is 2^255, which is negative if interpreted as signed, so unsigned division must give 2^254
executeFunction(FunctionCode.DIV_A_BY_B,
new long[] { 0L, 0L, 0L, TOP_BIT },
new long[] { 2L, 0L, 0L, 0L });
assertB("division should be unsigned", 0L, 0L, 0L, 0x4000000000000000L);
}
@Test
public void testDivAByZeroBIsFatalError() throws ExecutionException {
executeFunction(FunctionCode.DIV_A_BY_B,
new long[] { 7L, 0L, 0L, 0L },
new long[] { 0L, 0L, 0L, 0L });
assertTrue(state.isFinished());
assertTrue(state.hadFatalError());
}
@Test
public void testDivBByZeroAIsFatalError() throws ExecutionException {
executeFunction(FunctionCode.DIV_B_BY_A,
new long[] { 0L, 0L, 0L, 0L },
new long[] { 7L, 0L, 0L, 0L });
assertTrue(state.isFinished());
assertTrue(state.hadFatalError());
}
@Test
public void testAddAToBIsFatalErrorForVersion2() throws ExecutionException {
// Version 2 ATs must treat 0x0140 like an unknown function code
headerBytes = TestUtils.HEADER_BYTES;
executeFunction(FunctionCode.ADD_A_TO_B,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertTrue(state.isFinished());
assertTrue(state.hadFatalError());
// Execution must have faulted before reaching GET_A_DAT / GET_B_DAT, so result addresses remain zero
assertEquals(0L, getData(A_RESULT_ADDRESS));
assertEquals(0L, getData(B_RESULT_ADDRESS));
}
@Test
public void testAddBToAIsFatalErrorForVersion2() throws ExecutionException {
// Version 2 ATs must treat 0x0141 like an unknown function code
headerBytes = TestUtils.HEADER_BYTES;
executeFunction(FunctionCode.ADD_B_TO_A,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertTrue(state.isFinished());
assertTrue(state.hadFatalError());
// Execution must have faulted before reaching GET_A_DAT / GET_B_DAT, so result addresses remain zero
assertEquals(0L, getData(A_RESULT_ADDRESS));
assertEquals(0L, getData(B_RESULT_ADDRESS));
}
@Test
public void testAllArithmeticFunctionCodesAreFatalErrorForVersion2() throws ExecutionException {
FunctionCode[] functionCodes = new FunctionCode[] {
FunctionCode.ADD_A_TO_B, FunctionCode.ADD_B_TO_A,
FunctionCode.SUB_A_FROM_B, FunctionCode.SUB_B_FROM_A,
FunctionCode.MUL_A_BY_B, FunctionCode.MUL_B_BY_A,
FunctionCode.DIV_A_BY_B, FunctionCode.DIV_B_BY_A
};
for (FunctionCode functionCode : functionCodes) {
// Fresh buffers/API for each function code
beforeTest();
headerBytes = TestUtils.HEADER_BYTES;
executeFunction(functionCode,
new long[] { 3L, 0L, 0L, 0L },
new long[] { 5L, 0L, 0L, 0L });
assertTrue(functionCode.name() + " should be fatal error under version 2", state.hadFatalError());
}
}
/** Loads A and B registers with passed values, executes passed function, then saves A and B back into the data segment. */
private void executeFunction(FunctionCode functionCode, long[] aValues, long[] bValues) {
// A register source values
for (long aValue : aValues)
dataByteBuffer.putLong(aValue);
// B register source values
assertEquals(B_SOURCE_ADDRESS * MachineState.VALUE_SIZE, dataByteBuffer.position());
for (long bValue : bValues)
dataByteBuffer.putLong(bValue);
codeByteBuffer.put(OpCode.EXT_FUN_VAL.value).putShort(FunctionCode.SET_A_DAT.value).putLong(A_SOURCE_ADDRESS);
codeByteBuffer.put(OpCode.EXT_FUN_VAL.value).putShort(FunctionCode.SET_B_DAT.value).putLong(B_SOURCE_ADDRESS);
codeByteBuffer.put(OpCode.EXT_FUN.value).putShort(functionCode.value);
codeByteBuffer.put(OpCode.EXT_FUN_VAL.value).putShort(FunctionCode.GET_A_DAT.value).putLong(A_RESULT_ADDRESS);
codeByteBuffer.put(OpCode.EXT_FUN_VAL.value).putShort(FunctionCode.GET_B_DAT.value).putLong(B_RESULT_ADDRESS);
codeByteBuffer.put(OpCode.FIN_IMD.value);
execute(true);
}
private void assertA(String message, long expectedA1, long expectedA2, long expectedA3, long expectedA4) {
assertFalse(state.hadFatalError());
assertEquals(message + " (A1)", expectedA1, getData(A_RESULT_ADDRESS));
assertEquals(message + " (A2)", expectedA2, getData(A_RESULT_ADDRESS + 1));
assertEquals(message + " (A3)", expectedA3, getData(A_RESULT_ADDRESS + 2));
assertEquals(message + " (A4)", expectedA4, getData(A_RESULT_ADDRESS + 3));
}
private void assertB(String message, long expectedB1, long expectedB2, long expectedB3, long expectedB4) {
assertFalse(state.hadFatalError());
assertEquals(message + " (B1)", expectedB1, getData(B_RESULT_ADDRESS));
assertEquals(message + " (B2)", expectedB2, getData(B_RESULT_ADDRESS + 1));
assertEquals(message + " (B3)", expectedB3, getData(B_RESULT_ADDRESS + 2));
assertEquals(message + " (B4)", expectedB4, getData(B_RESULT_ADDRESS + 3));
}
}