Skip to content

Commit 4d3ff56

Browse files
Merge pull request #6270 from raymondliu0711/feat/opt_kzg_methods
feat(tvm): update kzg jni links
2 parents 1e8d839 + 0eaf62e commit 4d3ff56

File tree

9 files changed

+22
-24
lines changed

9 files changed

+22
-24
lines changed

common/src/main/java/org/tron/core/exception/TronError.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public enum ErrCode {
4444
PROMETHEUS_INIT(1),
4545
TRON_NET_SERVICE_INIT(1),
4646
ZCASH_INIT(1),
47+
CKZG_INIT(1),
4748
LOG_LOAD(1),
4849
WITNESS_INIT(1),
4950
RATE_LIMITER_INIT(1),

crypto/src/main/java/org/tron/common/crypto/ckzg4844/CKZG4844JNI.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,40 +47,28 @@ public static void loadNativeLibrary() {
4747
public static final BigInteger BLS_MODULUS =
4848
new BigInteger(
4949
"52435875175126190479447740508185965837690552500527637822603658699938581184513");
50-
5150
/** The number of bytes in a g1 point. */
5251
protected static final int BYTES_PER_G1 = 48;
53-
5452
/** The number of bytes in a g2 point. */
5553
protected static final int BYTES_PER_G2 = 96;
56-
5754
/** The number of bytes in a BLS scalar field element. */
5855
public static final int BYTES_PER_FIELD_ELEMENT = 32;
59-
6056
/** The number of bits in a BLS scalar field element. */
6157
protected static final int BITS_PER_FIELD_ELEMENT = 255;
62-
6358
/** The number of field elements in a blob. */
6459
public static final int FIELD_ELEMENTS_PER_BLOB = 4096;
65-
6660
/** The number of field elements in an extended blob. */
6761
protected static final int FIELD_ELEMENTS_PER_EXT_BLOB = FIELD_ELEMENTS_PER_BLOB * 2;
68-
6962
/** The number of field elements in a cell. */
7063
public static final int FIELD_ELEMENTS_PER_CELL = 64;
71-
7264
/** The number of bytes in a KZG commitment. */
7365
public static final int BYTES_PER_COMMITMENT = 48;
74-
7566
/** The number of bytes in a KZG proof. */
7667
public static final int BYTES_PER_PROOF = 48;
77-
7868
/** The number of bytes in a blob. */
7969
public static final int BYTES_PER_BLOB = FIELD_ELEMENTS_PER_BLOB * BYTES_PER_FIELD_ELEMENT;
80-
8170
/** The number of bytes in a single cell. */
8271
public static final int BYTES_PER_CELL = BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_CELL;
83-
8472
/** The number of cells in an extended blob. */
8573
public static final int CELLS_PER_EXT_BLOB =
8674
FIELD_ELEMENTS_PER_EXT_BLOB / FIELD_ELEMENTS_PER_CELL;
@@ -214,15 +202,6 @@ public static native boolean verifyBlobKzgProof(
214202
public static native boolean verifyBlobKzgProofBatch(
215203
byte[] blobs, byte[] commitmentsBytes, byte[] proofsBytes, long count);
216204

217-
/**
218-
* Get the cells for a given blob.
219-
*
220-
* @param blob the blob to get cells for
221-
* @return the cells
222-
* @throws CKZGException if there is a crypto error
223-
*/
224-
public static native byte[] computeCells(byte[] blob);
225-
226205
/**
227206
* Get the cells and proofs for a given blob.
228207
*
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-4.42 KB
Binary file not shown.

framework/src/main/java/org/tron/core/zen/KZGPointEvaluationInitService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.apache.commons.io.FileUtils;
1010
import org.springframework.stereotype.Component;
1111
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
12+
import org.tron.core.exception.TronError;
1213

1314
@Slf4j
1415
@Component
@@ -31,11 +32,15 @@ public static void initCKZG4844() {
3132
if (loaded.compareAndSet(false, true)) {
3233
logger.info("init ckzg 4844 begin");
3334

34-
CKZG4844JNI.loadNativeLibrary();
35+
try {
36+
CKZG4844JNI.loadNativeLibrary();
3537

36-
String setupFile = getSetupFile("trusted_setup.txt");
38+
String setupFile = getSetupFile("trusted_setup.txt");
3739

38-
CKZG4844JNI.loadTrustedSetup(setupFile, 0);
40+
CKZG4844JNI.loadTrustedSetup(setupFile, 0);
41+
} catch (Exception e) {
42+
throw new TronError(e, TronError.ErrCode.CKZG_INIT);
43+
}
3944

4045
logger.info("init ckzg 4844 done");
4146
}

framework/src/test/java/org/tron/core/zksnark/KZGPointEvaluationTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package org.tron.core.zksnark;
22

3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
5+
import static org.mockito.Mockito.mockStatic;
6+
37
import java.util.Arrays;
48
import lombok.extern.slf4j.Slf4j;
59
import org.bouncycastle.util.encoders.Hex;
610
import org.junit.Assert;
711
import org.junit.Test;
12+
import org.mockito.MockedStatic;
813
import org.tron.common.crypto.ckzg4844.CKZG4844JNI;
914
import org.tron.common.crypto.ckzg4844.CKZGException;
1015
import org.tron.common.crypto.ckzg4844.CellsAndProofs;
1116
import org.tron.common.crypto.ckzg4844.ProofAndY;
17+
import org.tron.core.exception.TronError;
1218
import org.tron.core.zen.KZGPointEvaluationInitService;
1319

1420
@Slf4j
@@ -79,5 +85,12 @@ public void testVerifyBlobKzgProof() {
7985
}
8086

8187
KZGPointEvaluationInitService.freeSetup();
88+
89+
try (MockedStatic<CKZG4844JNI> mock = mockStatic(CKZG4844JNI.class)) {
90+
mock.when(CKZG4844JNI::loadNativeLibrary).thenThrow(new RuntimeException());
91+
TronError thrown = assertThrows(TronError.class,
92+
KZGPointEvaluationInitService::initCKZG4844);
93+
assertEquals(TronError.ErrCode.CKZG_INIT, thrown.getErrCode());
94+
}
8295
}
8396
}

0 commit comments

Comments
 (0)