Skip to content

Commit

Permalink
chore: refactor tests to load json curves using backwards-compatible API
Browse files Browse the repository at this point in the history
There is no unified loader API for Node 16 and Node 22+.
This fixes test compatibility with Node 16.

https://stackoverflow.com/questions/78876691/syntaxerror-unexpected-identifier-assert-on-json-import-in-node-v22
  • Loading branch information
legobeat committed Aug 28, 2024
1 parent f0077fa commit 66c4e83
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 78 deletions.
3 changes: 2 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { json } from './utils.js';
import { deepStrictEqual, notDeepStrictEqual, throws } from 'node:assert';
import { should, describe } from 'micro-should';
import * as fc from 'fast-check';
Expand All @@ -15,11 +16,11 @@ import { pallas, vesta } from '../esm/pasta.js';
import { bn254_weierstrass } from '../esm/bn254.js';
import { jubjub } from '../esm/jubjub.js';
import { bls12_381 } from '../esm/bls12-381.js';
import { default as wyche_curves } from './wycheproof/ec_prime_order_curves_test.json' with { type: 'json' };
import { createCurve } from '../esm/_shortw_utils.js';
import { Field } from '../esm/abstract/modular.js';
import { sha256 } from '@noble/hashes/sha256';
import { bn254 } from '../esm/bn254.js';
const wyche_curves = json('./wycheproof/ec_prime_order_curves_test.json');

// Fields tests
const FIELDS = {
Expand Down
9 changes: 5 additions & 4 deletions test/bls12-381.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deepStrictEqual, notDeepStrictEqual, throws } from 'node:assert';
import { json } from './utils.js';
import { deepStrictEqual, throws } from 'node:assert';
import * as fc from 'fast-check';
import { readFileSync } from 'node:fs';
import { describe, should } from 'micro-should';
Expand All @@ -9,9 +10,9 @@ import { bls12_381 as bls, bls12_381 } from '../esm/bls12-381.js';

import * as utils from '../esm/abstract/utils.js';

import eip2537 from './bls12-381/eip2537.json' with { type: 'json' };
import zkVectors from './bls12-381/zkcrypto/converted.json' with { type: 'json' };
import pairingVectors from './bls12-381/go_pairing_vectors/pairing.json' with { type: 'json' };
const eip2537 = json('./bls12-381/eip2537.json');
const zkVectors = json('./bls12-381/zkcrypto/converted.json');
const pairingVectors = json('./bls12-381/go_pairing_vectors/pairing.json');
const G1_VECTORS = readFileSync('./test/bls12-381/bls12-381-g1-test-vectors.txt', 'utf-8')
.trim()
.split('\n')
Expand Down
3 changes: 2 additions & 1 deletion test/ed25519-addons.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { json } from './utils.js';
import { sha512 } from '@noble/hashes/sha512';
import { bytesToHex as hex, hexToBytes } from '@noble/hashes/utils';
import { deepStrictEqual, throws } from 'node:assert';
import { describe, should } from 'micro-should';
import { bytesToNumberLE, numberToBytesLE } from '../esm/abstract/utils.js';
import { default as x25519vectors } from './wycheproof/x25519_test.json' with { type: 'json' };
const x25519vectors = json('./wycheproof/x25519_test.json');
import {
ed25519,
ed25519ctx,
Expand Down
9 changes: 5 additions & 4 deletions test/ed25519.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { json } from './utils.js';
import { deepStrictEqual, strictEqual, throws } from 'node:assert';
import { readFileSync } from 'node:fs';
import { bytesToHex, concatBytes, hexToBytes, utf8ToBytes, randomBytes } from '@noble/hashes/utils';
import * as fc from 'fast-check';
import { describe, should } from 'micro-should';
import { ed25519 as ed, ED25519_TORSION_SUBGROUP, numberToBytesLE } from './ed25519.helpers.js';
// Old vectors allow to test sign() because they include private key
import { default as ed25519vectors_OLD } from './ed25519/ed25519_test_OLD.json' with { type: 'json' };
import { default as ed25519vectors } from './wycheproof/ed25519_test.json' with { type: 'json' };
import { default as zip215 } from './ed25519/zip215.json' with { type: 'json' };
import { default as edgeCases } from './ed25519/edge-cases.json' with { type: 'json' };
const ed25519vectors_OLD = json('./ed25519/ed25519_test_OLD.json');
const ed25519vectors = json('./wycheproof/ed25519_test.json');
const zip215 = json('./ed25519/zip215.json');
const edgeCases = json('./ed25519/edge-cases.json');

// Any changes to the file will need to be aware of the fact
// the file is shared between noble-curves and noble-ed25519.
Expand Down
7 changes: 4 additions & 3 deletions test/ed448.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { json } from './utils.js';
import { deepStrictEqual, throws } from 'node:assert';
import { describe, should } from 'micro-should';
import * as fc from 'fast-check';
import { ed448, ed448ph, x448 } from '../esm/ed448.js';
import { bytesToHex, concatBytes, hexToBytes, randomBytes } from '@noble/hashes/utils';
import { numberToBytesLE } from '../esm/abstract/utils.js';
// Old vectors allow to test sign() because they include private key
import { default as ed448vectorsOld } from './ed448/ed448_test_OLD.json' with { type: 'json' };
import { default as ed448vectors } from './wycheproof/ed448_test.json' with { type: 'json' };
import { default as x448vectors } from './wycheproof/x448_test.json' with { type: 'json' };
const ed448vectorsOld = json('./ed448/ed448_test_OLD.json');
const ed448vectors = json('./wycheproof/ed448_test.json');
const x448vectors = json('./wycheproof/x448_test.json');

describe('ed448', () => {
const ed = ed448;
Expand Down
45 changes: 23 additions & 22 deletions test/hash-to-curve.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { json } from './utils.js';
import { deepStrictEqual } from 'node:assert';
import { describe, should } from 'micro-should';
import { bytesToHex } from '@noble/hashes/utils';
Expand All @@ -15,37 +16,37 @@ 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' with { type: 'json' };
import { default as xmd_sha256_256 } from './hash-to-curve/expand_message_xmd_SHA256_256.json' with { type: 'json' };
import { default as xmd_sha512_38 } from './hash-to-curve/expand_message_xmd_SHA512_38.json' with { type: 'json' };
const xmd_sha256_38 = json('./hash-to-curve/expand_message_xmd_SHA256_38.json');
const xmd_sha256_256 = json('./hash-to-curve/expand_message_xmd_SHA256_256.json');
const xmd_sha512_38 = json('./hash-to-curve/expand_message_xmd_SHA512_38.json');
// XOF
import { default as xof_shake128_36 } from './hash-to-curve/expand_message_xof_SHAKE128_36.json' with { type: 'json' };
import { default as xof_shake128_256 } from './hash-to-curve/expand_message_xof_SHAKE128_256.json' with { type: 'json' };
import { default as xof_shake256_36 } from './hash-to-curve/expand_message_xof_SHAKE256_36.json' with { type: 'json' };
const xof_shake128_36 = json('./hash-to-curve/expand_message_xof_SHAKE128_36.json');
const xof_shake128_256 = json('./hash-to-curve/expand_message_xof_SHAKE128_256.json');
const xof_shake256_36 = json('./hash-to-curve/expand_message_xof_SHAKE256_36.json');
// P256
import { default as p256_ro } from './hash-to-curve/P256_XMD_SHA-256_SSWU_RO_.json' with { type: 'json' };
import { default as p256_nu } from './hash-to-curve/P256_XMD_SHA-256_SSWU_NU_.json' with { type: 'json' };
const p256_ro = json('./hash-to-curve/P256_XMD_SHA-256_SSWU_RO_.json');
const p256_nu = json('./hash-to-curve/P256_XMD_SHA-256_SSWU_NU_.json');
// P384
import { default as p384_ro } from './hash-to-curve/P384_XMD_SHA-384_SSWU_RO_.json' with { type: 'json' };
import { default as p384_nu } from './hash-to-curve/P384_XMD_SHA-384_SSWU_NU_.json' with { type: 'json' };
const p384_ro = json('./hash-to-curve/P384_XMD_SHA-384_SSWU_RO_.json');
const p384_nu = json('./hash-to-curve/P384_XMD_SHA-384_SSWU_NU_.json');
// P521
import { default as p521_ro } from './hash-to-curve/P521_XMD_SHA-512_SSWU_RO_.json' with { type: 'json' };
import { default as p521_nu } from './hash-to-curve/P521_XMD_SHA-512_SSWU_NU_.json' with { type: 'json' };
const p521_ro = json('./hash-to-curve/P521_XMD_SHA-512_SSWU_RO_.json');
const p521_nu = json('./hash-to-curve/P521_XMD_SHA-512_SSWU_NU_.json');
// secp256k1
import { default as secp256k1_ro } from './hash-to-curve/secp256k1_XMD_SHA-256_SSWU_RO_.json' with { type: 'json' };
import { default as secp256k1_nu } from './hash-to-curve/secp256k1_XMD_SHA-256_SSWU_NU_.json' with { type: 'json' };
const secp256k1_ro = json('./hash-to-curve/secp256k1_XMD_SHA-256_SSWU_RO_.json');
const secp256k1_nu = json('./hash-to-curve/secp256k1_XMD_SHA-256_SSWU_NU_.json');
// bls-G1
import { default as g1_ro } from './hash-to-curve/BLS12381G1_XMD_SHA-256_SSWU_RO_.json' with { type: 'json' };
import { default as g1_nu } from './hash-to-curve/BLS12381G1_XMD_SHA-256_SSWU_NU_.json' with { type: 'json' };
const g1_ro = json('./hash-to-curve/BLS12381G1_XMD_SHA-256_SSWU_RO_.json');
const g1_nu = json('./hash-to-curve/BLS12381G1_XMD_SHA-256_SSWU_NU_.json');
// bls-G2
import { default as g2_ro } from './hash-to-curve/BLS12381G2_XMD_SHA-256_SSWU_RO_.json' with { type: 'json' };
import { default as g2_nu } from './hash-to-curve/BLS12381G2_XMD_SHA-256_SSWU_NU_.json' with { type: 'json' };
const g2_ro = json('./hash-to-curve/BLS12381G2_XMD_SHA-256_SSWU_RO_.json');
const g2_nu = json('./hash-to-curve/BLS12381G2_XMD_SHA-256_SSWU_NU_.json');
// ed25519
import { default as ed25519_ro } from './hash-to-curve/edwards25519_XMD_SHA-512_ELL2_RO_.json' with { type: 'json' };
import { default as ed25519_nu } from './hash-to-curve/edwards25519_XMD_SHA-512_ELL2_NU_.json' with { type: 'json' };
const ed25519_ro = json('./hash-to-curve/edwards25519_XMD_SHA-512_ELL2_RO_.json');
const ed25519_nu = json('./hash-to-curve/edwards25519_XMD_SHA-512_ELL2_NU_.json');
// ed448
import { default as ed448_ro } from './hash-to-curve/edwards448_XOF_SHAKE256_ELL2_RO_.json' with { type: 'json' };
import { default as ed448_nu } from './hash-to-curve/edwards448_XOF_SHAKE256_ELL2_NU_.json' with { type: 'json' };
const ed448_ro = json('./hash-to-curve/edwards448_XOF_SHAKE256_ELL2_RO_.json');
const ed448_nu = json('./hash-to-curve/edwards448_XOF_SHAKE256_ELL2_NU_.json');

function testExpandXMD(hash, vectors) {
describe(`${vectors.hash}/${vectors.DST.length}`, () => {
Expand Down
73 changes: 37 additions & 36 deletions test/nist.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { json } from './utils.js';
import { deepStrictEqual, throws } from 'node:assert';
import { describe, should } from 'micro-should';
import { secp192r1, secp224r1, p192, p224 } from './_more-curves.helpers.js';
Expand All @@ -7,48 +8,48 @@ 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' with { type: 'json' };
import { default as ecdh } from './wycheproof/ecdh_test.json' with { type: 'json' };
import { default as rfc6979 } from './vectors/rfc6979.json' with { type: 'json' };
import { default as endoVectors } from './vectors/secp256k1/endomorphism.json' with { type: 'json' };
const ecdsa = json('./wycheproof/ecdsa_test.json');
const ecdh = json('./wycheproof/ecdh_test.json');
const rfc6979 = json('./vectors/rfc6979.json');
const endoVectors = json('./vectors/secp256k1/endomorphism.json');

import { default as ecdh_secp224r1_test } from './wycheproof/ecdh_secp224r1_test.json' with { type: 'json' };
import { default as ecdh_secp256r1_test } from './wycheproof/ecdh_secp256r1_test.json' with { type: 'json' };
import { default as ecdh_secp256k1_test } from './wycheproof/ecdh_secp256k1_test.json' with { type: 'json' };
import { default as ecdh_secp384r1_test } from './wycheproof/ecdh_secp384r1_test.json' with { type: 'json' };
import { default as ecdh_secp521r1_test } from './wycheproof/ecdh_secp521r1_test.json' with { type: 'json' };
const ecdh_secp224r1_test = json('./wycheproof/ecdh_secp224r1_test.json');
const ecdh_secp256r1_test = json('./wycheproof/ecdh_secp256r1_test.json');
const ecdh_secp256k1_test = json('./wycheproof/ecdh_secp256k1_test.json');
const ecdh_secp384r1_test = json('./wycheproof/ecdh_secp384r1_test.json');
const ecdh_secp521r1_test = json('./wycheproof/ecdh_secp521r1_test.json');
// Tests with custom hashes
import { default as secp224r1_sha224_test } from './wycheproof/ecdsa_secp224r1_sha224_test.json' with { type: 'json' };
import { default as secp224r1_sha256_test } from './wycheproof/ecdsa_secp224r1_sha256_test.json' with { type: 'json' };
import { default as secp224r1_sha3_224_test } from './wycheproof/ecdsa_secp224r1_sha3_224_test.json' with { type: 'json' };
import { default as secp224r1_sha3_256_test } from './wycheproof/ecdsa_secp224r1_sha3_256_test.json' with { type: 'json' };
import { default as secp224r1_sha3_512_test } from './wycheproof/ecdsa_secp224r1_sha3_512_test.json' with { type: 'json' };
import { default as secp224r1_sha512_test } from './wycheproof/ecdsa_secp224r1_sha512_test.json' with { type: 'json' };
import { default as secp224r1_shake128_test } from './wycheproof/ecdsa_secp224r1_shake128_test.json' with { type: 'json' };
const secp224r1_sha224_test = json('./wycheproof/ecdsa_secp224r1_sha224_test.json');
const secp224r1_sha256_test = json('./wycheproof/ecdsa_secp224r1_sha256_test.json');
const secp224r1_sha3_224_test = json('./wycheproof/ecdsa_secp224r1_sha3_224_test.json');
const secp224r1_sha3_256_test = json('./wycheproof/ecdsa_secp224r1_sha3_256_test.json');
const secp224r1_sha3_512_test = json('./wycheproof/ecdsa_secp224r1_sha3_512_test.json');
const secp224r1_sha512_test = json('./wycheproof/ecdsa_secp224r1_sha512_test.json');
const secp224r1_shake128_test = json('./wycheproof/ecdsa_secp224r1_shake128_test.json');

import { default as secp256k1_sha256_bitcoin_test } from './wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json' with { type: 'json' };
import { default as secp256k1_sha256_test } from './wycheproof/ecdsa_secp256k1_sha256_test.json' with { type: 'json' };
import { default as secp256k1_sha3_256_test } from './wycheproof/ecdsa_secp256k1_sha3_256_test.json' with { type: 'json' };
import { default as secp256k1_sha3_512_test } from './wycheproof/ecdsa_secp256k1_sha3_512_test.json' with { type: 'json' };
import { default as secp256k1_sha512_test } from './wycheproof/ecdsa_secp256k1_sha512_test.json' with { type: 'json' };
import { default as secp256k1_shake128_test } from './wycheproof/ecdsa_secp256k1_shake128_test.json' with { type: 'json' };
import { default as secp256k1_shake256_test } from './wycheproof/ecdsa_secp256k1_shake256_test.json' with { type: 'json' };
const secp256k1_sha256_bitcoin_test = json('./wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json');
const secp256k1_sha256_test = json('./wycheproof/ecdsa_secp256k1_sha256_test.json');
const secp256k1_sha3_256_test = json('./wycheproof/ecdsa_secp256k1_sha3_256_test.json');
const secp256k1_sha3_512_test = json('./wycheproof/ecdsa_secp256k1_sha3_512_test.json');
const secp256k1_sha512_test = json('./wycheproof/ecdsa_secp256k1_sha512_test.json');
const secp256k1_shake128_test = json('./wycheproof/ecdsa_secp256k1_shake128_test.json');
const secp256k1_shake256_test = json('./wycheproof/ecdsa_secp256k1_shake256_test.json');

import { default as secp256r1_sha256_test } from './wycheproof/ecdsa_secp256r1_sha256_test.json' with { type: 'json' };
import { default as secp256r1_sha3_256_test } from './wycheproof/ecdsa_secp256r1_sha3_256_test.json' with { type: 'json' };
import { default as secp256r1_sha3_512_test } from './wycheproof/ecdsa_secp256r1_sha3_512_test.json' with { type: 'json' };
import { default as secp256r1_sha512_test } from './wycheproof/ecdsa_secp256r1_sha512_test.json' with { type: 'json' };
import { default as secp256r1_shake128_test } from './wycheproof/ecdsa_secp256r1_shake128_test.json' with { type: 'json' };
const secp256r1_sha256_test = json('./wycheproof/ecdsa_secp256r1_sha256_test.json');
const secp256r1_sha3_256_test = json('./wycheproof/ecdsa_secp256r1_sha3_256_test.json');
const secp256r1_sha3_512_test = json('./wycheproof/ecdsa_secp256r1_sha3_512_test.json');
const secp256r1_sha512_test = json('./wycheproof/ecdsa_secp256r1_sha512_test.json');
const secp256r1_shake128_test = json('./wycheproof/ecdsa_secp256r1_shake128_test.json');

import { default as secp384r1_sha384_test } from './wycheproof/ecdsa_secp384r1_sha384_test.json' with { type: 'json' };
import { default as secp384r1_sha3_384_test } from './wycheproof/ecdsa_secp384r1_sha3_384_test.json' with { type: 'json' };
import { default as secp384r1_sha3_512_test } from './wycheproof/ecdsa_secp384r1_sha3_512_test.json' with { type: 'json' };
import { default as secp384r1_sha512_test } from './wycheproof/ecdsa_secp384r1_sha512_test.json' with { type: 'json' };
import { default as secp384r1_shake256_test } from './wycheproof/ecdsa_secp384r1_shake256_test.json' with { type: 'json' };
const secp384r1_sha384_test = json('./wycheproof/ecdsa_secp384r1_sha384_test.json');
const secp384r1_sha3_384_test = json('./wycheproof/ecdsa_secp384r1_sha3_384_test.json');
const secp384r1_sha3_512_test = json('./wycheproof/ecdsa_secp384r1_sha3_512_test.json');
const secp384r1_sha512_test = json('./wycheproof/ecdsa_secp384r1_sha512_test.json');
const secp384r1_shake256_test = json('./wycheproof/ecdsa_secp384r1_shake256_test.json');

import { default as secp521r1_sha3_512_test } from './wycheproof/ecdsa_secp521r1_sha3_512_test.json' with { type: 'json' };
import { default as secp521r1_sha512_test } from './wycheproof/ecdsa_secp521r1_sha512_test.json' with { type: 'json' };
import { default as secp521r1_shake256_test } from './wycheproof/ecdsa_secp521r1_shake256_test.json' with { type: 'json' };
const secp521r1_sha3_512_test = json('./wycheproof/ecdsa_secp521r1_sha3_512_test.json');
const secp521r1_sha512_test = json('./wycheproof/ecdsa_secp521r1_sha512_test.json');
const secp521r1_shake256_test = json('./wycheproof/ecdsa_secp521r1_shake256_test.json');

import { sha3_224, sha3_256, sha3_384, sha3_512, shake128, shake256 } from '@noble/hashes/sha3';
import { sha512, sha384 } from '@noble/hashes/sha512';
Expand Down
5 changes: 3 additions & 2 deletions test/poseidon.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { deepStrictEqual, throws } from 'node:assert';
import { json } from './utils.js';
import { deepStrictEqual } from 'node:assert';
import { should, describe } from 'micro-should';
import * as poseidon from '../esm/abstract/poseidon.js';
import * as stark from './_poseidon.helpers.js';
import * as mod from '../esm/abstract/modular.js';
import { default as pvectors } from './vectors/poseidon.json' with { type: 'json' };
const pvectors = json('./vectors/poseidon.json');
const { st1, st2, st3, st4 } = pvectors;

describe('Stark', () => {
Expand Down
11 changes: 6 additions & 5 deletions test/secp256k1.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { json } from './utils.js';
import { hexToBytes, bytesToHex as hex } from '@noble/hashes/utils';
import { deepStrictEqual, throws } from 'node:assert';
import * as fc from 'fast-check';
Expand All @@ -8,11 +9,11 @@ import {
secp, sigFromDER, sigToDER, selectHash, normVerifySig, mod, bytesToNumberBE, numberToBytesBE
} from './secp256k1.helpers.js';

import { default as ecdsa } from './vectors/secp256k1/ecdsa.json' with { type: 'json' };
import { default as ecdh } from './wycheproof/ecdh_secp256k1_test.json' with { type: 'json' };
import { default as privates } from './vectors/secp256k1/privates.json' with { type: 'json' };
import { default as points } from './vectors/secp256k1/points.json' with { type: 'json' };
import { default as wp } from './wycheproof/ecdsa_secp256k1_sha256_test.json' with { type: 'json' };
const ecdsa = json('./vectors/secp256k1/ecdsa.json');
const ecdh = json('./wycheproof/ecdh_secp256k1_test.json');
const privates = json('./vectors/secp256k1/privates.json');
const points = json('./vectors/secp256k1/points.json');
const wp = json('./wycheproof/ecdsa_secp256k1_sha256_test.json');

// Any changes to the file will need to be aware of the fact
// the file is shared between noble-curves and noble-secp256k1.
Expand Down
4 changes: 4 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export function jsonGZ(path) {
const unz = gunzipSync(readFileSync(joinPath(__dirname, path)));
return JSON.parse(unz.toString('utf8'));
}

export function json(path) {
return JSON.parse(readFileSync(joinPath(__dirname, path), { encoding: 'utf-8' }));
}

0 comments on commit 66c4e83

Please sign in to comment.