@@ -5,81 +5,81 @@ import { expect } from "chai";
5
5
6
6
// Public key
7
7
interface PublicKey {
8
- value : string ;
8
+ value : string ;
9
9
}
10
10
11
11
// Ciphertext
12
12
interface Ciphertext {
13
- value : string ;
13
+ value : string ;
14
14
}
15
15
16
16
describe ( "Paillier" , function ( ) {
17
- it ( "should add 2 ciphertexts" , async function ( ) {
18
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
17
+ it ( "should add 2 ciphertexts" , async function ( ) {
18
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
19
19
20
- const { publicKey, privateKey } =
21
- await paillierBigint . generateRandomKeys ( 256 ) ;
22
- const a : bigint = BigInt ( 1 ) ;
23
- const b : bigint = BigInt ( 2 ) ;
24
- const enc_a : Ciphertext = {
25
- value : ethers . toBeHex ( publicKey . encrypt ( a ) ) ,
26
- } ;
27
- const enc_b : Ciphertext = {
28
- value : ethers . toBeHex ( publicKey . encrypt ( b ) ) ,
29
- } ;
20
+ const { publicKey, privateKey } =
21
+ await paillierBigint . generateRandomKeys ( 256 ) ;
22
+ const a : bigint = BigInt ( 1 ) ;
23
+ const b : bigint = BigInt ( 2 ) ;
24
+ const enc_a : Ciphertext = {
25
+ value : ethers . toBeHex ( publicKey . encrypt ( a ) ) ,
26
+ } ;
27
+ const enc_b : Ciphertext = {
28
+ value : ethers . toBeHex ( publicKey . encrypt ( b ) ) ,
29
+ } ;
30
30
31
- // Public key
32
- const pub_n : PublicKey = {
33
- value : ethers . toBeHex ( publicKey . n ) ,
34
- } ;
31
+ // Public key
32
+ const pub_n : PublicKey = {
33
+ value : ethers . toBeHex ( publicKey . n ) ,
34
+ } ;
35
35
36
- // bit length will differ to what has been stated in this script.
37
- // if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
38
- // Now lets deploy the contract and test the addition
39
- const enc_sum = await Paillier . add ( enc_a , enc_b , pub_n ) ;
40
- const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
36
+ // bit length will differ to what has been stated in this script.
37
+ // if using 256-bit key, bit_length will be 264 as "0x" prefix may have been factored in
38
+ // Now lets deploy the contract and test the addition
39
+ const enc_sum = await Paillier . add ( enc_a , enc_b , pub_n ) ;
40
+ const enc_sum_int = bigIntConversion . hexToBigint ( enc_sum [ 0 ] ) ;
41
41
42
- // Conversion to int for convenience
43
- const dec_sum = Number ( privateKey . decrypt ( enc_sum_int ) ) ;
44
- console . log ( "Decrypted Sum:" , dec_sum ) ;
42
+ // Conversion to int for convenience
43
+ const dec_sum = Number ( privateKey . decrypt ( enc_sum_int ) ) ;
44
+ console . log ( "Decrypted Sum:" , dec_sum ) ;
45
45
46
- // We want dec_sum to equal 3
47
- expect ( dec_sum ) . to . equal ( 3 ) ;
48
- } ) ;
46
+ // We want dec_sum to equal 3
47
+ expect ( dec_sum ) . to . equal ( 3 ) ;
48
+ } ) ;
49
49
50
- it ( "should encrypt zero" , async function ( ) {
51
- const { publicKey, privateKey } =
52
- await paillierBigint . generateRandomKeys ( 256 ) ;
53
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
54
- // Arbitary random number - 1000000
55
- const rand = ethers . toBeHex ( Math . floor ( Math . random ( ) * 1000000 ) ) ;
50
+ it ( "should encrypt zero" , async function ( ) {
51
+ const { publicKey, privateKey } =
52
+ await paillierBigint . generateRandomKeys ( 256 ) ;
53
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
54
+ // Arbitary random number - 1000000
55
+ const rand = ethers . toBeHex ( Math . floor ( Math . random ( ) * 1000000 ) ) ;
56
56
57
- // Public key
58
- const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
59
- const enc_zero = await Paillier . encryptZero ( rand , pub_n ) ;
60
- const enc_zero_int = bigIntConversion . hexToBigint ( enc_zero [ 0 ] ) ;
61
- const dec_zero = Number ( privateKey . decrypt ( enc_zero_int ) ) ;
62
- expect ( dec_zero ) . to . equal ( 0 ) ;
63
- } ) ;
57
+ // Public key
58
+ const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
59
+ const enc_zero = await Paillier . encryptZero ( rand , pub_n ) ;
60
+ const enc_zero_int = bigIntConversion . hexToBigint ( enc_zero [ 0 ] ) ;
61
+ const dec_zero = Number ( privateKey . decrypt ( enc_zero_int ) ) ;
62
+ expect ( dec_zero ) . to . equal ( 0 ) ;
63
+ } ) ;
64
64
65
- it ( "should multiply encrypted value by a scalar" , async function ( ) {
66
- const { publicKey, privateKey } =
67
- await paillierBigint . generateRandomKeys ( 256 ) ;
68
- const [ owner ] = await ethers . getSigners ( ) ;
69
- const Paillier = await ethers . deployContract ( "Paillier" ) ;
70
- const a : bigint = BigInt ( 2 ) ;
71
- const b : bigint = BigInt ( 5 ) ;
72
- const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
65
+ it ( "should multiply encrypted value by a scalar" , async function ( ) {
66
+ const { publicKey, privateKey } =
67
+ await paillierBigint . generateRandomKeys ( 256 ) ;
68
+ const [ owner ] = await ethers . getSigners ( ) ;
69
+ const Paillier = await ethers . deployContract ( "Paillier" ) ;
70
+ const a : bigint = BigInt ( 2 ) ;
71
+ const b : bigint = BigInt ( 5 ) ;
72
+ const enc_a = { value : ethers . toBeHex ( publicKey . encrypt ( a ) ) } ;
73
73
74
- // Public key
75
- const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
76
- const enc_scalar = await Paillier . mul ( enc_a , b , pub_n ) ;
74
+ // Public key
75
+ const pub_n = { value : ethers . toBeHex ( publicKey . n ) } ;
76
+ const enc_scalar = await Paillier . mul ( enc_a , b , pub_n ) ;
77
77
78
- // returns tuple so get first index
79
- const enc_scalar_int = bigIntConversion . hexToBigint ( enc_scalar [ 0 ] ) ;
78
+ // returns tuple so get first index
79
+ const enc_scalar_int = bigIntConversion . hexToBigint ( enc_scalar [ 0 ] ) ;
80
80
81
- const dec_scalar = Number ( privateKey . decrypt ( enc_scalar_int ) ) ;
82
- console . log ( "Decrypted Scalar:" , dec_scalar ) ;
83
- expect ( dec_scalar ) . to . equal ( 10 ) ;
84
- } ) ;
81
+ const dec_scalar = Number ( privateKey . decrypt ( enc_scalar_int ) ) ;
82
+ console . log ( "Decrypted Scalar:" , dec_scalar ) ;
83
+ expect ( dec_scalar ) . to . equal ( 10 ) ;
84
+ } ) ;
85
85
} ) ;
0 commit comments