Remove array length check from DataWord and use better test
This commit is contained in:
parent
de0f67ae3b
commit
4868ecfc0e
|
@ -7,34 +7,31 @@ import java.math.BigInteger;
|
|||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* www.ethereumJ.com
|
||||
* User: Roman Mandeleil
|
||||
* Created on: 01/06/2014 19:47
|
||||
* DataWord is the 32-byte array representation of a 256-bit number
|
||||
* Calculations can be done on this word with other DataWords
|
||||
*/
|
||||
|
||||
public class DataWord {
|
||||
|
||||
static DataWord ZERO = new DataWord(new byte[32]); // don't push it in to the stack
|
||||
|
||||
byte[] data = new byte[32];
|
||||
|
||||
public DataWord(){
|
||||
data = new byte[32];
|
||||
}
|
||||
public DataWord() {
|
||||
data = new byte[32];
|
||||
}
|
||||
|
||||
public DataWord(int num){
|
||||
ByteBuffer bInt = ByteBuffer.allocate(4).putInt(num);
|
||||
ByteBuffer data = ByteBuffer.allocate(32);
|
||||
System.arraycopy(bInt.array(), 0, data.array(), 28, 4);
|
||||
this.data = data.array();
|
||||
}
|
||||
public DataWord(int num) {
|
||||
ByteBuffer bInt = ByteBuffer.allocate(4).putInt(num);
|
||||
ByteBuffer data = ByteBuffer.allocate(32);
|
||||
System.arraycopy(bInt.array(), 0, data.array(), 28, 4);
|
||||
this.data = data.array();
|
||||
}
|
||||
|
||||
public DataWord(byte[] data){
|
||||
if (data == null || data.length > 32)
|
||||
throw new RuntimeException("bad push data: " + data);
|
||||
|
||||
System.arraycopy(data, 0, this.data, 32 - data.length, data.length);
|
||||
}
|
||||
public DataWord(byte[] data) {
|
||||
if (data == null || data.length > 32)
|
||||
throw new RuntimeException("bad push data: " + data);
|
||||
System.arraycopy(data, 0, this.data, 32 - data.length, data.length);
|
||||
}
|
||||
|
||||
public byte[] getData() {
|
||||
return data;
|
||||
|
@ -95,8 +92,19 @@ public class DataWord {
|
|||
}
|
||||
}
|
||||
|
||||
// todo: add can be done in more efficient way
|
||||
// todo without BigInteger quick hack
|
||||
// By : Holger
|
||||
// From : http://stackoverflow.com/a/24023466/459349
|
||||
public void add(DataWord word) {
|
||||
byte[] result = new byte[32];
|
||||
for (int i = 31, overflow = 0; i >= 0; i--) {
|
||||
int v = (this.data[i] & 0xff) + (word.data[i] & 0xff) + overflow;
|
||||
result[i] = (byte) v;
|
||||
overflow = v >>> 8;
|
||||
}
|
||||
this.data = result;
|
||||
}
|
||||
|
||||
// old add-method with BigInteger quick hack
|
||||
public void add2(DataWord word){
|
||||
|
||||
BigInteger result = value().add( word.value() );
|
||||
|
@ -107,24 +115,10 @@ public class DataWord {
|
|||
this.data = data.array();
|
||||
}
|
||||
|
||||
// By : Holger
|
||||
// From : http://stackoverflow.com/a/24023466/459349
|
||||
public void add(DataWord word) {
|
||||
if (this.data.length != 32 || word.data.length != 32)
|
||||
throw new IllegalArgumentException();
|
||||
byte[] result = new byte[32];
|
||||
for (int i = 31, overflow = 0; i >= 0; i--) {
|
||||
int v = (this.data[i] & 0xff) + (word.data[i] & 0xff) + overflow;
|
||||
result[i] = (byte) v;
|
||||
overflow = v >>> 8;
|
||||
}
|
||||
this.data = result;
|
||||
}
|
||||
|
||||
// todo: mull can be done in more efficient way
|
||||
// todo: mul can be done in more efficient way
|
||||
// todo: with shift left shift right trick
|
||||
// todo without BigInteger quick hack
|
||||
public void mull(DataWord word){
|
||||
public void mul(DataWord word){
|
||||
|
||||
BigInteger result = value().multiply( word.value() );
|
||||
byte[] bytes = result.toByteArray();
|
||||
|
|
|
@ -55,7 +55,7 @@ public class VM {
|
|||
|
||||
DataWord word1 = program.stackPop();
|
||||
DataWord word2 = program.stackPop();
|
||||
word1.mull(word2);
|
||||
word1.mul(word2);
|
||||
program.stackPush(word1);
|
||||
program.step();
|
||||
}
|
||||
|
|
|
@ -2,9 +2,8 @@ package org.ethereum.vm;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
public class DataWordTest {
|
||||
|
||||
|
@ -41,15 +40,15 @@ public class DataWordTest {
|
|||
@Test
|
||||
public void testAdd2() {
|
||||
byte[] two = new byte[32];
|
||||
two[31] = 0x01; // 0x00000000000000000000000000000000000000000000000000000000000001
|
||||
two[31] = (byte) 0xff; // 0x000000000000000000000000000000000000000000000000000000000000ff
|
||||
|
||||
DataWord x = new DataWord(two);
|
||||
x.add(new DataWord(two));
|
||||
System.out.println(new BigInteger(1, x.data));
|
||||
System.out.println(Hex.toHexString(x.data));
|
||||
|
||||
DataWord y = new DataWord(two);
|
||||
y.add2(new DataWord(two));
|
||||
System.out.println(new BigInteger(1, y.data));
|
||||
System.out.println(Hex.toHexString(y.data));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -62,11 +61,11 @@ public class DataWordTest {
|
|||
DataWord x = new DataWord(three);
|
||||
x.add(new DataWord(three));
|
||||
assertEquals(32, x.data.length);
|
||||
System.out.println(new BigInteger(1, x.data));
|
||||
System.out.println(Hex.toHexString(x.data));
|
||||
|
||||
// FAIL
|
||||
// DataWord y = new DataWord(three);
|
||||
// y.add2(new DataWord(three));
|
||||
// System.out.println(new BigInteger(1, y.data));
|
||||
// System.out.println(Hex.toHexString(y.data));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package org.ethereum.vm;
|
||||
|
||||
import org.ethereum.crypto.HashUtil;
|
||||
import org.junit.Test;
|
||||
import org.spongycastle.util.encoders.Hex;
|
||||
|
||||
|
|
Loading…
Reference in New Issue