Skip to content

feat(crypto): optimize zksnark config #6371

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

Open
wants to merge 5 commits into
base: release_v4.8.1
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,27 @@ private void executeShielded(List<SpendDescription> spends, List<ReceiveDescript
}
nullifierStore.put(new BytesCapsule(spend.getNullifier().toByteArray()));
}
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()) {
IncrementalMerkleTreeContainer currentMerkle = merkleContainer.getCurrentMerkle();

IncrementalMerkleTreeContainer currentMerkle = merkleContainer.getCurrentMerkle();
try {
currentMerkle.wfcheck();
} catch (ZksnarkException e) {
ret.setStatus(fee, code.FAILED);
ret.setShieldedTransactionFee(fee);
throw new ContractExeException(e.getMessage());
}
//handle receives
for (ReceiveDescription receive : receives) {
try {
currentMerkle.wfcheck();
merkleContainer
.saveCmIntoMerkleTree(currentMerkle, receive.getNoteCommitment().toByteArray());
} catch (ZksnarkException e) {
ret.setStatus(fee, code.FAILED);
ret.setStatus(0, code.FAILED);
ret.setShieldedTransactionFee(fee);
throw new ContractExeException(e.getMessage());
}
//handle receives
for (ReceiveDescription receive : receives) {
try {
merkleContainer
.saveCmIntoMerkleTree(currentMerkle, receive.getNoteCommitment().toByteArray());
} catch (ZksnarkException e) {
ret.setStatus(0, code.FAILED);
ret.setShieldedTransactionFee(fee);
throw new ContractExeException(e.getMessage());
}
}
merkleContainer.setCurrentMerkle(currentMerkle);
}
merkleContainer.setCurrentMerkle(currentMerkle);
}

@Override
Expand Down Expand Up @@ -236,8 +235,7 @@ public boolean validate() throws ContractValidateException {
throw new ContractValidateException("duplicate sapling nullifiers in this transaction");
}
nfSet.add(spendDescription.getNullifier());
if (CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs()
&& !merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
if (!merkleContainer.merkleRootExist(spendDescription.getAnchor().toByteArray())) {
throw new ContractValidateException("Rt is invalid.");
}
if (nullifierStore.has(spendDescription.getNullifier().toByteArray())) {
Expand Down
107 changes: 3 additions & 104 deletions chainbase/src/main/java/org/tron/common/zksnark/JLibrustzcash.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,42 @@
@Slf4j
public class JLibrustzcash {

private static Librustzcash INSTANCE;
private static Librustzcash INSTANCE = LibrustzcashWrapper.getInstance();

public static void librustzcashZip32XskMaster(Zip32XskMasterParams params) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashZip32XskMaster(params.getData(), params.getSize(), params.getM_bytes());
}

public static void librustzcashInitZksnarkParams(InitZksnarkParams params) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashInitZksnarkParams(params.getSpend_path(),
params.getSpend_hash(), params.getOutput_path(), params.getOutput_hash());
}

public static void librustzcashZip32XskDerive(Zip32XskDeriveParams params) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashZip32XskDerive(params.getData(), params.getSize(), params.getM_bytes());
}

public static boolean librustzcashZip32XfvkAddress(Zip32XfvkAddressParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashZip32XfvkAddress(params.getXfvk(), params.getJ(),
params.getJ_ret(), params.getAddr_ret());
}

public static void librustzcashCrhIvk(CrhIvkParams params) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashCrhIvk(params.getAk(), params.getNk(), params.getIvk());
}

public static boolean librustzcashKaAgree(KaAgreeParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingKaAgree(params.getP(), params.getSk(), params.getResult());
}

public static boolean librustzcashComputeCm(ComputeCmParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingComputeCm(params.getD(), params.getPkD(),
params.getValue(), params.getR(), params.getCm());
}

public static boolean librustzcashComputeNf(ComputeNfParams params) {
if (isOpenZen()) {
INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(),
params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult());
}
INSTANCE.librustzcashSaplingComputeNf(params.getD(), params.getPkD(), params.getValue(),
params.getR(), params.getAk(), params.getNk(), params.getPosition(), params.getResult());
return true;
}

Expand All @@ -96,9 +73,6 @@ public static boolean librustzcashComputeNf(ComputeNfParams params) {
* @return ak 32 bytes
*/
public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException {
if (!isOpenZen()) {
return ByteUtil.EMPTY_BYTE_ARRAY;
}
LibrustzcashParam.valid32Params(ask);
byte[] ak = new byte[32];
INSTANCE.librustzcashAskToAk(ask, ak);
Expand All @@ -110,9 +84,6 @@ public static byte[] librustzcashAskToAk(byte[] ask) throws ZksnarkException {
* @return 32 bytes
*/
public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException {
if (!isOpenZen()) {
return ByteUtil.EMPTY_BYTE_ARRAY;
}
LibrustzcashParam.valid32Params(nsk);
byte[] nk = new byte[32];
INSTANCE.librustzcashNskToNk(nsk, nk);
Expand All @@ -125,26 +96,17 @@ public static byte[] librustzcashNskToNk(byte[] nsk) throws ZksnarkException {
* @return r: random number, less than r_J, 32 bytes
*/
public static byte[] librustzcashSaplingGenerateR(byte[] r) throws ZksnarkException {
if (!isOpenZen()) {
return ByteUtil.EMPTY_BYTE_ARRAY;
}
LibrustzcashParam.valid32Params(r);
INSTANCE.librustzcashSaplingGenerateR(r);
return r;
}

public static boolean librustzcashSaplingKaDerivepublic(KaDerivepublicParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingKaDerivepublic(params.getDiversifier(), params.getEsk(),
params.getResult());
}

public static long librustzcashSaplingProvingCtxInit() {
if (!isOpenZen()) {
return 0;
}
return INSTANCE.librustzcashSaplingProvingCtxInit();
}

Expand All @@ -154,44 +116,29 @@ public static long librustzcashSaplingProvingCtxInit() {
* @param d 11 bytes
*/
public static boolean librustzcashCheckDiversifier(byte[] d) throws ZksnarkException {
if (!isOpenZen()) {
return true;
}
LibrustzcashParam.valid11Params(d);
return INSTANCE.librustzcashCheckDiversifier(d);
}

public static boolean librustzcashSaplingSpendProof(SpendProofParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingSpendProof(params.getCtx(), params.getAk(),
params.getNsk(), params.getD(), params.getR(), params.getAlpha(), params.getValue(),
params.getAnchor(), params.getVoucherPath(), params.getCv(), params.getRk(),
params.getZkproof());
}

public static boolean librustzcashSaplingOutputProof(OutputProofParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingOutputProof(params.getCtx(), params.getEsk(),
params.getD(), params.getPkD(), params.getR(), params.getValue(), params.getCv(),
params.getZkproof());
}

public static boolean librustzcashSaplingSpendSig(SpendSigParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingSpendSig(params.getAsk(), params.getAlpha(),
params.getSigHash(), params.getResult());
}

public static boolean librustzcashSaplingBindingSig(BindingSigParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingBindingSig(params.getCtx(),
params.getValueBalance(), params.getSighash(), params.getResult());
}
Expand All @@ -203,98 +150,62 @@ public static boolean librustzcashSaplingBindingSig(BindingSigParams params) {
* @param data 32 bytes
*/
public static void librustzcashToScalar(byte[] value, byte[] data) throws ZksnarkException {
if (!isOpenZen()) {
return;
}
LibrustzcashParam.validParamLength(value, 64);
LibrustzcashParam.valid32Params(data);
INSTANCE.librustzcashToScalar(value, data);
}

public static void librustzcashSaplingProvingCtxFree(long ctx) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashSaplingProvingCtxFree(ctx);
}

public static long librustzcashSaplingVerificationCtxInit() {
if (!isOpenZen()) {
return 0;
}
return INSTANCE.librustzcashSaplingVerificationCtxInit();
}

public static boolean librustzcashSaplingCheckSpend(CheckSpendParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckSpend(params.getCtx(), params.getCv(),
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
params.getSpendAuthSig(), params.getSighashValue());
}

public static boolean librustzcashSaplingCheckOutput(CheckOutputParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckOutput(params.getCtx(), params.getCv(),
params.getCm(), params.getEphemeralKey(), params.getZkproof());
}

public static boolean librustzcashSaplingFinalCheck(FinalCheckParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingFinalCheck(params.getCtx(),
params.getValueBalance(), params.getBindingSig(), params.getSighashValue());
}

public static boolean librustzcashSaplingCheckSpendNew(CheckSpendNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckSpendNew(params.getCv(),
params.getAnchor(), params.getNullifier(), params.getRk(), params.getZkproof(),
params.getSpendAuthSig(), params.getSighashValue());
}

public static boolean librustzcashSaplingCheckOutputNew(CheckOutputNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashSaplingCheckOutputNew(params.getCv(), params.getCm(),
params.getEphemeralKey(), params.getZkproof());
}

public static boolean librustzcashSaplingFinalCheckNew(FinalCheckNewParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE
.librustzcashSaplingFinalCheckNew(params.getValueBalance(), params.getBindingSig(),
params.getSighashValue(), params.getSpendCv(), params.getSpendCvLen(),
params.getOutputCv(), params.getOutputCvLen());
}

public static void librustzcashSaplingVerificationCtxFree(long ctx) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashSaplingVerificationCtxFree(ctx);
}

public static boolean librustzcashIvkToPkd(IvkToPkdParams params) {
if (!isOpenZen()) {
return true;
}
return INSTANCE.librustzcashIvkToPkd(params.getIvk(), params.getD(), params.getPkD());
}

public static void librustzcashMerkleHash(MerkleHashParams params) {
if (!isOpenZen()) {
return;
}
INSTANCE.librustzcashMerkleHash(params.getDepth(), params.getA(), params.getB(),
params.getResult());
}
Expand All @@ -303,19 +214,7 @@ public static void librustzcashMerkleHash(MerkleHashParams params) {
* @param result uncommitted value, 32 bytes
*/
public static void librustzcashTreeUncommitted(byte[] result) throws ZksnarkException {
if (!isOpenZen()) {
return;
}
LibrustzcashParam.valid32Params(result);
INSTANCE.librustzcashTreeUncommitted(result);
}

public static boolean isOpenZen() {
boolean res = CommonParameter.getInstance().isFullNodeAllowShieldedTransactionArgs();
if (res) {
INSTANCE = LibrustzcashWrapper.getInstance();
}
return res;
}

}
Loading