{"name":"@noble/secp256k1","version":"2.2.3","keywords":["secp256k1","rfc6979","signature","ecdsa","noble","cryptography","elliptic curve","ecc","curve","schnorr","bitcoin","ethereum"],"author":{"url":"https://paulmillr.com","name":"Paul Miller"},"license":"MIT","_id":"@noble/secp256k1@2.2.3","maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"homepage":"https://paulmillr.com/noble/","bugs":{"url":"https://github.com/paulmillr/noble-secp256k1/issues"},"dist":{"shasum":"c505ced542328ed13315a8d811684d042f7acc5b","tarball":"http://registry-npm.ywwl.com/@noble/secp256k1/download/@noble/secp256k1-2.2.3.tgz","fileCount":6,"integrity":"sha512-l7r5oEQym9Us7EAigzg30/PQAvynhMt2uoYtT3t26eGDVm9Yii5mZ5jWSWmZ/oSIR2Et0xfc6DXrG0bZ787V3w==","signatures":[{"sig":"MEYCIQDVrUH43VRXWsvYiYEexZsk+jhdvdSqODiE0yVK5OEE8gIhALsjBPyfSyFsZwwFIlonCnrXZ+saQQs54MYsjsjKTqOp","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@noble%2fsecp256k1@2.2.3","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":85614,"size":27016,"noattachment":false,"key":"/@noble/secp256k1/-/@noble/secp256k1-2.2.3.tgz"},"main":"index.js","type":"module","types":"index.d.ts","module":"index.js","exports":{".":{"types":"./index.d.ts","default":"./index.js"}},"funding":"https://paulmillr.com/funding/","gitHead":"65c1cab5ceb53cc0bb912483314dd4fc32cf283c","scripts":{"loc":"echo \"`npm run --silent build:min | wc -c` symbols `wc -l < index.ts` LOC, `npm run --silent build:mingz | wc -c`B gzipped\"","test":"node test/index.js","bench":"node test/benchmark.js","build":"tsc","build:min":"cd test/build; npm install; npm run terser","build:mingz":"npm run --silent build:min | gzip -c8","build:release":"npm run --silent build:min > test/build/noble-secp256k1.min.js; npm run --silent build:mingz > test/build/noble-secp256k1.min.js.gz","test:webcrypto":"node test/index.webcrypto.js"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"repository":{"url":"git+https://github.com/paulmillr/noble-secp256k1.git","type":"git"},"_npmVersion":"11.0.0","description":"Fastest 4KB JS implementation of secp256k1 ECDH & ECDSA signatures compliant with RFC6979","directories":{},"sideEffects":false,"_nodeVersion":"20.18.1","_hasShrinkwrap":false,"devDependencies":{"fast-check":"3.0.0","typescript":"5.5.2","micro-bmark":"0.3.1","micro-should":"0.4.0","@noble/hashes":"1.7.0","@paulmillr/jsbt":"0.2.1"},"_npmOperationalInternal":{"tmp":"tmp/secp256k1_2.2.3_1736286067837_0.029021707149834874","host":"s3://npm-registry-packages-npm-production"},"readme":"# noble-secp256k1\n\nFastest 5KB JS implementation of secp256k1 signatures & ECDH.\n\n- ✍️ [ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)\n  signatures compliant with [RFC6979](https://www.rfc-editor.org/rfc/rfc6979)\n- ➰ Schnorr\n  signatures compliant with [BIP340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki)\n- 🤝 Elliptic Curve Diffie-Hellman [ECDH](https://en.wikipedia.org/wiki/Elliptic-curve_Diffie–Hellman)\n- 🔒 Supports [hedged signatures](https://paulmillr.com/posts/deterministic-signatures/) guarding against fault attacks\n- 🪶 4.8KB (gzipped) - 10-25x smaller than similar libraries\n\nThe module is a sister project of [noble-curves](https://github.com/paulmillr/noble-curves).\nUse noble-secp256k1 if you need smaller attack surface & better auditability.\nSwitch to noble-curves (drop-in) if you need features like MSM, DER encoding, custom point precomputes.\n\n898-byte version of the library is available for learning purposes in [`test/misc/1kb.min.js`](https://github.com/paulmillr/noble-secp256k1/blob/c38e57d17a2ecfdb9b8a80890a8e1a2cc140aa04/test/misc/1kb.min.js),\nit was created for the article [Learning fast elliptic-curve cryptography](https://paulmillr.com/posts/noble-secp256k1-fast-ecc/).\n\n### This library belongs to _noble_ cryptography\n\n> **noble-cryptography** — high-security, easily auditable set of contained cryptographic libraries and tools.\n\n- Zero or minimal dependencies\n- Highly readable TypeScript / JS code\n- PGP-signed releases and transparent NPM builds\n- All libraries:\n  [ciphers](https://github.com/paulmillr/noble-ciphers),\n  [curves](https://github.com/paulmillr/noble-curves),\n  [hashes](https://github.com/paulmillr/noble-hashes),\n  [post-quantum](https://github.com/paulmillr/noble-post-quantum),\n  5kb [secp256k1](https://github.com/paulmillr/noble-secp256k1) /\n  [ed25519](https://github.com/paulmillr/noble-ed25519)\n- WASM version: [awasm-noble](https://github.com/paulmillr/awasm-noble)\n- [Check out the homepage](https://paulmillr.com/noble/)\n  for reading resources, documentation, and apps built with noble\n\n## Usage\n\n> `npm install @noble/secp256k1`\n\n> `deno add jsr:@noble/secp256k1`\n\nWe support all major platforms and runtimes. For React Native, additional polyfills are needed: see below.\n\n```js\nimport * as secp from '@noble/secp256k1';\n(async () => {\n  const { secretKey, publicKey } = secp.keygen();\n  // const publicKey = secp.getPublicKey(secretKey);\n  const msg = new TextEncoder().encode('hello noble');\n  const sig = await secp.signAsync(msg, secretKey);\n  const isValid = await secp.verifyAsync(sig, msg, publicKey);\n})();\n\n// ECDH, key recovery\n(async () => {\n  const alice = secp.keygen();\n  const bob = secp.keygen();\n  const shared = secp.getSharedSecret(alice.secretKey, bob.publicKey);\n  const msg = new TextEncoder().encode('hello noble');\n\n  // recovery\n  const sigr = await secp.signAsync(msg, alice.secretKey, { format: 'recovered' });\n  const publicKey2 = await secp.recoverPublicKeyAsync(sigr, msg);\n})();\n\n// Schnorr signatures from BIP340\n(async () => {\n  const schnorr = secp.schnorr;\n  const { secretKey, publicKey } = schnorr.keygen();\n  const msg = new TextEncoder().encode('hello noble');\n  const sig = await schnorr.signAsync(msg, secretKey);\n  const isValid = await schnorr.verifyAsync(sig, msg, publicKey);\n})();\n```\n\n### Enabling synchronous methods\n\nOnly async methods are available by default, to keep the library dependency-free.\nTo enable sync methods:\n\n> `npm install @noble/hashes`\n\n```ts\nimport * as secp from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\n```\n\n### React Native: polyfill getRandomValues and sha256\n\nReact Native does not provide secure getRandomValues by default.\nThis can't be securely polyfilled from our end, so one will need a RN-specific compile-time dep.\n\n```ts\nimport 'react-native-get-random-values';\nimport * as secp from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\nsecp.hashes.hmacSha256Async = async (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256Async = async (msg) => sha256(msg);\n```\n\n## API\n\nThere are 4 main methods, which accept Uint8Array-s:\n\n* `keygen()`\n* `getPublicKey(secretKey)`\n* `sign(messageHash, secretKey)` and `signAsync(messageHash, secretKey)`\n* `verify(signature, messageHash, publicKey)` and `verifyAsync(signature, messageHash, publicKey)`\n\n### keygen\n\n```ts\nimport * as secp from '@noble/secp256k1';\n(async () => {\n  const keys = secp.keygen();\n  const { secretKey, publicKey } = keys;\n})();\n```\n\n### getPublicKey\n\n```ts\nimport * as secp from '@noble/secp256k1';\nconst secretKey = secp.utils.randomSecretKey();\nconst pubKey33b = secp.getPublicKey(secretKey);\n\n// Variants\nconst pubKey65b = secp.getPublicKey(secretKey, false);\nconst pubKeyPoint = secp.Point.fromBytes(pubKey65b);\nconst samePoint = pubKeyPoint.toBytes();\n```\n\nGenerates 33-byte compressed (default) or 65-byte public key from 32-byte private key.\n\n### sign\n\n```ts\nimport * as secp from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { keccak_256 } from '@noble/hashes/sha3.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\nconst { secretKey } = secp.keygen();\nconst msg = new TextEncoder().encode('hello noble');\nconst sig = secp.sign(msg, secretKey);\n\n// async\nconst sigB = await secp.signAsync(msg, secretKey);\n\n// recovered, allows `recoverPublicKey(sigR, msg)`\nconst sigR = secp.sign(msg, secretKey, { format: 'recovered' });\nconst sigH = secp.sign(keccak_256(msg), secretKey, { prehash: false });\n// hedged sig\nconst sigC = secp.sign(msg, secretKey, { extraEntropy: true });\nconst sigC2 = secp.sign(msg, secretKey, { extraEntropy: Uint8Array.from([0xca, 0xfe]) });\n// malleable sig\nconst sigD = secp.sign(msg, secretKey, { lowS: false });\n```\n\nGenerates low-s deterministic-k RFC6979 ECDSA signature.\n\n- Message will be hashed with sha256. If you want to use a different hash function,\nmake sure to use `{ prehash: false }`.\n- `extraEntropy: true` enables hedged signatures. They incorporate\nextra randomness into RFC6979 (described in section 3.6),\nto provide additional protection against fault attacks.\nCheck out blog post [Deterministic signatures are not your friends](https://paulmillr.com/posts/deterministic-signatures/).\nEven if their RNG is broken, they will fall back to determinism.\n- Default behavior `lowS: true` prohibits signatures which have (sig.s >= CURVE.n/2n) and is compatible with BTC/ETH. Setting `lowS: false` allows to create malleable signatures, which is default openssl behavior. Non-malleable signatures can still be successfully verified in openssl.\n\n### verify\n\n```ts\nimport * as secp from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { keccak_256 } from '@noble/hashes/sha3.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\nconst { secretKey, publicKey } = secp.keygen();\nconst msg = new TextEncoder().encode('hello noble');\nconst sig = secp.sign(msg, secretKey);\nconst isValid = secp.verify(sig, msg, publicKey);\n\nconst sigH = secp.sign(keccak_256(msg), secretKey, { prehash: false });\n```\n\nVerifies ECDSA signature.\n\n- Message will be hashed with sha256. If you want to use a different hash function,\nmake sure to use `{ prehash: false }`.\n- Default behavior `lowS: true` prohibits malleable signatures which have (`sig.s >= CURVE.n/2n`) and\n  is compatible with BTC / ETH.\n  Setting `lowS: false` allows to create signatures, which is default openssl behavior.\n\n### getSharedSecret\n\n```ts\nimport * as secp from '@noble/secp256k1';\nconst alice = secp.keygen();\nconst bob = secp.keygen();\nconst shared33b = secp.getSharedSecret(alice.secretKey, bob.publicKey);\nconst shared65b = secp.getSharedSecret(bob.secretKey, alice.publicKey, false);\nconst sharedPoint = secp.Point.fromBytes(bob.publicKey).multiply(\n  secp.etc.secretKeyToScalar(alice.secretKey)\n);\n```\n\nComputes ECDH (Elliptic Curve Diffie-Hellman) shared secret between\nkey A and different key B.\n\n### recoverPublicKey\n\n```ts\nimport * as secp from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nimport { keccak_256 } from '@noble/hashes/sha3.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\n\nconst { secretKey, publicKey } = secp.keygen();\nconst msg = new TextEncoder().encode('hello noble');\nconst sigR = secp.sign(msg, secretKey, { format: 'recovered' });\nconst publicKey2 = secp.recoverPublicKey(sigR, msg);\n\nconst sigRH = secp.sign(keccak_256(msg), secretKey, { format: 'recovered', prehash: false });\nconst publicKeyH = secp.recoverPublicKey(sigRH, keccak_256(msg), { prehash: false });\n```\n\nRecover public key from Signature instance with `recovery` bit set.\n\n### schnorr\n\n```ts\nimport * as secp from '@noble/secp256k1';\nimport { schnorr } from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\nsecp.hashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nsecp.hashes.sha256 = sha256;\nconst { secretKey, publicKey } = schnorr.keygen();\nconst msg = new TextEncoder().encode('hello noble');\nconst sig = schnorr.sign(msg, secretKey);\nconst isValid = schnorr.verify(sig, msg, publicKey);\n```\n\nAsync methods work without extra setup:\n\n```ts\nimport { schnorr } from '@noble/secp256k1';\nconst { secretKey, publicKey } = schnorr.keygen();\nconst msg = new TextEncoder().encode('hello noble');\nconst sigA = await schnorr.signAsync(msg, secretKey);\nconst isValidA = await schnorr.verifyAsync(sigA, msg, publicKey);\n```\n\nSchnorr\nsignatures compliant with [BIP340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki)\nare supported.\n\n### utils\n\nA bunch of useful **utilities** are also exposed:\n\n```ts\nimport * as secp from '@noble/secp256k1';\n\nconst { bytesToHex, hexToBytes, concatBytes, mod, invert, randomBytes } = secp.etc;\nconst { isValidSecretKey, isValidPublicKey, randomSecretKey } = secp.utils;\nconst { Point } = secp;\nconsole.log(Point.CURVE(), Point.BASE);\n/*\nclass Point {\n  static BASE: Point;\n  static ZERO: Point;\n  readonly X: bigint;\n  readonly Y: bigint;\n  readonly Z: bigint;\n  constructor(X: bigint, Y: bigint, Z: bigint);\n  static CURVE(): WeierstrassOpts<bigint>;\n  static fromAffine(ap: AffinePoint): Point;\n  static fromBytes(bytes: Bytes): Point;\n  static fromHex(hex: string): Point;\n  get x(): bigint;\n  get y(): bigint;\n  equals(other: Point): boolean;\n  is0(): boolean;\n  negate(): Point;\n  double(): Point;\n  add(other: Point): Point;\n  subtract(other: Point): Point;\n  multiply(n: bigint): Point;\n  multiplyUnsafe(scalar: bigint): Point;\n  toAffine(): AffinePoint;\n  assertValidity(): Point;\n  toBytes(isCompressed?: boolean): Bytes;\n  toHex(isCompressed?: boolean): string;\n}\n*/\n```\n\n## Security\n\nThe module is production-ready.\n\nWe cross-test against sister project [noble-curves](https://github.com/paulmillr/noble-curves), which was audited and provides improved security.\n\n- The current version has not been independently audited. It is a rewrite of v1, which has been audited by cure53 in Apr 2021:\n  [PDF](https://cure53.de/pentest-report_noble-lib.pdf) (funded by [Umbra.cash](https://umbra.cash) & community).\n- It's being fuzzed [in a separate repository](https://github.com/paulmillr/fuzzing)\n\n### Constant-timeness\n\nWe're targetting algorithmic constant time. _JIT-compiler_ and _Garbage Collector_ make \"constant time\"\nextremely hard to achieve [timing attack](https://en.wikipedia.org/wiki/Timing_attack) resistance\nin a scripting language. Which means _any other JS library can't have\nconstant-timeness_. Even statically typed Rust, a language without GC,\n[makes it harder to achieve constant-time](https://www.chosenplaintext.ca/open-source/rust-timing-shield/security)\nfor some cases. If your goal is absolute security, don't use any JS lib — including bindings to native ones.\nUse low-level libraries & languages.\n\n### Supply chain security\n\n- **Commits** are signed with PGP keys to prevent forgery. Be sure to verify the commit signatures\n- **Releases** are made transparently through token-less GitHub CI and Trusted Publishing. Be sure to verify the [provenance logs](https://docs.npmjs.com/generating-provenance-statements) for authenticity.\n- **Rare releasing** is practiced to minimize the need for re-audits by end-users.\n- **Dependencies** are minimized and strictly pinned to reduce supply-chain risk.\n  - We use as few dependencies as possible.\n  - Version ranges are locked, and changes are checked with npm-diff.\n- **Dev dependencies** are excluded from end-user installs; they’re only used for development and build steps.\n\nFor this package, there are 0 dependencies; and a few dev dependencies:\n\n- [noble-hashes](https://github.com/paulmillr/noble-hashes) provides cryptographic hashing functionality\n- jsbt is used for benchmarking / testing / build tooling and developed by the same author\n- prettier and typescript are used for code quality / ts compilation; @paulmillr/jsbt for property-based tests\n\n### Randomness\n\nWe rely on the built-in\n[`crypto.getRandomValues`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues),\nwhich is considered a cryptographically secure PRNG.\n\nBrowsers have had weaknesses in the past - and could again - but implementing a userspace CSPRNG is even worse, as there’s no reliable userspace source of high-quality entropy.\n\n### Quantum computers\n\nCryptographically relevant quantum computer, if built, will allow to\nbreak elliptic curve cryptography (both ECDSA / EdDSA & ECDH) using Shor's algorithm.\n\nConsider switching to newer / hybrid algorithms, such as SPHINCS+. They are available in\n[noble-post-quantum](https://github.com/paulmillr/noble-post-quantum).\n\nNIST prohibits classical cryptography (RSA, DSA, ECDSA, ECDH) [after 2035](https://nvlpubs.nist.gov/nistpubs/ir/2024/NIST.IR.8547.ipd.pdf). Australian ASD prohibits it [after 2030](https://www.cyber.gov.au/resources-business-and-government/essential-cyber-security/ism/cyber-security-guidelines/guidelines-cryptography).\n\n## Upgrading\n\n### v2 to v3\n\nv3 brings the package closer to noble-curves v2.\n\n- Add Schnorr signatures\n- Most methods now expect Uint8Array, string hex inputs are prohibited\n- Add `keygen`, `keygenAsync` method\n- sign, verify: Switch to **prehashed messages**. Instead of\n  messageHash, the methods now expect unhashed message.\n  To bring back old behavior, use option `{prehash: false}`\n- sign, verify: Switch to **Uint8Array signatures** (format: 'compact') by default.\n- verify: **der format must be explicitly specified** in `{format: 'der'}`.\n  This reduces malleability\n- verify: **prohibit Signature-instance** signature. User must now always do\n  `signature.toBytes()`\n- Node v20.19 is now the minimum required version\n- Various small changes for types\n- etc: hashes are now set in `hashes` object. Also sha256 needs to be set now for `prehash: true`:\n\n```js\nimport { etc, hashes } from '@noble/secp256k1';\nimport { hmac } from '@noble/hashes/hmac.js';\nimport { sha256 } from '@noble/hashes/sha2.js';\n// before\netc.hmacSha256Sync = (key, ...messages) => hmac(sha256, key, etc.concatBytes(...messages));\netc.hmacSha256Async = (key, ...messages) => Promise.resolve(etc.hmacSha256Sync(key, ...messages));\n// after\nhashes.hmacSha256 = (key, msg) => hmac(sha256, key, msg);\nhashes.sha256 = sha256;\nhashes.hmacSha256Async = async (key, msg) => hmac(sha256, key, msg);\nhashes.sha256Async = async (msg) => sha256(msg);\n```\n\n### v1 to v2\n\nv2 improves security and reduces attack surface.\nThe goal of v2 is to provide minimum possible JS library which is safe and fast.\n\n- Disable some features to ensure 4x smaller than v1, 5KB bundle size:\n  - The features are now a part of [noble-curves](https://github.com/paulmillr/noble-curves),\n    **switch to curves if you need them**. Curves is drop-in replacement.\n  - DER encoding: toDERHex, toDERRawBytes, signing / verification of DER sigs\n  - Schnorr signatures\n  - Support for environments which don't support bigint literals\n  - Common.js support\n  - Support for node.js 18 and older without [shim](#usage)\n  - Using `utils.precompute()` for non-base point\n- `getPublicKey`\n  - now produce 33-byte compressed signatures by default\n  - to use old behavior, which produced 65-byte uncompressed keys, set\n    argument `isCompressed` to `false`: `getPublicKey(priv, false)`\n- `sign`\n  - is now sync; use `signAsync` for async version\n  - now returns `Signature` instance with `{ r, s, recovery }` properties\n  - `canonical` option was renamed to `lowS`\n  - `recovered` option has been removed because recovery bit is always returned now\n  - `der` option has been removed. There are 2 options:\n    1. Use compact encoding: `fromCompact`, `toBytes`, `toCompactHex`.\n       Compact encoding is simply a concatenation of 32-byte r and 32-byte s.\n    2. If you must use DER encoding, switch to noble-curves (see above).\n- `verify`\n  - `strict` option was renamed to `lowS`\n- `getSharedSecret`\n  - now produce 33-byte compressed signatures by default\n  - to use old behavior, which produced 65-byte uncompressed keys, set\n    argument `isCompressed` to `false`: `getSharedSecret(a, b, false)`\n- `recoverPublicKey(msg, sig, rec)` was changed to `sig.recoverPublicKey(msg)`\n- `number` type for private keys have been removed: use `bigint` instead\n- `Point` (2d xy) has been changed to `ProjectivePoint` (3d xyz)\n- `utils` were split into `utils` (same api as in noble-curves) and\n  `etc` (`hmacSha256Sync` and others)\n\n## Speed\n\n    npm run bench\n\nBenchmarks measured with Apple M4. [noble-curves](https://github.com/paulmillr/noble-curves) enable faster performance.\n\n```\nkeygen x 7,267 ops/sec @ 137μs/op\nsign x 6,888 ops/sec @ 145μs/op\nverify x 788 ops/sec @ 1ms/op\ngetSharedSecret x 654 ops/sec @ 1ms/op\nrecoverPublicKey x 766 ops/sec @ 1ms/op\n\nsignAsync x 4,353 ops/sec @ 229μs/op\nverifyAsync x 773 ops/sec @ 1ms/op\n\nPoint.fromBytes x 13,322 ops/sec @ 75μs/op\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2019 Paul Miller [(https://paulmillr.com)](https://paulmillr.com)\n\nSee LICENSE file.\n","publish_time":1736286068025,"_cnpm_publish_time":1736286068025,"dist-tags":{"latest":"3.2.0","legacy-v1":"1.7.2"}}