@dorafactory/maci-sdk 0.1.3-pre.23 → 0.1.3-pre.25
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 +140 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +136 -27
- package/dist/index.mjs.map +1 -1
- package/dist/libs/crypto/curve.d.ts +39 -3
- package/package.json +9 -10
- package/src/libs/crypto/adapter.ts +64 -30
- package/src/libs/crypto/curve.ts +146 -25
- package/src/types/lib.d.ts +0 -7
package/dist/index.js
CHANGED
|
@@ -1212,59 +1212,168 @@ async function genKeypairFromSign({
|
|
|
1212
1212
|
}
|
|
1213
1213
|
|
|
1214
1214
|
// src/libs/crypto/curve.ts
|
|
1215
|
-
var
|
|
1216
|
-
var
|
|
1217
|
-
var
|
|
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(
|
|
1218
1220
|
"21888242871839275222246405745257275088548364400416034343698204186575808495617"
|
|
1219
1221
|
);
|
|
1220
|
-
var bls12381q =
|
|
1221
|
-
"
|
|
1222
|
-
16
|
|
1222
|
+
var bls12381q = BigInt(
|
|
1223
|
+
"0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab"
|
|
1223
1224
|
);
|
|
1224
|
-
var bn128q =
|
|
1225
|
+
var bn128q = BigInt(
|
|
1225
1226
|
"21888242871839275222246405745257275088696311157297823662689037894645226208583"
|
|
1226
1227
|
);
|
|
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
|
+
}
|
|
1227
1316
|
async function getCurveFromR(r2) {
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
curve = await (0, import_ffjavascript.buildBls12381)();
|
|
1317
|
+
if (r2 === bn128r) {
|
|
1318
|
+
return createBn254Curve();
|
|
1319
|
+
} else if (r2 === bls12381r) {
|
|
1320
|
+
return createBls12381Curve();
|
|
1233
1321
|
} else {
|
|
1234
|
-
throw new Error(`Curve not supported: ${
|
|
1322
|
+
throw new Error(`Curve not supported: ${r2.toString()}`);
|
|
1235
1323
|
}
|
|
1236
|
-
return curve;
|
|
1237
1324
|
}
|
|
1238
1325
|
async function getCurveFromQ(q) {
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
curve = await (0, import_ffjavascript.buildBls12381)();
|
|
1326
|
+
if (q === bn128q) {
|
|
1327
|
+
return createBn254Curve();
|
|
1328
|
+
} else if (q === bls12381q) {
|
|
1329
|
+
return createBls12381Curve();
|
|
1244
1330
|
} else {
|
|
1245
|
-
throw new Error(`Curve not supported: ${
|
|
1331
|
+
throw new Error(`Curve not supported: ${q.toString()}`);
|
|
1246
1332
|
}
|
|
1247
|
-
return curve;
|
|
1248
1333
|
}
|
|
1249
1334
|
async function getCurveFromName(name) {
|
|
1250
|
-
let curve;
|
|
1251
1335
|
const normName = normalizeName(name);
|
|
1252
1336
|
if (["BN128", "BN254", "ALTBN128"].indexOf(normName) >= 0) {
|
|
1253
|
-
|
|
1337
|
+
return createBn254Curve();
|
|
1254
1338
|
} else if (["BLS12381"].indexOf(normName) >= 0) {
|
|
1255
|
-
|
|
1339
|
+
return createBls12381Curve();
|
|
1256
1340
|
} else {
|
|
1257
1341
|
throw new Error(`Curve not supported: ${name}`);
|
|
1258
1342
|
}
|
|
1259
|
-
return curve;
|
|
1260
1343
|
function normalizeName(n) {
|
|
1261
1344
|
return (n.toUpperCase().match(/[A-Za-z0-9]+/g) || []).join("");
|
|
1262
1345
|
}
|
|
1263
1346
|
}
|
|
1264
1347
|
|
|
1265
1348
|
// src/libs/crypto/adapter.ts
|
|
1266
|
-
var
|
|
1267
|
-
|
|
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
|
+
};
|
|
1268
1377
|
var Bytes2Str = (arr) => {
|
|
1269
1378
|
let str = "";
|
|
1270
1379
|
for (let i = 0; i < arr.length; i++) {
|
|
@@ -9651,7 +9760,7 @@ var import_snarkjs = require("snarkjs");
|
|
|
9651
9760
|
|
|
9652
9761
|
// src/libs/keypairs/eddsa-poseidon/keypair.ts
|
|
9653
9762
|
var import_blake2b = require("@noble/hashes/blake2b");
|
|
9654
|
-
var
|
|
9763
|
+
var import_utils10 = require("@noble/hashes/utils");
|
|
9655
9764
|
var import_bip32 = require("@scure/bip32");
|
|
9656
9765
|
var import_sha2563 = require("@noble/hashes/sha256");
|
|
9657
9766
|
var import_eddsa_poseidon4 = require("@zk-kit/eddsa-poseidon");
|
|
@@ -9857,7 +9966,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
|
|
|
9857
9966
|
static fromSecretKey(secretKey, options) {
|
|
9858
9967
|
if (typeof secretKey === "string") {
|
|
9859
9968
|
const cleanSecretKey = secretKey.startsWith("0x") ? secretKey.slice(2) : secretKey;
|
|
9860
|
-
const decoded = buffer2Bigint((0,
|
|
9969
|
+
const decoded = buffer2Bigint((0, import_utils10.hexToBytes)(cleanSecretKey));
|
|
9861
9970
|
return this.fromSecretKey(decoded, options);
|
|
9862
9971
|
}
|
|
9863
9972
|
const unPackedPublicKey = genPubKey(secretKey);
|
|
@@ -9865,7 +9974,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
|
|
|
9865
9974
|
if (!options || !options.skipValidation) {
|
|
9866
9975
|
const encoder = new TextEncoder();
|
|
9867
9976
|
const signData = encoder.encode("dora validation");
|
|
9868
|
-
const msgHash = (0,
|
|
9977
|
+
const msgHash = (0, import_utils10.bytesToHex)((0, import_blake2b.blake2b)(signData, { dkLen: 16 }));
|
|
9869
9978
|
const signature = (0, import_eddsa_poseidon4.signMessage)(bigInt2Buffer(secretKey), msgHash);
|
|
9870
9979
|
if (!(0, import_eddsa_poseidon4.verifySignature)(msgHash, signature, unPackedPublicKey)) {
|
|
9871
9980
|
throw new Error("Provided secretKey is invalid");
|
|
@@ -9895,7 +10004,7 @@ var EdDSAPoseidonKeypair = class _EdDSAPoseidonKeypair extends Keypair3 {
|
|
|
9895
10004
|
* @returns The secret key encoded as a hexadecimal string
|
|
9896
10005
|
*/
|
|
9897
10006
|
getSecretKey() {
|
|
9898
|
-
return (0,
|
|
10007
|
+
return (0, import_utils10.bytesToHex)(bigInt2Buffer(this.keypair.secretKey));
|
|
9899
10008
|
}
|
|
9900
10009
|
getFormatedPrivKey() {
|
|
9901
10010
|
return this.keypair.formatedPrivKey;
|