From d77ac16f51e8873b389ca07ba296d74e41eb9f1b Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Mon, 13 Mar 2023 23:32:09 +0000 Subject: [PATCH] Bring back common.js for now. Need more thorough work with consumers --- README.md | 2 +- esm/package.json | 2 +- package.json | 30 ++++++++++++++++++++++++++---- test/_more-curves.helpers.js | 4 ++-- test/_poseidon.helpers.js | 6 +++--- test/basic.test.js | 24 ++++++++++++------------ test/bls12-381.test.js | 8 ++++---- test/ed25519-addons.test.js | 4 ++-- test/ed25519.helpers.js | 2 +- test/ed448.test.js | 4 ++-- test/hash-to-curve.test.js | 18 +++++++++--------- test/jubjub.test.js | 2 +- test/nist.test.js | 10 +++++----- test/poseidon.test.js | 4 ++-- test/secp256k1-schnorr.test.js | 2 +- test/secp256k1.helpers.js | 8 ++++---- tsconfig.esm.json | 20 ++++++++++++++++++++ 17 files changed, 96 insertions(+), 54 deletions(-) create mode 100644 tsconfig.esm.json diff --git a/README.md b/README.md index 748825d..66e557e 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Instead, you need to import specific primitives. This is done to ensure small si Each curve can be used in the following way: ```ts -import { secp256k1 } from '@noble/curves/secp256k1'; // ECMAScript Modules (ESM) +import { secp256k1 } from '@noble/curves/secp256k1'; // ECMAScript Modules (ESM) and Common.js // import { secp256k1 } from 'npm:@noble/curves@1.2.0/secp256k1'; // Deno const priv = secp256k1.utils.randomPrivateKey(); const pub = secp256k1.getPublicKey(priv); diff --git a/esm/package.json b/esm/package.json index 1517420..267769c 100644 --- a/esm/package.json +++ b/esm/package.json @@ -2,6 +2,6 @@ "type": "module", "browser": { "crypto": false, - "./crypto": "./esm/cryptoBrowser.js" + "./crypto": "./esm/crypto.js" } } diff --git a/package.json b/package.json index 0f33408..e90a8c6 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { "name": "@noble/curves", "version": "0.8.0", - "description": "Audited & minimal JS implementation of elliptic curve cryptography", + "description": "Minimal, auditable JS implementation of elliptic curve cryptography", "files": [ "abstract", + "esm", "src", "*.js", "*.js.map", @@ -12,9 +13,9 @@ ], "scripts": { "bench": "cd benchmark; node secp256k1.js; node curves.js; node ecdh.js; node bls.js", - "build": "tsc", - "build:clean": "rm *.{js,d.ts,js.map} esm/*.{js,js.map} 2> /dev/null", + "build": "tsc && tsc -p tsconfig.esm.json", "build:release": "rollup -c rollup.config.js", + "build:clean": "rm *.{js,d.ts,js.map} esm/*.{js,js.map} 2> /dev/null", "lint": "prettier --check 'src/**/*.{js,ts}' 'test/*.js'", "format": "prettier --write 'src/**/*.{js,ts}' 'test/*.js'", "test": "node test/index.test.js" @@ -40,94 +41,115 @@ "typescript": "4.7.3" }, "main": "index.js", - "type": "module", "exports": { ".": { "types": "./index.d.ts", + "import": "./esm/index.js", "default": "./index.js" }, "./abstract/edwards": { "types": "./abstract/edwards.d.ts", + "import": "./esm/abstract/edwards.js", "default": "./abstract/edwards.js" }, "./abstract/modular": { "types": "./abstract/modular.d.ts", + "import": "./esm/abstract/modular.js", "default": "./abstract/modular.js" }, "./abstract/montgomery": { "types": "./abstract/montgomery.d.ts", + "import": "./esm/abstract/montgomery.js", "default": "./abstract/montgomery.js" }, "./abstract/weierstrass": { "types": "./abstract/weierstrass.d.ts", + "import": "./esm/abstract/weierstrass.js", "default": "./abstract/weierstrass.js" }, "./abstract/bls": { "types": "./abstract/bls.d.ts", + "import": "./esm/abstract/bls.js", "default": "./abstract/bls.js" }, "./abstract/hash-to-curve": { "types": "./abstract/hash-to-curve.d.ts", + "import": "./esm/abstract/hash-to-curve.js", "default": "./abstract/hash-to-curve.js" }, "./abstract/curve": { "types": "./abstract/curve.d.ts", + "import": "./esm/abstract/curve.js", "default": "./abstract/curve.js" }, "./abstract/utils": { "types": "./abstract/utils.d.ts", + "import": "./esm/abstract/utils.js", "default": "./abstract/utils.js" }, "./abstract/poseidon": { "types": "./abstract/poseidon.d.ts", + "import": "./esm/abstract/poseidon.js", "default": "./abstract/poseidon.js" }, "./_shortw_utils": { "types": "./_shortw_utils.d.ts", + "import": "./esm/_shortw_utils.js", "default": "./_shortw_utils.js" }, "./bls12-381": { "types": "./bls12-381.d.ts", + "import": "./esm/bls12-381.js", "default": "./bls12-381.js" }, "./bn": { "types": "./bn.d.ts", + "import": "./esm/bn.js", "default": "./bn.js" }, "./ed25519": { "types": "./ed25519.d.ts", + "import": "./esm/ed25519.js", "default": "./ed25519.js" }, "./ed448": { "types": "./ed448.d.ts", + "import": "./esm/ed448.js", "default": "./ed448.js" }, "./index": { "types": "./index.d.ts", + "import": "./esm/index.js", "default": "./index.js" }, "./jubjub": { "types": "./jubjub.d.ts", + "import": "./esm/jubjub.js", "default": "./jubjub.js" }, "./p256": { "types": "./p256.d.ts", + "import": "./esm/p256.js", "default": "./p256.js" }, "./p384": { "types": "./p384.d.ts", + "import": "./esm/p384.js", "default": "./p384.js" }, "./p521": { "types": "./p521.d.ts", + "import": "./esm/p521.js", "default": "./p521.js" }, "./pasta": { "types": "./pasta.d.ts", + "import": "./esm/pasta.js", "default": "./pasta.js" }, "./secp256k1": { "types": "./secp256k1.d.ts", + "import": "./esm/secp256k1.js", "default": "./secp256k1.js" } }, diff --git a/test/_more-curves.helpers.js b/test/_more-curves.helpers.js index dde70c5..c218e52 100644 --- a/test/_more-curves.helpers.js +++ b/test/_more-curves.helpers.js @@ -1,7 +1,7 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ -import { createCurve } from '../_shortw_utils.js'; +import { createCurve } from '../esm/_shortw_utils.js'; import { sha224, sha256 } from '@noble/hashes/sha256'; -import { Fp } from '../abstract/modular.js'; +import { Fp } from '../esm/abstract/modular.js'; // NIST secp192r1 aka P192 // https://www.secg.org/sec2-v2.pdf, https://neuromancer.sk/std/secg/secp192r1 diff --git a/test/_poseidon.helpers.js b/test/_poseidon.helpers.js index 06d693f..25adfba 100644 --- a/test/_poseidon.helpers.js +++ b/test/_poseidon.helpers.js @@ -1,9 +1,9 @@ /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ import { sha256 } from '@noble/hashes/sha256'; import { utf8ToBytes } from '@noble/hashes/utils'; -import { Fp, validateField } from '../abstract/modular.js'; -import { poseidon } from '../abstract/poseidon.js'; -import * as u from '../abstract/utils.js'; +import { Fp, validateField } from '../esm/abstract/modular.js'; +import { poseidon } from '../esm/abstract/poseidon.js'; +import * as u from '../esm/abstract/utils.js'; // Poseidon hash https://docs.starkware.co/starkex/stark-curve.html export const Fp253 = Fp( diff --git a/test/basic.test.js b/test/basic.test.js index daf9a12..3faaef0 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,20 +1,20 @@ import { deepStrictEqual, throws } from 'assert'; import { should, describe } from 'micro-should'; import * as fc from 'fast-check'; -import * as mod from '../abstract/modular.js'; -import { bytesToHex as toHex } from '../abstract/utils.js'; +import * as mod from '../esm/abstract/modular.js'; +import { bytesToHex as toHex } from '../esm/abstract/utils.js'; // Generic tests for all curves in package import { secp192r1, secp224r1 } from './_more-curves.helpers.js'; -import { secp256r1 } from '../p256.js'; -import { secp384r1 } from '../p384.js'; -import { secp521r1 } from '../p521.js'; -import { secp256k1 } from '../secp256k1.js'; -import { ed25519, ed25519ctx, ed25519ph, x25519 } from '../ed25519.js'; -import { ed448, ed448ph } from '../ed448.js'; -import { pallas, vesta } from '../pasta.js'; -import { bn254 } from '../bn.js'; -import { jubjub } from '../jubjub.js'; -import { bls12_381 } from '../bls12-381.js'; +import { secp256r1 } from '../esm/p256.js'; +import { secp384r1 } from '../esm/p384.js'; +import { secp521r1 } from '../esm/p521.js'; +import { secp256k1 } from '../esm/secp256k1.js'; +import { ed25519, ed25519ctx, ed25519ph, x25519 } from '../esm/ed25519.js'; +import { ed448, ed448ph } from '../esm/ed448.js'; +import { pallas, vesta } from '../esm/pasta.js'; +import { bn254 } from '../esm/bn.js'; +import { jubjub } from '../esm/jubjub.js'; +import { bls12_381 } from '../esm/bls12-381.js'; // Fields tests const FIELDS = { diff --git a/test/bls12-381.test.js b/test/bls12-381.test.js index d3183c2..18d1efa 100644 --- a/test/bls12-381.test.js +++ b/test/bls12-381.test.js @@ -2,10 +2,10 @@ import { deepStrictEqual, notDeepStrictEqual, throws } from 'assert'; import * as fc from 'fast-check'; import { readFileSync } from 'fs'; import { describe, should } from 'micro-should'; -import { wNAF } from '../abstract/curve.js'; -import { bytesToHex, utf8ToBytes } from '../abstract/utils.js'; -import { hash_to_field } from '../abstract/hash-to-curve.js'; -import { bls12_381 as bls } from '../bls12-381.js'; +import { wNAF } from '../esm/abstract/curve.js'; +import { bytesToHex, utf8ToBytes } from '../esm/abstract/utils.js'; +import { hash_to_field } from '../esm/abstract/hash-to-curve.js'; +import { bls12_381 as bls } from '../esm/bls12-381.js'; import zkVectors from './bls12-381/zkcrypto/converted.json' assert { type: 'json' }; import pairingVectors from './bls12-381/go_pairing_vectors/pairing.json' assert { type: 'json' }; diff --git a/test/ed25519-addons.test.js b/test/ed25519-addons.test.js index ad859f0..03e010a 100644 --- a/test/ed25519-addons.test.js +++ b/test/ed25519-addons.test.js @@ -2,9 +2,9 @@ import { sha512 } from '@noble/hashes/sha512'; import { hexToBytes, bytesToHex, randomBytes } from '@noble/hashes/utils'; import { deepStrictEqual, strictEqual, throws } from 'assert'; import { describe, should } from 'micro-should'; -import { bytesToNumberLE, numberToBytesLE } from '../abstract/utils.js'; +import { bytesToNumberLE, numberToBytesLE } from '../esm/abstract/utils.js'; import { default as x25519vectors } from './wycheproof/x25519_test.json' assert { type: 'json' }; -import { ed25519ctx, ed25519ph, RistrettoPoint, x25519 } from '../ed25519.js'; +import { ed25519ctx, ed25519ph, RistrettoPoint, x25519 } from '../esm/ed25519.js'; // const ed = ed25519; const hex = bytesToHex; diff --git a/test/ed25519.helpers.js b/test/ed25519.helpers.js index d030bec..c426896 100644 --- a/test/ed25519.helpers.js +++ b/test/ed25519.helpers.js @@ -1 +1 @@ -export { ed25519, ED25519_TORSION_SUBGROUP } from '../ed25519.js'; +export { ed25519, ED25519_TORSION_SUBGROUP } from '../esm/ed25519.js'; diff --git a/test/ed448.test.js b/test/ed448.test.js index 534e1ca..f892cad 100644 --- a/test/ed448.test.js +++ b/test/ed448.test.js @@ -1,9 +1,9 @@ import { deepStrictEqual, throws } from 'assert'; import { describe, should } from 'micro-should'; import * as fc from 'fast-check'; -import { ed448, ed448ph, x448 } from '../ed448.js'; +import { ed448, ed448ph, x448 } from '../esm/ed448.js'; import { hexToBytes, bytesToHex, randomBytes } from '@noble/hashes/utils'; -import { numberToBytesLE } from '../abstract/utils.js'; +import { numberToBytesLE } from '../esm/abstract/utils.js'; import { default as ed448vectors } from './wycheproof/ed448_test.json' assert { type: 'json' }; import { default as x448vectors } from './wycheproof/x448_test.json' assert { type: 'json' }; diff --git a/test/hash-to-curve.test.js b/test/hash-to-curve.test.js index 2bdc4bc..a23b67f 100644 --- a/test/hash-to-curve.test.js +++ b/test/hash-to-curve.test.js @@ -5,15 +5,15 @@ import { bytesToHex } from '@noble/hashes/utils'; import { sha256 } from '@noble/hashes/sha256'; import { sha512 } from '@noble/hashes/sha512'; import { shake128, shake256 } from '@noble/hashes/sha3'; -import * as secp256r1 from '../p256.js'; -import * as secp384r1 from '../p384.js'; -import * as secp521r1 from '../p521.js'; -import * as ed25519 from '../ed25519.js'; -import * as ed448 from '../ed448.js'; -import * as secp256k1 from '../secp256k1.js'; -import { bls12_381 } from '../bls12-381.js'; -import { expand_message_xmd, expand_message_xof } from '../abstract/hash-to-curve.js'; -import { utf8ToBytes } from '../abstract/utils.js'; +import * as secp256r1 from '../esm/p256.js'; +import * as secp384r1 from '../esm/p384.js'; +import * as secp521r1 from '../esm/p521.js'; +import * as ed25519 from '../esm/ed25519.js'; +import * as ed448 from '../esm/ed448.js'; +import * as secp256k1 from '../esm/secp256k1.js'; +import { bls12_381 } from '../esm/bls12-381.js'; +import { expand_message_xmd, expand_message_xof } from '../esm/abstract/hash-to-curve.js'; +import { utf8ToBytes } from '../esm/abstract/utils.js'; // XMD import { default as xmd_sha256_38 } from './hash-to-curve/expand_message_xmd_SHA256_38.json' assert { type: 'json' }; import { default as xmd_sha256_256 } from './hash-to-curve/expand_message_xmd_SHA256_256.json' assert { type: 'json' }; diff --git a/test/jubjub.test.js b/test/jubjub.test.js index d97dcf0..e443bff 100644 --- a/test/jubjub.test.js +++ b/test/jubjub.test.js @@ -1,4 +1,4 @@ -import { jubjub, findGroupHash } from '../jubjub.js'; +import { jubjub, findGroupHash } from '../esm/jubjub.js'; import { describe, should } from 'micro-should'; import { deepStrictEqual, throws } from 'assert'; const Point = jubjub.ExtendedPoint; diff --git a/test/nist.test.js b/test/nist.test.js index 5aa21a3..62751b3 100644 --- a/test/nist.test.js +++ b/test/nist.test.js @@ -1,11 +1,11 @@ import { deepStrictEqual } from 'assert'; import { describe, should } from 'micro-should'; import { secp192r1, secp224r1, P192, P224 } from './_more-curves.helpers.js'; -import { secp256r1, P256 } from '../p256.js'; -import { secp384r1, P384 } from '../p384.js'; -import { secp521r1, P521 } from '../p521.js'; -import { secp256k1 } from '../secp256k1.js'; -import { hexToBytes, bytesToHex } from '../abstract/utils.js'; +import { secp256r1, P256 } from '../esm/p256.js'; +import { secp384r1, P384 } from '../esm/p384.js'; +import { secp521r1, P521 } from '../esm/p521.js'; +import { secp256k1 } from '../esm/secp256k1.js'; +import { hexToBytes, bytesToHex } from '../esm/abstract/utils.js'; import { default as ecdsa } from './wycheproof/ecdsa_test.json' assert { type: 'json' }; import { default as ecdh } from './wycheproof/ecdh_test.json' assert { type: 'json' }; import { default as rfc6979 } from './fixtures/rfc6979.json' assert { type: 'json' }; diff --git a/test/poseidon.test.js b/test/poseidon.test.js index 76308b4..3ba0df3 100644 --- a/test/poseidon.test.js +++ b/test/poseidon.test.js @@ -1,8 +1,8 @@ import { deepStrictEqual, throws } from 'assert'; import { should, describe } from 'micro-should'; -import * as poseidon from '../abstract/poseidon.js'; +import * as poseidon from '../esm/abstract/poseidon.js'; import * as stark from './_poseidon.helpers.js'; -import * as mod from '../abstract/modular.js'; +import * as mod from '../esm/abstract/modular.js'; import { default as pvectors } from './vectors/poseidon.json' assert { type: 'json' }; const { st1, st2, st3, st4 } = pvectors; diff --git a/test/secp256k1-schnorr.test.js b/test/secp256k1-schnorr.test.js index 4080ca7..6e0d887 100644 --- a/test/secp256k1-schnorr.test.js +++ b/test/secp256k1-schnorr.test.js @@ -2,7 +2,7 @@ import { deepStrictEqual, throws } from 'assert'; import { readFileSync } from 'fs'; import { should, describe } from 'micro-should'; import { bytesToHex as hex } from '@noble/hashes/utils'; -import { schnorr } from '../secp256k1.js'; +import { schnorr } from '../esm/secp256k1.js'; const schCsv = readFileSync('./test/vectors/schnorr.csv', 'utf-8'); describe('schnorr.sign()', () => { diff --git a/test/secp256k1.helpers.js b/test/secp256k1.helpers.js index 5d871a8..c907ddd 100644 --- a/test/secp256k1.helpers.js +++ b/test/secp256k1.helpers.js @@ -1,8 +1,8 @@ // @ts-ignore -export { secp256k1 as secp } from '../secp256k1.js'; -import { secp256k1 as _secp } from '../secp256k1.js'; -export { bytesToNumberBE, numberToBytesBE } from '../abstract/utils.js'; -export { mod } from '../abstract/modular.js'; +export { secp256k1 as secp } from '../esm/secp256k1.js'; +import { secp256k1 as _secp } from '../esm/secp256k1.js'; +export { bytesToNumberBE, numberToBytesBE } from '../esm/abstract/utils.js'; +export { mod } from '../esm/abstract/modular.js'; export const sigFromDER = (der) => { return _secp.Signature.fromDER(der); }; diff --git a/tsconfig.esm.json b/tsconfig.esm.json new file mode 100644 index 0000000..1f86353 --- /dev/null +++ b/tsconfig.esm.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "strict": true, + "outDir": "esm", + "target": "es2020", + "module": "es6", + "moduleResolution": "node16", + "noUnusedLocals": true, + "sourceMap": true, + "baseUrl": ".", + "paths": { + "@noble/hashes/crypto": [ "src/crypto" ] + }, + }, + "include": ["src"], + "exclude": [ + "node_modules", + "lib", + ], +} \ No newline at end of file