circomlibjs-hinkal-fork 0.0.1
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/.github/workflows/npm-publish.yml +21 -0
- package/.vscode/launch.json +40 -0
- package/README.md +18 -0
- package/badmsges.md +0 -0
- package/build/main.cjs +27397 -0
- package/hardhat.config.js +6 -0
- package/main.js +25 -0
- package/package.json +40 -0
- package/rollup.cjs.config.js +16 -0
- package/src/babyjub.js +139 -0
- package/src/eddsa.js +286 -0
- package/src/evmasm.js +209 -0
- package/src/mimc7.js +78 -0
- package/src/mimc7_gencontract.js +113 -0
- package/src/mimc7_print_iv.js +18 -0
- package/src/mimc7_printconstants.js +26 -0
- package/src/mimc7_printcontract.js +14 -0
- package/src/mimcsponge.js +100 -0
- package/src/mimcsponge_gencontract.js +125 -0
- package/src/mimcsponge_printconstants.js +25 -0
- package/src/mimcsponge_printcontract.js +14 -0
- package/src/pedersen_hash.js +132 -0
- package/src/pedersen_printbases.js +36 -0
- package/src/poseidon_constants.js +207 -0
- package/src/poseidon_constants.json +206 -0
- package/src/poseidon_constants_opt.js +24806 -0
- package/src/poseidon_constants_opt.json +24806 -0
- package/src/poseidon_gencontract.js +209 -0
- package/src/poseidon_opt.js +119 -0
- package/src/poseidon_printcontract.js +14 -0
- package/src/poseidon_printmatrix.js +22 -0
- package/src/poseidon_reference.js +83 -0
- package/src/poseidon_wasm.js +440 -0
- package/src/smt.js +309 -0
- package/src/smt_hashes_mimc.js +16 -0
- package/src/smt_hashes_poseidon.js +17 -0
- package/src/smt_memdb.js +57 -0
- package/src/testblake.js +18 -0
- package/tools/poseidon_optimize_constants.js +301 -0
package/main.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export {default as buildBabyjub} from "./src/babyjub.js";
|
|
2
|
+
export {default as buildEddsa} from "./src/eddsa.js";
|
|
3
|
+
export {default as evmasm} from "./src/evmasm.js";
|
|
4
|
+
|
|
5
|
+
export {default as buildMimc7} from "./src/mimc7.js";
|
|
6
|
+
import * as _mimc7Contract from "./src/mimc7_gencontract.js";
|
|
7
|
+
export const mimc7Contract=_mimc7Contract;
|
|
8
|
+
|
|
9
|
+
export {default as buildMimcSponge} from "./src/mimcsponge.js";
|
|
10
|
+
import * as _mimcSpongeContract from "./src/mimcsponge_gencontract.js";
|
|
11
|
+
export const mimcSpongecontract=_mimcSpongeContract;
|
|
12
|
+
|
|
13
|
+
export {default as buildPedersenHash} from "./src/pedersen_hash.js";
|
|
14
|
+
|
|
15
|
+
export { buildPoseidon, buildPoseidonWasm } from "./src/poseidon_wasm.js";
|
|
16
|
+
import * as _poseidonContract from "./src/poseidon_gencontract.js";
|
|
17
|
+
export const poseidonContract=_poseidonContract;
|
|
18
|
+
|
|
19
|
+
export {default as buildPoseidonReference} from "./src/poseidon_reference.js";
|
|
20
|
+
export {default as buildPoseidonOpt} from "./src/poseidon_opt.js";
|
|
21
|
+
|
|
22
|
+
export {SMT, buildSMT, newMemEmptyTrie} from "./src/smt.js";
|
|
23
|
+
|
|
24
|
+
export { default as SMTMemDb } from "./src/smt_memdb.js";
|
|
25
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "circomlibjs-hinkal-fork",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"main": "./build/main.cjs",
|
|
5
|
+
"module": "./main.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"import": "./main.js",
|
|
8
|
+
"require": "./build/main.cjs"
|
|
9
|
+
},
|
|
10
|
+
"version": "0.0.1",
|
|
11
|
+
"description": "Javascript library to work with circomlib",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "mocha",
|
|
14
|
+
"poseidonOptimizeConstants": "node tools/poseidon_optimize_constants.js",
|
|
15
|
+
"build": "rollup -c rollup.cjs.config.js"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"circom",
|
|
19
|
+
"circomlib",
|
|
20
|
+
"iden3",
|
|
21
|
+
"snarkjs",
|
|
22
|
+
"snark",
|
|
23
|
+
"zero",
|
|
24
|
+
"knowledge"
|
|
25
|
+
],
|
|
26
|
+
"license": "GPL-3.0",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"chai": "^4.3.4",
|
|
29
|
+
"ganache": "^7.3.0",
|
|
30
|
+
"mocha": "^9.1.3"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"blake-hash": "^2.0.0",
|
|
34
|
+
"blake2b": "^2.1.3",
|
|
35
|
+
"ethers": "^5.5.1",
|
|
36
|
+
"ffjavascript": "^0.3.0",
|
|
37
|
+
"buffer": "6.0.3",
|
|
38
|
+
"process": "0.11.10"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import { builtinModules as builtin } from "module";
|
|
3
|
+
|
|
4
|
+
const pkg = JSON.parse(fs.readFileSync("./package.json"));
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
input: "./main.js",
|
|
8
|
+
output: {
|
|
9
|
+
file: "build/main.cjs",
|
|
10
|
+
format: "cjs",
|
|
11
|
+
},
|
|
12
|
+
external: [
|
|
13
|
+
...Object.keys(pkg.dependencies),
|
|
14
|
+
...builtin,
|
|
15
|
+
]
|
|
16
|
+
};
|
package/src/babyjub.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { getCurveFromName, Scalar } from "ffjavascript";
|
|
2
|
+
|
|
3
|
+
export default async function buildBabyJub() {
|
|
4
|
+
const bn128 = await getCurveFromName("bn128", true);
|
|
5
|
+
return new BabyJub(bn128.Fr);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
class BabyJub {
|
|
9
|
+
constructor(F) {
|
|
10
|
+
this.F = F;
|
|
11
|
+
this.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
|
12
|
+
this.pm1d2 = Scalar.div(Scalar.sub(this.p, Scalar.e(1)), Scalar.e(2));
|
|
13
|
+
|
|
14
|
+
this.Generator = [
|
|
15
|
+
F.e("995203441582195749578291179787384436505546430278305826713579947235728471134"),
|
|
16
|
+
F.e("5472060717959818805561601436314318772137091100104008585924551046643952123905")
|
|
17
|
+
];
|
|
18
|
+
this.Base8 = [
|
|
19
|
+
F.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
|
20
|
+
F.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
|
21
|
+
];
|
|
22
|
+
this.order = Scalar.fromString("21888242871839275222246405745257275088614511777268538073601725287587578984328");
|
|
23
|
+
this.subOrder = Scalar.shiftRight(this.order, 3);
|
|
24
|
+
this.A = F.e("168700");
|
|
25
|
+
this.D = F.e("168696");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
addPoint(a,b) {
|
|
30
|
+
const F = this.F;
|
|
31
|
+
|
|
32
|
+
const res = [];
|
|
33
|
+
|
|
34
|
+
/* does the equivalent of:
|
|
35
|
+
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt("1") + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
|
36
|
+
res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(bigInt("1") - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
const beta = F.mul(a[0],b[1]);
|
|
40
|
+
const gamma = F.mul(a[1],b[0]);
|
|
41
|
+
const delta = F.mul(
|
|
42
|
+
F.sub(a[1], F.mul(this.A, a[0])),
|
|
43
|
+
F.add(b[0], b[1])
|
|
44
|
+
);
|
|
45
|
+
const tau = F.mul(beta, gamma);
|
|
46
|
+
const dtau = F.mul(this.D, tau);
|
|
47
|
+
|
|
48
|
+
res[0] = F.div(
|
|
49
|
+
F.add(beta, gamma),
|
|
50
|
+
F.add(F.one, dtau)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
res[1] = F.div(
|
|
54
|
+
F.add(delta, F.sub(F.mul(this.A,beta), gamma)),
|
|
55
|
+
F.sub(F.one, dtau)
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
return res;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
mulPointEscalar(base, e) {
|
|
62
|
+
const F = this.F;
|
|
63
|
+
let res = [F.e("0"),F.e("1")];
|
|
64
|
+
let rem = e;
|
|
65
|
+
let exp = base;
|
|
66
|
+
|
|
67
|
+
while (! Scalar.isZero(rem)) {
|
|
68
|
+
if (Scalar.isOdd(rem)) {
|
|
69
|
+
res = this.addPoint(res, exp);
|
|
70
|
+
}
|
|
71
|
+
exp = this.addPoint(exp, exp);
|
|
72
|
+
rem = Scalar.shiftRight(rem, 1);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return res;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
inSubgroup(P) {
|
|
79
|
+
const F = this.F;
|
|
80
|
+
if (!this.inCurve(P)) return false;
|
|
81
|
+
const res= this.mulPointEscalar(P, this.subOrder);
|
|
82
|
+
return (F.isZero(res[0]) && F.eq(res[1], F.one));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
inCurve(P) {
|
|
86
|
+
const F = this.F;
|
|
87
|
+
const x2 = F.square(P[0]);
|
|
88
|
+
const y2 = F.square(P[1]);
|
|
89
|
+
|
|
90
|
+
if (!F.eq(
|
|
91
|
+
F.add(F.mul(this.A, x2), y2),
|
|
92
|
+
F.add(F.one, F.mul(F.mul(x2, y2), this.D)))) return false;
|
|
93
|
+
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
packPoint(P) {
|
|
98
|
+
const F = this.F;
|
|
99
|
+
const buff = new Uint8Array(32);
|
|
100
|
+
F.toRprLE(buff, 0, P[1]);
|
|
101
|
+
const n = F.toObject(P[0]);
|
|
102
|
+
if (Scalar.gt(n, this.pm1d2)) {
|
|
103
|
+
buff[31] = buff[31] | 0x80;
|
|
104
|
+
}
|
|
105
|
+
return buff;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
unpackPoint(buff) {
|
|
109
|
+
const F = this.F;
|
|
110
|
+
let sign = false;
|
|
111
|
+
const P = new Array(2);
|
|
112
|
+
if (buff[31] & 0x80) {
|
|
113
|
+
sign = true;
|
|
114
|
+
buff[31] = buff[31] & 0x7F;
|
|
115
|
+
}
|
|
116
|
+
P[1] = F.fromRprLE(buff, 0);
|
|
117
|
+
if (Scalar.gt(F.toObject(P[1]), this.p)) return null;
|
|
118
|
+
|
|
119
|
+
const y2 = F.square(P[1]);
|
|
120
|
+
|
|
121
|
+
const x2 = F.div(
|
|
122
|
+
F.sub(F.one, y2),
|
|
123
|
+
F.sub(this.A, F.mul(this.D, y2))
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const x2h = F.exp(x2, F.half);
|
|
127
|
+
if (! F.eq(F.one, x2h)) return null;
|
|
128
|
+
|
|
129
|
+
let x = F.sqrt(x2);
|
|
130
|
+
|
|
131
|
+
if (x == null) return null;
|
|
132
|
+
|
|
133
|
+
if (sign) x = F.neg(x);
|
|
134
|
+
|
|
135
|
+
P[0] = x;
|
|
136
|
+
|
|
137
|
+
return P;
|
|
138
|
+
}
|
|
139
|
+
}
|
package/src/eddsa.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { Scalar } from "ffjavascript";
|
|
2
|
+
import buildBabyJub from "./babyjub.js";
|
|
3
|
+
import buildPedersenHash from "./pedersen_hash.js";
|
|
4
|
+
import buildMimc7 from "./mimc7.js";
|
|
5
|
+
import { buildPoseidon } from "./poseidon_wasm.js";
|
|
6
|
+
import buildMimcSponge from "./mimcsponge.js";
|
|
7
|
+
import createBlakeHash from "blake-hash";
|
|
8
|
+
import {Buffer} from 'buffer';
|
|
9
|
+
|
|
10
|
+
export default async function buildEddsa() {
|
|
11
|
+
const babyJub = await buildBabyJub("bn128");
|
|
12
|
+
const pedersenHash = await buildPedersenHash();
|
|
13
|
+
const mimc7 = await buildMimc7();
|
|
14
|
+
const poseidon = await buildPoseidon();
|
|
15
|
+
const mimcSponge = await buildMimcSponge();
|
|
16
|
+
return new Eddsa(babyJub, pedersenHash, mimc7, poseidon, mimcSponge);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
class Eddsa {
|
|
20
|
+
|
|
21
|
+
constructor(babyJub, pedersenHash, mimc7, poseidon, mimcSponge) {
|
|
22
|
+
this.babyJub = babyJub;
|
|
23
|
+
this.pedersenHash = pedersenHash;
|
|
24
|
+
this.mimc7 = mimc7;
|
|
25
|
+
this.poseidon = poseidon;
|
|
26
|
+
this.mimcSponge = mimcSponge;
|
|
27
|
+
this.F = babyJub.F;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
pruneBuffer(buff) {
|
|
31
|
+
buff[0] = buff[0] & 0xF8;
|
|
32
|
+
buff[31] = buff[31] & 0x7F;
|
|
33
|
+
buff[31] = buff[31] | 0x40;
|
|
34
|
+
return buff;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
prv2pub(prv) {
|
|
38
|
+
const F = this.babyJub.F;
|
|
39
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
40
|
+
let s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
41
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s,3));
|
|
42
|
+
return A;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
signPedersen(prv, msg) {
|
|
46
|
+
const F = this.babyJub.F;
|
|
47
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
48
|
+
const s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
49
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));
|
|
50
|
+
|
|
51
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
52
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
53
|
+
composeBuff.set(msg, 32);
|
|
54
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
55
|
+
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
56
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
57
|
+
const R8p = this.babyJub.packPoint(R8);
|
|
58
|
+
const Ap = this.babyJub.packPoint(A);
|
|
59
|
+
|
|
60
|
+
const composeBuff2 = new Uint8Array(64 + msg.length);
|
|
61
|
+
composeBuff2.set(R8p, 0);
|
|
62
|
+
composeBuff2.set(Ap, 32);
|
|
63
|
+
composeBuff2.set(msg, 64);
|
|
64
|
+
|
|
65
|
+
const hmBuff = this.pedersenHash.hash(composeBuff2);
|
|
66
|
+
const hm = Scalar.fromRprLE(hmBuff, 0, 32);
|
|
67
|
+
|
|
68
|
+
const S = Scalar.mod(
|
|
69
|
+
Scalar.add(
|
|
70
|
+
r,
|
|
71
|
+
Scalar.mul(hm, s)
|
|
72
|
+
),
|
|
73
|
+
this.babyJub.subOrder
|
|
74
|
+
)
|
|
75
|
+
return {
|
|
76
|
+
R8: R8,
|
|
77
|
+
S: S
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
signMiMC(prv, msg) {
|
|
82
|
+
const F = this.babyJub.F;
|
|
83
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
84
|
+
const s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
85
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
89
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
90
|
+
F.toRprLE(composeBuff, 32, msg);
|
|
91
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
92
|
+
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
93
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
94
|
+
|
|
95
|
+
const hm = this.mimc7.multiHash([R8[0], R8[1], A[0], A[1], msg]);
|
|
96
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
97
|
+
const S = Scalar.mod(
|
|
98
|
+
Scalar.add(
|
|
99
|
+
r,
|
|
100
|
+
Scalar.mul(hms, s)
|
|
101
|
+
),
|
|
102
|
+
this.babyJub.subOrder
|
|
103
|
+
)
|
|
104
|
+
return {
|
|
105
|
+
R8: R8,
|
|
106
|
+
S: S
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
signMiMCSponge(prv, msg) {
|
|
111
|
+
const F = this.babyJub.F;
|
|
112
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
113
|
+
const s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
114
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));
|
|
115
|
+
|
|
116
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
117
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
118
|
+
F.toRprLE(composeBuff, 32, msg);
|
|
119
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
120
|
+
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
121
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
122
|
+
|
|
123
|
+
const hm = this.mimcSponge.multiHash([R8[0], R8[1], A[0], A[1], msg]);
|
|
124
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
125
|
+
const S = Scalar.mod(
|
|
126
|
+
Scalar.add(
|
|
127
|
+
r,
|
|
128
|
+
Scalar.mul(hms, s)
|
|
129
|
+
),
|
|
130
|
+
this.babyJub.subOrder
|
|
131
|
+
)
|
|
132
|
+
return {
|
|
133
|
+
R8: R8,
|
|
134
|
+
S: S
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
signPoseidon(prv, msg) {
|
|
139
|
+
const F = this.babyJub.F;
|
|
140
|
+
const sBuff = this.pruneBuffer(createBlakeHash("blake512").update(Buffer.from(prv)).digest());
|
|
141
|
+
const s = Scalar.fromRprLE(sBuff, 0, 32);
|
|
142
|
+
const A = this.babyJub.mulPointEscalar(this.babyJub.Base8, Scalar.shr(s, 3));
|
|
143
|
+
|
|
144
|
+
const composeBuff = new Uint8Array(32 + msg.length);
|
|
145
|
+
composeBuff.set(sBuff.slice(32), 0);
|
|
146
|
+
F.toRprLE(composeBuff, 32, msg);
|
|
147
|
+
const rBuff = createBlakeHash("blake512").update(Buffer.from(composeBuff)).digest();
|
|
148
|
+
let r = Scalar.mod(Scalar.fromRprLE(rBuff, 0, 64), this.babyJub.subOrder);
|
|
149
|
+
const R8 = this.babyJub.mulPointEscalar(this.babyJub.Base8, r);
|
|
150
|
+
|
|
151
|
+
const hm = this.poseidon([R8[0], R8[1], A[0], A[1], msg]);
|
|
152
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
153
|
+
const S = Scalar.mod(
|
|
154
|
+
Scalar.add(
|
|
155
|
+
r,
|
|
156
|
+
Scalar.mul(hms, s)
|
|
157
|
+
),
|
|
158
|
+
this.babyJub.subOrder
|
|
159
|
+
)
|
|
160
|
+
return {
|
|
161
|
+
R8: R8,
|
|
162
|
+
S: S
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
verifyPedersen(msg, sig, A) {
|
|
167
|
+
// Check parameters
|
|
168
|
+
if (typeof sig != "object") return false;
|
|
169
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
170
|
+
if (sig.R8.length!= 2) return false;
|
|
171
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
172
|
+
if (!Array.isArray(A)) return false;
|
|
173
|
+
if (A.length!= 2) return false;
|
|
174
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
175
|
+
if (Scalar.geq(sig.S, this.babyJub.subOrder)) return false;
|
|
176
|
+
|
|
177
|
+
const R8p = this.babyJub.packPoint(sig.R8);
|
|
178
|
+
const Ap = this.babyJub.packPoint(A);
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
const composeBuff2 = new Uint8Array(64 + msg.length);
|
|
182
|
+
composeBuff2.set(R8p, 0);
|
|
183
|
+
composeBuff2.set(Ap, 32);
|
|
184
|
+
composeBuff2.set(msg, 64);
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
const hmBuff = this.pedersenHash.hash(composeBuff2);
|
|
188
|
+
const hm = Scalar.fromRprLE(hmBuff, 0, 32);
|
|
189
|
+
|
|
190
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
191
|
+
let Pright = this.babyJub.mulPointEscalar(A, Scalar.mul(hm,8));
|
|
192
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
193
|
+
|
|
194
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
195
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
verifyMiMC(msg, sig, A) {
|
|
200
|
+
// Check parameters
|
|
201
|
+
if (typeof sig != "object") return false;
|
|
202
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
203
|
+
if (sig.R8.length!= 2) return false;
|
|
204
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
205
|
+
if (!Array.isArray(A)) return false;
|
|
206
|
+
if (A.length!= 2) return false;
|
|
207
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
208
|
+
if (sig.S>= this.babyJub.subOrder) return false;
|
|
209
|
+
|
|
210
|
+
const hm = this.mimc7.multiHash([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
|
|
211
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
212
|
+
|
|
213
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
214
|
+
let Pright = this.babyJub.mulPointEscalar(A, Scalar.mul(hms, 8));
|
|
215
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
216
|
+
|
|
217
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
218
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
verifyPoseidon(msg, sig, A) {
|
|
223
|
+
|
|
224
|
+
// Check parameters
|
|
225
|
+
if (typeof sig != "object") return false;
|
|
226
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
227
|
+
if (sig.R8.length!= 2) return false;
|
|
228
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
229
|
+
if (!Array.isArray(A)) return false;
|
|
230
|
+
if (A.length!= 2) return false;
|
|
231
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
232
|
+
if (sig.S>= this.babyJub.subOrder) return false;
|
|
233
|
+
|
|
234
|
+
const hm = this.poseidon([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
|
|
235
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
236
|
+
|
|
237
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
238
|
+
let Pright = this.babyJub.mulPointEscalar(A, Scalar.mul(hms, 8));
|
|
239
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
240
|
+
|
|
241
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
242
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
verifyMiMCSponge(msg, sig, A) {
|
|
247
|
+
|
|
248
|
+
// Check parameters
|
|
249
|
+
if (typeof sig != "object") return false;
|
|
250
|
+
if (!Array.isArray(sig.R8)) return false;
|
|
251
|
+
if (sig.R8.length!= 2) return false;
|
|
252
|
+
if (!this.babyJub.inCurve(sig.R8)) return false;
|
|
253
|
+
if (!Array.isArray(A)) return false;
|
|
254
|
+
if (A.length!= 2) return false;
|
|
255
|
+
if (!this.babyJub.inCurve(A)) return false;
|
|
256
|
+
if (sig.S>= this.babyJub.subOrder) return false;
|
|
257
|
+
|
|
258
|
+
const hm = this.mimcSponge.multiHash([sig.R8[0], sig.R8[1], A[0], A[1], msg]);
|
|
259
|
+
const hms = Scalar.e(this.babyJub.F.toObject(hm));
|
|
260
|
+
|
|
261
|
+
const Pleft = this.babyJub.mulPointEscalar(this.babyJub.Base8, sig.S);
|
|
262
|
+
let Pright = this.babyJub.mulPointEscalar(A, Scalar.mul(hms, 8));
|
|
263
|
+
Pright = this.babyJub.addPoint(sig.R8, Pright);
|
|
264
|
+
|
|
265
|
+
if (!this.babyJub.F.eq(Pleft[0],Pright[0])) return false;
|
|
266
|
+
if (!this.babyJub.F.eq(Pleft[1],Pright[1])) return false;
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
packSignature(sig) {
|
|
271
|
+
const buff = new Uint8Array(64);
|
|
272
|
+
const R8p = this.babyJub.packPoint(sig.R8);
|
|
273
|
+
buff.set(R8p, 0)
|
|
274
|
+
const Sp = Scalar.toRprLE(buff, 32, sig.S, 32);
|
|
275
|
+
return buff;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
unpackSignature(sigBuff) {
|
|
279
|
+
return {
|
|
280
|
+
R8: this.babyJub.unpackPoint(sigBuff.slice(0,32)),
|
|
281
|
+
S: Scalar.fromRprLE(sigBuff, 32, 32)
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
|