-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr1.ts
76 lines (64 loc) · 1.86 KB
/
r1.ts
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
import BN from "bn.js";
import * as elliptic from "elliptic";
const secp256r1 = new elliptic.ec("p256");
export const compressedToFullPublicKey = (publicKey: Uint8Array) => {
const ec = new elliptic.ec("p256");
const key = ec.keyFromPublic(publicKey);
const fullPublicKey = key.getPublic(false, "hex");
return fullPublicKey.slice(2);
};
export function isPoint(p: Uint8Array): boolean {
try {
const { result } = secp256r1.keyFromPublic(p).validate();
return result;
} catch {
return false;
}
}
export function isPrivate(d: Uint8Array): boolean {
const { result } = secp256r1.keyFromPrivate(d).validate();
return result;
}
export function pointFromScalar(
d: Uint8Array,
compressed?: boolean
): Uint8Array | null {
if (!isPrivate(d)) return null;
return new TextEncoder().encode(
secp256r1.keyFromPrivate(d).getPublic((compressed ??= true), "hex")
);
}
export function pointAddScalar(
p: Uint8Array,
tweak: Uint8Array,
compressed?: boolean
): Uint8Array | null {
if (new BN(tweak).isZero()) return p;
if (!isPrivate(tweak)) return null;
const point = (secp256r1.keyFromPublic(p) as any).pub;
const result = secp256r1.g.mul(tweak).add(point);
return Buffer.from(result.encode('hex', !!compressed), 'hex');
}
export function privateAdd(
d: Uint8Array,
tweak: Uint8Array
): Uint8Array | null {
if (new BN(tweak).isZero()) return d;
if (!isPrivate(tweak)) return null;
return new BN(d)
.add(new BN(tweak))
.mod(secp256r1.n as any)
.toBuffer();
}
export function sign(h: Uint8Array, d: Uint8Array, e?: Uint8Array): Uint8Array {
const sig = secp256r1.sign(h, Buffer.from(d.buffer));
return new TextEncoder().encode(sig.toDER("hex"));
}
export function verify(
h: Uint8Array,
Q: Uint8Array,
signature: Uint8Array,
strict?: boolean
): boolean {
return secp256r1.verify(h, signature, Buffer.from(Q.buffer));
}