Skip to content

Commit

Permalink
Add bn128 benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
AllFi committed Dec 5, 2023
1 parent a66316c commit 1555a37
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ src/test/java/org/tron/consensus2
src/main/java/META-INF/
src/main/resources/META-INF/
/bin/
bin

# Eclipse IDE specific files and folders
/.project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
import static org.tron.core.config.Parameter.ChainConstant.BLOCK_PRODUCED_INTERVAL;
import static org.tron.core.db.TransactionTrace.convertToTronAddress;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.bouncycastle.util.Arrays;
Expand Down Expand Up @@ -40,6 +48,7 @@
import org.tron.core.vm.config.VMConfig;
import org.tron.core.vm.repository.Repository;
import org.tron.core.vm.repository.RepositoryImpl;
import org.tron.core.zksnark.SendCoinShieldTest;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.AccountType;
import org.tron.protos.Protocol.Proposal.State;
Expand Down Expand Up @@ -95,6 +104,14 @@ public class PrecompiledContractsTest extends BaseTest {
private static final DataWord totalAcquiredResourceAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000001000015");

// bn128
private static final DataWord altBN128AddAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000000006");
private static final DataWord altBN128MulAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000000007");
private static final DataWord altBN128PairingAddr = new DataWord(
"0000000000000000000000000000000000000000000000000000000000000008");

private static final String ACCOUNT_NAME = "account";
private static final String OWNER_ADDRESS;
private static final String WITNESS_NAME = "witness";
Expand Down Expand Up @@ -1126,6 +1143,25 @@ public void totalAcquiredResourceTest() {
Assert.assertEquals(0, ByteArray.toLong(res.getRight()));
}

@Test
public void bn128Bench() throws Exception {
PrecompiledContract bn128Add = createPrecompiledContract(altBN128AddAddr, OWNER_ADDRESS);
JSONObject testCase = readJsonFile("bn256Add.json").getJSONObject(0);
byte[] input = Hex.decode(testCase.getString("Input"));
bench(bn128Add, input, 10000);

PrecompiledContract bn128Mul = createPrecompiledContract(altBN128MulAddr, OWNER_ADDRESS);
testCase = readJsonFile("bn256ScalarMul.json").getJSONObject(1);
input = Hex.decode(testCase.getString("Input"));
bench(bn128Mul, input, 1000);

PrecompiledContract bn128Pairing =
createPrecompiledContract(altBN128PairingAddr, OWNER_ADDRESS);
testCase = readJsonFile("bn256Pairing.json").getJSONObject(13);
input = Hex.decode(testCase.getString("Input"));
bench(bn128Pairing, input, 100);
}

//@Test
public void convertFromTronBase58AddressNative() {
// 27WnTihwXsqCqpiNedWvtKCZHsLjDt4Hfmf TestNet address
Expand Down Expand Up @@ -1165,4 +1201,23 @@ private static byte[] encodeMultiWord(byte[]... words) {
return res;
}

private JSONArray readJsonFile(String fileName) throws Exception {
String file1 = SendCoinShieldTest.class.getClassLoader()
.getResource("json" + File.separator + fileName).getFile();
List<String> readLines = Files.readLines(new File(file1),
Charsets.UTF_8);

return JSONArray
.parseArray(readLines.stream().reduce((s, s2) -> s + s2).get());
}

private static void bench(PrecompiledContract contract, byte[] input, int itersCount) {
long start = System.nanoTime();
for (int i = 0; i < itersCount; i++) {
contract.execute(input);
}
long end = System.nanoTime();
System.out.println(
contract.getClass().getSimpleName() + " cost " + (end - start) / itersCount + "ns");
}
}
114 changes: 114 additions & 0 deletions framework/src/test/resources/json/bn256Add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
{
"Input": "18b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f3726607c2b7f58a84bd6145f00c9c2bc0bb1a187f20ff2c92963a88019e7c6a014eed06614e20c147e940f2d70da3f74c9a17df361706a4485c742bd6788478fa17d7",
"Expected": "2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c915",
"Name": "chfast1",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "2243525c5efd4b9c3d3c45ac0ca3fe4dd85e830a4ce6b65fa1eeaee202839703301d1d33be6da8e509df21cc35964723180eed7532537db9ae5e7d48f195c91518b18acfb4c2c30276db5411368e7185b311dd124691610c5d3b74034e093dc9063c909c4720840cb5134cb9f59fa749755796819658d32efc0d288198f37266",
"Expected": "2bd3e6d0f3b142924f5ca7b49ce5b9d54c4703d7ae5648e61d02268b1a0a9fb721611ce0a6af85915e2f1d70300909ce2e49dfad4a4619c8390cae66cefdb204",
"Name": "chfast2",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio1",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio2",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio3",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio4",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio5",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio6",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio7",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Name": "cdetrio8",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Gas": 150,
"Name": "cdetrio9",
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Gas": 150,
"Name": "cdetrio10",
"NoBenchmark": false
},
{
"Input": "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
"Expected": "030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd315ed738c0e0a7c92e7845f96b2ae9c0a68a6a449e3538fc7ff3ebf7a5a18a2c4",
"Name": "cdetrio11",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "030644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd315ed738c0e0a7c92e7845f96b2ae9c0a68a6a449e3538fc7ff3ebf7a5a18a2c4",
"Name": "cdetrio12",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa901e0559bacb160664764a357af8a9fe70baa9258e0b959273ffc5718c6d4cc7c039730ea8dff1254c0fee9c0ea777d29a9c710b7e616683f194f18c43b43b869073a5ffcc6fc7a28c30723d6e58ce577356982d65b833a5a5c15bf9024b43d98",
"Expected": "15bf2bb17880144b5d1cd2b1f46eff9d617bffd1ca57c37fb5a49bd84e53cf66049c797f9ce0d17083deb32b5e36f2ea2a212ee036598dd7624c168993d1355f",
"Name": "cdetrio13",
"Gas": 150,
"NoBenchmark": false
},
{
"Input": "17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa901e0559bacb160664764a357af8a9fe70baa9258e0b959273ffc5718c6d4cc7c17c139df0efee0f766bc0204762b774362e4ded88953a39ce849a8a7fa163fa92e83f8d734803fc370eba25ed1f6b8768bd6d83887b87165fc2434fe11a830cb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Expected": "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"Name": "cdetrio14",
"Gas": 150,
"NoBenchmark": false
}
]
Loading

0 comments on commit 1555a37

Please sign in to comment.