-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
AllFi
wants to merge
2
commits into
tronprotocol:develop
Choose a base branch
from
zkBob:feature/optimize_bn128_precompiles
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,4 @@ Wallet | |
|
||
/framework/propPath | ||
.cache | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
chainbase/src/main/java/org/tron/common/zksnark/JLibarkworks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thus one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please elaborate?