circomlibjs-hinkal-fork 0.0.5 → 0.0.6
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/build/main.cjs +90 -0
- package/package.json +3 -2
package/build/main.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var ffjavascript = require('ffjavascript');
|
|
4
|
+
var createBlakeHash = require('blake-hash');
|
|
4
5
|
|
|
5
6
|
async function buildBabyJub() {
|
|
6
7
|
const bn128 = await ffjavascript.getCurveFromName("bn128", true);
|
|
@@ -25376,6 +25377,95 @@ function buildPoseidonWasm(module) {
|
|
|
25376
25377
|
module.exportFunction("poseidon");
|
|
25377
25378
|
}
|
|
25378
25379
|
|
|
25380
|
+
async function buildEddsa() {
|
|
25381
|
+
const babyJub = await buildBabyJub();
|
|
25382
|
+
const poseidon = await buildPoseidon();
|
|
25383
|
+
return new Eddsa(babyJub, poseidon);
|
|
25384
|
+
}
|
|
25385
|
+
|
|
25386
|
+
class Eddsa {
|
|
25387
|
+
|
|
25388
|
+
constructor(babyJub, poseidon) {
|
|
25389
|
+
this.babyJub = babyJub;
|
|
25390
|
+
this.poseidon = poseidon;
|
|
25391
|
+
this.F = babyJub.F;
|
|
25392
|
+
}
|
|
25393
|
+
|
|
25394
|
+
pruneBuffer(buff) {
|
|
25395
|
+
buff[0] = buff[0] & 0xF8;
|
|
25396
|
+
buff[31] = buff[31] & 0x7F;
|
|
25397
|
+
buff[31] = buff[31] | 0x40;
|
|
25398
|
+
return buff;
|
|
25399
|
+
}
|
|
25400
|
+
|
|
25401
|
+
signPoseidon(prv, msg) {
|
|
25402
|
+
const F = this.babyJub.F;
|
|
25403
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
25404
|
+
const s = ffjavascript.Scalar.fromRprLE(sBuff, 0, 32);
|
|
25405
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, ffjavascript.Scalar.shr(s, 3));
|
|
25406
|
+
|
|
25407
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
25408
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
25409
|
+
F.toRprLE(composeBuff, 32, msg);
|
|
25410
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
25411
|
+
let r = ffjavascript.Scalar.mod(ffjavascript.Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
25412
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
25413
|
+
|
|
25414
|
+
const hm = this.poseidon([R8[0], R8[1], A[0], A[1], msg]);
|
|
25415
|
+
const hms = ffjavascript.Scalar.e(this.babyJub.F.toObject(hm));
|
|
25416
|
+
const S = ffjavascript.Scalar.mod(
|
|
25417
|
+
ffjavascript.Scalar.add(
|
|
25418
|
+
r,
|
|
25419
|
+
ffjavascript.Scalar.mul(hms, s)
|
|
25420
|
+
),
|
|
25421
|
+
this.babyJub.subOrder
|
|
25422
|
+
);
|
|
25423
|
+
return {
|
|
25424
|
+
R8: R8,
|
|
25425
|
+
S: S
|
|
25426
|
+
};
|
|
25427
|
+
}
|
|
25428
|
+
|
|
25429
|
+
verifyPoseidon(msg, sig, A) {
|
|
25430
|
+
// Check parameters
|
|
25431
|
+
if (typeof sig != "object") return false;
|
|
25432
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
25433
|
+
if (sig.R8.length!= 2) return false;
|
|
25434
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
25435
|
+
if (!Array.isArray(A)) return false;
|
|
25436
|
+
if (A.length!= 2) return false;
|
|
25437
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
25438
|
+
if (sig.S>= this.babyJub.subOrder) return false;
|
|
25439
|
+
|
|
25440
|
+
const hm = this.poseidon([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
|
|
25441
|
+
const hms = ffjavascript.Scalar.e(this.babyJub.F.toObject(hm));
|
|
25442
|
+
|
|
25443
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
25444
|
+
let Pright = this.babyJub.mulPointEscalar(A, ffjavascript.Scalar.mul(hms, 8));
|
|
25445
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
25446
|
+
|
|
25447
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
25448
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
25449
|
+
return true;
|
|
25450
|
+
}
|
|
25451
|
+
|
|
25452
|
+
packSignature(sig) {
|
|
25453
|
+
const buff = new Uint8Array(64);
|
|
25454
|
+
const R8p = this.babyJub.packPoint(sig.R8);
|
|
25455
|
+
buff.set(R8p, 0);
|
|
25456
|
+
ffjavascript.Scalar.toRprLE(buff, 32, sig.S, 32);
|
|
25457
|
+
return buff;
|
|
25458
|
+
}
|
|
25459
|
+
|
|
25460
|
+
unpackSignature(sigBuff) {
|
|
25461
|
+
return {
|
|
25462
|
+
R8: this.babyJub.unpackPoint(sigBuff.slice(0,32)),
|
|
25463
|
+
S: ffjavascript.Scalar.fromRprLE(sigBuff, 32, 32)
|
|
25464
|
+
};
|
|
25465
|
+
}
|
|
25466
|
+
}
|
|
25467
|
+
|
|
25379
25468
|
exports.buildBabyjub = buildBabyJub;
|
|
25469
|
+
exports.buildEddsa = buildEddsa;
|
|
25380
25470
|
exports.buildPoseidon = buildPoseidon;
|
|
25381
25471
|
exports.buildPoseidonWasm = buildPoseidonWasm;
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
"import": "./main.js",
|
|
8
8
|
"require": "./build/main.cjs"
|
|
9
9
|
},
|
|
10
|
-
"version": "0.0.
|
|
10
|
+
"version": "0.0.6",
|
|
11
11
|
"description": "Javascript library to work with circomlib",
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "mocha",
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"chai": "^4.3.4",
|
|
29
29
|
"ganache": "^7.3.0",
|
|
30
|
-
"mocha": "^9.1.3"
|
|
30
|
+
"mocha": "^9.1.3",
|
|
31
|
+
"rollup": "^4.60.4"
|
|
31
32
|
},
|
|
32
33
|
"dependencies": {
|
|
33
34
|
"ffjavascript": "^0.3.0"
|