-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecc.ts
32 lines (26 loc) · 836 Bytes
/
ecc.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
import { Point } from "./point.ts";
import { PrivateKey } from "./types.ts";
import { Curve, Secp256k1 } from "./curve.ts";
import { getRandomNumber, int2Hex } from "./utils.ts";
export class ECC {
readonly sk: bigint;
readonly pk: Point;
readonly curve: Curve;
constructor(sk?: PrivateKey, curve?: Curve) {
this.curve = new Secp256k1();
if (curve) {
this.curve = curve;
}
const privateKey = sk || getRandomNumber(32, this.curve.n);
const G = new Point(this.curve, this.curve.gx, this.curve.gy);
const publicKey = G.scalarMul(privateKey);
this.sk = privateKey;
this.pk = publicKey;
}
// See: https://datatracker.ietf.org/doc/html/rfc5480
getPublicKey(): string {
const x = int2Hex(this.pk.x, false);
const y = int2Hex(this.pk.y, false);
return `0x04${x}${y}`;
}
}