Skip to content

Commit a20a357

Browse files
committed
Initial commit
0 parents  commit a20a357

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+293009
-0
lines changed

.github/funding.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: paulmillr
2+
# custom: https://paulmillr.com/funding/

.github/workflows/nodejs.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Node CI
2+
3+
on: [push, pull_request]
4+
jobs:
5+
test:
6+
name: v18 @ ubuntu-latest
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v3
10+
- name: Use Node.js ${{ matrix.node }}
11+
uses: actions/setup-node@v3
12+
with:
13+
node-version: 18
14+
- run: npm install
15+
- run: npm run build --if-present
16+
- run: cd curve-definitions; npm install; npm run build --if-present
17+
- run: npm test
18+
- run: npm run lint --if-present

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
build/
2+
node_modules/
3+
coverage/
4+
/lib/**/*.js
5+
/lib/**/*.ts
6+
/lib/**/*.d.ts.map
7+
/curve-definitions/lib

.prettierrc.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"printWidth": 100,
3+
"singleQuote": true
4+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Paul Miller (https://paulmillr.com)
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
13+
all 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
21+
THE SOFTWARE.

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# noble-curves
2+
3+
Minimal, zero-dependency JS implementation of elliptic curve cryptography.
4+
5+
Implements Short Weierstrass curves with ECDSA signature scheme.
6+
7+
To keep the package minimal, no curve definitions are provided out-of-box.
8+
Main reason for that is the fact hashing library is usually required for full functionality. Use separate package that defines popular curves: `micro-curve-definitions` for P192, P224, P256, P384, P521, secp256k1, stark curve, bn254, pasta (pallas/vesta) - it depends on `@noble/hashes`.
9+
10+
Future plans:
11+
12+
- Edwards, Twisted Edwards & Montgomery curves
13+
- hash-to-curve standard
14+
- pairings
15+
16+
### This library belongs to _noble_ crypto
17+
18+
> **noble-crypto** — high-security, easily auditable set of contained cryptographic libraries and tools.
19+
20+
- No dependencies, small files
21+
- Easily auditable TypeScript/JS code
22+
- Supported in all major browsers and stable node.js versions
23+
- All releases are signed with PGP keys
24+
- Check out [homepage](https://paulmillr.com/noble/) & all libraries:
25+
[secp256k1](https://github.com/paulmillr/noble-secp256k1),
26+
[ed25519](https://github.com/paulmillr/noble-ed25519),
27+
[bls12-381](https://github.com/paulmillr/noble-bls12-381),
28+
[hashes](https://github.com/paulmillr/noble-hashes),
29+
[curves](https://github.com/paulmillr/noble-curves)
30+
31+
## Usage
32+
33+
Use NPM in node.js / browser, or include single file from
34+
[GitHub's releases page](https://github.com/paulmillr/noble-curves/releases):
35+
36+
## Usage
37+
38+
```sh
39+
npm install @noble/curves
40+
```
41+
42+
```ts
43+
// Short Weierstrass curve
44+
import shortw from '@noble/curves/shortw';
45+
import { sha256 } from '@noble/hashes/sha256';
46+
import { hmac } from '@noble/hashes/hmac';
47+
import { concatBytes, randomBytes } from '@noble/hashes/utils';
48+
49+
export const secp256k1 = shortw({
50+
a: 0n,
51+
b: 7n,
52+
// Field over which we'll do calculations
53+
P: 2n ** 256n - 2n ** 32n - 2n ** 9n - 2n ** 8n - 2n ** 7n - 2n ** 6n - 2n ** 4n - 1n,
54+
// Curve order, total count of valid points in the field
55+
n: 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n,
56+
// Base point (x, y) aka generator point
57+
Gx: 55066263022277343669578718895168534326250603453777594175500187360389116729240n,
58+
Gy: 32670510020758816978083085130507043184471273380659243275938904335757337482424n,
59+
hash: sha256,
60+
hmac: (k: Uint8Array, ...msgs: Uint8Array[]) => hmac(sha256, key, concatBytes(...msgs)),
61+
randomBytes: randomBytes
62+
});
63+
64+
// secp256k1.getPublicKey(priv)
65+
// secp256k1.sign(msg, priv)
66+
// secp256k1.verify(sig, msg, pub)
67+
```
68+
69+
## License
70+
71+
MIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.

curve-definitions/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022 Paul Miller (https://paulmillr.com)
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
13+
all 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
21+
THE SOFTWARE.

curve-definitions/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# micro-curve-definitions
2+
3+
Elliptic curves implementations. `@noble/curves` is zero-dependency library for internal arithmetics.
4+
5+
`micro-curve-definitions` is the actual implementations. Current functionality:
6+
7+
- NIST curves: P192, P224, P256, P384, P521 (ECDSA)
8+
- secp256k1 (ECDSA, without Schnorr)
9+
- stark curve
10+
- bn254
11+
12+
Pairings are not implemented.
13+
14+
## Usage
15+
16+
```sh
17+
npm install micro-curve-definitions
18+
```
19+
20+
```ts
21+
import * as nist from 'micro-curve-definitions';
22+
23+
// P192, P224, P256, P384, P521, bn254
24+
```
25+
26+
## License
27+
28+
MIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.

curve-definitions/package.json

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "micro-curve-definitions",
3+
"version": "0.1.0",
4+
"description": "Curve definitions for @noble/curves",
5+
"files": [
6+
"index.js",
7+
"index.d.ts",
8+
"index.d.ts.map",
9+
"index.ts"
10+
],
11+
"type": "module",
12+
"main": "index.js",
13+
"module": "index.js",
14+
"types": "index.d.ts",
15+
"dependencies": {
16+
"@noble/curves": "file:../",
17+
"@noble/hashes": "1.1.4"
18+
},
19+
"devDependencies": {
20+
"@scure/base": "~1.1.0",
21+
"@scure/bip32": "^1.1.1",
22+
"@scure/bip39": "^1.1.0",
23+
"@types/node": "^18.11.3",
24+
"fast-check": "3.0.0",
25+
"micro-should": "0.2.0",
26+
"prettier": "2.6.2",
27+
"typescript": "4.7.3"
28+
},
29+
"author": "Paul Miller (https://paulmillr.com)",
30+
"license": "MIT",
31+
"homepage": "https://github.com/paulmillr/noble-curves",
32+
"repository": {
33+
"type": "git",
34+
"url": "git+https://github.com/paulmillr/noble-curves.git"
35+
},
36+
"scripts": {
37+
"build": "tsc",
38+
"lint": "prettier --check index.ts",
39+
"test": "node test/index.test.js"
40+
},
41+
"keywords": [
42+
"secp192r1",
43+
"secp224r1",
44+
"secp256r1",
45+
"secp384r1",
46+
"secp521r1",
47+
"NIST P192",
48+
"NIST P224",
49+
"NIST P256",
50+
"NIST P384",
51+
"NIST P521",
52+
"NIST curves",
53+
"EC",
54+
"elliptic curves"
55+
],
56+
"funding": [
57+
{
58+
"type": "individual",
59+
"url": "https://paulmillr.com/funding/"
60+
}
61+
]
62+
}

curve-definitions/src/bn.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*! @noble/curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
2+
import { weierstrass, CHash } from '@noble/curves/shortw';
3+
import { concatBytes, randomBytes } from '@noble/hashes/utils';
4+
import { hmac } from '@noble/hashes/hmac';
5+
import { sha256 } from '@noble/hashes/sha256';
6+
7+
function getHash(hash: CHash) {
8+
return {
9+
hash,
10+
hmac: (key: Uint8Array, ...msgs: Uint8Array[]) => hmac(hash, key, concatBytes(...msgs)),
11+
randomBytes,
12+
};
13+
}
14+
15+
// Was known as alt_bn128 when it had 128-bit security. Now that it's much lower, the naming
16+
// has been changed to its prime bit count.
17+
export const bn254 = weierstrass({
18+
a: 0n,
19+
b: 3n,
20+
P: 0x30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47n,
21+
n: 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n,
22+
Gx: 1n,
23+
Gy: 2n,
24+
...getHash(sha256),
25+
});

0 commit comments

Comments
 (0)