Skip to content

Commit 6dc56e2

Browse files
committed
add demo for jni
1 parent 26d24b8 commit 6dc56e2

File tree

9 files changed

+238
-184
lines changed

9 files changed

+238
-184
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cp ./lab-core-dynamic-libs/* ./solution/vcl/src/main/resources/WeDPR_dynamic_lib
2020
# 编译项目
2121
bash ./gradlew clean build
2222
# 进入项目目录
23-
cd solution/vcl/dist/
23+
cd demo/dist/
2424
# 运行demo
25-
java -cp "apps/*:conf/:libs/*" com.webank.wedpr.vcl.DemoMain
25+
java -cp "apps/*:conf/:libs/*" com.webank.wedpr.demo.DemoMain
2626
```

demo/build.gradle

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id 'java'
3+
id 'com.github.sherter.google-java-format' version '0.8'
4+
}
5+
6+
group 'com.webank'
7+
version '1.0.0-SNAPSHOT'
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
test.onlyIf { project.hasProperty('runTest') }
16+
verifyGoogleJavaFormat.onlyIf { project.hasProperty('runVerifyGoogleJavaFormat') }
17+
18+
dependencies {
19+
compile project(':common')
20+
compile project(':solution:crypto')
21+
compile project(':solution:vcl')
22+
testCompile group: 'junit', name: 'junit', version: '4.12'
23+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.webank.wedpr.demo;
2+
3+
import com.webank.wedpr.crypto.CryptoClient;
4+
import com.webank.wedpr.crypto.CryptoResult;
5+
import java.util.Base64;
6+
7+
/** Minimalist demo of WeDPR Crypto Tools. */
8+
public class CryptoDemo {
9+
public static void run(CryptoClient cryptoClient) throws Exception {
10+
System.out.println("\n*******\nCRYPTO TOOL RUN\n*******");
11+
12+
CryptoResult cryptoResult = cryptoClient.secp256k1GenKeyPair();
13+
String publicKey = cryptoResult.publicKey;
14+
String privateKey = cryptoResult.privateKey;
15+
System.out.println("public key = " + publicKey);
16+
System.out.println("private key = " + privateKey);
17+
18+
// Base64 encoding for "WeDPR Demo", which is currently required to pass bytes input to API.
19+
// TODO: Allow non-encoded UTF8 input.
20+
String message = Base64.getEncoder().encodeToString("WeDPR Demo".getBytes());
21+
String messageHash = cryptoClient.keccak256Hash(message).hash;
22+
System.out.println("messageHash = " + messageHash);
23+
24+
String signature = cryptoClient.secp256k1Sign(privateKey, messageHash).signature;
25+
System.out.println("signature = " + signature);
26+
27+
boolean result = cryptoClient.secp256k1Verify(publicKey, messageHash, signature).booleanResult;
28+
System.out.println("signature verify result = " + result);
29+
30+
String encryptedData = cryptoClient.secp256k1EciesEncrypt(publicKey, messageHash).encryptedData;
31+
System.out.println("encryptedData = " + encryptedData);
32+
33+
String decryptedData =
34+
cryptoClient.secp256k1EciesDecrypt(privateKey, encryptedData).decryptedData;
35+
System.out.println("decryptedData = " + decryptedData);
36+
}
37+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2020 WeDPR Lab Project Authors. Licensed under Apache-2.0.
2+
3+
package com.webank.wedpr.demo;
4+
5+
import com.webank.wedpr.crypto.CryptoClient;
6+
import com.webank.wedpr.vcl.VclClient;
7+
8+
/** Demo Launcher. */
9+
public class DemoMain {
10+
11+
public static void main(String[] args) throws Exception {
12+
CryptoClient cryptoClient = new CryptoClient();
13+
CryptoDemo.run(cryptoClient);
14+
15+
VclClient vclClient = new VclClient();
16+
VclDemo.run(vclClient, 2, 2, 4);
17+
VclDemo.run(vclClient, 3, 4, 12);
18+
VclDemo.run(vclClient, 1, 2, 3);
19+
VclDemo.run(vclClient, 3, 4, 5);
20+
VclDemo.run(vclClient, -1, 4, 3);
21+
}
22+
}
Lines changed: 87 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,87 @@
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+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ rootProject.name = 'WeDPR-Lab-Java-SDK'
1111
include 'common'
1212
include 'solution:vcl'
1313
include 'solution:crypto'
14+
include 'demo'
15+

solution/crypto/src/main/java/com/webank/wedpr/crypto/CryptoClient.java

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,81 @@
44
import com.webank.wedpr.common.WedprException;
55

66
/**
7-
* Client class used by WeDPR crypto. This is the main interface class for Java apps using VCL
7+
* Client class used by crypto tools. This is the main interface class for Java apps using crypto
88
* functions.
99
*/
1010
public class CryptoClient {
1111

1212
/**
13-
* @param pubKey
14-
* @param plaintext
15-
* @return
16-
* @throws WedprException
13+
* Encrypts an encoded message by ECIES with a public key on the secp256k1 curve.
14+
*
15+
* @param publicKey encoded public key.
16+
* @param message encoded message to encrypt.
17+
* @return CryptoResult containing data for encryptedData.
18+
* @throws WedprException if any error occurred.
1719
*/
18-
public CryptoResult secp256k1EciesEncrypt(String pubKey, String plaintext) throws WedprException {
19-
return NativeInterface.secp256k1EciesEncrypt(pubKey, plaintext).expectNoError();
20+
public CryptoResult secp256k1EciesEncrypt(String publicKey, String message)
21+
throws WedprException {
22+
return NativeInterface.secp256k1EciesEncrypt(publicKey, message).expectNoError();
2023
}
2124

22-
public CryptoResult secp256k1EciesDecrypt(String priKey, String ciphertext)
25+
/**
26+
* Decrypts an encoded encryptedData by ECIES with a private key on the secp256k1 curve.
27+
*
28+
* @param privateKey encoded private key.
29+
* @param encryptedData encoded ciphertext to decrypt.
30+
* @return CryptoResult containing data for decryptedData.
31+
* @throws WedprException if any error occurred.
32+
*/
33+
public CryptoResult secp256k1EciesDecrypt(String privateKey, String encryptedData)
2334
throws WedprException {
24-
return NativeInterface.secp256k1EciesDecrypt(priKey, ciphertext).expectNoError();
35+
return NativeInterface.secp256k1EciesDecrypt(privateKey, encryptedData).expectNoError();
2536
}
2637

38+
/**
39+
* Generates a new key pair for signature algorithm on the secp256k1 curve.
40+
*
41+
* @return CryptoResult containing data for publicKey, privateKey.
42+
* @throws WedprException if any error occurred.
43+
*/
2744
public CryptoResult secp256k1GenKeyPair() throws WedprException {
2845
return NativeInterface.secp256k1GenKeyPair().expectNoError();
2946
}
3047

31-
public CryptoResult keccak256Hash(String message) throws WedprException {
32-
return NativeInterface.keccak256Hash(message).expectNoError();
33-
}
34-
35-
public CryptoResult secp256k1Sign(String priKey, String messageHash) throws WedprException {
36-
return NativeInterface.secp256k1Sign(priKey, messageHash).expectNoError();
48+
/**
49+
* Signs a message hash with the private key on the secp256k1 curve.
50+
*
51+
* @param privateKey encoded private key.
52+
* @param messageHash encoded hash of a message to sign.
53+
* @return CryptoResult containing data for signature.
54+
* @throws WedprException if any error occurred.
55+
*/
56+
public CryptoResult secp256k1Sign(String privateKey, String messageHash) throws WedprException {
57+
return NativeInterface.secp256k1Sign(privateKey, messageHash).expectNoError();
3758
}
3859

39-
public CryptoResult secp256k1Verify(String pubKey, String messageHash, String signature)
60+
/**
61+
* Verifies a message hash with the public key on the secp256k1 curve.
62+
*
63+
* @param publicKey encoded public key.
64+
* @param messageHash encoded hash of a message to verify.
65+
* @param signature encoded signature
66+
* @return CryptoResult containing data for booleanResult.
67+
* @throws WedprException if any error occurred.
68+
*/
69+
public CryptoResult secp256k1Verify(String publicKey, String messageHash, String signature)
4070
throws WedprException {
41-
return NativeInterface.secp256k1Verify(pubKey, messageHash, signature).expectNoError();
71+
return NativeInterface.secp256k1Verify(publicKey, messageHash, signature).expectNoError();
4272
}
4373

44-
// TODO: Add a getVclConfig function to expose the value of RANGE_SIZE_IN_BITS.
74+
/**
75+
* Generates a keccak256 hash string from a bytes array of any length.
76+
*
77+
* @param message
78+
* @return CryptoResult containing data for hash.
79+
* @throws WedprException if any error occurred.
80+
*/
81+
public CryptoResult keccak256Hash(String message) throws WedprException {
82+
return NativeInterface.keccak256Hash(message).expectNoError();
83+
}
4584
}

0 commit comments

Comments
 (0)