Skip to content

Commit 3cca6fb

Browse files
committed
fix(crypto): optimize the zkSNARK and shielded txs config
1 parent 1e35f79 commit 3cca6fb

File tree

8 files changed

+18
-160
lines changed

8 files changed

+18
-160
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void executeShielded(List<SpendDescription> spends, List<ReceiveDescript
170170
}
171171
nullifierStore.put(new BytesCapsule(spend.getNullifier().toByteArray()));
172172
}
173-
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()) {
173+
if (chainBaseManager.getDynamicPropertiesStore().supportShieldedTransaction()) {
174174
IncrementalMerkleTreeContainer currentMerkle = merkleContainer.getCurrentMerkle();
175175
try {
176176
currentMerkle.wfcheck();
@@ -236,8 +236,7 @@ public boolean validate() throws ContractValidateException {
236236
throw new ContractValidateException("duplicate sapling nullifiers in this transaction");
237237
}
238238
nfSet.add(spendDescription.getNullifier());
239-
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()
240-
&& !merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
239+
if (!merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
241240
throw new ContractValidateException("Rt is invalid.");
242241
}
243242
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
}

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,25 @@ public class JLibsodium {
1212

1313
public static final int CRYPTO_GENERICHASH_BLAKE2B_PERSONALBYTES = 16;
1414
public static final int CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12;
15-
private static Libsodium INSTANCE;
15+
private static Libsodium INSTANCE = LibsodiumWrapper.getInstance();
1616

1717
public static int cryptoGenerichashBlake2bInitSaltPersonal(Blake2bInitSaltPersonalParams params) {
18-
if (!isOpenZen()) {
19-
return 0;
20-
}
2118
return INSTANCE
2219
.cryptoGenerichashBlake2BInitSaltPersonal(params.getState(), params.getKey(),
2320
params.getKeyLen(), params.getOutLen(), params.getSalt(), params.getPersonal());
2421
}
2522

2623
public static int cryptoGenerichashBlake2bUpdate(Blake2bUpdateParams params) {
27-
if (!isOpenZen()) {
28-
return 0;
29-
}
3024
return INSTANCE
3125
.cryptoGenerichashBlake2BUpdate(params.getState(), params.getIn(), params.getInLen());
3226
}
3327

3428
public static int cryptoGenerichashBlake2bFinal(Blake2bFinalParams params) {
35-
if (!isOpenZen()) {
36-
return 0;
37-
}
3829
return INSTANCE.cryptoGenerichashBlake2BFinal(params.getState(),
3930
params.getOut(), params.getOutLen());
4031
}
4132

4233
public static int cryptoGenerichashBlack2bSaltPersonal(Black2bSaltPersonalParams params) {
43-
if (!isOpenZen()) {
44-
return 0;
45-
}
4634
return INSTANCE.cryptoGenerichashBlake2BSaltPersonal(params.getOut(), params.getOutLen(),
4735
params.getIn(), params.getInLen(), params.getKey(), params.getKeyLen(),
4836
params.getSalt(),
@@ -51,9 +39,6 @@ public static int cryptoGenerichashBlack2bSaltPersonal(Black2bSaltPersonalParams
5139

5240
public static int cryptoAeadChacha20poly1305IetfDecrypt(
5341
Chacha20poly1305IetfDecryptParams params) {
54-
if (!isOpenZen()) {
55-
return 0;
56-
}
5742
return INSTANCE
5843
.cryptoAeadChacha20Poly1305IetfDecrypt(params.getM(), params.getMLenP(),
5944
params.getNSec(),
@@ -63,35 +48,17 @@ public static int cryptoAeadChacha20poly1305IetfDecrypt(
6348

6449
public static int cryptoAeadChacha20Poly1305IetfEncrypt(
6550
Chacha20Poly1305IetfEncryptParams params) {
66-
if (!isOpenZen()) {
67-
return 0;
68-
}
6951
return INSTANCE
7052
.cryptoAeadChacha20Poly1305IetfEncrypt(params.getC(), params.getCLenP(), params.getM(),
7153
params.getMLen(), params.getAd(), params.getAdLen(),
7254
params.getNSec(), params.getNPub(), params.getK());
7355
}
7456

7557
public static long initState() {
76-
if (!isOpenZen()) {
77-
return 0;
78-
}
7958
return INSTANCE.cryptoGenerichashBlake2BStateInit();
8059
}
8160

8261
public static void freeState(long state) {
83-
if (!isOpenZen()) {
84-
return;
85-
}
8662
INSTANCE.cryptoGenerichashBlake2BStateFree(state);
8763
}
88-
89-
private static boolean isOpenZen() {
90-
boolean res = CommonParameter.getInstance()
91-
.isFullNodeAllowShieldedTransactionArgs();
92-
if (res) {
93-
INSTANCE = LibsodiumWrapper.getInstance();
94-
}
95-
return res;
96-
}
9764
}

framework/src/main/java/org/tron/core/db/Manager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,8 @@ public boolean pushTransaction(final TransactionCapsule trx)
868868
TooBigTransactionException, TransactionExpirationException,
869869
ReceiptCheckErrException, VMIllegalException, TooBigTransactionResultException {
870870

871-
if (isShieldedTransaction(trx.getInstance()) && !Args.getInstance()
872-
.isFullNodeAllowShieldedTransactionArgs()) {
871+
if (isShieldedTransaction(trx.getInstance()) && !chainBaseManager.getDynamicPropertiesStore()
872+
.supportShieldedTransaction()) {
873873
return true;
874874
}
875875

0 commit comments

Comments
 (0)