Skip to content

Commit

Permalink
Pedersen hasher to use new jni methods (hyperledger#50)
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Fukushima <[email protected]>
  • Loading branch information
gfukushima authored Mar 26, 2024
1 parent f6faf23 commit 308ef27
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public Bytes commit(Bytes32[] inputs) {
*/
@Override
public Bytes32 commitRoot(final Bytes32[] inputs) {
return Bytes32.wrap(LibIpaMultipoint.commitRoot(Bytes.concatenate(inputs).toArray()));
return Bytes32.wrap(LibIpaMultipoint.commitAsCompressed(Bytes.concatenate(inputs).toArray()));
}

/**
Expand All @@ -69,7 +69,7 @@ public Bytes32 commitRoot(final Bytes32[] inputs) {
*/
@Override
public Bytes32 groupToField(Bytes input) {
return Bytes32.wrap(LibIpaMultipoint.groupToField(input.toArray()));
return Bytes32.wrap(LibIpaMultipoint.hash(input.toArray()));
}

/**
Expand Down Expand Up @@ -104,13 +104,14 @@ public Bytes32 trieKeyHash(Bytes address, Bytes32 index) {
chunks[i + 1] = Bytes32.rightPad(chunk);
}

final Bytes hashBE =
Bytes.wrap(LibIpaMultipoint.commitRoot(Bytes.concatenate(chunks).toArray()));
final Bytes hash =
Bytes.wrap(
LibIpaMultipoint.hash(LibIpaMultipoint.commit(Bytes.concatenate(chunks).toArray())));

// commitRoot returns the hash in big endian format, so we reverse it to get it
// in little endian
// format. When we migrate to using `groupToField`, this reverse will not be
// needed.
return Bytes32.wrap(hashBE.reverse());
return Bytes32.wrap(hash);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public void testStorageKey() {
UInt256 storageKey = UInt256.valueOf(32);
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d260");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034460");
assertThat(adapter.storageKey(address, storageKey)).isEqualTo(expected);
}

@Test
public void testStorageKeyForMainStorage() {
UInt256 storageKey = UInt256.valueOf(64);
Bytes32 expected =
Bytes32.fromHexString("0x22f146f007e60a6a8781a35e7181f02a71fddfaec78a4adce111374a5f61af40");
Bytes32.fromHexString("0x6127e4b0c266bee72914ce7261d0e4595c414c1ef439d9b0eb7d13cda5dc7640");
assertThat(adapter.storageKey(address, storageKey)).isEqualTo(expected);
}

Expand All @@ -60,7 +60,7 @@ public void testCodeChunkKey() {
UInt256 chunkId = UInt256.valueOf(24);
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d298");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034498");
assertThat(adapter.codeChunkKey(address, chunkId)).isEqualTo(expected);
}

Expand All @@ -70,47 +70,47 @@ public void testCodeChunkKey2() {
UInt256 chunkId =
UInt256.fromHexString("0x0000000000000000000000000000000000000000000000000000000000000080");
Bytes32 expected =
Bytes32.fromHexString("0x07361aa490296db032a91d3a5a69d3c3ef0e477baf8fced7f6775e4739614200");
Bytes32.fromHexString("0x64465862f6244f410f93da62f24f4219a6e99fc3d0ad603da813b4be8e5c9500");
assertThat(adapter.codeChunkKey(addr, chunkId)).isEqualTo(expected);
}

@Test
public void testVersionKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d200");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034400");
assertThat(adapter.versionKey(address)).isEqualTo(expected);
}

@Test
public void testBalanceKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d201");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034401");
assertThat(adapter.balanceKey(address)).isEqualTo(expected);
}

@Test
public void testNonceKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d202");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034402");
assertThat(adapter.nonceKey(address)).isEqualTo(expected);
}

@Test
public void testCodeKeccakKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d203");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034403");
assertThat(adapter.codeKeccakKey(address)).isEqualTo(expected);
}

@Test
public void testCodeSizeKey() {
// Need to change this once commit is fixed
Bytes32 expected =
Bytes32.fromHexString("0xc224b9edaff18b27ce2604ad0f33a162d19840fd25290436dedcce42ad15d204");
Bytes32.fromHexString("0x46b95e4e504b92d984c91d6f17eba4b60b904fb370818f0b6e74bc3ae5034404");
assertThat(adapter.codeSizeKey(address)).isEqualTo(expected);
}

Expand All @@ -121,7 +121,8 @@ public static List<TestChunkifyData> JsonChunkifyData() throws IOException {
return objectMapper.readValue(inputStream, new TypeReference<List<TestChunkifyData>>() {});
}

public static List<TestChunkifyData> JsonContractCodeDataWithPush32On31Byte() throws IOException {
public static List<TestChunkifyData> JsonContractCodeDataWithPush32On31stByte()
throws IOException {
InputStream inputStream =
TrieKeyAdapterTest.class.getResourceAsStream("/chukifyCodePush32on31stByte.json");
return objectMapper.readValue(inputStream, new TypeReference<List<TestChunkifyData>>() {});
Expand All @@ -133,7 +134,7 @@ static class TestChunkifyData {
}

@ParameterizedTest
@MethodSource({"JsonChunkifyData", "JsonContractCodeDataWithPush32On31Byte"})
@MethodSource({"JsonChunkifyData", "JsonContractCodeDataWithPush32On31stByte"})
public void TestChunkifyCode(TestChunkifyData testData) {
Bytes bytecode = Bytes.fromHexString(testData.bytecode);
List<Bytes32> result = adapter.chunkifyCode(bytecode);
Expand Down
Loading

0 comments on commit 308ef27

Please sign in to comment.