{"name":"@noble/secp256k1","version":"1.5.2","description":"Fastest JS implementation of secp256k1. Independently audited, high-security, 0-dependency ECDSA & Schnorr signatures","main":"lib/index.js","module":"lib/esm/index.js","types":"lib/index.d.ts","scripts":{"build":"tsc -d && tsc -p tsconfig.esm.json","build:release":"rollup -c rollup.config.js","lint":"prettier --print-width 100 --single-quote --check index.ts","test":"jest","coverage":"jest --coverage","bench":"node test/benchmark.js"},"author":{"name":"Paul Miller","url":"https://paulmillr.com"},"homepage":"https://paulmillr.com/noble/","repository":{"type":"git","url":"git+https://github.com/paulmillr/noble-secp256k1.git"},"license":"MIT","browser":{"crypto":false},"devDependencies":{"@noble/hashes":"~1.0.0","@rollup/plugin-commonjs":"^21.0.0","@rollup/plugin-node-resolve":"^13.0.0","@types/jest":"^27","@types/node":"^16","fast-check":"^2.14.0","jest":"^27.4.5","micro-bmark":"^0.1.3","prettier":"^2.5.1","rollup":"^2.62.0","ts-jest":"^27.1.2","typescript":"4.5.4"},"keywords":["secp256k1","secp","secp256","elliptic","elliptic curve","curve","signature","ecc","rfc6979","schnorr","sig","bip0340","bip340","ecdsa","endomorphism","cryptography","security","noble"],"exports":{".":{"import":"./lib/esm/index.js","default":"./lib/index.js"},"./index.d.ts":"./lib/index.d.ts"},"jest":{"testRegex":"/test/.*?\\.ts","transform":{"^.+\\.ts$":"ts-jest"}},"funding":[{"type":"individual","url":"https://paulmillr.com/funding/"}],"gitHead":"fe665f652b08f30948fb2b66949c4e94e64e3f6e","bugs":{"url":"https://github.com/paulmillr/noble-secp256k1/issues"},"_id":"@noble/secp256k1@1.5.2","_nodeVersion":"16.13.2","_npmVersion":"8.3.2","dist":{"integrity":"sha512-5mzA40W2q55VCRuC9XzmkiEnODdY0c5a7qsK2QcOfI5/MuVQyBaWGQyE6YOEF7kDwp+tDVWGsCDVJUME+wsWWw==","shasum":"40399e4fba54f588fda14fc03a4499044fdcab24","tarball":"http://registry-npm.ywwl.com/@noble/secp256k1/download/@noble/secp256k1-1.5.2.tgz","fileCount":7,"unpackedSize":95007,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJh8WNOCRA9TVsSAnZWagAA99kP/ibL80dPY0FjckxErMTN\n6z0sfFDicN0bMiLxu7JbxaF0gSviLixV4rzSOMKwB5yryKfIAtXOIbYV5LMy\nZxZSwcaR3wOtw+l5lnM2ce0yzhzTOZZwN0m+4CHd7epVPcrPzNqZj4ifJr0F\n2kZB97xVls1bgaD54DSnaG374Y61vxbkbjOUse4ikXRE49OWCvyJxGdY72i1\nkG166jEDiJuycEaAOEOxIT6ZorF344Ght4CG3+igvO3AMKCyMiq5dl4gIJK8\nq1ItP/n3jzem96CP7TVcdIztPBILqUuy6FdFRfofAZy2qfGz1JhSAftzUHk0\nsYAJwnP0QDOyxsa2Ow1VDMdTYO3bCgqENywo4RO7CpghqiNiyO7fbWPilgwd\nriHQZvkCfA/DG4uxO7RVqQlx+uxNLYzfwucjAFWPMWuLhmJfY2+VsZ+BRkG6\nKiDt+gfC0jjz1iwiWbnIwN817xLc+GGnewTHKnK+Sg8L+bp66XNJangKSsbq\n/88xOOuQU3bnnGiwfDmQGhbNJw2EH+/70TB5RCTeTBYD5lHK5jayEjRgGHsT\nqC7FxNmiRL6PQlFUx+73vMvknEkzyEBDxbLeLvLlBJpK7nrXnlgLObVTn1vC\nh7a3i0uH8tvU+Ldvvl6bnixAsWAp0+W0uoi30uf15FdgDU1jhoTD/YoAJDMj\nGWJt\r\n=iu2P\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCHhqIK4QizrmUWUsi8XLFIvMlwYJYeYntIbnW2AuMBtgIgQKYMpP5TeAKUWcaed44bdLeeiBR6/I+doC+RTF3RT2I="}],"size":24816,"noattachment":false,"key":"/@noble/secp256k1/-/@noble/secp256k1-1.5.2.tgz"},"_npmUser":{"name":"paulmillr","email":"paul@paulmillr.com"},"directories":{},"maintainers":[{"name":"paulmillr","email":"paul@paulmillr.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/secp256k1_1.5.2_1643209550595_0.4494706907330428"},"_hasShrinkwrap":false,"readme":"# noble-secp256k1\n\nFastest 4KB JS implementation of secp256k1 signatures & ECDH.\n\n- ✍️ Deterministic [ECDSA](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)\n  signatures compliant with [RFC6979](https://www.rfc-editor.org/rfc/rfc6979)\n- 🤝 Elliptic Curve Diffie-Hellman [ECDH](https://en.wikipedia.org/wiki/Elliptic-curve_Diffie–Hellman)\n- 📦 Pure ESM, can be imported without transpilers\n- 🪶 4KB gzipped, 450 lines of code\n\nUse larger drop-in replacement [noble-curves](https://github.com/paulmillr/noble-curves) instead,\nif you need additional features such as common.js, Schnorr signatures, DER encoding or support for different hash functions. To upgrade from v1 to v2, see [Upgrading](#upgrading).\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 with provenance\n- Check out [homepage](https://paulmillr.com/noble/) & 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  4kb [secp256k1](https://github.com/paulmillr/noble-secp256k1) /\n  [ed25519](https://github.com/paulmillr/noble-ed25519)\n\n## Usage\n\n> npm install @noble/secp256k1\n\nWe support all major platforms and runtimes. For node.js <= 18 and React Native, additional polyfills are needed: see below.\n\n```js\nimport * as secp from '@noble/secp256k1';\n// import * as secp from \"https://deno.land/x/secp256k1/mod.ts\"; // Deno\n// import * as secp from \"https://unpkg.com/@noble/secp256k1\"; // Unpkg\n(async () => {\n  // keys, messages & other inputs can be Uint8Arrays or hex strings\n  // Uint8Array.from([0xde, 0xad, 0xbe, 0xef]) === 'deadbeef'\n  const privKey = secp.utils.randomPrivateKey(); // Secure random private key\n  // sha256 of 'hello world'\n  const msgHash = 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';\n  const pubKey = secp.getPublicKey(privKey);\n  const signature = await secp.signAsync(msgHash, privKey); // Sync methods below\n  const isValid = secp.verify(signature, msgHash, pubKey);\n\n  const alicesPubkey = secp.getPublicKey(secp.utils.randomPrivateKey());\n  secp.getSharedSecret(privKey, alicesPubkey); // Elliptic curve diffie-hellman\n  signature.recoverPublicKey(msgHash); // Public key recovery\n})();\n```\n\nAdditional polyfills for some environments:\n\n```ts\n// 1. Enable synchronous methods.\n// Only async methods are available by default, to keep the library dependency-free.\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha256 } from '@noble/hashes/sha256';\nsecp.etc.hmacSha256Sync = (k, ...m) => hmac(sha256, k, secp.etc.concatBytes(...m))\n// Sync methods can be used now:\n// secp.sign(msgHash, privKey);\n\n// 2. node.js 18 and older, requires polyfilling globalThis.crypto\nimport { webcrypto } from 'node:crypto';\n// @ts-ignore\nif (!globalThis.crypto) globalThis.crypto = webcrypto;\n\n// 3. React Native needs crypto.getRandomValues polyfill and sha512\nimport 'react-native-get-random-values';\nimport { hmac } from '@noble/hashes/hmac';\nimport { sha256 } from '@noble/hashes/sha256';\nsecp.etc.hmacSha256Sync = (k, ...m) => hmac(sha256, k, secp.etc.concatBytes(...m));\nsecp.etc.hmacSha256Async = (k, ...m) => Promise.resolve(secp.etc.hmacSha256Sync(k, ...m));\n```\n\n## API\n\nThere are 3 main methods: `getPublicKey(privateKey)`,\n`sign(messageHash, privateKey)` and\n`verify(signature, messageHash, publicKey)`.\nWe accept Hex type everywhere:\n\n```ts\ntype Hex = Uint8Array | string\n```\n\n### getPublicKey\n\n```ts\nfunction getPublicKey(privateKey: Hex, isCompressed?: boolean): Uint8Array;\n```\n\nGenerates 33-byte compressed public key from 32-byte private key.\n\n- If you need uncompressed 65-byte public key, set second argument to `false`.\n- Use `ProjectivePoint.fromPrivateKey(privateKey)` for Point instance.\n- Use `ProjectivePoint.fromHex(publicKey)` to convert Hex / Uint8Array into Point.\n\n### sign\n\n```ts\nfunction sign(\n  messageHash: Hex, // message hash (not message) which would be signed\n  privateKey: Hex, // private key which will sign the hash\n  opts?: { lowS: boolean, extraEntropy: boolean | Hex } // optional params\n): Signature;\nfunction signAsync(\n  messageHash: Hex,\n  privateKey: Hex,\n  opts?: { lowS: boolean; extraEntropy: boolean | Hex }\n): Promise<Signature>;\n\nsign(msgHash, privKey, { lowS: false }); // Malleable signature\nsign(msgHash, privKey, { extraEntropy: true }); // Improved security\n```\n\nGenerates low-s deterministic-k RFC6979 ECDSA signature. Assumes hash of message,\nwhich means you'll need to do something like `sha256(message)` before signing.\n\n1. `lowS: false` allows to create malleable signatures, for compatibility with openssl.\n   Default `lowS: true` prohibits signatures which have (sig.s >= CURVE.n/2n) and is compatible with BTC/ETH.\n2. `extraEntropy: true` improves security by adding entropy, follows section 3.6 of RFC6979:\n    - No disadvantage: if an entropy generator is broken, sigs would be the same\n      as they are without the option\n    - It would help a lot in case there is an error somewhere in `k` gen.\n      Exposing `k` could leak private keys\n    - Sigs with extra entropy would have different `r` / `s`, which means they\n      would still be valid, but may break some test vectors if you're\n      cross-testing against other libs\n\n### verify\n\n```ts\nfunction verify(\n  signature: Hex | Signature, // returned by the `sign` function\n  messageHash: Hex, // message hash (not message) that must be verified\n  publicKey: Hex, // public (not private) key\n  opts?: { lowS: boolean } // optional params; { lowS: true } by default\n): boolean;\n```\n\nVerifies ECDSA signature and ensures it has lowS (compatible with BTC/ETH).\n`lowS: false` turns off malleability check, but makes it OpenSSL-compatible.\n\n### getSharedSecret\n\n```ts\nfunction getSharedSecret(\n  privateKeyA: Uint8Array | string, // Alices's private key\n  publicKeyB: Uint8Array | string, // Bob's public key\n  isCompressed = true // optional arg. (default) true=33b key, false=65b.\n): Uint8Array;\n```\n\nComputes ECDH (Elliptic Curve Diffie-Hellman) shared secret between\nkey A and different key B.\n\nUse `ProjectivePoint.fromHex(publicKeyB).multiply(privateKeyA)` for Point instance\n\n### recoverPublicKey\n\n```ts\nsignature.recoverPublicKey(\n  msgHash: Uint8Array | string\n): Uint8Array | undefined;\n```\n\nRecover public key from Signature instance with `recovery` bit set.\n\n### utils\n\nA bunch of useful **utilities** are also exposed:\n\n```typescript\ntype Bytes = Uint8Array;\nconst etc: {\n  hexToBytes: (hex: string) => Bytes;\n  bytesToHex: (b: Bytes) => string;\n  concatBytes: (...arrs: Bytes[]) => Bytes;\n  bytesToNumberBE: (b: Bytes) => bigint;\n  numberToBytesBE: (num: bigint) => Bytes;\n  mod: (a: bigint, b?: bigint) => bigint;\n  invert: (num: bigint, md?: bigint) => bigint;\n  hmacSha256Async: (key: Bytes, ...msgs: Bytes[]) => Promise<Bytes>;\n  hmacSha256Sync: HmacFnSync;\n  hashToPrivateKey: (hash: Hex) => Bytes;\n  randomBytes: (len: number) => Bytes;\n};\nconst utils: {\n  normPrivateKeyToScalar: (p: PrivKey) => bigint;\n  randomPrivateKey: () => Bytes; // Uses CSPRNG https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues\n  isValidPrivateKey: (key: Hex) => boolean;\n  precompute(p: ProjectivePoint, windowSize?: number): ProjectivePoint;\n};\nclass ProjectivePoint {\n  constructor(px: bigint, py: bigint, pz: bigint);\n  static readonly BASE: ProjectivePoint;\n  static readonly ZERO: ProjectivePoint;\n  static fromAffine(point: AffinePoint): ProjectivePoint;\n  static fromHex(hex: Hex): ProjectivePoint;\n  static fromPrivateKey(n: PrivKey): ProjectivePoint;\n  get x(): bigint;\n  get y(): bigint;\n  add(other: ProjectivePoint): ProjectivePoint;\n  assertValidity(): void;\n  equals(other: ProjectivePoint): boolean;\n  multiply(n: bigint): ProjectivePoint;\n  negate(): ProjectivePoint;\n  subtract(other: ProjectivePoint): ProjectivePoint;\n  toAffine(): AffinePoint;\n  toHex(isCompressed?: boolean): string;\n  toRawBytes(isCompressed?: boolean): Bytes;\n}\nclass Signature {\n  constructor(r: bigint, s: bigint, recovery?: number | undefined);\n  static fromCompact(hex: Hex): Signature;\n  readonly r: bigint;\n  readonly s: bigint;\n  readonly recovery?: number | undefined;\n  ok(): Signature;\n  hasHighS(): boolean;\n  normalizeS(): Signature;\n  recoverPublicKey(msgh: Hex): Point;\n  toCompactRawBytes(): Bytes;\n  toCompactHex(): string;\n}\nCURVE // curve prime; order; equation params, generator coordinates\n```\n\n## Security\n\nThe module is production-ready.\nIt is cross-tested against [noble-curves](https://github.com/paulmillr/noble-curves),\nand has similar security.\n\n1. The current version is rewrite of v1, which has been audited by cure53:\n[PDF](https://cure53.de/pentest-report_noble-lib.pdf) (funded by [Umbra.cash](https://umbra.cash) & community).\n2. It's being fuzzed by [Guido Vranken's cryptofuzz](https://github.com/guidovranken/cryptofuzz):\nrun the fuzzer by yourself to check.\n\nOur EC multiplication is hardened to be algorithmically constant time.\nWe're using built-in JS `BigInt`, which is potentially vulnerable to\n[timing attacks](https://en.wikipedia.org/wiki/Timing_attack) as\n[per MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#cryptography).\nBut, _JIT-compiler_ and _Garbage Collector_ make \"constant time\" extremely hard\nto achieve in a scripting language. Which means _any other JS library doesn't\nuse constant-time bigints_. Including bn.js or anything else.\nEven 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 —\nincluding bindings to native ones. Use low-level libraries & languages.\n\nWe consider infrastructure attacks like rogue NPM modules very important;\nthat's why it's crucial to minimize the amount of 3rd-party dependencies & native\nbindings. If your app uses 500 dependencies, any dep could get hacked and you'll\nbe downloading malware with every `npm install`. Our goal is to minimize this attack vector.\n\nAs for key generation, we're deferring to built-in\n[crypto.getRandomValues](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)\nwhich is considered cryptographically secure (CSPRNG).\n\n## Speed\n\nUse [noble-curves](https://github.com/paulmillr/noble-curves) if you need even higher performance.\n\nBenchmarks measured with Apple M2 on MacOS 13 with node.js 20.\n\n    getPublicKey(utils.randomPrivateKey()) x 6,430 ops/sec @ 155μs/op\n    sign x 3,367 ops/sec @ 296μs/op\n    verify x 600 ops/sec @ 1ms/op\n    getSharedSecret x 505 ops/sec @ 1ms/op\n    recoverPublicKey x 612 ops/sec @ 1ms/op\n    Point.fromHex (decompression) x 9,185 ops/sec @ 108μs/op\n\nCompare to other libraries on M1 (`openssl` uses native bindings, not JS):\n\n    elliptic#getPublicKey x 1,940 ops/sec\n    sjcl#getPublicKey x 211 ops/sec\n\n    elliptic#sign x 1,808 ops/sec\n    sjcl#sign x 199 ops/sec\n    openssl#sign x 4,243 ops/sec\n    ecdsa#sign x 116 ops/sec\n\n    elliptic#verify x 812 ops/sec\n    sjcl#verify x 166 ops/sec\n    openssl#verify x 4,452 ops/sec\n    ecdsa#verify x 80 ops/sec\n\n    elliptic#ecdh x 971 ops/sec\n\n## Contributing\n\n1. Clone the repository.\n2. `npm install` to install build dependencies like TypeScript\n3. `npm run build` to compile TypeScript code\n4. `npm test` to run jest on `test/index.ts`\n\nSpecial thanks to [Roman Koblov](https://github.com/romankoblov), who have\nhelped to improve scalar multiplication speed.\n\n## Upgrading\n\nnoble-secp256k1 v2 features improved security and smaller attack surface.\nThe goal of v2 is to provide minimum possible JS library which is safe and fast.\n\nThat means the library was reduced 4x, to just over 400 lines. In order to\nachieve the goal, **some features were moved** to\n[noble-curves](https://github.com/paulmillr/noble-curves), which is\neven safer and faster drop-in replacement library with same API.\nSwitch to curves if you intend to keep using these features:\n\n- DER encoding: toDERHex, toDERRawBytes, signing / verification of DER sigs\n- Schnorr signatures\n- Using `utils.precompute()` for non-base point\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\nOther changes for upgrading from @noble/secp256k1 1.7 to 2.0:\n\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`, `toCompactRawBytes`, `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## License\n\nMIT (c) Paul Miller [(https://paulmillr.com)](https://paulmillr.com), see LICENSE file.\n","publish_time":1643209550757,"_cnpm_publish_time":1643209550757,"dist-tags":{"latest":"3.2.0","legacy-v1":"1.7.2"}}