-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblindFactorProof.js
121 lines (96 loc) · 3.56 KB
/
blindFactorProof.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const debug = require('debug');
const ChaumPedersen = require('../chaum-pedersen');
const ElGamal = require('basic_simple_elgamal');
const bigInteger = require('big-integer');
const log = debug('app::NIZKP::Chaum-Pedersen::test::BlindFactor');
async function test() {
const elgamal = new ElGamal()
await elgamal.initializeRemotely(2048);
elgamal.checkSecurity();
log('public key after security checking: ', elgamal.publicKey);
//ElGamal is ready to use:
let message = await elgamal.randomGropuMember();
log('message: ', message);
/**
* Encrypt the message:
*/
let cipherText = await elgamal.encrypt(
message
);
log('cipherText: ', cipherText);
let secrets = elgamal.export(true);
/**
* obtain a random blind factor:
*/
let blindFactor = await elgamal.randomGropuMember();
log('blind factor: ', blindFactor)
/**
* Blind original encryption:
*/
let sharedSecretBlinder = elgamal.power(blindFactor);
let messageBlinder = bigInteger(elgamal.publicKey).modPow(blindFactor, elgamal.modulus);
log('Public key after sharedBlinder computations:', elgamal.publicKey);
let sharedSecretBlinded = elgamal.multiply(cipherText.c1, sharedSecretBlinder);
let messageBlinded = elgamal.multiply(cipherText.c2, messageBlinder);
log('blinder secret: ', elgamal.power(blindFactor));
log('blinder message: ', bigInteger(elgamal.publicKey).modPow(blindFactor, elgamal.modulus));
log('Public key after messageBlinder computations:', elgamal.publicKey);
/**
* Prepare chaum-pedersen protocol:
*/
let chaumPedersen = new ChaumPedersen(elgamal);
log('Public key after initializing ChaumPedersen', elgamal.publicKey);
log('g^{r+s} = g^r^s: ',
sharedSecretBlinded.equals(
elgamal.power(
blindFactor.add(secrets.r).mod(elgamal.groupOrder)
)
)
);
log('g^{r+s}/g^r = g^s: ',
sharedSecretBlinded.multiply(
elgamal.power(secrets.r).modInv(elgamal.modulus)
).mod(elgamal.modulus).equals(
elgamal.power(blindFactor)
)
);
log('g^{s} verification: ',
sharedSecretBlinded.multiply(cipherText.c1.modInv(elgamal.modulus)).mod(elgamal.modulus).equals(
elgamal.power(blindFactor)
));
elgamal.power(blindFactor);
elgamal.publicKey;
bigInteger(elgamal.publicKey).modPow(blindFactor, elgamal.modulus);
log('Public key after all tests: ', elgamal.publicKey);
/**
* Prove the correctness of shared secret:
*/
let proof = await chaumPedersen.prove(
blindFactor,
elgamal.power(blindFactor),
elgamal.publicKey,
bigInteger(elgamal.publicKey).modPow(blindFactor, elgamal.modulus)
);
log('Public key after proof:', elgamal.publicKey)
log('proof: ', proof);
let result = chaumPedersen.verify(
proof,
sharedSecretBlinded.multiply(cipherText.c1.modInv(elgamal.modulus)).mod(elgamal.modulus),
elgamal.publicKey,
messageBlinded.multiply(cipherText.c2.modInv(elgamal.modulus)).mod(elgamal.modulus)
);
/*
log('y^{s}: ',
sharedSecretBlinded.multiply(cipherText.c1.modInv(elgamal.modulus)).mod(elgamal.modulus).equals(
elgamal.power(blindFactor)
)
);
log('g^{s}: ',
messageBlinded.multiply(cipherText.c2.modInv(elgamal.modulus)).mod(elgamal.modulus).equals(
messageBlinded.multiply(cipherText.c2.modInv(elgamal.modulus)).mod(elgamal.modulus)
)
)
*/
log('Verify: ', result);
}
test();