-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
265 lines (241 loc) · 7.19 KB
/
index.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
'use strict'
const assert = require('assert')
const {isProbablyPrime} = require('bigint-crypto-utils')
const {eGcd, modPow, modInv} = require('bigint-mod-arith')
const {webcrypto: {subtle}} = require('crypto')
const tf = require('typeforce')
const constants = require('./constants')
const generatePrimes = require('./generate_primes')
const type = require('./type')
class Accumulator {
/**
* Creates a new Accumulator instance. An Accumulator is a trusted party that stores a secret
* and can modify the accumulation of member elements.
* @param {(String|function)} H The name of a hash algorithm or a function that returns a digest
* for an input String or Buffer.
* @param {(Primes|BigInt)} [key] Optional secret primes or public modulus. If no argument
* given, secret primes will be generated.
*/
constructor(H, key) {
tf(tf.tuple(type.Hash, tf.maybe(tf.oneOf(type.Primes, type.BigInt))), arguments)
if (type.BigInt(key)) {
this.n = key
} else {
if (!type.Primes(key)) {
key = generatePrimes(constants.MODULUS_BITS)
}
const {p, q} = key
this.n = p * q
this.d = (p - 1n) * (q - 1n)
}
this.H = H
this.z = BigInt(constants.BASE)
}
/**
* Add an element to the accumulator.
* @param {(String|Buffer)} x The element to add.
* @returns {Promise<Witness>} A witness of the element's membership.
*/
async add(x) {
tf(tf.tuple(type.Data), arguments)
const {y, nonce} = await mapToPrime(this.H, x)
const w = this.z
const n = this.n
this.z = modPow(this.z, y, this.n)
return new Witness(x, nonce, w)
}
/**
* Delete an element from the accumulation.
* @param {Witness} witness Witness of element to delete.
* @returns {Promise<BigInt>} The new accumulation.
*/
async del({x, nonce}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
const n = this.n
this.z = modPow(this.z, modInv(y, this.d), this.n)
return this.z
}
/**
* Verify an element is a member of the accumulation.
* @param {Witness} witness A witness of the element's membership.
* @returns {Promise<Boolean>} True if element is a member of the accumulation;
* false otherwise.
*/
async verify({x, nonce, w}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
return modPow(w, y, this.n) === this.z
}
/**
* Prove an element's membership.
* @param {(String|Buffer)} x The element to prove.
* @returns {Promise<Witness>} A witness of the element's membership.
*/
async prove(x) {
tf(tf.tuple(type.Data), arguments)
const {y, nonce} = await mapToPrime(this.H, x)
const w = modPow(this.z, modInv(y, this.d), this.n)
const n = this.n
const z = this.z
return new Witness(x, nonce, w, n, z)
}
}
class Witness {
/**
* Creates a new Witness instance.
* @param {Data} x The element.
* @param {BigInt} nonce Sums to a prime when added to `H(x)`.
* @param {BigInt} w The accumulation value less the element.
*/
constructor(x, nonce, w) {
tf(tf.tuple(type.Data, type.BigInt, type.BigInt), arguments)
this.x = x
this.nonce = nonce
this.w = w
}
}
class Update {
/**
* Creates a new Update instance.
* @param {Accumulator} accumulator The current accumulation.
*/
constructor({H, n, z}) {
tf(tf.tuple(type.Accumulator), arguments)
this.H = H
this.n = n
this.z = z
this.piA = 1n
this.piD = 1n
}
/**
* Absorb an addition to the update.
* @param {Witness} witness A witness of the element's addition.
*/
async add({x, nonce}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
this.piA *= y
}
/**
* Absorb a deletion to the update.
* @param {Witness} witness A witness of the element's addition.
*/
async del({x, nonce}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
this.piD *= y
}
/**
* Remove an addition from the update.
* @param {Witness} witness A witness of the element's addition.
*/
async undoAdd({x, nonce}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
this.piA /= y
}
/**
* Remove a deletion from the update.
* @param {Witness} witness A witness of the element's addition.
*/
async undoDel({x, nonce}) {
tf(tf.tuple(type.Witness), arguments)
const y = await mapToBigInt(this.H, x) + nonce
this.piD /= y
}
/**
* Update the witness. This must be called after each addition to or deletion
* from the accumulation for each remaining element before it may be successfully verified.
* @param {Witness} witness The witness to update.
* @returns {Promise<Witness>} An updated witness.
*/
async update({x, nonce, w}) {
tf(tf.tuple(tf.oneOf(type.Witness)), arguments)
const y = await mapToBigInt(this.H, x) + nonce
const {x: a, y: b} = eGcd(this.piD, y)
return new Witness(
x,
nonce,
(modPow(w, a * this.piA, this.n) * modPow(this.z, b, this.n)) % this.n,
)
}
}
/**
* Find a prime including or after a number.
* @param {BigInt} y The number.
* @returns {BigInt} The prime.
* @private
*/
function nextPrime(y) {
tf(tf.tuple(type.BigInt), arguments)
if (y % 2n === 0n) {
y += 1n
if (isProbablyPrime(y, 24)) {
return y
}
}
do {
y += 2n
if (isProbablyPrime(y, 24)) {
return y
}
} while (true)
}
/**
* Return a hex string representing the data in a buffer.
* @param {Buffer} buffer The buffer to hexlify.
* @returns {String} The hex representation of the buffer.
* @private
*/
function bufferToHex(buffer) {
tf(tf.tuple(tf.oneOf(tf.Buffer, tf.quacksLike('ArrayBuffer'))), arguments)
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('')
}
/**
* Produce a hash digest from some data.
* @param {(String|function)} H The name of a hash algorithm or a function that produces a
* digest for an input String or Buffer.
* @param {(String|Buffer)} x The element to map.
* @returns {Promise<Buffer>} The digest.
* @private
*/
async function hash(H, x) {
tf(tf.tuple(type.Hash, type.Data), arguments)
if (typeof(H) === 'string') {
return await subtle.digest(H, x)
}
return H(x)
}
/**
* Hash an element and interpret it as a number.
* @param {(String|Buffer)} x The element to map.
* @returns {Promise<Buffer>} The digest.
* @private
*/
async function mapToBigInt(H, x) {
tf(tf.tuple(type.Hash, type.Data), arguments)
const maxPrime = 1n << BigInt(constants.PRIME_BITS)
return BigInt('0x' + bufferToHex(await hash(H, x))) & (maxPrime - 1n)
}
/**
* Map data to a prime number.
* @param {(String|function)} H The name of a hash algorithm or a function that produces a
* digest for an input String or Buffer.
* @param {(String|Buffer)} x The element to map.
* @returns {BigInt} The prime.
* @private
*/
async function mapToPrime(H, x) {
tf(tf.tuple(type.Hash, type.Data), arguments)
const maxPrime = 1n << BigInt(constants.PRIME_BITS)
const d = await mapToBigInt(H, x)
const y = nextPrime(d) % maxPrime
const nonce = y - d
return {y, nonce}
}
module.exports = {
Accumulator,
Witness,
Update,
}