ecdsa-quirks 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.js +24 -16
- package/package.json +1 -1
- package/src/index.ts +31 -21
package/dist/src/index.js
CHANGED
|
@@ -7,24 +7,26 @@ import { Command } from "commander";
|
|
|
7
7
|
import { expect } from "chai";
|
|
8
8
|
// based on https://www.di.ens.fr/david.pointcheval/Documents/Papers/2002_cryptoA.pdf
|
|
9
9
|
function quirk(message1, message2, eip191) {
|
|
10
|
+
let message1Hash;
|
|
11
|
+
let message2Hash;
|
|
10
12
|
// EIP-191 hash
|
|
11
13
|
if (eip191) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
message1Hash = ethers.hashMessage(message1);
|
|
15
|
+
message2Hash = ethers.hashMessage(message2);
|
|
14
16
|
}
|
|
15
17
|
else {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
message1Hash = ethers.keccak256(ethers.toUtf8Bytes(message1));
|
|
19
|
+
message2Hash = ethers.keccak256(ethers.toUtf8Bytes(message2));
|
|
18
20
|
}
|
|
19
21
|
const n = secp256k1.Point.CURVE().n;
|
|
20
22
|
const k = ethers.hexlify(ethers.randomBytes(32));
|
|
21
23
|
const r = "0x" + ethers.hexlify(secp256k1.getPublicKey(Buffer.from(k.slice(2), "hex"))).slice(4); // take x coordinate
|
|
22
24
|
// x = -((h1 + h2) / 2r) (mod n)
|
|
23
|
-
const numer = BigInt(
|
|
25
|
+
const numer = BigInt(message1Hash) + BigInt(message2Hash);
|
|
24
26
|
const denom = BigInt(modInv(2n * BigInt(r), BigInt(n)));
|
|
25
27
|
const x = BigInt(n) - ((numer * denom) % BigInt(n));
|
|
26
28
|
// regular ECDSA signature
|
|
27
|
-
let s = (BigInt(modInv(BigInt(k), BigInt(n))) * (BigInt(
|
|
29
|
+
let s = (BigInt(modInv(BigInt(k), BigInt(n))) * (BigInt(message1Hash) + x * BigInt(r))) % BigInt(n);
|
|
28
30
|
// make s be from the lower part of the curve
|
|
29
31
|
if (s > n / 2n) {
|
|
30
32
|
s = n - s;
|
|
@@ -32,14 +34,14 @@ function quirk(message1, message2, eip191) {
|
|
|
32
34
|
const wallet = new ethers.Wallet(ethers.toBeHex(x, 32));
|
|
33
35
|
let sig1 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1b";
|
|
34
36
|
let sig2 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1c";
|
|
35
|
-
if (ethers.
|
|
37
|
+
if (ethers.recoverAddress(message1Hash, sig1) != wallet.address) {
|
|
36
38
|
const tmp = sig1;
|
|
37
39
|
sig1 = sig2;
|
|
38
40
|
sig2 = tmp;
|
|
39
41
|
}
|
|
40
42
|
// sanity check
|
|
41
|
-
expect(ethers.
|
|
42
|
-
expect(ethers.
|
|
43
|
+
expect(ethers.recoverAddress(message1Hash, sig1)).to.eq(wallet.address);
|
|
44
|
+
expect(ethers.recoverAddress(message2Hash, sig2)).to.eq(wallet.address);
|
|
43
45
|
return {
|
|
44
46
|
privateKey: ethers.toBeHex(x, 32),
|
|
45
47
|
address: wallet.address,
|
|
@@ -47,7 +49,7 @@ function quirk(message1, message2, eip191) {
|
|
|
47
49
|
signature2: sig2,
|
|
48
50
|
};
|
|
49
51
|
}
|
|
50
|
-
function
|
|
52
|
+
function printQuirked(message1, message2, quirked) {
|
|
51
53
|
console.log("Private key: " + quirked.privateKey);
|
|
52
54
|
console.log("Address: " + quirked.address);
|
|
53
55
|
console.log();
|
|
@@ -66,12 +68,18 @@ async function main() {
|
|
|
66
68
|
.option("--m2, --message2 <MSG>", "The second message")
|
|
67
69
|
.option("--eip191", "Whether to EIP-191 hash the messages before signing", false)
|
|
68
70
|
.showHelpAfterError();
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
try {
|
|
72
|
+
const parsed = await program.parseAsync(process.argv);
|
|
73
|
+
const opts = parsed.opts();
|
|
74
|
+
if (!opts.message1 || !opts.message2) {
|
|
75
|
+
throw new Error("Specify both messages to generate the signature for");
|
|
76
|
+
}
|
|
77
|
+
const quirked = quirk(opts.message1, opts.message2, opts.eip191);
|
|
78
|
+
printQuirked(opts.message1, opts.message2, quirked);
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
82
|
+
throw new Error(msg);
|
|
73
83
|
}
|
|
74
|
-
const quirked = quirk(opts.message1, opts.message2, opts.eip191);
|
|
75
|
-
printQuired(opts.message1, opts.message2, quirked);
|
|
76
84
|
}
|
|
77
85
|
void main();
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -18,13 +18,16 @@ type Quirked = {
|
|
|
18
18
|
|
|
19
19
|
// based on https://www.di.ens.fr/david.pointcheval/Documents/Papers/2002_cryptoA.pdf
|
|
20
20
|
function quirk(message1: string, message2: string, eip191: boolean): Quirked {
|
|
21
|
+
let message1Hash;
|
|
22
|
+
let message2Hash;
|
|
23
|
+
|
|
21
24
|
// EIP-191 hash
|
|
22
25
|
if (eip191) {
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
message1Hash = ethers.hashMessage(message1);
|
|
27
|
+
message2Hash = ethers.hashMessage(message2);
|
|
25
28
|
} else {
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
message1Hash = ethers.keccak256(ethers.toUtf8Bytes(message1));
|
|
30
|
+
message2Hash = ethers.keccak256(ethers.toUtf8Bytes(message2));
|
|
28
31
|
}
|
|
29
32
|
|
|
30
33
|
const n = secp256k1.Point.CURVE().n;
|
|
@@ -32,12 +35,13 @@ function quirk(message1: string, message2: string, eip191: boolean): Quirked {
|
|
|
32
35
|
const r = "0x" + ethers.hexlify(secp256k1.getPublicKey(Buffer.from(k.slice(2), "hex"))).slice(4); // take x coordinate
|
|
33
36
|
|
|
34
37
|
// x = -((h1 + h2) / 2r) (mod n)
|
|
35
|
-
const numer = BigInt(
|
|
38
|
+
const numer = BigInt(message1Hash) + BigInt(message2Hash);
|
|
36
39
|
const denom = BigInt(modInv(2n * BigInt(r), BigInt(n)));
|
|
37
40
|
const x = BigInt(n) - ((numer * denom) % BigInt(n));
|
|
38
41
|
|
|
39
42
|
// regular ECDSA signature
|
|
40
|
-
let s =
|
|
43
|
+
let s =
|
|
44
|
+
(BigInt(modInv(BigInt(k), BigInt(n))) * (BigInt(message1Hash) + x * BigInt(r))) % BigInt(n);
|
|
41
45
|
|
|
42
46
|
// make s be from the lower part of the curve
|
|
43
47
|
if (s > n / 2n) {
|
|
@@ -49,15 +53,15 @@ function quirk(message1: string, message2: string, eip191: boolean): Quirked {
|
|
|
49
53
|
let sig1 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1b";
|
|
50
54
|
let sig2 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1c";
|
|
51
55
|
|
|
52
|
-
if (ethers.
|
|
56
|
+
if (ethers.recoverAddress(message1Hash, sig1) != wallet.address) {
|
|
53
57
|
const tmp = sig1;
|
|
54
58
|
sig1 = sig2;
|
|
55
59
|
sig2 = tmp;
|
|
56
60
|
}
|
|
57
61
|
|
|
58
62
|
// sanity check
|
|
59
|
-
expect(ethers.
|
|
60
|
-
expect(ethers.
|
|
63
|
+
expect(ethers.recoverAddress(message1Hash, sig1)).to.eq(wallet.address);
|
|
64
|
+
expect(ethers.recoverAddress(message2Hash, sig2)).to.eq(wallet.address);
|
|
61
65
|
|
|
62
66
|
return {
|
|
63
67
|
privateKey: ethers.toBeHex(x, 32),
|
|
@@ -67,7 +71,7 @@ function quirk(message1: string, message2: string, eip191: boolean): Quirked {
|
|
|
67
71
|
};
|
|
68
72
|
}
|
|
69
73
|
|
|
70
|
-
function
|
|
74
|
+
function printQuirked(message1: string, message2: string, quirked: Quirked) {
|
|
71
75
|
console.log("Private key: " + quirked.privateKey);
|
|
72
76
|
console.log("Address: " + quirked.address);
|
|
73
77
|
|
|
@@ -92,19 +96,25 @@ async function main(): Promise<void> {
|
|
|
92
96
|
.option("--eip191", "Whether to EIP-191 hash the messages before signing", false)
|
|
93
97
|
.showHelpAfterError();
|
|
94
98
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
99
|
+
try {
|
|
100
|
+
const parsed = await program.parseAsync(process.argv);
|
|
101
|
+
const opts = parsed.opts<{
|
|
102
|
+
message1: string;
|
|
103
|
+
message2: string;
|
|
104
|
+
eip191: boolean;
|
|
105
|
+
}>();
|
|
101
106
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
107
|
+
if (!opts.message1 || !opts.message2) {
|
|
108
|
+
throw new Error("Specify both messages to generate the signature for");
|
|
109
|
+
}
|
|
105
110
|
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
const quirked = quirk(opts.message1, opts.message2, opts.eip191);
|
|
112
|
+
printQuirked(opts.message1, opts.message2, quirked);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
115
|
+
|
|
116
|
+
throw new Error(msg);
|
|
117
|
+
}
|
|
108
118
|
}
|
|
109
119
|
|
|
110
120
|
void main();
|