|
1 |
| -// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0. |
2 |
| - |
3 |
| -package com.webank.wedpr.vcl; |
4 |
| - |
5 |
| -import com.webank.wedpr.common.WedprException; |
6 |
| - |
7 |
| -/** |
8 |
| - * Minimalist demo of verifiable confidential ledger (VCL). |
9 |
| - * |
10 |
| - * <p>For a better interactive demo, please try our Rust version at |
11 |
| - * https://github.com/WeBankBlockchain/WeDPR-Lab-Core |
12 |
| - */ |
13 |
| -public class DemoMain { |
14 |
| - |
15 |
| - public static void main(String[] args) throws Exception { |
16 |
| - VclClient vclClient = new VclClient(); |
17 |
| - |
18 |
| - runVclProofs(vclClient, 2, 2, 4); |
19 |
| - runVclProofs(vclClient, 3, 4, 12); |
20 |
| - runVclProofs(vclClient, 1, 2, 3); |
21 |
| - runVclProofs(vclClient, 3, 4, 5); |
22 |
| - runVclProofs(vclClient, -1, 4, 3); |
23 |
| - } |
24 |
| - |
25 |
| - private static void runVclProofs(VclClient vclClient, long c1Value, long c2Value, long c3Value) |
26 |
| - throws WedprException { |
27 |
| - System.out.println("\n*******\nVCL PROOF RUN\n*******"); |
28 |
| - System.out.println( |
29 |
| - "c1_value = " + c1Value + ", c2_value = " + c2Value + ", c3_value = " + c3Value + "\n"); |
30 |
| - |
31 |
| - if (c1Value < 0 || c2Value < 0 || c3Value < 0) { |
32 |
| - System.out.println( |
33 |
| - "[WARNING] Non-positive value detected.\n" |
34 |
| - + "All the balance proofs (sum and product) will fail intentionally.\n"); |
35 |
| - } |
36 |
| - |
37 |
| - // Create confidential credit records for those values. |
38 |
| - VclResult c1Result = vclClient.makeCredit(c1Value); |
39 |
| - System.out.println("c1_credit (publicly verifiable) = " + c1Result.confidentialCredit); |
40 |
| - System.out.println("c1_secret (only known by the owner) = " + c1Result.ownerSecret); |
41 |
| - |
42 |
| - VclResult c2Result = vclClient.makeCredit(c2Value); |
43 |
| - System.out.println("c2_credit (publicly verifiable) = " + c2Result.confidentialCredit); |
44 |
| - System.out.println("c2_secret (only known by the owner) = " + c2Result.ownerSecret); |
45 |
| - |
46 |
| - VclResult c3Result = vclClient.makeCredit(c3Value); |
47 |
| - System.out.println("c3_credit (publicly verifiable) = " + c3Result.confidentialCredit); |
48 |
| - System.out.println("c3_secret (only known by the owner) = " + c3Result.ownerSecret); |
49 |
| - |
50 |
| - // Prove c1_value + c2_value = c3_value. |
51 |
| - VclResult sumResult = |
52 |
| - vclClient.proveSumBalance(c1Result.ownerSecret, c2Result.ownerSecret, c3Result.ownerSecret); |
53 |
| - System.out.println( |
54 |
| - "\nproof of " + c1Value + " + " + c2Value + " =? " + c3Value + ":\n" + sumResult.proof); |
55 |
| - |
56 |
| - VclResult verifySumResult = |
57 |
| - vclClient.verifySumBalance( |
58 |
| - c1Result.confidentialCredit, |
59 |
| - c2Result.confidentialCredit, |
60 |
| - c3Result.confidentialCredit, |
61 |
| - sumResult.proof); |
62 |
| - if (verifySumResult.verificationResult) { |
63 |
| - System.out.println(">> Pass: " + c1Value + " + " + c2Value + " == " + c3Value); |
64 |
| - } else { |
65 |
| - System.out.println("<< Fail: " + c1Value + " + " + c2Value + " != " + c3Value); |
66 |
| - } |
67 |
| - |
68 |
| - // Prove c1_value * c2_value = c3_value. |
69 |
| - VclResult productResult = |
70 |
| - vclClient.proveProductBalance( |
71 |
| - c1Result.ownerSecret, c2Result.ownerSecret, c3Result.ownerSecret); |
72 |
| - System.out.println( |
73 |
| - "\nproof of " + c1Value + " * " + c2Value + " =? " + c3Value + ":\n" + productResult.proof); |
74 |
| - |
75 |
| - VclResult verifyMultiResult = |
76 |
| - vclClient.verifyProductBalance( |
77 |
| - c1Result.confidentialCredit, |
78 |
| - c2Result.confidentialCredit, |
79 |
| - c3Result.confidentialCredit, |
80 |
| - productResult.proof); |
81 |
| - if (verifyMultiResult.verificationResult) { |
82 |
| - System.out.println(">> Pass: " + c1Value + " * " + c2Value + " == " + c3Value); |
83 |
| - } else { |
84 |
| - System.out.println("<< Fail: " + c1Value + " * " + c2Value + " != " + c3Value); |
85 |
| - } |
86 |
| - |
87 |
| - // Prove c1_value in [0, 2^32-1]. |
88 |
| - VclResult rangeResult = vclClient.proveRange(c1Result.ownerSecret); |
89 |
| - System.out.println("\nproof of " + c1Value + " in [0, 2^32-1]:\n" + productResult.proof); |
90 |
| - |
91 |
| - VclResult verifyRangeResult = |
92 |
| - vclClient.verifyRange(c1Result.confidentialCredit, rangeResult.proof); |
93 |
| - if (verifyRangeResult.verificationResult) { |
94 |
| - System.out.println(">> Pass: " + c1Value + " in [0, 2^32-1]"); |
95 |
| - } else { |
96 |
| - System.out.println("<< Fail: " + c1Value + " not in [0, 2^32-1]"); |
97 |
| - } |
98 |
| - } |
99 |
| -} |
| 1 | +package com.webank.wedpr.demo; |
| 2 | + |
| 3 | +import com.webank.wedpr.vcl.VclClient; |
| 4 | +import com.webank.wedpr.vcl.VclResult; |
| 5 | + |
| 6 | +/** |
| 7 | + * Minimalist demo of verifiable confidential ledger (VCL). |
| 8 | + * |
| 9 | + * <p>For a better interactive demo, please try our Rust version at |
| 10 | + * https://github.com/WeBankBlockchain/WeDPR-Lab-Core |
| 11 | + */ |
| 12 | +public class VclDemo { |
| 13 | + public static void run(VclClient vclClient, long c1Value, long c2Value, long c3Value) |
| 14 | + throws Exception { |
| 15 | + System.out.println("\n*******\nVCL PROOF RUN\n*******"); |
| 16 | + System.out.println( |
| 17 | + "c1_value = " + c1Value + ", c2_value = " + c2Value + ", c3_value = " + c3Value + "\n"); |
| 18 | + |
| 19 | + if (c1Value < 0 || c2Value < 0 || c3Value < 0) { |
| 20 | + System.out.println( |
| 21 | + "[WARNING] Non-positive value detected.\n" |
| 22 | + + "All the balance proofs (sum and product) will fail intentionally.\n"); |
| 23 | + } |
| 24 | + |
| 25 | + // Create confidential credit records for those values. |
| 26 | + VclResult c1Result = vclClient.makeCredit(c1Value); |
| 27 | + System.out.println("c1_credit (publicly verifiable) = " + c1Result.confidentialCredit); |
| 28 | + System.out.println("c1_secret (only known by the owner) = " + c1Result.ownerSecret); |
| 29 | + |
| 30 | + VclResult c2Result = vclClient.makeCredit(c2Value); |
| 31 | + System.out.println("c2_credit (publicly verifiable) = " + c2Result.confidentialCredit); |
| 32 | + System.out.println("c2_secret (only known by the owner) = " + c2Result.ownerSecret); |
| 33 | + |
| 34 | + VclResult c3Result = vclClient.makeCredit(c3Value); |
| 35 | + System.out.println("c3_credit (publicly verifiable) = " + c3Result.confidentialCredit); |
| 36 | + System.out.println("c3_secret (only known by the owner) = " + c3Result.ownerSecret); |
| 37 | + |
| 38 | + // Prove c1_value + c2_value = c3_value. |
| 39 | + VclResult sumResult = |
| 40 | + vclClient.proveSumBalance(c1Result.ownerSecret, c2Result.ownerSecret, c3Result.ownerSecret); |
| 41 | + System.out.println( |
| 42 | + "\nproof of " + c1Value + " + " + c2Value + " =? " + c3Value + ":\n" + sumResult.proof); |
| 43 | + |
| 44 | + VclResult verifySumResult = |
| 45 | + vclClient.verifySumBalance( |
| 46 | + c1Result.confidentialCredit, |
| 47 | + c2Result.confidentialCredit, |
| 48 | + c3Result.confidentialCredit, |
| 49 | + sumResult.proof); |
| 50 | + if (verifySumResult.verificationResult) { |
| 51 | + System.out.println(">> Pass: " + c1Value + " + " + c2Value + " == " + c3Value); |
| 52 | + } else { |
| 53 | + System.out.println("<< Fail: " + c1Value + " + " + c2Value + " != " + c3Value); |
| 54 | + } |
| 55 | + |
| 56 | + // Prove c1_value * c2_value = c3_value. |
| 57 | + VclResult productResult = |
| 58 | + vclClient.proveProductBalance( |
| 59 | + c1Result.ownerSecret, c2Result.ownerSecret, c3Result.ownerSecret); |
| 60 | + System.out.println( |
| 61 | + "\nproof of " + c1Value + " * " + c2Value + " =? " + c3Value + ":\n" + productResult.proof); |
| 62 | + |
| 63 | + VclResult verifyMultiResult = |
| 64 | + vclClient.verifyProductBalance( |
| 65 | + c1Result.confidentialCredit, |
| 66 | + c2Result.confidentialCredit, |
| 67 | + c3Result.confidentialCredit, |
| 68 | + productResult.proof); |
| 69 | + if (verifyMultiResult.verificationResult) { |
| 70 | + System.out.println(">> Pass: " + c1Value + " * " + c2Value + " == " + c3Value); |
| 71 | + } else { |
| 72 | + System.out.println("<< Fail: " + c1Value + " * " + c2Value + " != " + c3Value); |
| 73 | + } |
| 74 | + |
| 75 | + // Prove c1_value in [0, 2^32-1]. |
| 76 | + VclResult rangeResult = vclClient.proveRange(c1Result.ownerSecret); |
| 77 | + System.out.println("\nproof of " + c1Value + " in [0, 2^32-1]:\n" + productResult.proof); |
| 78 | + |
| 79 | + VclResult verifyRangeResult = |
| 80 | + vclClient.verifyRange(c1Result.confidentialCredit, rangeResult.proof); |
| 81 | + if (verifyRangeResult.verificationResult) { |
| 82 | + System.out.println(">> Pass: " + c1Value + " in [0, 2^32-1]"); |
| 83 | + } else { |
| 84 | + System.out.println("<< Fail: " + c1Value + " not in [0, 2^32-1]"); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments