Skip to content

Commit 44973ef

Browse files
committed
add encode/edcode for SpendProofParams; modify the bug of deafultDiversity; modify the test case
1 parent 1d2cdb7 commit 44973ef

3 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎src/main/java/org/tron/common/zksnark/LibrustzcashParam.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.sun.jna.Pointer;
44
import lombok.Getter;
55
import lombok.Setter;
6+
import org.tron.common.utils.ByteArray;
67
import org.tron.common.utils.ByteUtil;
78
import org.tron.core.exception.ZksnarkException;
89

@@ -425,6 +426,54 @@ public void valid() throws ZksnarkException {
425426
valid32Params(rk);
426427
validParamLength(zkproof, 192);
427428
}
429+
430+
public static SpendProofParams decode(Pointer ctx, byte[] data)
431+
throws ZksnarkException {
432+
byte[] ak = new byte[32];
433+
byte[] nsk = new byte[32];
434+
byte[] d = new byte[11];
435+
byte[] r = new byte[32];
436+
byte[] alpha = new byte[32];
437+
byte[] valueByte = new byte[8];
438+
byte[] anchor = new byte[32];
439+
byte[] voucherPath = new byte[1065];
440+
byte[] cv = new byte[32];
441+
byte[] rk = new byte[192];
442+
byte[] zkproof = new byte[64];
443+
444+
System.arraycopy(data, 0, ak, 0, 32);
445+
System.arraycopy(data, 32, nsk, 0, 32);
446+
System.arraycopy(data, 64, d, 0, 11);
447+
System.arraycopy(data, 75, r, 0, 32);
448+
System.arraycopy(data, 107, alpha, 0, 32);
449+
System.arraycopy(data, 139, valueByte, 0, 8);
450+
System.arraycopy(data, 147, anchor, 0, 32);
451+
System.arraycopy(data, 179, voucherPath, 0, 1065);
452+
System.arraycopy(data, 179 + 1065, cv, 0, 32);
453+
System.arraycopy(data, 211 + 1065, rk, 0, 192);
454+
System.arraycopy(data, 243 + 1065, zkproof, 0, 64);
455+
456+
return new SpendProofParams(ctx, ak, nsk, d, r, alpha, ByteArray.toLong(valueByte),
457+
anchor,voucherPath,cv,rk,zkproof);
458+
}
459+
460+
public byte[] encode(){
461+
byte[] data = new byte[32+32+11+32+32+8+32+1065+32+32+192];
462+
463+
System.arraycopy(ak, 0, data, 0, 32);
464+
System.arraycopy(nsk, 0, data, 32, 32);
465+
System.arraycopy(d, 0, data, 64, 11);
466+
System.arraycopy(r, 0, data, 75, 32);
467+
System.arraycopy(alpha, 0,data, 107, 32);
468+
System.arraycopy(ByteArray.fromLong(value), 0, data, 139, 8);
469+
System.arraycopy(anchor, 0, data, 147, 32);
470+
System.arraycopy(voucherPath, 0, data, 179, 1065);
471+
System.arraycopy(cv, 0, data, 179 + 1065, 32);
472+
System.arraycopy(rk, 0, data, 211 + 1065, 32);
473+
System.arraycopy(zkproof, 0, data, 243 + 1065, 192);
474+
475+
return data;
476+
}
428477
}
429478

430479
/**

‎src/main/java/org/tron/core/zen/address/SpendingKey.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public PaymentAddress defaultAddress() throws BadItemException, ZksnarkException
8888
public DiversifierT defaultDiversifier() throws BadItemException, ZksnarkException {
8989
byte[] res = new byte[Constant.ZC_DIVERSIFIER_SIZE];
9090
byte[] blob = new byte[34];
91-
ZksnarkUtils.sort(this.value);
91+
//ZksnarkUtils.sort(this.value);
9292
System.arraycopy(this.value, 0, blob, 0, 32);
9393
blob[32] = 3;
9494
blob[33] = 0;
@@ -107,7 +107,6 @@ public DiversifierT defaultDiversifier() throws BadItemException, ZksnarkExcepti
107107
}
108108
blob[33] += 1;
109109
}
110-
System.out.println("res1 = " + ByteArray.toHexString(res));
111110
DiversifierT diversifierT = new DiversifierT();
112111
diversifierT.setData(res);
113112
return diversifierT;

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,6 @@ private ReceiveDescriptionCapsule changeGenerateOutputProof(ReceiveDescriptionIn
681681
receiveDescriptionCapsule.setCEnc(enc.encCiphertext);
682682
receiveDescriptionCapsule.setZkproof(zkProof);
683683

684-
System.out.println("right cm:" + ByteArray.toHexString(cm));
685-
686684
OutgoingPlaintext outPlaintext =
687685
new OutgoingPlaintext(output.getNote().pkD, encryptor.esk);
688686
receiveDescriptionCapsule.setCOut(outPlaintext
@@ -740,8 +738,6 @@ private ReceiveDescriptionCapsule changeGenerateOutputProof(ReceiveDescriptionIn
740738
break;
741739
}
742740

743-
System.out.println("wrong cm:" + ByteArray.toHexString(receiveDescriptionCapsule.getCm().toByteArray()));
744-
745741
return receiveDescriptionCapsule;
746742
}
747743

@@ -1897,7 +1893,6 @@ public void testIsolateSignature()
18971893
transactionHash,
18981894
builder,
18991895
ctx);
1900-
19011896
//filled with Sapling spendAuth in builder
19021897
List<SpendDescriptionInfo> spends = builder.getSpends();
19031898
for (int i = 0; i < spends.size(); i++) {
@@ -1939,12 +1934,12 @@ public TransactionCapsule buildShieldedTransactionWithoutSpendAuthSig(PaymentAdd
19391934
throws ZksnarkException, BadItemException, ContractValidateException {
19401935
// generate input
19411936
Note note = new Note(address, 100 * 1000000);
1942-
1937+
note.rcm = ByteArray.
1938+
fromHexString("eb1aa5dd257b9da4e4064a853dec94651be38078e29fe441a9a8075016cfa701");
19431939
IncrementalMerkleVoucherContainer voucher = createSimpleMerkleVoucherContainer(note.cm());
19441940
byte[] anchor = voucher.root().getContent().toByteArray();
19451941
dbManager.getMerkleContainer()
19461942
.putMerkleTreeIntoStore(anchor, voucher.getVoucherCapsule().getTree());
1947-
19481943
SpendDescriptionInfo skSpend = new SpendDescriptionInfo(ak,
19491944
nsk,
19501945
ovk,

0 commit comments

Comments
 (0)