-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathprivates.js
77 lines (64 loc) · 1.98 KB
/
privates.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
import test from "tape";
import { fromHex } from "./util.js";
import fprivates from "./fixtures/privates.json" assert { type: "json" };
export default function (secp256k1, type) {
test(`isPrivate (${type})`, (t) => {
for (const f of fprivates.valid.isPrivate) {
const d = fromHex(f.d);
t.equal(
secp256k1.isPrivate(d),
f.expected,
`${f.d} is ${f.expected ? "OK" : "rejected"}`
);
}
t.end();
});
test(`privateAdd (${type})`, (t) => {
for (const f of fprivates.valid.privateAdd) {
const d = fromHex(f.d);
const tweak = fromHex(f.tweak);
const expected = f.expected ? fromHex(f.expected) : null;
let description = `${f.d} + ${f.tweak} = ${
f.expected ? f.expected : null
}`;
if (f.description) description += ` (${f.description})`;
t.same(secp256k1.privateAdd(d, tweak), expected, description);
}
for (const f of fprivates.invalid.privateAdd) {
const d = fromHex(f.d);
const tweak = fromHex(f.tweak);
t.throws(
() => {
secp256k1.privateAdd(d, tweak);
},
new RegExp(f.exception),
`${f.description} throws ${f.exception}`
);
}
t.end();
});
test(`privateSub (${type})`, (t) => {
for (const f of fprivates.valid.privateSub) {
const d = fromHex(f.d);
const tweak = fromHex(f.tweak);
const expected = f.expected ? fromHex(f.expected) : null;
let description = `${f.d} - ${f.tweak} = ${
f.expected ? f.expected : null
}`;
if (f.description) description += ` (${f.description})`;
t.same(secp256k1.privateSub(d, tweak), expected, description);
}
for (const f of fprivates.invalid.privateSub) {
const d = fromHex(f.d);
const tweak = fromHex(f.tweak);
t.throws(
() => {
secp256k1.privateSub(d, tweak);
},
new RegExp(f.exception),
`${f.description} throws ${f.exception}`
);
}
t.end();
});
}