@@ -5,7 +5,8 @@ import { expect } from "chai";
5
5
6
6
// Public key
7
7
interface PublicKey {
8
- value : string ;
8
+ n : string ;
9
+ g : string ;
9
10
}
10
11
11
12
// Ciphertext
@@ -29,14 +30,15 @@ describe("Paillier", function () {
29
30
} ;
30
31
31
32
// Public key
32
- const pub_n : PublicKey = {
33
- value : ethers . toBeHex ( publicKey . n ) ,
33
+ const pubKey : PublicKey = {
34
+ n : ethers . toBeHex ( publicKey . n ) ,
35
+ g : ethers . toBeHex ( publicKey . g ) ,
34
36
} ;
35
37
36
38
// bit length will differ to what has been stated in this script.
37
39
// if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
38
40
// Now lets deploy the contract and test the addition
39
- const enc_sum = await Paillier . add ( enc_a , enc_b , pub_n ) ;
41
+ const enc_sum = await Paillier . add ( enc_a , enc_b , pubKey ) ;
40
42
const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
41
43
42
44
// Conversion to int for convenience
@@ -47,6 +49,68 @@ describe("Paillier", function () {
47
49
expect ( dec_sum ) . to . equal ( 3 ) ;
48
50
} ) ;
49
51
52
+ it ( "should add a ciphertext and plaintext" , async function ( ) {
53
+ const { publicKey, privateKey } =
54
+ await paillierBigint . generateRandomKeys ( 256 ) ;
55
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
56
+ const a : bigint = BigInt ( 1 ) ;
57
+ const b : bigint = BigInt ( 2 ) ;
58
+ const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
59
+ const enc_b = { value : ethers . toBeHex ( publicKey . encrypt ( b ) ) } ;
60
+
61
+ // Public key
62
+ const pubKey : PublicKey = {
63
+ n : ethers . toBeHex ( publicKey . n ) ,
64
+ g : ethers . toBeHex ( publicKey . g ) ,
65
+ } ;
66
+ const enc_sum = await Paillier . add_const ( enc_a , b , pubKey ) ;
67
+ const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
68
+ const dec_sum = Number ( privateKey . decrypt ( enc_sum_int ) ) ;
69
+ console . log ( "Decrypted Sum:" , dec_sum ) ;
70
+ expect ( dec_sum ) . to . equal ( 3 ) ;
71
+ } ) ;
72
+
73
+ it ( "should subtract 2 ciphertexts" , async function ( ) {
74
+ const { publicKey, privateKey } =
75
+ await paillierBigint . generateRandomKeys ( 256 ) ;
76
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
77
+ const a : bigint = BigInt ( 5 ) ;
78
+ const b : bigint = BigInt ( 2 ) ;
79
+ const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
80
+ const enc_b = { value : ethers . toBeHex ( publicKey . encrypt ( b ) ) } ;
81
+
82
+ // Public key
83
+ const pubKey : PublicKey = {
84
+ n : ethers . toBeHex ( publicKey . n ) ,
85
+ g : ethers . toBeHex ( publicKey . g ) ,
86
+ } ;
87
+ const enc_diff = await Paillier . sub ( enc_a , enc_b , pubKey ) ;
88
+ const enc_diff_int = bigIntConversion . hexToBigint ( enc_diff [ 0 ] ) ;
89
+ const dec_diff = Number ( privateKey . decrypt ( enc_diff_int ) ) ;
90
+ console . log ( "Decrypted Difference:" , dec_diff ) ;
91
+ expect ( dec_diff ) . to . equal ( 3 ) ;
92
+ } ) ;
93
+
94
+ it ( "should subtract a ciphertext and plaintext" , async function ( ) {
95
+ const { publicKey, privateKey } =
96
+ await paillierBigint . generateRandomKeys ( 256 ) ;
97
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
98
+ const a : bigint = BigInt ( 42 ) ;
99
+ const b : bigint = BigInt ( 5 ) ;
100
+ const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
101
+
102
+ // Public key
103
+ const pubKey : PublicKey = {
104
+ n : ethers . toBeHex ( publicKey . n ) ,
105
+ g : ethers . toBeHex ( publicKey . g ) ,
106
+ } ;
107
+ const enc_diff = await Paillier . sub_const ( enc_a , b , pubKey ) ;
108
+ const enc_diff_int = bigIntConversion . hexToBigint ( enc_diff [ 0 ] ) ;
109
+ const dec_diff = Number ( privateKey . decrypt ( enc_diff_int ) ) ;
110
+ console . log ( "Decrypted Difference:" , dec_diff ) ;
111
+ expect ( dec_diff ) . to . equal ( 37 ) ;
112
+ } ) ;
113
+
50
114
it ( "should encrypt zero" , async function ( ) {
51
115
const { publicKey, privateKey } =
52
116
await paillierBigint . generateRandomKeys ( 256 ) ;
@@ -55,8 +119,11 @@ describe("Paillier", function () {
55
119
const rand = ethers . toBeHex ( Math . floor ( Math . random ( ) * 1000000 ) ) ;
56
120
57
121
// Public key
58
- const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
59
- const enc_zero = await Paillier . encryptZero ( rand , pub_n ) ;
122
+ const pubKey : PublicKey = {
123
+ n : ethers . toBeHex ( publicKey . n ) ,
124
+ g : ethers . toBeHex ( publicKey . g ) ,
125
+ } ;
126
+ const enc_zero = await Paillier . encryptZero ( rand , pubKey ) ;
60
127
const enc_zero_int = bigIntConversion . hexToBigint ( enc_zero [ 0 ] ) ;
61
128
const dec_zero = Number ( privateKey . decrypt ( enc_zero_int ) ) ;
62
129
expect ( dec_zero ) . to . equal ( 0 ) ;
@@ -72,8 +139,11 @@ describe("Paillier", function () {
72
139
const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
73
140
74
141
// Public key
75
- const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
76
- const enc_scalar = await Paillier . mul ( enc_a , b , pub_n ) ;
142
+ const pubKey : PublicKey = {
143
+ n : ethers . toBeHex ( publicKey . n ) ,
144
+ g : ethers . toBeHex ( publicKey . g ) ,
145
+ } ;
146
+ const enc_scalar = await Paillier . mul_const ( enc_a , b , pubKey ) ;
77
147
78
148
// returns tuple so get first index
79
149
const enc_scalar_int = bigIntConversion . hexToBigint ( enc_scalar [ 0 ] ) ;
0 commit comments