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

feat(precompiles): optimize bn128 precompiles #5507

Open
wants to merge 2 commits 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ Wallet

/framework/propPath
.cache
bin
75 changes: 37 additions & 38 deletions actuator/src/main/java/org/tron/core/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,14 @@
import org.tron.common.crypto.Hash;
import org.tron.common.crypto.SignUtils;
import org.tron.common.crypto.SignatureInterface;
import org.tron.common.crypto.zksnark.BN128;
import org.tron.common.crypto.zksnark.BN128Fp;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thus one

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • [ ]

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please elaborate?

import org.tron.common.crypto.zksnark.BN128G1;
import org.tron.common.crypto.zksnark.BN128G2;
import org.tron.common.crypto.zksnark.Fp;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • @.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please elaborate?

import org.tron.common.crypto.zksnark.PairingCheck;
import org.tron.common.es.ExecutorServiceManager;
import org.tron.common.parameter.CommonParameter;
import org.tron.common.runtime.ProgramResult;
import org.tron.common.runtime.vm.DataWord;
import org.tron.common.utils.BIUtil;
import org.tron.common.utils.ByteArray;
import org.tron.common.utils.ByteUtil;
import org.tron.common.utils.Sha256Hash;
import org.tron.common.zksnark.JLibarkworks;
import org.tron.common.zksnark.JLibrustzcash;
import org.tron.common.zksnark.LibrustzcashParam;
import org.tron.core.capsule.AccountCapsule;
Expand Down Expand Up @@ -706,7 +700,7 @@ private BigInteger parseArg(byte[] data, int offset, int len) {
}

/**
* Computes point addition on Barreto–Naehrig curve. See {@link BN128Fp} for details<br/> <br/>
* Computes point addition on Barreto–Naehrig curve.<br/> <br/>
* <p>
* input data[]:<br/> two points encoded as (x, y), where x and y are 32-byte left-padded
* integers,<br/> if input is shorter than expected, it's assumed to be right-padded with zero
Expand Down Expand Up @@ -742,25 +736,28 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
byte[] x2 = parseWord(data, 2);
byte[] y2 = parseWord(data, 3);

BN128<Fp> p1 = BN128Fp.create(x1, y1);
if (p1 == null) {

if (!JLibarkworks.libarkworksG1IsValid(x1, y1)) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}
byte[] p1 = ArrayUtils.addAll(x1, y1);

BN128<Fp> p2 = BN128Fp.create(x2, y2);
if (p2 == null) {
if (!JLibarkworks.libarkworksG1IsValid(x2, y2)) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}
byte[] p2 = ArrayUtils.addAll(x2, y2);

BN128<Fp> res = p1.add(p2).toEthNotation();

return Pair.of(true, encodeRes(res.x().bytes(), res.y().bytes()));
byte[] res = JLibarkworks.libarkworksAddG1(p1, p2);
if (res == null) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}
return Pair.of(true, res);
}
}

/**
* Computes multiplication of scalar value on a point belonging to Barreto–Naehrig curve. See
* {@link BN128Fp} for details<br/> <br/>
* Computes multiplication of scalar value on a point belonging to Barreto–Naehrig curve.
* <br/> <br/>
* <p>
* input data[]:<br/> point encoded as (x, y) is followed by scalar s, where x, y and s are
* 32-byte left-padded integers,<br/> if input is shorter than expected, it's assumed to be
Expand Down Expand Up @@ -795,23 +792,25 @@ public Pair<Boolean, byte[]> execute(byte[] data) {

byte[] s = parseWord(data, 2);

BN128<Fp> p = BN128Fp.create(x, y);
if (p == null) {
if (!JLibarkworks.libarkworksG1IsValid(x, y)) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}
byte[] p = ArrayUtils.addAll(x, y);

BN128<Fp> res = p.mul(BIUtil.toBI(s)).toEthNotation();

return Pair.of(true, encodeRes(res.x().bytes(), res.y().bytes()));
byte[] res = JLibarkworks.libarkworksMulG1(p, s);
if (res == null) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}
return Pair.of(true, res);
}
}

/**
* Computes pairing check. <br/> See {@link PairingCheck} for details.<br/> <br/>
* Computes pairing check.<br/> <br/>
* <p>
* Input data[]: <br/> an array of points (a1, b1, ... , ak, bk), <br/> where "ai" is a point of
* {@link BN128Fp} curve and encoded as two 32-byte left-padded integers (x; y) <br/> "bi" is a
* point of {@link BN128G2} curve and encoded as four 32-byte left-padded integers {@code (ai + b;
* Input data[]: <br/> an array of points (a1, b1, ... , ak, bk), <br/> where "ai" is an element
* of group G1 and encoded as two 32-byte left-padded integers (x; y) <br/> "bi" is an
* element of group G2 and encoded as four 32-byte left-padded integers {@code (ai + b;
* ci + d)}, each coordinate of the point is a big-endian {@link } number, so {@code b} precedes
* {@code a} in the encoding: {@code (b, a; d, c)} <br/> thus each pair (ai, bi) has 192 bytes
* length, if 192 is not a multiple of {@code data.length} then execution fails <br/> the number
Expand Down Expand Up @@ -854,38 +853,39 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}

PairingCheck check = PairingCheck.create();
int pairs = data.length / PAIR_SIZE;

// iterating over all pairs
byte[] g1s = new byte[0];
byte[] g2s = new byte[0];
for (int offset = 0; offset < data.length; offset += PAIR_SIZE) {

Pair<BN128G1, BN128G2> pair = decodePair(data, offset);
Pair<byte[], byte[]> pair = decodePair(data, offset);

// fail if decoding has failed
if (pair == null) {
return Pair.of(false, EMPTY_BYTE_ARRAY);
}

check.addPair(pair.getLeft(), pair.getRight());
g1s = ArrayUtils.addAll(g1s, pair.getLeft());
g2s = ArrayUtils.addAll(g2s, pair.getRight());
}

check.run();
int result = check.result();
int result = JLibarkworks.libarkworksPairingCheck(g1s, g2s, pairs) ? 1 : 0;

return Pair.of(true, new DataWord(result).getData());
}

private Pair<BN128G1, BN128G2> decodePair(byte[] in, int offset) {
private Pair<byte[], byte[]> decodePair(byte[] in, int offset) {

byte[] x = parseWord(in, offset, 0);
byte[] y = parseWord(in, offset, 1);

BN128G1 p1 = BN128G1.create(x, y);

// fail if point is invalid
if (p1 == null) {
if (!JLibarkworks.libarkworksG1IsValid(x, y)) {
return null;
}
byte[] p1 = ArrayUtils.addAll(x, y);

// (b, a)
byte[] b = parseWord(in, offset, 2);
Expand All @@ -895,12 +895,11 @@ private Pair<BN128G1, BN128G2> decodePair(byte[] in, int offset) {
byte[] d = parseWord(in, offset, 4);
byte[] c = parseWord(in, offset, 5);

BN128G2 p2 = BN128G2.create(a, b, c, d);

// fail if point is invalid
if (p2 == null) {
if (!JLibarkworks.libarkworksG2IsValid(a, b, c, d)) {
return null;
}
byte[] p2 = ArrayUtils.addAll(ArrayUtils.addAll(a, b), ArrayUtils.addAll(c, d));

return Pair.of(p1, p2);
}
Expand Down
7 changes: 6 additions & 1 deletion chainbase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
compile "org.fusesource.jansi:jansi:$jansiVersion"
compile group: 'org.rocksdb', name: 'rocksdbjni', version: '5.15.10'
compile group: 'com.typesafe', name: 'config', version: '1.3.2'
compile 'io.github.tronprotocol:zksnark-java-sdk:1.0.0'
compile 'com.github.zkbob:zksnark-java-sdk:feature~arkworks_alt_bn128-SNAPSHOT'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.8.5'
compile project(":protocol")
compile project(":common")
Expand Down Expand Up @@ -88,4 +88,9 @@ jacocoTestReport {
}
}

repositories {
// ...
maven { url "https://jitpack.io" }
}

build.dependsOn jacocoTestReport
37 changes: 37 additions & 0 deletions chainbase/src/main/java/org/tron/common/zksnark/JLibarkworks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.tron.common.zksnark;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JLibarkworks {

public static boolean libarkworksG1IsValid(byte[] x, byte[] y) {
return LibarkworksWrapper.getInstance().libarkworksG1IsValid(x, y);
}

public static boolean libarkworksG2IsValid(byte[] a, byte[] b, byte[] c, byte[] d) {
return LibarkworksWrapper.getInstance().libarkworksG2IsValid(a, b, c, d);
}

public static byte[] libarkworksAddG1(byte[] a, byte[] b) {
byte[] result = new byte[64];
boolean success = LibarkworksWrapper.getInstance().libarkworksAddG1(a, b, result);
if (!success) {
return null;
}
return result;
}

public static byte[] libarkworksMulG1(byte[] p, byte[] s) {
byte[] result = new byte[64];
boolean success = LibarkworksWrapper.getInstance().libarkworksMulG1(p, s, result);
if (!success) {
return null;
}
return result;
}

public static boolean libarkworksPairingCheck(byte[] g1s, byte[] g2s, int pairs) {
return LibarkworksWrapper.getInstance().libarkworksPairingCheck(g1s, g2s, pairs);
}
}
5 changes: 3 additions & 2 deletions common/src/main/java/org/tron/common/utils/ByteUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static byte[] parseBytes(byte[] input, int offset, int len) {
* @param idx an index of the word starting from {@code 0}
*/
public static byte[] parseWord(byte[] input, int idx) {
return parseBytes(input, WORD_SIZE * idx, WORD_SIZE);
return parseWord(input, 0, idx);
}

/**
Expand All @@ -368,7 +368,8 @@ public static byte[] parseWord(byte[] input, int idx) {
* @param offset an offset in {@code input} array to start parsing from
*/
public static byte[] parseWord(byte[] input, int offset, int idx) {
return parseBytes(input, offset + WORD_SIZE * idx, WORD_SIZE);
byte[] bytes = parseBytes(input, offset + WORD_SIZE * idx, WORD_SIZE);
return bytes == EMPTY_BYTE_ARRAY ? new byte[WORD_SIZE] : bytes;
}

public static boolean greater(byte[] bytes1, byte[] bytes2) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package org.tron.common.runtime.vm;

import static org.junit.Assert.assertArrayEquals;
import static org.tron.common.utils.ByteUtil.stripLeadingZeroes;
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 +49,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 +105,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 +1144,71 @@ public void totalAcquiredResourceTest() {
Assert.assertEquals(0, ByteArray.toLong(res.getRight()));
}

@Test
public void bn128AdditionTest() throws Exception {
PrecompiledContract bn128Add = createPrecompiledContract(altBN128AddAddr, OWNER_ADDRESS);
// https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/bn256Add.json
JSONArray testCases = readJsonFile("bn256Add.json");
for (int i = 0; i < testCases.size(); i++) {
JSONObject testCase = testCases.getJSONObject(i);
String name = testCase.getString("Name");
byte[] input = Hex.decode(testCase.getString("Input"));
Boolean expectedResult = testCase.getBoolean("Result");
byte[] expectedOutput = Hex.decode(testCase.getString("Expected"));

Pair<Boolean, byte[]> res = bn128Add.execute(input);
Boolean actualResult = res.getLeft();
byte[] actualOutput = res.getRight();

Assert.assertEquals(String.format("test %s failed", name), expectedResult, actualResult);
assertArrayEquals(String.format("test %s failed", name), expectedOutput, actualOutput);
}
}

@Test
public void bn128MultiplicationTest() throws Exception {
PrecompiledContract bn128Mul = createPrecompiledContract(altBN128MulAddr, OWNER_ADDRESS);
// https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/bn256ScalarMul.json
JSONArray testCases = readJsonFile("bn256ScalarMul.json");
for (int i = 0; i < testCases.size(); i++) {
JSONObject testCase = testCases.getJSONObject(i);
String name = testCase.getString("Name");
byte[] input = Hex.decode(testCase.getString("Input"));
Boolean expectedResult = testCase.getBoolean("Result");
byte[] expectedOutput = Hex.decode(testCase.getString("Expected"));

Pair<Boolean, byte[]> res = bn128Mul.execute(input);
Boolean actualResult = res.getLeft();
byte[] actualOutput = res.getRight();

Assert.assertEquals(String.format("test %s failed", name), expectedResult, actualResult);
assertArrayEquals(String.format("test %s failed", name), expectedOutput, actualOutput);
}
}

@Test
public void bn128PairingTest() throws Exception {
PrecompiledContract bn128Pairing =
createPrecompiledContract(altBN128PairingAddr, OWNER_ADDRESS);
// https://github.com/ethereum/go-ethereum/blob/master/core/vm/testdata/precompiles/bn256Pairing.json
JSONArray testCases = readJsonFile("bn256Pairing.json");
for (int i = 0; i < testCases.size(); i++) {
JSONObject testCase = testCases.getJSONObject(i);
String name = testCase.getString("Name");
byte[] input = Hex.decode(testCase.getString("Input"));
Boolean expectedResult = testCase.getBoolean("Result");
byte[] expectedOutput = Hex.decode(testCase.getString("Expected"));


Pair<Boolean, byte[]> res = bn128Pairing.execute(input);
Boolean actualResult = res.getLeft();
byte[] actualOutput = res.getRight();

Assert.assertEquals(String.format("test %s failed", name), expectedResult, actualResult);
assertArrayEquals(String.format("test %s failed", name), expectedOutput, actualOutput);
}
}

//@Test
public void convertFromTronBase58AddressNative() {
// 27WnTihwXsqCqpiNedWvtKCZHsLjDt4Hfmf TestNet address
Expand Down Expand Up @@ -1165,4 +1248,13 @@ 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());
}
}
Loading