@dorafactory/maci-sdk 0.1.3-pre.25 → 0.1.3-pre.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1212,168 +1212,59 @@ async function genKeypairFromSign({
1212
1212
  }
1213
1213
 
1214
1214
  // src/libs/crypto/curve.ts
1215
- var import_bn254 = require("@noble/curves/bn254");
1216
- var import_bls12_381 = require("@noble/curves/bls12-381");
1217
- var import_utils = require("@noble/curves/abstract/utils");
1218
- var bls12381r = BigInt("0x73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001");
1219
- var bn128r = BigInt(
1215
+ var import_ffjavascript = require("ffjavascript");
1216
+ var bls12381r = import_ffjavascript.Scalar.e("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001", 16);
1217
+ var bn128r = import_ffjavascript.Scalar.e(
1220
1218
  "21888242871839275222246405745257275088548364400416034343698204186575808495617"
1221
1219
  );
1222
- var bls12381q = BigInt(
1223
- "0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
1220
+ var bls12381q = import_ffjavascript.Scalar.e(
1221
+ "1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab",
1222
+ 16
1224
1223
  );
1225
- var bn128q = BigInt(
1224
+ var bn128q = import_ffjavascript.Scalar.e(
1226
1225
  "21888242871839275222246405745257275088696311157297823662689037894645226208583"
1227
1226
  );
1228
- function createBn254Curve() {
1229
- return {
1230
- name: "bn254",
1231
- r: bn128r,
1232
- q: bn128q,
1233
- G1: {
1234
- fromObject: (obj) => obj,
1235
- toUncompressed: (point) => {
1236
- const x = BigInt(point[0]);
1237
- const y = BigInt(point[1]);
1238
- const p = new import_bn254.bn254.G1.ProjectivePoint(x, y, 1n);
1239
- p.assertValidity();
1240
- const affine = p.toAffine();
1241
- const xBytes = (0, import_utils.numberToBytesBE)(affine.x, 32);
1242
- const yBytes = (0, import_utils.numberToBytesBE)(affine.y, 32);
1243
- const bytes = new Uint8Array(64);
1244
- bytes.set(xBytes, 0);
1245
- bytes.set(yBytes, 32);
1246
- return bytes;
1247
- }
1248
- },
1249
- G2: {
1250
- fromObject: (obj) => obj,
1251
- toUncompressed: (point) => {
1252
- const Fp2 = import_bn254.bn254.fields.Fp2;
1253
- const x = Fp2.create({ c0: BigInt(point[0][0]), c1: BigInt(point[0][1]) });
1254
- const y = Fp2.create({ c0: BigInt(point[1][0]), c1: BigInt(point[1][1]) });
1255
- const p = new import_bn254.bn254.G2.ProjectivePoint(x, y, Fp2.ONE);
1256
- p.assertValidity();
1257
- const affine = p.toAffine();
1258
- const x0Bytes = (0, import_utils.numberToBytesBE)(affine.x.c0, 32);
1259
- const x1Bytes = (0, import_utils.numberToBytesBE)(affine.x.c1, 32);
1260
- const y0Bytes = (0, import_utils.numberToBytesBE)(affine.y.c0, 32);
1261
- const y1Bytes = (0, import_utils.numberToBytesBE)(affine.y.c1, 32);
1262
- const bytes = new Uint8Array(128);
1263
- bytes.set(x1Bytes, 0);
1264
- bytes.set(x0Bytes, 32);
1265
- bytes.set(y1Bytes, 64);
1266
- bytes.set(y0Bytes, 96);
1267
- return bytes;
1268
- }
1269
- }
1270
- };
1271
- }
1272
- function createBls12381Curve() {
1273
- return {
1274
- name: "bls12_381",
1275
- r: bls12381r,
1276
- q: bls12381q,
1277
- G1: {
1278
- fromObject: (obj) => obj,
1279
- toUncompressed: (point) => {
1280
- const x = BigInt(point[0]);
1281
- const y = BigInt(point[1]);
1282
- const p = new import_bls12_381.bls12_381.G1.ProjectivePoint(x, y, 1n);
1283
- p.assertValidity();
1284
- const affine = p.toAffine();
1285
- const xBytes = (0, import_utils.numberToBytesBE)(affine.x, 48);
1286
- const yBytes = (0, import_utils.numberToBytesBE)(affine.y, 48);
1287
- const bytes = new Uint8Array(96);
1288
- bytes.set(xBytes, 0);
1289
- bytes.set(yBytes, 48);
1290
- return bytes;
1291
- }
1292
- },
1293
- G2: {
1294
- fromObject: (obj) => obj,
1295
- toUncompressed: (point) => {
1296
- const Fp2 = import_bls12_381.bls12_381.fields.Fp2;
1297
- const x = Fp2.create({ c0: BigInt(point[0][0]), c1: BigInt(point[0][1]) });
1298
- const y = Fp2.create({ c0: BigInt(point[1][0]), c1: BigInt(point[1][1]) });
1299
- const p = new import_bls12_381.bls12_381.G2.ProjectivePoint(x, y, Fp2.ONE);
1300
- p.assertValidity();
1301
- const affine = p.toAffine();
1302
- const x0Bytes = (0, import_utils.numberToBytesBE)(affine.x.c0, 48);
1303
- const x1Bytes = (0, import_utils.numberToBytesBE)(affine.x.c1, 48);
1304
- const y0Bytes = (0, import_utils.numberToBytesBE)(affine.y.c0, 48);
1305
- const y1Bytes = (0, import_utils.numberToBytesBE)(affine.y.c1, 48);
1306
- const bytes = new Uint8Array(192);
1307
- bytes.set(x1Bytes, 0);
1308
- bytes.set(x0Bytes, 48);
1309
- bytes.set(y1Bytes, 96);
1310
- bytes.set(y0Bytes, 144);
1311
- return bytes;
1312
- }
1313
- }
1314
- };
1315
- }
1316
1227
  async function getCurveFromR(r2) {
1317
- if (r2 === bn128r) {
1318
- return createBn254Curve();
1319
- } else if (r2 === bls12381r) {
1320
- return createBls12381Curve();
1228
+ let curve;
1229
+ if (import_ffjavascript.Scalar.eq(r2, bn128r)) {
1230
+ curve = await (0, import_ffjavascript.buildBn128)();
1231
+ } else if (import_ffjavascript.Scalar.eq(r2, bls12381r)) {
1232
+ curve = await (0, import_ffjavascript.buildBls12381)();
1321
1233
  } else {
1322
- throw new Error(`Curve not supported: ${r2.toString()}`);
1234
+ throw new Error(`Curve not supported: ${import_ffjavascript.Scalar.toString(r2)}`);
1323
1235
  }
1236
+ return curve;
1324
1237
  }
1325
1238
  async function getCurveFromQ(q) {
1326
- if (q === bn128q) {
1327
- return createBn254Curve();
1328
- } else if (q === bls12381q) {
1329
- return createBls12381Curve();
1239
+ let curve;
1240
+ if (import_ffjavascript.Scalar.eq(q, bn128q)) {
1241
+ curve = await (0, import_ffjavascript.buildBn128)();
1242
+ } else if (import_ffjavascript.Scalar.eq(q, bls12381q)) {
1243
+ curve = await (0, import_ffjavascript.buildBls12381)();
1330
1244
  } else {
1331
- throw new Error(`Curve not supported: ${q.toString()}`);
1245
+ throw new Error(`Curve not supported: ${import_ffjavascript.Scalar.toString(q)}`);
1332
1246
  }
1247
+ return curve;
1333
1248
  }
1334
1249
  async function getCurveFromName(name) {
1250
+ let curve;
1335
1251
  const normName = normalizeName(name);
1336
1252
  if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) {
1337
- return createBn254Curve();
1253
+ curve = await (0, import_ffjavascript.buildBn128)();
1338
1254
  } else if (["BLS12381"].indexOf(normName) >= 0) {
1339
- return createBls12381Curve();
1255
+ curve = await (0, import_ffjavascript.buildBls12381)();
1340
1256
  } else {
1341
1257
  throw new Error(`Curve not supported: ${name}`);
1342
1258
  }
1259
+ return curve;
1343
1260
  function normalizeName(n) {
1344
1261
  return (n.toUpperCase().match(/[A-Za-z0-9]+/g) || []).join("");
1345
1262
  }
1346
1263
  }
1347
1264
 
1348
1265
  // src/libs/crypto/adapter.ts
1349
- var unstringifyBigInts = (obj) => {
1350
- if (typeof obj === "string") {
1351
- if (obj.startsWith("0x")) {
1352
- try {
1353
- return BigInt(obj);
1354
- } catch {
1355
- return obj;
1356
- }
1357
- }
1358
- if (/^-?\d+$/.test(obj)) {
1359
- return BigInt(obj);
1360
- }
1361
- return obj;
1362
- }
1363
- if (Array.isArray(obj)) {
1364
- return obj.map(unstringifyBigInts);
1365
- }
1366
- if (typeof obj === "object" && obj !== null) {
1367
- const result = {};
1368
- for (const key in obj) {
1369
- if (obj.hasOwnProperty(key)) {
1370
- result[key] = unstringifyBigInts(obj[key]);
1371
- }
1372
- }
1373
- return result;
1374
- }
1375
- return obj;
1376
- };
1266
+ var import_ffjavascript2 = require("ffjavascript");
1267
+ var { unstringifyBigInts } = import_ffjavascript2.utils;
1377
1268
  var Bytes2Str = (arr) => {
1378
1269
  let str = "";
1379
1270
  for (let i = 0; i < arr.length; i++) {
@@ -9760,7 +9651,7 @@ var import_snarkjs = require("snarkjs");
9760
9651
 
9761
9652
  // src/libs/keypairs/eddsa-poseidon/keypair.ts
9762
9653
  var import_blake2b = require("@noble/hashes/blake2b");
9763
- var import_utils10 = require("@noble/hashes/utils");
9654
+ var import_utils9 = require("@noble/hashes/utils");
9764
9655
  var import_bip32 = require("@scure/bip32");
9765
9656
  var import_sha2563 = require("@noble/hashes/sha256");
9766
9657
  var import_eddsa_poseidon4 = require("@zk-kit/eddsa-poseidon");
@@ -9966,7 +9857,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
9966
9857
  static fromSecretKey(secretKey, options) {
9967
9858
  if (typeof secretKey === "string") {
9968
9859
  const cleanSecretKey = secretKey.startsWith("0x") ? secretKey.slice(2) : secretKey;
9969
- const decoded = buffer2Bigint((0, import_utils10.hexToBytes)(cleanSecretKey));
9860
+ const decoded = buffer2Bigint((0, import_utils9.hexToBytes)(cleanSecretKey));
9970
9861
  return this.fromSecretKey(decoded, options);
9971
9862
  }
9972
9863
  const unPackedPublicKey = genPubKey(secretKey);
@@ -9974,7 +9865,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
9974
9865
  if (!options || !options.skipValidation) {
9975
9866
  const encoder = new TextEncoder();
9976
9867
  const signData = encoder.encode("dora validation");
9977
- const msgHash = (0, import_utils10.bytesToHex)((0, import_blake2b.blake2b)(signData, { dkLen: 16 }));
9868
+ const msgHash = (0, import_utils9.bytesToHex)((0, import_blake2b.blake2b)(signData, { dkLen: 16 }));
9978
9869
  const signature = (0, import_eddsa_poseidon4.signMessage)(bigInt2Buffer(secretKey), msgHash);
9979
9870
  if (!(0, import_eddsa_poseidon4.verifySignature)(msgHash, signature, unPackedPublicKey)) {
9980
9871
  throw new Error("Provided secretKey is invalid");
@@ -10004,7 +9895,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
10004
9895
  * @returns The secret key encoded as a hexadecimal string
10005
9896
  */
10006
9897
  getSecretKey() {
10007
- return (0, import_utils10.bytesToHex)(bigInt2Buffer(this.keypair.secretKey));
9898
+ return (0, import_utils9.bytesToHex)(bigInt2Buffer(this.keypair.secretKey));
10008
9899
  }
10009
9900
  getFormatedPrivKey() {
10010
9901
  return this.keypair.formatedPrivKey;