forked from bitcoinjs/tiny-secp256k1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
26,105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/benches/node_modules | ||
/examples/react-app/dist | ||
/examples/react-app/node_modules | ||
/lib | ||
/node_modules | ||
/target | ||
/tests/browser | ||
/types | ||
|
||
/package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
## random-in-node | ||
|
||
Generate data and demonstrate arguments/result for different methods of `tiny-secp256k1`. | ||
|
||
``` | ||
npm start | ||
``` | ||
|
||
## react-app | ||
|
||
[React](https://reactjs.org/) application with inputs for testing `tiny-secp256k1` methods. | ||
|
||
``` | ||
npm start | ||
``` | ||
|
||
and open [http://localhost:8080/](http://localhost:8080/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import * as secp256k1 from "../../lib/index.js"; | ||
import { generate } from "./index.js"; | ||
|
||
const toHex = (v) => | ||
v instanceof Uint8Array ? Buffer.from(v).toString("hex") : v; | ||
|
||
const lineDash = new Array(80).fill("-").join(""); | ||
const lineEq = new Array(80).fill("=").join(""); | ||
function print(items) { | ||
let firstPrint = true; | ||
for (const item of items) { | ||
if (firstPrint) { | ||
console.log(lineEq); | ||
firstPrint = false; | ||
} | ||
console.log(`Method: ${item.name}`); | ||
console.log(lineDash); | ||
for (let i = 0; i < item.args.length; ++i) { | ||
console.log(`Arg${(i + 1).toString()}: ${toHex(item.args[i])}`); | ||
} | ||
console.log(`Result: ${toHex(secp256k1[item.name](...item.args))}`); | ||
console.log(lineEq); | ||
} | ||
} | ||
|
||
const data = generate(); | ||
print([ | ||
{ name: "isPoint", args: [data.pubkey_uncompressed] }, | ||
{ name: "isPointCompressed", args: [data.pubkey] }, | ||
{ name: "isPrivate", args: [data.seckey] }, | ||
{ name: "pointAdd", args: [data.pubkey, data.pubkey2] }, | ||
{ name: "pointAddScalar", args: [data.pubkey, data.tweak] }, | ||
{ name: "pointCompress", args: [data.pubkey_uncompressed, true] }, | ||
{ name: "pointFromScalar", args: [data.seckey] }, | ||
{ name: "pointMultiply", args: [data.pubkey, data.tweak] }, | ||
{ name: "privateAdd", args: [data.seckey, data.tweak] }, | ||
{ name: "privateSub", args: [data.seckey, data.tweak] }, | ||
{ name: "sign", args: [data.hash, data.seckey] }, | ||
{ name: "signWithEntropy", args: [data.hash, data.seckey, data.entropy] }, | ||
{ | ||
name: "verify", | ||
args: [data.hash, data.pubkey, secp256k1.sign(data.hash, data.seckey)], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { randomBytes } from "crypto"; | ||
import * as secp256k1 from "../../lib/index.js"; | ||
|
||
const eq = (v1, v2) => | ||
v1.length === v2.length && v1.every((v, i) => v === v2[i]); | ||
|
||
function isValidData(data) { | ||
return ( | ||
secp256k1.isPoint(data.pubkey) && | ||
secp256k1.isPoint(data.pubkey_uncompressed) && | ||
secp256k1.isPoint(data.pubkey2) && | ||
secp256k1.isPointCompressed(data.pubkey) && | ||
secp256k1.isPointCompressed(data.pubkey2) && | ||
secp256k1.isPrivate(data.seckey) && | ||
secp256k1.isPrivate(data.seckey2) && | ||
secp256k1.pointAdd(data.pubkey, data.pubkey2) !== null && | ||
secp256k1.pointAddScalar(data.pubkey, data.tweak) !== null && | ||
secp256k1.pointAddScalar(data.pubkey2, data.tweak) !== null && | ||
eq(secp256k1.pointCompress(data.pubkey, false), data.pubkey_uncompressed) && | ||
eq(secp256k1.pointFromScalar(data.seckey, true), data.pubkey) && | ||
eq( | ||
secp256k1.pointFromScalar(data.seckey, false), | ||
data.pubkey_uncompressed | ||
) && | ||
eq(secp256k1.pointFromScalar(data.seckey2, true), data.pubkey2) && | ||
secp256k1.pointMultiply(data.pubkey, data.tweak) !== null && | ||
secp256k1.pointMultiply(data.pubkey2, data.tweak) !== null && | ||
secp256k1.privateAdd(data.seckey, data.tweak) !== null && | ||
secp256k1.privateAdd(data.seckey2, data.tweak) !== null && | ||
secp256k1.privateSub(data.seckey, data.tweak) !== null && | ||
secp256k1.privateSub(data.seckey2, data.tweak) !== null && | ||
secp256k1.verify( | ||
data.hash, | ||
data.pubkey, | ||
secp256k1.sign(data.hash, data.seckey) | ||
) | ||
); | ||
} | ||
|
||
export function generate() { | ||
for (;;) { | ||
const seckey = new Uint8Array(randomBytes(32)); | ||
const seckey2 = new Uint8Array(randomBytes(32)); | ||
const hash = new Uint8Array(randomBytes(32)); | ||
|
||
const data = { | ||
seckey, | ||
pubkey: secp256k1.pointFromScalar(seckey, true), | ||
pubkey_uncompressed: secp256k1.pointFromScalar(seckey, false), | ||
seckey2, | ||
pubkey2: secp256k1.pointFromScalar(seckey2, true), | ||
tweak: new Uint8Array(randomBytes(32)), | ||
hash, | ||
entropy: new Uint8Array(randomBytes(32)), | ||
signature: secp256k1.sign(hash, seckey), | ||
}; | ||
|
||
if (isValidData(data)) return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "random-in-node", | ||
"version": "0.0.0", | ||
"private": true, | ||
"main": "./index.js", | ||
"scripts": { | ||
"start": "node bin.js" | ||
}, | ||
"type": "module" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>tiny-secp256k1 React example</title> | ||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script src="./bundle.js"></script> | ||
</body> | ||
</html> |
Oops, something went wrong.