-
Notifications
You must be signed in to change notification settings - Fork 25
/
BigInteger.java
427 lines (344 loc) · 15.6 KB
/
BigInteger.java
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package s.java.math;
import a.ByteArray;
import i.*;
import s.java.lang.Comparable;
import s.java.lang.String;
import s.java.lang.Number;
import org.aion.avm.RuntimeMethodFeeSchedule;
public class BigInteger extends Number implements Comparable<BigInteger> {
static {
// Shadow classes MUST be loaded during bootstrap phase.
IInstrumentation.attachedThreadInstrumentation.get().bootstrapOnly();
}
public BigInteger(ByteArray val, int off, int len) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_constructor);
setUnderlying(new java.math.BigInteger(val.getUnderlying(), off, len));
}
public BigInteger(ByteArray val) {
this(val, 0, val.length());
}
public BigInteger(int signum, ByteArray magnitude, int off, int len) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_constructor_2);
setUnderlying(new java.math.BigInteger(signum, magnitude.getUnderlying(), off, len));
}
public BigInteger(int signum, ByteArray magnitude) {
this(signum, magnitude, 0, magnitude.length());
}
public BigInteger(String val, int radix) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_constructor_4);
setUnderlying(new java.math.BigInteger(val.getUnderlying(), radix));
}
public BigInteger(String val) {
this(val, 10);
}
public static BigInteger avm_valueOf(long val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_valueOf);
return new BigInteger(java.math.BigInteger.valueOf(val));
}
public static final BigInteger avm_ZERO = new BigInteger(java.math.BigInteger.ZERO, new ConstantToken(ShadowClassConstantId.BigInteger_avm_ZERO));
public static final BigInteger avm_ONE = new BigInteger(java.math.BigInteger.ONE, new ConstantToken(ShadowClassConstantId.BigInteger_avm_ONE));
public static final BigInteger avm_TWO = new BigInteger(java.math.BigInteger.TWO, new ConstantToken(ShadowClassConstantId.BigInteger_avm_TWO));
public static final BigInteger avm_TEN = new BigInteger(java.math.BigInteger.TEN, new ConstantToken(ShadowClassConstantId.BigInteger_avm_TEN));
public BigInteger avm_add(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_add);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.add(val.v));
}
public BigInteger avm_subtract(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_subtract);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.subtract(val.v));
}
public BigInteger avm_multiply(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_multiply);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.multiply(val.v));
}
public BigInteger avm_divide(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_divide);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.divide(val.v));
}
public BigInteger avm_remainder(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_remainder);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.remainder(val.v));
}
public BigInteger avm_sqrt() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_sqrt);
lazyLoad();
return new BigInteger(v.sqrt());
}
public BigInteger avm_gcd(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_gcd);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.gcd(val.v));
}
public BigInteger avm_abs() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_abs);
lazyLoad();
return new BigInteger(v.abs());
}
public BigInteger avm_negate() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_negate);
lazyLoad();
return new BigInteger(v.negate());
}
public int avm_signum() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_signum);
lazyLoad();
return v.signum();
}
public BigInteger avm_mod(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_mod);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.mod(val.v));
}
public BigInteger avm_modPow(BigInteger exponent, BigInteger m) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_modPow);
lazyLoad();
exponent.lazyLoad();
m.lazyLoad();
return new BigInteger(v.modPow(exponent.v, m.v));
}
public BigInteger avm_modInverse(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_modInverse);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.modInverse(val.v));
}
public BigInteger avm_shiftLeft(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_shiftLeft);
verifyBitLength(n);
lazyLoad();
return new BigInteger(v.shiftLeft(n));
}
public BigInteger avm_shiftRight(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_shiftRight);
verifyBitLength(n);
lazyLoad();
return new BigInteger(v.shiftRight(n));
}
public BigInteger avm_and(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_and);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.and(val.v));
}
public BigInteger avm_or(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_or);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.or(val.v));
}
public BigInteger avm_xor(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_xor);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.xor(val.v));
}
public BigInteger avm_not() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_not);
lazyLoad();
return new BigInteger(v.not());
}
public BigInteger avm_andNot(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_andNot);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.andNot(val.v));
}
public boolean avm_testBit(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_testBit);
verifyBitLength(n);
lazyLoad();
return v.testBit(n);
}
public BigInteger avm_setBit(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_setBit);
verifyBitLength(n);
lazyLoad();
return new BigInteger(v.setBit(n));
}
public BigInteger avm_clearBit(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_clearBit);
verifyBitLength(n);
lazyLoad();
return new BigInteger(v.clearBit(n));
}
public BigInteger avm_flipBit(int n) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_flipBit);
verifyBitLength(n);
lazyLoad();
return new BigInteger(v.flipBit(n));
}
public int avm_getLowestSetBit() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_getLowestSetBit);
lazyLoad();
return v.getLowestSetBit();
}
public int avm_bitLength() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_bitLength);
lazyLoad();
return v.bitLength();
}
public int avm_bitCount() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_bitCount);
lazyLoad();
return v.bitCount();
}
public int avm_compareTo(BigInteger val) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_compareTo);
lazyLoad();
val.lazyLoad();
return v.compareTo(val.v);
}
public boolean avm_equals(IObject x) {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_equals);
if (x == this)
return true;
if (!(x instanceof BigInteger))
return false;
BigInteger xInt = (BigInteger) x;
lazyLoad();
xInt.lazyLoad();
return v.equals(xInt.v);
}
public BigInteger avm_min(BigInteger val){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_min);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.min(val.v));
}
public BigInteger avm_max(BigInteger val){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_max);
lazyLoad();
val.lazyLoad();
return new BigInteger(v.max(val.v));
}
public int avm_hashCode() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_hashCode);
lazyLoad();
return v.hashCode();
}
public String avm_toString(int radix){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_toString);
lazyLoad();
return new String(v.toString(radix));
}
public String avm_toString(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_toString_1);
lazyLoad();
return new String(v.toString());
}
public ByteArray avm_toByteArray() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_toByteArray);
lazyLoad();
return new ByteArray(v.toByteArray());
}
public int avm_intValue(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_intValue);
lazyLoad();
return v.intValue();
}
public long avm_longValue(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_longValue);
lazyLoad();
return v.longValue();
}
public float avm_floatValue(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_floatValue);
lazyLoad();
return v.floatValue();
}
public double avm_doubleValue(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_doubleValue);
lazyLoad();
return v.doubleValue();
}
public long avm_longValueExact(){
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_longValueExact);
lazyLoad();
return v.longValueExact();
}
public int avm_intValueExact() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_intValueExact);
lazyLoad();
return v.intValueExact();
}
public short avm_shortValueExact() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_shortValueExact);
lazyLoad();
return v.shortValueExact();
}
public byte avm_byteValueExact() {
IInstrumentation.attachedThreadInstrumentation.get().chargeEnergy(RuntimeMethodFeeSchedule.BigInteger_avm_byteValueExact);
lazyLoad();
return v.byteValueExact();
}
//========================================================
// Methods below are used by runtime and test code only!
//========================================================
private java.math.BigInteger v;
public BigInteger(java.math.BigInteger u) {
setUnderlying(u);
}
private void setUnderlying(java.math.BigInteger u) {
if (isValidLength(u.toByteArray().length)) {
v = u;
}
}
public java.math.BigInteger getUnderlying() {
lazyLoad();
return v;
}
private BigInteger(java.math.BigInteger u, ConstantToken constantToken) {
super(constantToken);
setUnderlying(u);
}
// Deserializer support.
public BigInteger(Void ignore, int readIndex) {
super(ignore, readIndex);
}
public void deserializeSelf(java.lang.Class<?> firstRealImplementation, IObjectDeserializer deserializer) {
super.deserializeSelf(BigInteger.class, deserializer);
// We can deserialize this as its actual 2s compliment byte array.
this.v = new java.math.BigInteger(CodecIdioms.deserializeByteArray(deserializer));
}
public void serializeSelf(java.lang.Class<?> firstRealImplementation, IObjectSerializer serializer) {
super.serializeSelf(BigInteger.class, serializer);
// We can serialize this as its actual 2s compliment byte array.
CodecIdioms.serializeByteArray(serializer, this.v.toByteArray());
}
private boolean isValidLength(int length) {
if (length > 32) {
//we're limiting the size of BigInteger to 32 bytes to have better control over the billing
throw new ArithmeticException();
}
return true;
}
private void verifyBitLength(int length) {
if (length > 256) {
// since the maximum length is 32 bytes, the designated bit position cannot be bigger than 256
throw new ArithmeticException();
}
}
//========================================================
// Methods below are excluded from shadowing
//========================================================
//public BigInteger(int numBits, Random rnd)
//public BigInteger(int bitLength, int certainty, Random rnd)
//public static BigInteger probablePrime(int bitLength, Random rnd)
//private static BigInteger smallPrime(int bitLength, int certainty, Random rnd)
//private static BigInteger largePrime(int bitLength, int certainty, Random rnd)
//public BigInteger[] divideAndRemainder(BigInteger val)
//public BigInteger[] sqrtAndRemainder()
//public boolean isProbablePrime(int certainty)
}