@@ -5,151 +5,152 @@ import { expect } from "chai";
5
5
6
6
// Public key
7
7
interface PublicKey {
8
- n : string ;
9
- g : string ;
8
+ n : string ;
9
+ g : string ;
10
10
}
11
11
12
12
// Ciphertext
13
13
interface Ciphertext {
14
- value : string ;
14
+ value : string ;
15
15
}
16
16
17
17
describe ( "Paillier" , function ( ) {
18
- it ( "should add 2 ciphertexts" , async function ( ) {
19
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
20
-
21
- const { publicKey, privateKey } =
22
- await paillierBigint . generateRandomKeys ( 256 ) ;
23
- const a : bigint = BigInt ( 1 ) ;
24
- const b : bigint = BigInt ( 2 ) ;
25
- const enc_a : Ciphertext = {
26
- value : ethers . toBeHex ( publicKey . encrypt ( a ) ) ,
27
- } ;
28
- const enc_b : Ciphertext = {
29
- value : ethers . toBeHex ( publicKey . encrypt ( b ) ) ,
30
- } ;
31
-
32
- // Public key
33
- const pubKey : PublicKey = {
34
- n : ethers . toBeHex ( publicKey . n ) ,
35
- g : ethers . toBeHex ( publicKey . g ) ,
36
- } ;
37
-
38
- // bit length will differ to what has been stated in this script.
39
- // if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
40
- // Now lets deploy the contract and test the addition
41
- const enc_sum = await Paillier . add ( enc_a , enc_b , pubKey ) ;
42
- const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
43
-
44
- // Conversion to int for convenience
45
- const dec_sum = Number ( privateKey . decrypt ( enc_sum_int ) ) ;
46
- console . log ( "Decrypted Sum:" , dec_sum ) ;
47
-
48
- // We want dec_sum to equal 3
49
- expect ( dec_sum ) . to . equal ( 3 ) ;
50
- } ) ;
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
-
114
- it ( "should encrypt zero" , async function ( ) {
115
- const { publicKey, privateKey } =
116
- await paillierBigint . generateRandomKeys ( 256 ) ;
117
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
118
- // Arbitary random number - 1000000
119
- const rand = ethers . toBeHex ( Math . floor ( Math . random ( ) * 1000000 ) ) ;
120
-
121
- // Public key
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 ) ;
127
- const enc_zero_int = bigIntConversion . hexToBigint ( enc_zero [ 0 ] ) ;
128
- const dec_zero = Number ( privateKey . decrypt ( enc_zero_int ) ) ;
129
- expect ( dec_zero ) . to . equal ( 0 ) ;
130
- } ) ;
131
-
132
- it ( "should multiply encrypted value by a scalar" , async function ( ) {
133
- const { publicKey, privateKey } =
134
- await paillierBigint . generateRandomKeys ( 256 ) ;
135
- const [ owner ] = await ethers . getSigners ( ) ;
136
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
137
- const a : bigint = BigInt ( 2 ) ;
138
- const b : bigint = BigInt ( 5 ) ;
139
- const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
140
-
141
- // Public key
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 ) ;
147
-
148
- // returns tuple so get first index
149
- const enc_scalar_int = bigIntConversion . hexToBigint ( enc_scalar [ 0 ] ) ;
150
-
151
- const dec_scalar = Number ( privateKey . decrypt ( enc_scalar_int ) ) ;
152
- console . log ( "Decrypted Scalar:" , dec_scalar ) ;
153
- expect ( dec_scalar ) . to . equal ( 10 ) ;
154
- } ) ;
18
+ it ( "should add 2 ciphertexts" , async function ( ) {
19
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
20
+
21
+ const { publicKey, privateKey } =
22
+ await paillierBigint . generateRandomKeys ( 256 ) ;
23
+ const a : bigint = BigInt ( 1 ) ;
24
+ const b : bigint = BigInt ( 2 ) ;
25
+ const enc_a : Ciphertext = {
26
+ value : ethers . toBeHex ( publicKey . encrypt ( a ) ) ,
27
+ } ;
28
+ const enc_b : Ciphertext = {
29
+ value : ethers . toBeHex ( publicKey . encrypt ( b ) ) ,
30
+ } ;
31
+
32
+ // Public key
33
+ const pubKey : PublicKey = {
34
+ n : ethers . toBeHex ( publicKey . n ) ,
35
+ g : ethers . toBeHex ( publicKey . g ) ,
36
+ } ;
37
+
38
+ // bit length will differ to what has been stated in this script.
39
+ // if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
40
+ // Now lets deploy the contract and test the addition
41
+ const enc_sum = await Paillier . add ( enc_a , enc_b , pubKey ) ;
42
+ const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
43
+
44
+ // Conversion to int for convenience
45
+ const dec_sum = Number ( privateKey . decrypt ( enc_sum_int ) ) ;
46
+ console . log ( "Decrypted Sum:" , dec_sum ) ;
47
+
48
+ // We want dec_sum to equal 3
49
+ expect ( dec_sum ) . to . equal ( 3 ) ;
50
+ } ) ;
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
+
114
+ it ( "should encrypt zero" , async function ( ) {
115
+ const { publicKey, privateKey } =
116
+ await paillierBigint . generateRandomKeys ( 256 ) ;
117
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
118
+ // Arbitary random number - 1000000
119
+ const rand = ethers . toBeHex ( Math . floor ( Math . random ( ) * 1000000 ) ) ;
120
+
121
+ // Public key
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 ) ;
127
+ const enc_zero_int = bigIntConversion . hexToBigint ( enc_zero [ 0 ] ) ;
128
+ const dec_zero = Number ( privateKey . decrypt ( enc_zero_int ) ) ;
129
+ expect ( dec_zero ) . to . equal ( 0 ) ;
130
+ } ) ;
131
+
132
+ it ( "should multiply encrypted value by a scalar" , async function ( ) {
133
+ const { publicKey, privateKey } =
134
+ await paillierBigint . generateRandomKeys ( 256 ) ;
135
+ const [ owner ] = await ethers . getSigners ( ) ;
136
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
137
+ const a : bigint = BigInt ( 2 ) ;
138
+ const b : bigint = BigInt ( 5 ) ;
139
+ const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
140
+
141
+ // Public key
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 ) ;
147
+
148
+ // returns tuple so get first index
149
+ const enc_scalar_int = bigIntConversion . hexToBigint ( enc_scalar [ 0 ] ) ;
150
+
151
+ const dec_scalar = Number ( privateKey . decrypt ( enc_scalar_int ) ) ;
152
+ console . log ( "Decrypted Scalar:" , dec_scalar ) ;
153
+ expect ( dec_scalar ) . to . equal ( 10 ) ;
154
+ } ) ;
155
+
155
156
} ) ;
0 commit comments