Skip to content

Commit d2d49bb

Browse files
authored
feat(crypto): optimize zksnark config (#6371)
* fix(crypto): optimize the zkSNARK and shielded txs config * feat(rpc): remove redundant toggle check * fix(crypto): remove the shielded tx execute toggle * fix(crypto): rename the shielded Transaction api toggle * feat: optimize the test * feat: optimize ZksnarkParams initialization in tests. * remove redundant check * make zk param initialization thread safe * add compatibility with previous configuration
1 parent 50d6f69 commit d2d49bb

File tree

23 files changed

+137
-433
lines changed

23 files changed

+137
-433
lines changed

actuator/src/main/java/org/tron/core/actuator/ShieldedTransferActuator.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,28 +170,27 @@ private void executeShielded(List<SpendDescription> spends, List<ReceiveDescript
170170
}
171171
nullifierStore.put(new BytesCapsule(spend.getNullifier().toByteArray()));
172172
}
173-
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()) {
174-
IncrementalMerkleTreeContainer currentMerkle = merkleContainer.getCurrentMerkle();
173+
174+
IncrementalMerkleTreeContainer currentMerkle = merkleContainer.getCurrentMerkle();
175+
try {
176+
currentMerkle.wfcheck();
177+
} catch (ZksnarkException e) {
178+
ret.setStatus(fee, code.FAILED);
179+
ret.setShieldedTransactionFee(fee);
180+
throw new ContractExeException(e.getMessage());
181+
}
182+
//handle receives
183+
for (ReceiveDescription receive : receives) {
175184
try {
176-
currentMerkle.wfcheck();
185+
merkleContainer
186+
.saveCmIntoMerkleTree(currentMerkle, receive.getNoteCommitment().toByteArray());
177187
} catch (ZksnarkException e) {
178-
ret.setStatus(fee, code.FAILED);
188+
ret.setStatus(0, code.FAILED);
179189
ret.setShieldedTransactionFee(fee);
180190
throw new ContractExeException(e.getMessage());
181191
}
182-
//handle receives
183-
for (ReceiveDescription receive : receives) {
184-
try {
185-
merkleContainer
186-
.saveCmIntoMerkleTree(currentMerkle, receive.getNoteCommitment().toByteArray());
187-
} catch (ZksnarkException e) {
188-
ret.setStatus(0, code.FAILED);
189-
ret.setShieldedTransactionFee(fee);
190-
throw new ContractExeException(e.getMessage());
191-
}
192-
}
193-
merkleContainer.setCurrentMerkle(currentMerkle);
194192
}
193+
merkleContainer.setCurrentMerkle(currentMerkle);
195194
}
196195

197196
@Override
@@ -236,8 +235,7 @@ public boolean validate() throws ContractValidateException {
236235
throw new ContractValidateException("duplicate sapling nullifiers in this transaction");
237236
}
238237
nfSet.add(spendDescription.getNullifier());
239-
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()
240-
&& !merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
238+
if (!merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
241239
throw new ContractValidateException("Rt is invalid.");
242240
}
243241
if (nullifierStore.has(spendDescription.getNullifier().toByteArray())) {

chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java

Lines changed: 3 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -29,65 +29,42 @@
2929
@Slf4j
3030
public class JLibrustzcash {
3131

32-
private static Librustzcash INSTANCE;
32+
private static Librustzcash INSTANCE = LibrustzcashWrapper.getInstance();
3333

3434
public static void librustzcashZip32XskMaster(Zip32XskMasterParams params) {
35-
if (!isOpenZen()) {
36-
return;
37-
}
3835
INSTANCE.librustzcashZip32XskMaster(params.getData(), params.getSize(), params.getM_bytes());
3936
}
4037

4138
public static void librustzcashInitZksnarkParams(InitZksnarkParams params) {
42-
if (!isOpenZen()) {
43-
return;
44-
}
4539
INSTANCE.librustzcashInitZksnarkParams(params.getSpend_path(),
4640
params.getSpend_hash(), params.getOutput_path(), params.getOutput_hash());
4741
}
4842

4943
public static void librustzcashZip32XskDerive(Zip32XskDeriveParams params) {
50-
if (!isOpenZen()) {
51-
return;
52-
}
5344
INSTANCE.librustzcashZip32XskDerive(params.getData(), params.getSize(), params.getM_bytes());
5445
}
5546

5647
public static boolean librustzcashZip32XfvkAddress(Zip32XfvkAddressParams params) {
57-
if (!isOpenZen()) {
58-
return true;
59-
}
6048
return INSTANCE.librustzcashZip32XfvkAddress(params.getXfvk(), params.getJ(),
6149
params.getJ_ret(), params.getAddr_ret());
6250
}
6351

6452
public static void librustzcashCrhIvk(CrhIvkParams params) {
65-
if (!isOpenZen()) {
66-
return;
67-
}
6853
INSTANCE.librustzcashCrhIvk(params.getAk(), params.getNk(), params.getIvk());
6954
}
7055

7156
public static boolean librustzcashKaAgree(KaAgreeParams params) {
72-
if (!isOpenZen()) {
73-
return true;
74-
}
7557
return INSTANCE.librustzcashSaplingKaAgree(params.getP(), params.getSk(), params.getResult());
7658
}
7759

7860
public static boolean librustzcashComputeCm(ComputeCmParams params) {
79-
if (!isOpenZen()) {
80-
return true;
81-
}
8261
return INSTANCE.librustzcashSaplingComputeCm(params.getD(), params.getPkD(),
8362
params.getValue(), params.getR(), params.getCm());
8463
}
8564

8665
public static boolean librustzcashComputeNf(ComputeNfParams params) {
87-
if (isOpenZen()) {
88-
INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(),
89-
params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult());
90-
}
66+
INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(),
67+
params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult());
9168
return true;
9269
}
9370

@@ -96,9 +73,6 @@ public static boolean librustzcashComputeNf(ComputeNfParams params) {
9673
* @return ak 32 bytes
9774
*/
9875
public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException {
99-
if (!isOpenZen()) {
100-
return ByteUtil.EMPTY_BYTE_ARRAY;
101-
}
10276
LibrustzcashParam.valid32Params(ask);
10377
byte[] ak = new byte[32];
10478
INSTANCE.librustzcashAskToAk(ask, ak);
@@ -110,9 +84,6 @@ public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException {
11084
* @return 32 bytes
11185
*/
11286
public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException {
113-
if (!isOpenZen()) {
114-
return ByteUtil.EMPTY_BYTE_ARRAY;
115-
}
11687
LibrustzcashParam.valid32Params(nsk);
11788
byte[] nk = new byte[32];
11889
INSTANCE.librustzcashNskToNk(nsk, nk);
@@ -125,26 +96,17 @@ public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException {
12596
* @return r: random number, less than r_J, 32 bytes
12697
*/
12798
public static byte[] librustzcashSaplingGenerateR(byte[] r) throws ZksnarkException {
128-
if (!isOpenZen()) {
129-
return ByteUtil.EMPTY_BYTE_ARRAY;
130-
}
13199
LibrustzcashParam.valid32Params(r);
132100
INSTANCE.librustzcashSaplingGenerateR(r);
133101
return r;
134102
}
135103

136104
public static boolean librustzcashSaplingKaDerivepublic(KaDerivepublicParams params) {
137-
if (!isOpenZen()) {
138-
return true;
139-
}
140105
return INSTANCE.librustzcashSaplingKaDerivepublic(params.getDiversifier(), params.getEsk(),
141106
params.getResult());
142107
}
143108

144109
public static long librustzcashSaplingProvingCtxInit() {
145-
if (!isOpenZen()) {
146-
return 0;
147-
}
148110
return INSTANCE.librustzcashSaplingProvingCtxInit();
149111
}
150112

@@ -154,44 +116,29 @@ public static long librustzcashSaplingProvingCtxInit() {
154116
* @param d 11 bytes
155117
*/
156118
public static boolean librustzcashCheckDiversifier(byte[] d) throws ZksnarkException {
157-
if (!isOpenZen()) {
158-
return true;
159-
}
160119
LibrustzcashParam.valid11Params(d);
161120
return INSTANCE.librustzcashCheckDiversifier(d);
162121
}
163122

164123
public static boolean librustzcashSaplingSpendProof(SpendProofParams params) {
165-
if (!isOpenZen()) {
166-
return true;
167-
}
168124
return INSTANCE.librustzcashSaplingSpendProof(params.getCtx(), params.getAk(),
169125
params.getNsk(), params.getD(), params.getR(), params.getAlpha(), params.getValue(),
170126
params.getAnchor(), params.getVoucherPath(), params.getCv(), params.getRk(),
171127
params.getZkproof());
172128
}
173129

174130
public static boolean librustzcashSaplingOutputProof(OutputProofParams params) {
175-
if (!isOpenZen()) {
176-
return true;
177-
}
178131
return INSTANCE.librustzcashSaplingOutputProof(params.getCtx(), params.getEsk(),
179132
params.getD(), params.getPkD(), params.getR(), params.getValue(), params.getCv(),
180133
params.getZkproof());
181134
}
182135

183136
public static boolean librustzcashSaplingSpendSig(SpendSigParams params) {
184-
if (!isOpenZen()) {
185-
return true;
186-
}
187137
return INSTANCE.librustzcashSaplingSpendSig(params.getAsk(), params.getAlpha(),
188138
params.getSigHash(), params.getResult());
189139
}
190140

191141
public static boolean librustzcashSaplingBindingSig(BindingSigParams params) {
192-
if (!isOpenZen()) {
193-
return true;
194-
}
195142
return INSTANCE.librustzcashSaplingBindingSig(params.getCtx(),
196143
params.getValueBalance(), params.getSighash(), params.getResult());
197144
}
@@ -203,98 +150,62 @@ public static boolean librustzcashSaplingBindingSig(BindingSigParams params) {
203150
* @param data 32 bytes
204151
*/
205152
public static void librustzcashToScalar(byte[] value, byte[] data) throws ZksnarkException {
206-
if (!isOpenZen()) {
207-
return;
208-
}
209153
LibrustzcashParam.validParamLength(value, 64);
210154
LibrustzcashParam.valid32Params(data);
211155
INSTANCE.librustzcashToScalar(value, data);
212156
}
213157

214158
public static void librustzcashSaplingProvingCtxFree(long ctx) {
215-
if (!isOpenZen()) {
216-
return;
217-
}
218159
INSTANCE.librustzcashSaplingProvingCtxFree(ctx);
219160
}
220161

221162
public static long librustzcashSaplingVerificationCtxInit() {
222-
if (!isOpenZen()) {
223-
return 0;
224-
}
225163
return INSTANCE.librustzcashSaplingVerificationCtxInit();
226164
}
227165

228166
public static boolean librustzcashSaplingCheckSpend(CheckSpendParams params) {
229-
if (!isOpenZen()) {
230-
return true;
231-
}
232167
return INSTANCE.librustzcashSaplingCheckSpend(params.getCtx(), params.getCv(),
233168
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
234169
params.getSpendAuthSig(), params.getSighashValue());
235170
}
236171

237172
public static boolean librustzcashSaplingCheckOutput(CheckOutputParams params) {
238-
if (!isOpenZen()) {
239-
return true;
240-
}
241173
return INSTANCE.librustzcashSaplingCheckOutput(params.getCtx(), params.getCv(),
242174
params.getCm(), params.getEphemeralKey(), params.getZkproof());
243175
}
244176

245177
public static boolean librustzcashSaplingFinalCheck(FinalCheckParams params) {
246-
if (!isOpenZen()) {
247-
return true;
248-
}
249178
return INSTANCE.librustzcashSaplingFinalCheck(params.getCtx(),
250179
params.getValueBalance(), params.getBindingSig(), params.getSighashValue());
251180
}
252181

253182
public static boolean librustzcashSaplingCheckSpendNew(CheckSpendNewParams params) {
254-
if (!isOpenZen()) {
255-
return true;
256-
}
257183
return INSTANCE.librustzcashSaplingCheckSpendNew(params.getCv(),
258184
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
259185
params.getSpendAuthSig(), params.getSighashValue());
260186
}
261187

262188
public static boolean librustzcashSaplingCheckOutputNew(CheckOutputNewParams params) {
263-
if (!isOpenZen()) {
264-
return true;
265-
}
266189
return INSTANCE.librustzcashSaplingCheckOutputNew(params.getCv(), params.getCm(),
267190
params.getEphemeralKey(), params.getZkproof());
268191
}
269192

270193
public static boolean librustzcashSaplingFinalCheckNew(FinalCheckNewParams params) {
271-
if (!isOpenZen()) {
272-
return true;
273-
}
274194
return INSTANCE
275195
.librustzcashSaplingFinalCheckNew(params.getValueBalance(), params.getBindingSig(),
276196
params.getSighashValue(), params.getSpendCv(), params.getSpendCvLen(),
277197
params.getOutputCv(), params.getOutputCvLen());
278198
}
279199

280200
public static void librustzcashSaplingVerificationCtxFree(long ctx) {
281-
if (!isOpenZen()) {
282-
return;
283-
}
284201
INSTANCE.librustzcashSaplingVerificationCtxFree(ctx);
285202
}
286203

287204
public static boolean librustzcashIvkToPkd(IvkToPkdParams params) {
288-
if (!isOpenZen()) {
289-
return true;
290-
}
291205
return INSTANCE.librustzcashIvkToPkd(params.getIvk(), params.getD(), params.getPkD());
292206
}
293207

294208
public static void librustzcashMerkleHash(MerkleHashParams params) {
295-
if (!isOpenZen()) {
296-
return;
297-
}
298209
INSTANCE.librustzcashMerkleHash(params.getDepth(), params.getA(), params.getB(),
299210
params.getResult());
300211
}
@@ -303,19 +214,7 @@ public static void librustzcashMerkleHash(MerkleHashParams params) {
303214
* @param result uncommitted value, 32 bytes
304215
*/
305216
public static void librustzcashTreeUncommitted(byte[] result) throws ZksnarkException {
306-
if (!isOpenZen()) {
307-
return;
308-
}
309217
LibrustzcashParam.valid32Params(result);
310218
INSTANCE.librustzcashTreeUncommitted(result);
311219
}
312-
313-
public static boolean isOpenZen() {
314-
boolean res = CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs();
315-
if (res) {
316-
INSTANCE = LibrustzcashWrapper.getInstance();
317-
}
318-
return res;
319-
}
320-
321220
}

0 commit comments

Comments
 (0)