Skip to content

Commit b85bf97

Browse files
committed
add two test case
1 parent 74aa14d commit b85bf97

2 files changed

Lines changed: 176 additions & 17 deletions

File tree

‎src/test/java/org/tron/core/zksnark/SendCoinShieldTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,14 @@ public void testDecryptReceiveWithIvk() throws ZksnarkException {
349349
Librustzcash.librustzcashSaplingProvingCtxFree(ctx);
350350
}
351351

352+
public String byte2intstring(byte[] input){
353+
StringBuilder sb = new StringBuilder();
354+
for(byte b:input){
355+
sb.append(String.valueOf((int)b) + ", ");
356+
}
357+
return sb.toString();
358+
}
359+
352360
@Test
353361
public void testDecryptReceiveWithOvk() throws Exception {
354362
//decode c_out with ovk.

‎src/test/java/org/tron/core/zksnark/ShieldedReceiveTest.java‎

Lines changed: 168 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.tron.common.utils.FileUtil;
2525
import org.tron.common.utils.Sha256Hash;
2626
import org.tron.common.zksnark.Librustzcash;
27+
import org.tron.common.zksnark.LibrustzcashParam;
2728
import org.tron.common.zksnark.LibrustzcashParam.BindingSigParams;
2829
import org.tron.common.zksnark.LibrustzcashParam.InitZksnarkParams;
2930
import org.tron.common.zksnark.LibrustzcashParam.OutputProofParams;
@@ -41,12 +42,7 @@
4142
import org.tron.core.config.DefaultConfig;
4243
import org.tron.core.config.args.Args;
4344
import org.tron.core.db.Manager;
44-
import org.tron.core.exception.BadItemException;
45-
import org.tron.core.exception.ContractExeException;
46-
import org.tron.core.exception.ContractValidateException;
47-
import org.tron.core.exception.PermissionException;
48-
import org.tron.core.exception.SignatureFormatException;
49-
import org.tron.core.exception.ZksnarkException;
45+
import org.tron.core.exception.*;
5046
import org.tron.core.zen.ZenTransactionBuilder;
5147
import org.tron.core.zen.ZenTransactionBuilder.ReceiveDescriptionInfo;
5248
import org.tron.core.zen.ZenTransactionBuilder.SpendDescriptionInfo;
@@ -62,9 +58,11 @@
6258
import org.tron.core.zen.note.NoteEncryption;
6359
import org.tron.core.zen.note.Note.NotePlaintextEncryptionResult;
6460
import org.tron.core.zen.note.OutgoingPlaintext;
61+
import org.tron.protos.Contract;
6562
import org.tron.protos.Contract.PedersenHash;
6663
import org.tron.protos.Contract.ShieldedTransferContract;
6764
import org.tron.protos.Contract.SpendDescription;
65+
import org.tron.protos.Protocol;
6866
import org.tron.protos.Protocol.AccountType;
6967
import org.tron.protos.Protocol.Transaction;
7068
import org.tron.protos.Protocol.Transaction.Contract.ContractType;
@@ -84,17 +82,10 @@ public class ShieldedReceiveTest {
8482

8583
private static Wallet wallet;
8684

87-
public enum TestColumn {CV, ZKPOOF, D_CM, PKD_CM, VALUE_CM, R_CM}
88-
89-
;
90-
91-
public enum TestSignMissingColumn {FROM_ADDRESS, FROM_AMOUNT, SPEND_DESCRITPION, RECEIVE_DESCRIPTION, TO_ADDRESS, TO_AMOUNT}
92-
93-
;
94-
95-
public enum TestReceiveMissingColumn {CV, CM, EPK, C_ENC, C_OUT, ZKPROOF}
96-
97-
;
85+
public enum TestColumn {CV, ZKPOOF, D_CM, PKD_CM, VALUE_CM, R_CM};
86+
public enum TestSignMissingColumn {FROM_ADDRESS, FROM_AMOUNT, SPEND_DESCRITPION,
87+
RECEIVE_DESCRIPTION, TO_ADDRESS, TO_AMOUNT};
88+
public enum TestReceiveMissingColumn {CV, CM, EPK, C_ENC, C_OUT, ZKPROOF};
9889

9990
static {
10091
Args.setParam(new String[]{"--output-directory", dbPath}, "config-localtest.conf");
@@ -1962,4 +1953,164 @@ public TransactionCapsule buildShieldedTransactionWithoutSpendAuthSig(PaymentAdd
19621953
builder.getContractBuilder().setBindingSignature(ByteString.copyFrom(bindingSig));
19631954
return transactionCapsule;
19641955
}
1956+
1957+
@Test
1958+
public void testMemoTooLong()
1959+
throws ContractValidateException, TooBigTransactionException, TooBigTransactionResultException,
1960+
TaposException, TransactionExpirationException, ReceiptCheckErrException,
1961+
DupTransactionException, VMIllegalException, ValidateSignatureException, BadItemException,
1962+
ContractExeException, AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException {
1963+
Pointer ctx = Librustzcash.librustzcashSaplingProvingCtxInit();
1964+
1965+
librustzcashInitZksnarkParams();
1966+
dbManager.getDynamicPropertiesStore().saveAllowZksnarkTransaction(1);
1967+
dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(4010 * 1000000l);
1968+
ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet);
1969+
1970+
// generate spend proof
1971+
SpendingKey sk = SpendingKey
1972+
.decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee");
1973+
ExpandedSpendingKey expsk = sk.expandedSpendingKey();
1974+
PaymentAddress address = sk.defaultAddress();
1975+
Note note = new Note(address, 4010 * 1000000);
1976+
IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer(note.cm());
1977+
byte[] anchor = voucher.root().getContent().toByteArray();
1978+
dbManager.getMerkleContainer()
1979+
.putMerkleTreeIntoStore(anchor, voucher.getVoucherCapsule().getTree());
1980+
builder.addSpend(expsk, note, anchor, voucher);
1981+
1982+
// generate output proof
1983+
SpendingKey spendingKey = SpendingKey.random();
1984+
FullViewingKey fullViewingKey = spendingKey.fullViewingKey();
1985+
IncomingViewingKey incomingViewingKey = fullViewingKey.inViewingKey();
1986+
PaymentAddress paymentAddress = incomingViewingKey.address(new DiversifierT()).get();
1987+
byte[] memo = org.tron.keystore.Wallet.generateRandomBytes(1024);
1988+
builder.addOutput(fullViewingKey.getOvk(), paymentAddress, 4000 * 1000000, memo);
1989+
1990+
TransactionCapsule transactionCap = builder.build();
1991+
1992+
boolean ok = dbManager.pushTransaction(transactionCap);
1993+
Assert.assertTrue(ok);
1994+
1995+
// add here
1996+
byte[] ivk = fullViewingKey.inViewingKey().getValue();
1997+
Protocol.Transaction t = transactionCap.getInstance();
1998+
1999+
for (org.tron.protos.Protocol.Transaction.Contract c : t.getRawData().getContractList()) {
2000+
if (c.getType() != ContractType.ShieldedTransferContract) {
2001+
continue;
2002+
}
2003+
Contract.ShieldedTransferContract stContract = c.getParameter()
2004+
.unpack(Contract.ShieldedTransferContract.class);
2005+
Contract.ReceiveDescription receiveDescription = stContract.getReceiveDescription(0);
2006+
2007+
Optional<Note> ret1 = Note.decrypt(
2008+
receiveDescription.getCEnc().toByteArray(),//ciphertext
2009+
ivk,
2010+
receiveDescription.getEpk().toByteArray(),//epk
2011+
receiveDescription.getNoteCommitment().toByteArray() //cm
2012+
);
2013+
2014+
if (ret1.isPresent()) {
2015+
Note noteText = ret1.get();
2016+
byte[] pk_d = new byte[32];
2017+
if (!Librustzcash.librustzcashIvkToPkd(
2018+
new LibrustzcashParam.IvkToPkdParams(incomingViewingKey.getValue(), noteText.d.getData(), pk_d))) {
2019+
Librustzcash.librustzcashSaplingProvingCtxFree(ctx);
2020+
return;
2021+
}
2022+
Assert.assertArrayEquals(paymentAddress.getPkD(), pk_d);
2023+
Assert.assertEquals( 4000 * 1000000,noteText.value);
2024+
2025+
byte[] resultMemo = new byte[512];
2026+
System.arraycopy(memo,0,resultMemo,0,512);
2027+
Assert.assertArrayEquals(resultMemo, noteText.memo);
2028+
} else {
2029+
Assert.assertFalse(true);
2030+
}
2031+
}
2032+
// end here
2033+
Librustzcash.librustzcashSaplingProvingCtxFree(ctx);
2034+
Assert.assertTrue(ok);
2035+
}
2036+
2037+
@Test
2038+
public void testMemoNotEnough()
2039+
throws ContractValidateException, TooBigTransactionException, TooBigTransactionResultException,
2040+
TaposException, TransactionExpirationException, ReceiptCheckErrException,
2041+
DupTransactionException, VMIllegalException, ValidateSignatureException, BadItemException,
2042+
ContractExeException, AccountResourceInsufficientException, InvalidProtocolBufferException, ZksnarkException {
2043+
Pointer ctx = Librustzcash.librustzcashSaplingProvingCtxInit();
2044+
2045+
librustzcashInitZksnarkParams();
2046+
dbManager.getDynamicPropertiesStore().saveAllowZksnarkTransaction(1);
2047+
dbManager.getDynamicPropertiesStore().saveTotalShieldedPoolValue(4010 * 1000000l);
2048+
ZenTransactionBuilder builder = new ZenTransactionBuilder(wallet);
2049+
2050+
// generate spend proof
2051+
SpendingKey sk = SpendingKey
2052+
.decode("ff2c06269315333a9207f817d2eca0ac555ca8f90196976324c7756504e7c9ee");
2053+
ExpandedSpendingKey expsk = sk.expandedSpendingKey();
2054+
PaymentAddress address = sk.defaultAddress();
2055+
Note note = new Note(address, 4010 * 1000000);
2056+
IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer(note.cm());
2057+
byte[] anchor = voucher.root().getContent().toByteArray();
2058+
dbManager.getMerkleContainer()
2059+
.putMerkleTreeIntoStore(anchor, voucher.getVoucherCapsule().getTree());
2060+
builder.addSpend(expsk, note, anchor, voucher);
2061+
2062+
// generate output proof
2063+
SpendingKey spendingKey = SpendingKey.random();
2064+
FullViewingKey fullViewingKey = spendingKey.fullViewingKey();
2065+
IncomingViewingKey incomingViewingKey = fullViewingKey.inViewingKey();
2066+
PaymentAddress paymentAddress = incomingViewingKey.address(new DiversifierT()).get();
2067+
byte[] memo = org.tron.keystore.Wallet.generateRandomBytes(128);
2068+
builder.addOutput(fullViewingKey.getOvk(), paymentAddress, 4000 * 1000000, memo);
2069+
2070+
TransactionCapsule transactionCap = builder.build();
2071+
2072+
boolean ok = dbManager.pushTransaction(transactionCap);
2073+
Assert.assertTrue(ok);
2074+
2075+
// add here
2076+
byte[] ivk = fullViewingKey.inViewingKey().getValue();
2077+
Protocol.Transaction t = transactionCap.getInstance();
2078+
2079+
for (org.tron.protos.Protocol.Transaction.Contract c : t.getRawData().getContractList()) {
2080+
if (c.getType() != ContractType.ShieldedTransferContract) {
2081+
continue;
2082+
}
2083+
Contract.ShieldedTransferContract stContract = c.getParameter()
2084+
.unpack(Contract.ShieldedTransferContract.class);
2085+
Contract.ReceiveDescription receiveDescription = stContract.getReceiveDescription(0);
2086+
2087+
Optional<Note> ret1 = Note.decrypt(
2088+
receiveDescription.getCEnc().toByteArray(),//ciphertext
2089+
ivk,
2090+
receiveDescription.getEpk().toByteArray(),//epk
2091+
receiveDescription.getNoteCommitment().toByteArray() //cm
2092+
);
2093+
2094+
if (ret1.isPresent()) {
2095+
Note noteText = ret1.get();
2096+
byte[] pk_d = new byte[32];
2097+
if (!Librustzcash.librustzcashIvkToPkd(
2098+
new LibrustzcashParam.IvkToPkdParams(incomingViewingKey.getValue(), noteText.d.getData(), pk_d))) {
2099+
Librustzcash.librustzcashSaplingProvingCtxFree(ctx);
2100+
return;
2101+
}
2102+
Assert.assertArrayEquals(paymentAddress.getPkD(), pk_d);
2103+
Assert.assertEquals( 4000 * 1000000,noteText.value);
2104+
2105+
byte[] resultMemo = new byte[512];
2106+
System.arraycopy(memo,0,resultMemo,0,128);
2107+
Assert.assertArrayEquals(resultMemo, noteText.memo);
2108+
} else {
2109+
Assert.assertFalse(true);
2110+
}
2111+
}
2112+
// end here
2113+
Librustzcash.librustzcashSaplingProvingCtxFree(ctx);
2114+
Assert.assertTrue(ok);
2115+
}
19652116
}

0 commit comments

Comments
 (0)