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

Coverage #2

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
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
24 changes: 24 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Code Coverage"

on:
push:
# The branches below must be a subset of the branches above
branches: [ 'prcoverage' ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ 'develop' ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '8'
cache: 'gradle'
- run: ./gradlew clean build test --no-daemon
- uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
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;
import org.tron.common.crypto.zksnark.BN128G1;
import org.tron.common.crypto.zksnark.BN128G2;
import org.tron.common.crypto.zksnark.Fp;
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
Loading