Repository: EYBlockchain/nightfall_3 (js)

Issue:
Remove constants from config
There are still several maths constants in the default config file:
- BABYJUBJUB parameters
- ELIGATOR2 parameters
- BN128_GROUP_ORDER
- BN128_PRIME_FIELD
- MAX_PUBLIC_VALUES

These should be moved to the constants file

Pull request:
bigint constants refactored from config
fixes #874 

Patch:
diff --git a/common-files/classes/fq2.mjs b/common-files/classes/fq2.mjs
index f8b6857d..6b579689 100644
--- a/common-files/classes/fq2.mjs
+++ b/common-files/classes/fq2.mjs
@@ -3,11 +3,10 @@ Class representing a complex modular number.  In particular, this is useful
 for dealing with f_q^2 field elements in the alt BN128 curve.
 */
 
-// eslint-disable-next-line import/no-extraneous-dependencies
-import config from 'config';
 import { modDivide, complexDivMod } from '../utils/crypto/modular-division.mjs';
+import constants from '../constants/index.mjs';
 
-const { BN128_PRIME_FIELD } = config;
+const { BN128_PRIME_FIELD } = constants;
 
 class Fq2 {
   m = BN128_PRIME_FIELD;
diff --git a/common-files/constants/bigInts.js b/common-files/constants/bigInts.js
new file mode 100644
index 00000000..71fa1da8
--- /dev/null
+++ b/common-files/constants/bigInts.js
@@ -0,0 +1,29 @@
+/* eslint-disable no-extend-native */
+BigInt.prototype.toJSON = function () {
+  return `${this.toString()} BigInt`;
+};
+
+module.exports = {
+  // the various parameters needed to describe the Babyjubjub curve that we use for El-Gamal
+  // BABYJUBJUB
+  // Montgomery EC form is y^2 = x^3 + Ax^2 + Bx
+  // Montgomery EC form of BabyJubJub is y^2 = x^3 + 168698x^2 + x
+  // A = 168698 and B = 1
+  BABYJUBJUB: {
+    JUBJUBA: BigInt(168700),
+    JUBJUBD: BigInt(168696),
+    INFINITY: [BigInt(0), BigInt(1)],
+    GENERATOR: [
+      BigInt('16540640123574156134436876038791482806971768689494387082833631921987005038935'),
+      BigInt('20819045374670962167435360035096875258406992893633759881276124905556507972311'),
+    ],
+    JUBJUBE: BigInt(
+      '21888242871839275222246405745257275088614511777268538073601725287587578984328',
+    ),
+    JUBJUBC: BigInt(8),
+    MONTA: BigInt(168698),
+    MONTB: BigInt(1),
+  },
+  BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
+  BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
+};
diff --git a/common-files/constants/index.mjs b/common-files/constants/index.mjs
index 2048f3e5..0527bc8f 100644
--- a/common-files/constants/index.mjs
+++ b/common-files/constants/index.mjs
@@ -4,5 +4,6 @@ import { createRequire } from 'module';
 
 const require = createRequire(import.meta.url);
 const constants = require('./constants.json');
+const bigInts = require('./bigInts.js');
 
-export default constants;
+export default { ...constants, ...bigInts };
diff --git a/common-files/utils/crypto/modular-division.mjs b/common-files/utils/crypto/modular-division.mjs
index 1d908762..96bc3100 100644
--- a/common-files/utils/crypto/modular-division.mjs
+++ b/common-files/utils/crypto/modular-division.mjs
@@ -1,9 +1,8 @@
 // modular division
-// eslint-disable-next-line import/no-extraneous-dependencies
-import config from 'config';
 import { mulMod, addMod } from './number-theory.mjs';
+import constants from '../../constants/index.mjs';
 
-const { BN128_PRIME_FIELD } = config;
+const { BN128_PRIME_FIELD } = constants;
 
 // function for extended Euclidean Algorithm
 // (used to find modular inverse.
diff --git a/common-files/utils/curve-maths/curves.mjs b/common-files/utils/curve-maths/curves.mjs
index fb4c232f..5573a9e3 100644
--- a/common-files/utils/curve-maths/curves.mjs
+++ b/common-files/utils/curve-maths/curves.mjs
@@ -5,15 +5,14 @@ module for manupulating elliptic curve points for an alt-bn128 curve. This
 is the curve that Ethereum currently has pairing precompiles for. All the
 return values are BigInts (or arrays of BigInts).
 */
-// eslint-disable-next-line import/no-extraneous-dependencies
-import config from 'config';
 import utils from 'common-files/utils/crypto/merkle-tree/utils.mjs';
 import { mulMod, addMod, squareRootModPrime } from '../crypto/number-theory.mjs';
 import Fq2 from '../../classes/fq2.mjs';
 import Proof from '../../classes/proof.mjs';
 import { modDivide } from '../crypto/modular-division.mjs';
+import constants from '../../constants/index.mjs';
 
-const { BN128_PRIME_FIELD, BN128_GROUP_ORDER, BABYJUBJUB } = config;
+const { BN128_PRIME_FIELD, BN128_GROUP_ORDER, BABYJUBJUB } = constants;
 
 const one = BigInt(1);
 const { JUBJUBE, JUBJUBC, JUBJUBD, JUBJUBA } = BABYJUBJUB;
diff --git a/config/default.js b/config/default.js
index 08635f64..f0b74fce 100644
--- a/config/default.js
+++ b/config/default.js
@@ -5,11 +5,6 @@ function configureAWSBucket() {
   return `${bucket}-${mode}`;
 }
 
-/* eslint-disable no-extend-native */
-BigInt.prototype.toJSON = function () {
-  return `${this.toString()} BigInt`;
-};
-
 module.exports = {
   COMMITMENTS_DB: 'nightfall_commitments',
   OPTIMIST_DB: 'optimist_data',
@@ -87,28 +82,6 @@ module.exports = {
   RETRIES: Number(process.env.AUTOSTART_RETRIES) || 50,
   USE_STUBS: process.env.USE_STUBS === 'true',
   VK_IDS: { deposit: 0, transfer: 1, withdraw: 2 }, // used as an enum to mirror the Shield contracts enum for vk types. The keys of this object must correspond to a 'folderpath' (the .zok file without the '.zok' bit)
-  BN128_GROUP_ORDER: 21888242871839275222246405745257275088548364400416034343698204186575808495617n,
-  BN128_PRIME_FIELD: 21888242871839275222246405745257275088696311157297823662689037894645226208583n,
-  // the various parameters needed to describe the Babyjubjub curve that we use for El-Gamal
-  // BABYJUBJUB
-  // Montgomery EC form is y^2 = x^3 + Ax^2 + Bx
-  // Montgomery EC form of BabyJubJub is y^2 = x^3 + 168698x^2 + x
-  // A = 168698 and B = 1
-  BABYJUBJUB: {
-    JUBJUBA: BigInt(168700),
-    JUBJUBD: BigInt(168696),
-    INFINITY: [BigInt(0), BigInt(1)],
-    GENERATOR: [
-      BigInt('16540640123574156134436876038791482806971768689494387082833631921987005038935'),
-      BigInt('20819045374670962167435360035096875258406992893633759881276124905556507972311'),
-    ],
-    JUBJUBE: BigInt(
-      '21888242871839275222246405745257275088614511777268538073601725287587578984328',
-    ),
-    JUBJUBC: BigInt(8),
-    MONTA: BigInt(168698),
-    MONTB: BigInt(1),
-  },
   MPC: {
     MPC_PARAMS_URL:
       'https://nightfallv3-proving-files.s3.eu-west-1.amazonaws.com/phase2/mpc_params',
diff --git a/nightfall-client/src/classes/commitment.mjs b/nightfall-client/src/classes/commitment.mjs
index 176571b0..a2391e7f 100644
--- a/nightfall-client/src/classes/commitment.mjs
+++ b/nightfall-client/src/classes/commitment.mjs
@@ -2,12 +2,12 @@
 A commitment class
 */
 import gen from 'general-number';
-import config from 'config';
 import poseidon from 'common-files/utils/crypto/poseidon/poseidon.mjs';
+import constants from 'common-files/constants/index.mjs';
 import { ZkpKeys } from '../services/keys.mjs';
 
 const { generalise } = gen;
-const { BN128_GROUP_ORDER } = config;
+const { BN128_GROUP_ORDER } = constants;
 
 class Commitment {
   preimage;
diff --git a/nightfall-client/src/services/deposit.mjs b/nightfall-client/src/services/deposit.mjs
index 4c2d195c..17276483 100644
--- a/nightfall-client/src/services/deposit.mjs
+++ b/nightfall-client/src/services/deposit.mjs
@@ -18,9 +18,8 @@ import { storeCommitment } from './commitment-storage.mjs';
 import { ZkpKeys } from './keys.mjs';
 import { computeCircuitInputs } from '../utils/computeCircuitInputs.mjs';
 
-const { ZOKRATES_WORKER_HOST, PROVING_SCHEME, BACKEND, PROTOCOL, USE_STUBS, BN128_GROUP_ORDER } =
-  config;
-const { SHIELD_CONTRACT_NAME } = constants;
+const { ZOKRATES_WORKER_HOST, PROVING_SCHEME, BACKEND, PROTOCOL, USE_STUBS } = config;
+const { SHIELD_CONTRACT_NAME, BN128_GROUP_ORDER } = constants;
 const { generalise } = gen;
 
 async function deposit(items) {
diff --git a/nightfall-client/src/services/kem-dem.mjs b/nightfall-client/src/services/kem-dem.mjs
index 93df539c..20d5b6e1 100644
--- a/nightfall-client/src/services/kem-dem.mjs
+++ b/nightfall-client/src/services/kem-dem.mjs
@@ -1,10 +1,10 @@
 import { scalarMult } from 'common-files/utils/curve-maths/curves.mjs';
-import config from 'config';
 import { randValueLT } from 'common-files/utils/crypto/crypto-random.mjs';
 import { generalise, stitchLimbs } from 'general-number';
 import poseidon from 'common-files/utils/crypto/poseidon/poseidon.mjs';
+import constants from 'common-files/constants/index.mjs';
 
-const { BABYJUBJUB, BN128_GROUP_ORDER } = config;
+const { BABYJUBJUB, BN128_GROUP_ORDER } = constants;
 // DOMAIN_KEM = field(SHA256('nightfall-kem'))
 const DOMAIN_KEM = 21033365405711675223813179268586447041622169155539365736392974498519442361181n;
 // DOMAIN_KEM = field(SHA256('nightfall-dem'))
diff --git a/nightfall-client/src/services/keys.mjs b/nightfall-client/src/services/keys.mjs
index 95122cc1..c175e262 100644
--- a/nightfall-client/src/services/keys.mjs
+++ b/nightfall-client/src/services/keys.mjs
@@ -1,5 +1,4 @@
 import { GN, generalise } from 'general-number';
-import config from 'config';
 import poseidon from 'common-files/utils/crypto/poseidon/poseidon.mjs';
 import bip39Pkg from 'bip39';
 import pkg from 'ethereumjs-wallet';
@@ -8,12 +7,13 @@ import {
   edwardsCompress,
   edwardsDecompress,
 } from 'common-files/utils/curve-maths/curves.mjs';
+import constants from 'common-files/constants/index.mjs';
 
 const { hdkey } = pkg;
 const { validateMnemonic, mnemonicToSeedSync } = bip39Pkg;
 export const zkpPrivateKeys = [];
 export const nullifierKeys = [];
-const { BABYJUBJUB, BN128_GROUP_ORDER } = config;
+const { BABYJUBJUB, BN128_GROUP_ORDER } = constants;
 
 // 128 bits is 12 words; 256 bits is 24 words
 // // Returns a promise
diff --git a/nightfall-client/src/utils/computeCircuitInputs.mjs b/nightfall-client/src/utils/computeCircuitInputs.mjs
index 738da236..6a456c87 100644
--- a/nightfall-client/src/utils/computeCircuitInputs.mjs
+++ b/nightfall-client/src/utils/computeCircuitInputs.mjs
@@ -1,8 +1,8 @@
-import config from 'config';
 import gen from 'general-number';
+import constants from 'common-files/constants/index.mjs';
 
 const { generalise } = gen;
-const { BN128_GROUP_ORDER } = config;
+const { BN128_GROUP_ORDER } = constants;
 
 const NULL_COMMITMENT = {
   value: 0,
diff --git a/nightfall-client/src/utils/getCommitmentInfo.mjs b/nightfall-client/src/utils/getCommitmentInfo.mjs
index e8448773..d0b836df 100644
--- a/nightfall-client/src/utils/getCommitmentInfo.mjs
+++ b/nightfall-client/src/utils/getCommitmentInfo.mjs
@@ -1,7 +1,7 @@
 import gen from 'general-number';
-import config from 'config';
 import logger from 'common-files/utils/logger.mjs';
 import { randValueLT } from 'common-files/utils/crypto/crypto-random.mjs';
+import constants from 'common-files/constants/index.mjs';
 import Nullifier from '../classes/nullifier.mjs';
 import {
   clearPending,
@@ -13,7 +13,7 @@ import { ZkpKeys } from '../services/keys.mjs';
 
 const { GN, generalise } = gen;
 
-const { BN128_GROUP_ORDER } = config;
+const { BN128_GROUP_ORDER } = constants;
 
 // eslint-disable-next-line import/prefer-default-export
 export const getCommitmentInfo = async txInfo => {
diff --git a/wallet/config/webpack.config.js b/wallet/config/webpack.config.js
index aa9e6e86..7ea047e4 100644
--- a/wallet/config/webpack.config.js
+++ b/wallet/config/webpack.config.js
@@ -156,12 +156,12 @@ module.exports = function (webpackEnv) {
 
   const config = require('../../config/default');
   const constants = require('../../common-files/constants/constants.json');
-
+  const bigIntContants = require('../../common-files/constants/bigInts.js');
   return {
     mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
     externals: {
       config: JSON.stringify(config),
-      nightfallConstants: JSON.stringify(constants),
+      nightfallConstants: JSON.stringify({ ...constants, ...bigIntContants }),
     },
     // Stop compilation early in production
     bail: isEnvProduction,
diff --git a/wallet/src/@types/globals.d.ts b/wallet/src/@types/globals.d.ts
index 6ea5e68b..dc3a41ac 100644
--- a/wallet/src/@types/globals.d.ts
+++ b/wallet/src/@types/globals.d.ts
@@ -1,17 +1,6 @@
 // globals.d.ts
 declare module globalThis {
     var config = {
-        BN128_GROUP_ORDER: bigint,
-        BABYJUBJUB: {
-            JUBJUBA: bigint,
-            JUBJUBD: bigint,
-            INFINITY: [bigint,bigint],
-            GENERATOR: [bigint, bigint],
-            JUBJUBE: bigint,
-            JUBJUBC: bigint,
-            MONTA: bigint,
-            MONTB: bigint,
-        },
         COMMITMENTS_DB: string,
         TIMBER_COLLECTION: string,
         SUBMITTED_BLOCKS_COLLECTION: string,
@@ -20,5 +9,18 @@ declare module globalThis {
         KEYS_COLLECTION: string,
         CIRCUIT_COLLECTION: string,
         CIRCUIT_HASH_COLLECTION: string,
+    };
+    var nightfallConstants = {
+      BN128_GROUP_ORDER: bigint,
+      BABYJUBJUB: {
+          JUBJUBA: bigint,
+          JUBJUBD: bigint,
+          INFINITY: [bigint,bigint],
+          GENERATOR: [bigint, bigint],
+          JUBJUBE: bigint,
+          JUBJUBC: bigint,
+          MONTA: bigint,
+          MONTB: bigint,
+      },
     }
 }
diff --git a/wallet/src/common-files/classes/fq2.js b/wallet/src/common-files/classes/fq2.js
index 0975b707..302f38f7 100644
--- a/wallet/src/common-files/classes/fq2.js
+++ b/wallet/src/common-files/classes/fq2.js
@@ -6,7 +6,7 @@ for dealing with f_q^2 field elements in the alt BN128 curve.
 */
 import { modDivide, complexDivMod } from '../utils/crypto/modular-division';
 
-const { BN128_PRIME_FIELD } = global.config;
+const { BN128_PRIME_FIELD } = global.nightfallConstants;
 
 class Fq2 {
   m = BN128_PRIME_FIELD;
diff --git a/wallet/src/common-files/utils/crypto/modular-division.js b/wallet/src/common-files/utils/crypto/modular-division.js
index eb67eada..0d6e65ee 100644
--- a/wallet/src/common-files/utils/crypto/modular-division.js
+++ b/wallet/src/common-files/utils/crypto/modular-division.js
@@ -3,7 +3,7 @@
 // modular division
 import { mulMod, addMod } from './number-theory';
 
-const { BN128_PRIME_FIELD } = global.config;
+const { BN128_PRIME_FIELD } = global.nightfallConstants;
 
 // function for extended Euclidean Algorithm
 // (used to find modular inverse.
diff --git a/wallet/src/common-files/utils/curve-maths/curves.js b/wallet/src/common-files/utils/curve-maths/curves.js
index cfa909a7..1bd4158c 100644
--- a/wallet/src/common-files/utils/curve-maths/curves.js
+++ b/wallet/src/common-files/utils/curve-maths/curves.js
@@ -11,7 +11,7 @@ import Fq2 from '../../classes/fq2';
 import Proof from '../../classes/proof';
 import { modDivide } from '../crypto/modular-division';
 
-const { BN128_PRIME_FIELD, BN128_GROUP_ORDER, BABYJUBJUB } = global.config;
+const { BN128_PRIME_FIELD, BN128_GROUP_ORDER, BABYJUBJUB } = global.nightfallConstants;
 
 const one = BigInt(1);
 const { JUBJUBE, JUBJUBC, JUBJUBD, JUBJUBA } = BABYJUBJUB;
diff --git a/wallet/src/nightfall-browser/classes/commitment.js b/wallet/src/nightfall-browser/classes/commitment.js
index 26b78b4d..494cfd0e 100644
--- a/wallet/src/nightfall-browser/classes/commitment.js
+++ b/wallet/src/nightfall-browser/classes/commitment.js
@@ -8,7 +8,7 @@ import poseidon from '../../common-files/utils/crypto/poseidon/poseidon';
 import { ZkpKeys } from '../services/keys';
 
 const { generalise } = gen;
-const { BN128_GROUP_ORDER } = global.config;
+const { BN128_GROUP_ORDER } = global.nightfallConstants;
 
 class Commitment {
   preimage;
diff --git a/wallet/src/nightfall-browser/services/deposit.js b/wallet/src/nightfall-browser/services/deposit.js
index 4c8044e8..5fc08b3d 100644
--- a/wallet/src/nightfall-browser/services/deposit.js
+++ b/wallet/src/nightfall-browser/services/deposit.js
@@ -22,8 +22,8 @@ import { storeCommitment } from './commitment-storage';
 import { ZkpKeys } from './keys';
 import { checkIndexDBForCircuit, getStoreCircuit, getLatestTree, getMaxBlock } from './database';
 
-const { BN128_GROUP_ORDER, USE_STUBS } = global.config;
-const { SHIELD_CONTRACT_NAME } = global.nightfallConstants;
+const { USE_STUBS } = global.config;
+const { SHIELD_CONTRACT_NAME, BN128_GROUP_ORDER } = global.nightfallConstants;
 const { generalise } = gen;
 const circuitName = USE_STUBS ? 'deposit_stub' : 'deposit';
 
diff --git a/wallet/src/nightfall-browser/services/kem-dem.ts b/wallet/src/nightfall-browser/services/kem-dem.ts
index 2430ed43..b4ac3b03 100644
--- a/wallet/src/nightfall-browser/services/kem-dem.ts
+++ b/wallet/src/nightfall-browser/services/kem-dem.ts
@@ -3,7 +3,7 @@ import { scalarMult } from '../../common-files/utils/curve-maths/curves';
 import { randValueLT } from '../../common-files/utils/crypto/crypto-random';
 import poseidon from '../../common-files/utils/crypto/poseidon/poseidon';
 
-const { BABYJUBJUB, BN128_GROUP_ORDER } = global.config;
+const { BABYJUBJUB, BN128_GROUP_ORDER } = global.nightfallConstants;
 const { generalise, stitchLimbs } = gen;
 // DOMAIN_KEM = field(SHA256('nightfall-kem'))
 const DOMAIN_KEM = 21033365405711675223813179268586447041622169155539365736392974498519442361181n;
diff --git a/wallet/src/nightfall-browser/services/keys.ts b/wallet/src/nightfall-browser/services/keys.ts
index 7ed54751..4864371b 100644
--- a/wallet/src/nightfall-browser/services/keys.ts
+++ b/wallet/src/nightfall-browser/services/keys.ts
@@ -16,7 +16,7 @@ export const zkpPrivateKeys: any = [];
 export const nullifierKeys: any = [];
 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 // @ts-ignore
-const { BABYJUBJUB, BN128_GROUP_ORDER } = global.config;
+const { BABYJUBJUB, BN128_GROUP_ORDER } = global.nightfallConstants;
 
 export class ZkpKeys {
   rootKey;
diff --git a/wallet/src/nightfall-browser/utils/compute-witness.ts b/wallet/src/nightfall-browser/utils/compute-witness.ts
index 01231cb4..848666fc 100644
--- a/wallet/src/nightfall-browser/utils/compute-witness.ts
+++ b/wallet/src/nightfall-browser/utils/compute-witness.ts
@@ -3,7 +3,7 @@ import gen, { GeneralNumber } from 'general-number';
 const { generalise } = gen;
 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 // @ts-ignore
-const { BN128_GROUP_ORDER } = global.config;
+const { BN128_GROUP_ORDER } = global.nightfallConstants;
 const NULL_COMMITMENT = {
   value: 0,
   salt: 0,
diff --git a/wallet/src/nightfall-browser/utils/getCommitmentInfo.ts b/wallet/src/nightfall-browser/utils/getCommitmentInfo.ts
index 7bc7af5c..857e32f6 100644
--- a/wallet/src/nightfall-browser/utils/getCommitmentInfo.ts
+++ b/wallet/src/nightfall-browser/utils/getCommitmentInfo.ts
@@ -10,7 +10,7 @@ import {
 import { ZkpKeys } from '../services/keys';
 
 const { generalise } = gen;
-const { BN128_GROUP_ORDER } = global.config;
+const { BN128_GROUP_ORDER } = global.nightfallConstants;
 
 type CommitmentsInfo = {
   oldCommitments: any[];
diff --git a/wallet/src/utils/parseConfigs.js b/wallet/src/utils/parseConfigs.js
index 6a5a4dad..9b177ecb 100644
--- a/wallet/src/utils/parseConfigs.js
+++ b/wallet/src/utils/parseConfigs.js
@@ -15,5 +15,7 @@ function parse(configs) {
   }
 }
 parse(config);
+parse(constants);
+
 global.config = config;
 global.nightfallConstants = constants;


Test patch:
diff --git a/test/kem-dem.test.mjs b/test/kem-dem.test.mjs
index 0d92d7d6..3b3261c2 100644
--- a/test/kem-dem.test.mjs
+++ b/test/kem-dem.test.mjs
@@ -1,13 +1,13 @@
-import config from 'config';
 import chai from 'chai';
 import fc from 'fast-check';
 import { generalise } from 'general-number';
 import { scalarMult } from 'common-files/utils/curve-maths/curves.mjs';
+import constants from 'common-files/constants/index.mjs';
 import { encrypt, decrypt, packSecrets } from '../nightfall-client/src/services/kem-dem.mjs';
 
 const { expect } = chai;
 
-const { BN128_GROUP_ORDER, BABYJUBJUB } = config;
+const { BN128_GROUP_ORDER, BABYJUBJUB } = constants;
 
 describe('KEM-DEM Tests', () => {
   describe('Check encryption and decryption', () => {