ecdsa-quirks 0.1.0
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/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +77 -0
- package/package.json +68 -0
- package/src/index.ts +110 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Artem Chystiakov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ECDSA Quirks
|
|
2
|
+
|
|
3
|
+
A simple command line tool to generate the same ECDSA signature for two different messages.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g ecdsa-quirks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How to use
|
|
12
|
+
|
|
13
|
+
To generate the signatures, run:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
ecdsa-quirks --m1 "<some message>" --m2 "<some other message>" [--eip191]
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
The tool will log the private key and generated signatures via that private key.
|
|
20
|
+
|
|
21
|
+
## Disclaimer
|
|
22
|
+
|
|
23
|
+
The tool is based on [this research paper](https://www.di.ens.fr/david.pointcheval/Documents/Papers/2002_cryptoA.pdf). Please check it out to understand the math behind.
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { ethers } from "ethers";
|
|
3
|
+
// eslint-disable-next-line
|
|
4
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
5
|
+
import { modInv } from "bigint-mod-arith";
|
|
6
|
+
import { Command } from "commander";
|
|
7
|
+
import { expect } from "chai";
|
|
8
|
+
// based on https://www.di.ens.fr/david.pointcheval/Documents/Papers/2002_cryptoA.pdf
|
|
9
|
+
function quirk(message1, message2, eip191) {
|
|
10
|
+
// EIP-191 hash
|
|
11
|
+
if (eip191) {
|
|
12
|
+
message1 = ethers.hashMessage(message1);
|
|
13
|
+
message2 = ethers.hashMessage(message2);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
message1 = ethers.hexlify(ethers.toUtf8Bytes(message1));
|
|
17
|
+
message2 = ethers.hexlify(ethers.toUtf8Bytes(message2));
|
|
18
|
+
}
|
|
19
|
+
const n = secp256k1.Point.CURVE().n;
|
|
20
|
+
const k = ethers.hexlify(ethers.randomBytes(32));
|
|
21
|
+
const r = "0x" + ethers.hexlify(secp256k1.getPublicKey(Buffer.from(k.slice(2), "hex"))).slice(4); // take x coordinate
|
|
22
|
+
// x = -((h1 + h2) / 2r) (mod n)
|
|
23
|
+
const numer = BigInt(message1) + BigInt(message2);
|
|
24
|
+
const denom = BigInt(modInv(2n * BigInt(r), BigInt(n)));
|
|
25
|
+
const x = BigInt(n) - ((numer * denom) % BigInt(n));
|
|
26
|
+
// regular ECDSA signature
|
|
27
|
+
let s = (BigInt(modInv(BigInt(k), BigInt(n))) * (BigInt(message1) + x * BigInt(r))) % BigInt(n);
|
|
28
|
+
// make s be from the lower part of the curve
|
|
29
|
+
if (s > n / 2n) {
|
|
30
|
+
s = n - s;
|
|
31
|
+
}
|
|
32
|
+
const wallet = new ethers.Wallet(ethers.toBeHex(x, 32));
|
|
33
|
+
let sig1 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1b";
|
|
34
|
+
let sig2 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1c";
|
|
35
|
+
if (ethers.verifyMessage(message1, sig1) != wallet.address) {
|
|
36
|
+
const tmp = sig1;
|
|
37
|
+
sig1 = sig2;
|
|
38
|
+
sig2 = tmp;
|
|
39
|
+
}
|
|
40
|
+
// sanity check
|
|
41
|
+
expect(ethers.verifyMessage(message1, sig1) == wallet.address);
|
|
42
|
+
expect(ethers.verifyMessage(message2, sig2) == wallet.address);
|
|
43
|
+
return {
|
|
44
|
+
privateKey: ethers.toBeHex(x, 32),
|
|
45
|
+
address: wallet.address,
|
|
46
|
+
signature1: sig1,
|
|
47
|
+
signature2: sig2,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function printQuired(message1, message2, quirked) {
|
|
51
|
+
console.log("Private key: " + quirked.privateKey);
|
|
52
|
+
console.log("Address: " + quirked.address);
|
|
53
|
+
console.log();
|
|
54
|
+
console.log("Message1: " + message1);
|
|
55
|
+
console.log("Signature1: " + quirked.signature1);
|
|
56
|
+
console.log();
|
|
57
|
+
console.log("Message2: " + message2);
|
|
58
|
+
console.log("Signature2: " + quirked.signature2);
|
|
59
|
+
}
|
|
60
|
+
async function main() {
|
|
61
|
+
const program = new Command();
|
|
62
|
+
program
|
|
63
|
+
.name("ecdsa-quirks")
|
|
64
|
+
.description("Generate the same ECDSA signature for two different messages")
|
|
65
|
+
.option("--m1, --message1 <MSG>", "The first message")
|
|
66
|
+
.option("--m2, --message2 <MSG>", "The second message")
|
|
67
|
+
.option("--eip191", "Whether to EIP-191 hash the messages before signing", false)
|
|
68
|
+
.showHelpAfterError();
|
|
69
|
+
const parsed = await program.parseAsync(process.argv);
|
|
70
|
+
const opts = parsed.opts();
|
|
71
|
+
if (!opts.message1 || !opts.message2) {
|
|
72
|
+
throw new Error("Specify both messages to generate the signature for");
|
|
73
|
+
}
|
|
74
|
+
const quirked = quirk(opts.message1, opts.message2, opts.eip191);
|
|
75
|
+
printQuired(opts.message1, opts.message2, quirked);
|
|
76
|
+
}
|
|
77
|
+
void main();
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ecdsa-quirks",
|
|
3
|
+
"description": "Generate the same ECDSA signature for two different messages",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/src/index.js",
|
|
7
|
+
"types": "dist/src/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"ecdsa-quirks": "dist/src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": "./dist/src/index.js"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist/src/",
|
|
16
|
+
"src/",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/arvolear/ecdsa-quirks.git"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"ecdsa",
|
|
26
|
+
"quirks",
|
|
27
|
+
"ethereum"
|
|
28
|
+
],
|
|
29
|
+
"author": "Artem Chystiakov",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "git+https://github.com/arvolear/ecdsa-quirks.gitissues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "git+https://github.com/arvolear/ecdsa-quirks.git#readme",
|
|
35
|
+
"scripts": {
|
|
36
|
+
"prepare": "husky",
|
|
37
|
+
"prepack": "npm run build",
|
|
38
|
+
"build": "tsc --build .",
|
|
39
|
+
"lint": "prettier --check . && node --import tsx/esm ./node_modules/eslint/bin/eslint.js .",
|
|
40
|
+
"test": "node --import ./tests/loader-bootstrap.mjs --no-deprecation ./node_modules/mocha/bin/mocha.js \"tests/**/*.test.ts\" -t 120000",
|
|
41
|
+
"lint-fix": "prettier --write . && node --import tsx/esm ./node_modules/eslint/bin/eslint.js . --fix",
|
|
42
|
+
"publish": "npm run build && npm publish --access public"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@noble/curves": "2.0.1",
|
|
46
|
+
"bigint-mod-arith": "3.3.1",
|
|
47
|
+
"chai": "6.2.0",
|
|
48
|
+
"commander": "14.0.1",
|
|
49
|
+
"ethers": "6.16.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@eslint/js": "^9.13.0",
|
|
53
|
+
"@types/chai": "^5.2.3",
|
|
54
|
+
"@types/mocha": "^10.0.10",
|
|
55
|
+
"@types/node": "^24.9.1",
|
|
56
|
+
"eslint": "^9.13.0",
|
|
57
|
+
"eslint-plugin-n": "^17.10.3",
|
|
58
|
+
"globals": "^15.11.0",
|
|
59
|
+
"husky": "^9.1.7",
|
|
60
|
+
"jiti": "^2.0.0",
|
|
61
|
+
"mocha": "^11.7.4",
|
|
62
|
+
"prettier": "^3.3.3",
|
|
63
|
+
"ts-node": "^10.9.2",
|
|
64
|
+
"tsx": "^4.19.2",
|
|
65
|
+
"typescript": "^5.6.3",
|
|
66
|
+
"typescript-eslint": "^8.12.2"
|
|
67
|
+
}
|
|
68
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { ethers } from "ethers";
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line
|
|
6
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
7
|
+
import { modInv } from "bigint-mod-arith";
|
|
8
|
+
|
|
9
|
+
import { Command } from "commander";
|
|
10
|
+
import { expect } from "chai";
|
|
11
|
+
|
|
12
|
+
type Quirked = {
|
|
13
|
+
privateKey: string;
|
|
14
|
+
address: string;
|
|
15
|
+
signature1: string;
|
|
16
|
+
signature2: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// based on https://www.di.ens.fr/david.pointcheval/Documents/Papers/2002_cryptoA.pdf
|
|
20
|
+
function quirk(message1: string, message2: string, eip191: boolean): Quirked {
|
|
21
|
+
// EIP-191 hash
|
|
22
|
+
if (eip191) {
|
|
23
|
+
message1 = ethers.hashMessage(message1);
|
|
24
|
+
message2 = ethers.hashMessage(message2);
|
|
25
|
+
} else {
|
|
26
|
+
message1 = ethers.hexlify(ethers.toUtf8Bytes(message1));
|
|
27
|
+
message2 = ethers.hexlify(ethers.toUtf8Bytes(message2));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const n = secp256k1.Point.CURVE().n;
|
|
31
|
+
const k = ethers.hexlify(ethers.randomBytes(32));
|
|
32
|
+
const r = "0x" + ethers.hexlify(secp256k1.getPublicKey(Buffer.from(k.slice(2), "hex"))).slice(4); // take x coordinate
|
|
33
|
+
|
|
34
|
+
// x = -((h1 + h2) / 2r) (mod n)
|
|
35
|
+
const numer = BigInt(message1) + BigInt(message2);
|
|
36
|
+
const denom = BigInt(modInv(2n * BigInt(r), BigInt(n)));
|
|
37
|
+
const x = BigInt(n) - ((numer * denom) % BigInt(n));
|
|
38
|
+
|
|
39
|
+
// regular ECDSA signature
|
|
40
|
+
let s = (BigInt(modInv(BigInt(k), BigInt(n))) * (BigInt(message1) + x * BigInt(r))) % BigInt(n);
|
|
41
|
+
|
|
42
|
+
// make s be from the lower part of the curve
|
|
43
|
+
if (s > n / 2n) {
|
|
44
|
+
s = n - s;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const wallet = new ethers.Wallet(ethers.toBeHex(x, 32));
|
|
48
|
+
|
|
49
|
+
let sig1 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1b";
|
|
50
|
+
let sig2 = ethers.toBeHex(r, 32) + ethers.toBeHex(s, 32).slice(2) + "1c";
|
|
51
|
+
|
|
52
|
+
if (ethers.verifyMessage(message1, sig1) != wallet.address) {
|
|
53
|
+
const tmp = sig1;
|
|
54
|
+
sig1 = sig2;
|
|
55
|
+
sig2 = tmp;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// sanity check
|
|
59
|
+
expect(ethers.verifyMessage(message1, sig1) == wallet.address);
|
|
60
|
+
expect(ethers.verifyMessage(message2, sig2) == wallet.address);
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
privateKey: ethers.toBeHex(x, 32),
|
|
64
|
+
address: wallet.address,
|
|
65
|
+
signature1: sig1,
|
|
66
|
+
signature2: sig2,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function printQuired(message1: string, message2: string, quirked: Quirked) {
|
|
71
|
+
console.log("Private key: " + quirked.privateKey);
|
|
72
|
+
console.log("Address: " + quirked.address);
|
|
73
|
+
|
|
74
|
+
console.log();
|
|
75
|
+
|
|
76
|
+
console.log("Message1: " + message1);
|
|
77
|
+
console.log("Signature1: " + quirked.signature1);
|
|
78
|
+
|
|
79
|
+
console.log();
|
|
80
|
+
|
|
81
|
+
console.log("Message2: " + message2);
|
|
82
|
+
console.log("Signature2: " + quirked.signature2);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function main(): Promise<void> {
|
|
86
|
+
const program = new Command();
|
|
87
|
+
program
|
|
88
|
+
.name("ecdsa-quirks")
|
|
89
|
+
.description("Generate the same ECDSA signature for two different messages")
|
|
90
|
+
.option("--m1, --message1 <MSG>", "The first message")
|
|
91
|
+
.option("--m2, --message2 <MSG>", "The second message")
|
|
92
|
+
.option("--eip191", "Whether to EIP-191 hash the messages before signing", false)
|
|
93
|
+
.showHelpAfterError();
|
|
94
|
+
|
|
95
|
+
const parsed = await program.parseAsync(process.argv);
|
|
96
|
+
const opts = parsed.opts<{
|
|
97
|
+
message1: string;
|
|
98
|
+
message2: string;
|
|
99
|
+
eip191: boolean;
|
|
100
|
+
}>();
|
|
101
|
+
|
|
102
|
+
if (!opts.message1 || !opts.message2) {
|
|
103
|
+
throw new Error("Specify both messages to generate the signature for");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const quirked = quirk(opts.message1, opts.message2, opts.eip191);
|
|
107
|
+
printQuired(opts.message1, opts.message2, quirked);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
void main();
|