Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

few optimizations on pruning #1278

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions ethereumj-core/src/main/java/org/ethereum/crypto/HashUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.RIPEMD160Digest;
import org.spongycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -35,13 +36,12 @@

import static java.util.Arrays.copyOfRange;
import static org.ethereum.util.ByteUtil.EMPTY_BYTE_ARRAY;
import static org.ethereum.util.ByteUtil.bigIntegerToBytes;
import static org.ethereum.util.ByteUtil.bytesToBigInteger;

public class HashUtil {

private static final Logger LOG = LoggerFactory.getLogger(HashUtil.class);

private static final String[] HEX = hexDictionnary();
public static final byte[] EMPTY_DATA_HASH;
public static final byte[] EMPTY_LIST_HASH;
public static final byte[] EMPTY_TRIE_HASH;
Expand All @@ -63,8 +63,7 @@ public class HashUtil {
}

/**
* @param input
* - data for hashing
* @param input - data for hashing
* @return - sha256 hash of the data
*/
public static byte[] sha256(byte[] input) {
Expand Down Expand Up @@ -105,13 +104,10 @@ public static byte[] sha3(byte[] input1, byte[] input2) {

/**
* hashing chunk of the data
*
* @param input
* - data for hash
* @param start
* - start of hashing chunk
* @param length
* - length of hashing chunk
*
* @param input - data for hash
* @param start - start of hashing chunk
* @param length - length of hashing chunk
* @return - keccak hash of the chunk
*/
public static byte[] sha3(byte[] input, int start, int length) {
Expand Down Expand Up @@ -139,8 +135,7 @@ public static byte[] sha512(byte[] input) {
}

/**
* @param data
* - message to hash
* @param data - message to hash
* @return - reipmd160 hash of the message
*/
public static byte[] ripemd160(byte[] data) {
Expand All @@ -157,9 +152,8 @@ public static byte[] ripemd160(byte[] data) {
/**
* Calculates RIGTMOST160(SHA3(input)). This is used in address
* calculations. *
*
* @param input
* - data
*
* @param input - data
* @return - 20 right bytes of the hash keccak of the data
*/
public static byte[] sha3omit12(byte[] input) {
Expand All @@ -170,10 +164,8 @@ public static byte[] sha3omit12(byte[] input) {
/**
* The way to calculate new address inside ethereum
*
* @param addr
* - creating address
* @param nonce
* - nonce of creating address
* @param addr - creating address
* @param nonce - nonce of creating address
* @return new address
*/
public static byte[] calcNewAddr(byte[] addr, byte[] nonce) {
Expand All @@ -189,8 +181,8 @@ public static byte[] calcNewAddr(byte[] addr, byte[] nonce) {
* sha3(0xff ++ msg.sender ++ salt ++ sha3(init_code)))[12:]
*
* @param senderAddr - creating address
* @param initCode - contract init code
* @param salt - salt to make different result addresses
* @param initCode - contract init code
* @param salt - salt to make different result addresses
* @return new address
*/
public static byte[] calcSaltAddr(byte[] senderAddr, byte[] initCode, byte[] salt) {
Expand All @@ -209,11 +201,9 @@ public static byte[] calcSaltAddr(byte[] senderAddr, byte[] initCode, byte[] sal
}

/**
* @see #doubleDigest(byte[], int, int)
*
* @param input
* -
* @param input -
* @return -
* @see #doubleDigest(byte[], int, int)
*/
public static byte[] doubleDigest(byte[] input) {
return doubleDigest(input, 0, input.length);
Expand All @@ -224,12 +214,9 @@ public static byte[] doubleDigest(byte[] input) {
* resulting hash again. This is standard procedure in Bitcoin. The
* resulting hash is in big endian form.
*
* @param input
* -
* @param offset
* -
* @param length
* -
* @param input -
* @param offset -
* @param length -
* @return -
*/
public static byte[] doubleDigest(byte[] input, int offset, int length) {
Expand Down Expand Up @@ -272,7 +259,18 @@ public static byte[] randomHash() {
return randomHash;
}


public static String shortHash(byte[] hash) {
return Hex.toHexString(hash).substring(0, 6);
return HEX[hash[0] & 0xFF]
+ HEX[hash[1] & 0xFF]
+ HEX[hash[2] & 0xFF];
}

private static String[] hexDictionnary() {
String[] values = new String[0xff + 0x1];
for (int i = 0; i <= 0xff; i++) {
values[i] = String.format("%02x", i & 0xFF);
}
return values;
}
}
Loading