-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathset.js
48 lines (40 loc) · 1.04 KB
/
set.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
// this creates/verifies sets of proof of work challenges
var crypto = require('crypto');
var pence = require('./pence');
exports.generate = function(N)
{
var ret = {N:N};
ret.pence = {};
ret.secrets = {};
ret.nonce = crypto.randomBytes(24);
for(var i = 0; i < 100; i++)
{
var p = pence.pence(N, ret.nonce);
ret.secrets[p.ID.toString('hex')] = p;
ret.pence[p.ID.toString('hex')] = p.pN.toString('hex');
}
return ret;
}
exports.verify = function(set, secrets)
{
// generate a set from each secret and remove it by ID
var p0;
while(p0 = secrets.pop())
{
var p = pence.pence(set.N, set.nonce, p0);
var id = p.ID.toString('hex');
if(!set.pence[id]) return false;
// the pN must also match
if(set.pence[id].toString('hex') != p.pN.toString('hex')) return false;
delete set.pence[id];
}
// should only be one left un-verified
if(Object.keys(set.pence).length != 1) return false;
// all good
return true;
}
// in browser
if(typeof window !== "undefined")
{
window.libset = exports;
}