zkjson 0.7.1 → 0.8.0
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/cjs/circomlibjs/SMT.js +481 -0
- package/cjs/circomlibjs/SMTMemDb.js +173 -0
- package/cjs/circomlibjs/buildPoseidon.js +162 -0
- package/cjs/circomlibjs/buildPoseidonWasm.js +124 -0
- package/cjs/circomlibjs/getHashes.js +220 -0
- package/cjs/circomlibjs/poseidonConstants$1.js +1 -0
- package/cjs/circomlibjs.js +6 -6
- package/cjs/collection.js +2 -2
- package/cjs/collection_tree.js +148 -0
- package/cjs/db.js +2 -2
- package/cjs/db_tree.js +398 -0
- package/cjs/index.js +16 -0
- package/cjs/newMemEmptyTrie.js +42 -0
- package/esm/circomlibjs/SMT.js +349 -0
- package/esm/circomlibjs/SMTMemDb.js +69 -0
- package/esm/circomlibjs/buildPoseidon.js +113 -0
- package/esm/circomlibjs/buildPoseidonWasm.js +445 -0
- package/esm/circomlibjs/getHashes.js +515 -0
- package/esm/circomlibjs/poseidonConstants$1.js +24806 -0
- package/esm/circomlibjs.js +70 -25385
- package/esm/collection.js +1 -1
- package/esm/collection_tree.js +43 -0
- package/esm/db.js +1 -1
- package/esm/db_tree.js +122 -0
- package/esm/index.js +3 -1
- package/esm/newMemEmptyTrie.js +11 -0
- package/package.json +1 -1
@@ -0,0 +1,162 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports["default"] = buildPoseidon;
|
7
|
+
var ffjavascript = _interopRequireWildcard(require("ffjavascript"));
|
8
|
+
var _poseidonConstants$ = _interopRequireDefault(require("./poseidonConstants$1.js"));
|
9
|
+
var assert = _interopRequireWildcard(require("assert"));
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
11
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
12
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
13
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
14
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
15
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
16
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
17
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
18
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
19
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
20
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
21
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
22
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
23
|
+
var assert__default = assert;
|
24
|
+
function unsringifyConstants(Fr, o) {
|
25
|
+
if (typeof o == "string" && /^[0-9]+$/.test(o)) {
|
26
|
+
return Fr.e(o);
|
27
|
+
} else if (typeof o == "string" && /^0x[0-9a-fA-F]+$/.test(o)) {
|
28
|
+
return Fr.e(o);
|
29
|
+
} else if (Array.isArray(o)) {
|
30
|
+
return o.map(unsringifyConstants.bind(null, Fr));
|
31
|
+
} else if (_typeof(o) == "object") {
|
32
|
+
if (o === null) return null;
|
33
|
+
var res = {};
|
34
|
+
var keys = Object.keys(o);
|
35
|
+
keys.forEach(function (k) {
|
36
|
+
res[k] = unsringifyConstants(Fr, o[k]);
|
37
|
+
});
|
38
|
+
return res;
|
39
|
+
} else {
|
40
|
+
return o;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
function buildPoseidon() {
|
44
|
+
return _buildPoseidon.apply(this, arguments);
|
45
|
+
}
|
46
|
+
function _buildPoseidon() {
|
47
|
+
_buildPoseidon = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
48
|
+
var bn128, F, opt, N_ROUNDS_F, N_ROUNDS_P, pow5, poseidon;
|
49
|
+
return _regenerator().w(function (_context) {
|
50
|
+
while (1) switch (_context.n) {
|
51
|
+
case 0:
|
52
|
+
poseidon = function _poseidon(inputs, initState, nOut) {
|
53
|
+
assert__default["default"](inputs.length > 0);
|
54
|
+
assert__default["default"](inputs.length <= N_ROUNDS_P.length);
|
55
|
+
if (initState) {
|
56
|
+
initState = F.e(initState);
|
57
|
+
} else {
|
58
|
+
initState = F.zero;
|
59
|
+
}
|
60
|
+
nOut = nOut || 1;
|
61
|
+
var t = inputs.length + 1;
|
62
|
+
var nRoundsF = N_ROUNDS_F;
|
63
|
+
var nRoundsP = N_ROUNDS_P[t - 2];
|
64
|
+
var C = opt.C[t - 2];
|
65
|
+
var S = opt.S[t - 2];
|
66
|
+
var M = opt.M[t - 2];
|
67
|
+
var P = opt.P[t - 2];
|
68
|
+
var state = [initState].concat(_toConsumableArray(inputs.map(function (a) {
|
69
|
+
return F.e(a);
|
70
|
+
})));
|
71
|
+
state = state.map(function (a, i) {
|
72
|
+
return F.add(a, C[i]);
|
73
|
+
});
|
74
|
+
var _loop = function _loop(r) {
|
75
|
+
state = state.map(function (a) {
|
76
|
+
return pow5(a);
|
77
|
+
});
|
78
|
+
state = state.map(function (a, i) {
|
79
|
+
return F.add(a, C[(r + 1) * t + i]);
|
80
|
+
});
|
81
|
+
state = state.map(function (_, i) {
|
82
|
+
return state.reduce(function (acc, a, j) {
|
83
|
+
return F.add(acc, F.mul(M[j][i], a));
|
84
|
+
}, F.zero);
|
85
|
+
});
|
86
|
+
};
|
87
|
+
for (var r = 0; r < nRoundsF / 2 - 1; r++) {
|
88
|
+
_loop(r);
|
89
|
+
}
|
90
|
+
state = state.map(function (a) {
|
91
|
+
return pow5(a);
|
92
|
+
});
|
93
|
+
state = state.map(function (a, i) {
|
94
|
+
return F.add(a, C[(nRoundsF / 2 - 1 + 1) * t + i]);
|
95
|
+
});
|
96
|
+
state = state.map(function (_, i) {
|
97
|
+
return state.reduce(function (acc, a, j) {
|
98
|
+
return F.add(acc, F.mul(P[j][i], a));
|
99
|
+
}, F.zero);
|
100
|
+
});
|
101
|
+
var _loop2 = function _loop2(_r) {
|
102
|
+
state[0] = pow5(state[0]);
|
103
|
+
state[0] = F.add(state[0], C[(nRoundsF / 2 + 1) * t + _r]);
|
104
|
+
var s0 = state.reduce(function (acc, a, j) {
|
105
|
+
return F.add(acc, F.mul(S[(t * 2 - 1) * _r + j], a));
|
106
|
+
}, F.zero);
|
107
|
+
for (var k = 1; k < t; k++) {
|
108
|
+
state[k] = F.add(state[k], F.mul(state[0], S[(t * 2 - 1) * _r + t + k - 1]));
|
109
|
+
}
|
110
|
+
state[0] = s0;
|
111
|
+
};
|
112
|
+
for (var _r = 0; _r < nRoundsP; _r++) {
|
113
|
+
_loop2(_r);
|
114
|
+
}
|
115
|
+
var _loop3 = function _loop3(_r2) {
|
116
|
+
state = state.map(function (a) {
|
117
|
+
return pow5(a);
|
118
|
+
});
|
119
|
+
state = state.map(function (a, i) {
|
120
|
+
return F.add(a, C[(nRoundsF / 2 + 1) * t + nRoundsP + _r2 * t + i]);
|
121
|
+
});
|
122
|
+
state = state.map(function (_, i) {
|
123
|
+
return state.reduce(function (acc, a, j) {
|
124
|
+
return F.add(acc, F.mul(M[j][i], a));
|
125
|
+
}, F.zero);
|
126
|
+
});
|
127
|
+
};
|
128
|
+
for (var _r2 = 0; _r2 < nRoundsF / 2 - 1; _r2++) {
|
129
|
+
_loop3(_r2);
|
130
|
+
}
|
131
|
+
state = state.map(function (a) {
|
132
|
+
return pow5(a);
|
133
|
+
});
|
134
|
+
state = state.map(function (_, i) {
|
135
|
+
return state.reduce(function (acc, a, j) {
|
136
|
+
return F.add(acc, F.mul(M[j][i], a));
|
137
|
+
}, F.zero);
|
138
|
+
});
|
139
|
+
if (nOut == 1) {
|
140
|
+
return state[0];
|
141
|
+
} else {
|
142
|
+
return state.slice(0, nOut);
|
143
|
+
}
|
144
|
+
};
|
145
|
+
_context.n = 1;
|
146
|
+
return ffjavascript.getCurveFromName("bn128", true);
|
147
|
+
case 1:
|
148
|
+
bn128 = _context.v;
|
149
|
+
F = bn128.Fr;
|
150
|
+
opt = unsringifyConstants(F, _poseidonConstants$["default"]);
|
151
|
+
N_ROUNDS_F = 8;
|
152
|
+
N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
|
153
|
+
pow5 = function pow5(a) {
|
154
|
+
return F.mul(a, F.square(F.square(a, a)));
|
155
|
+
};
|
156
|
+
poseidon.F = F;
|
157
|
+
return _context.a(2, poseidon);
|
158
|
+
}
|
159
|
+
}, _callee);
|
160
|
+
}));
|
161
|
+
return _buildPoseidon.apply(this, arguments);
|
162
|
+
}
|
@@ -0,0 +1,124 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
4
|
+
value: true
|
5
|
+
});
|
6
|
+
exports["default"] = buildPoseidonWasm;
|
7
|
+
function buildPoseidonWasm(module) {
|
8
|
+
var F = new ffjavascript.F1Field(ffjavascript.Scalar.e("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
|
9
|
+
var N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
|
10
|
+
var NSizes = poseidonConstants$1.C.length;
|
11
|
+
var buffIdx = new Uint8Array(NSizes * 5 * 4);
|
12
|
+
var buffIdx32 = new Uint32Array(buffIdx.buffer);
|
13
|
+
for (var i = 0; i < NSizes; i++) {
|
14
|
+
buffIdx32[i * 5] = N_ROUNDS_P[i];
|
15
|
+
var buffC = new Uint8Array(32 * poseidonConstants$1.C[i].length);
|
16
|
+
for (var j = 0; j < poseidonConstants$1.C[i].length; j++) {
|
17
|
+
F.toRprLEM(buffC, j * 32, F.e(poseidonConstants$1.C[i][j]));
|
18
|
+
}
|
19
|
+
buffIdx32[i * 5 + 1] = module.alloc(buffC);
|
20
|
+
var buffS = new Uint8Array(32 * poseidonConstants$1.S[i].length);
|
21
|
+
for (var _j = 0; _j < poseidonConstants$1.S[i].length; _j++) {
|
22
|
+
F.toRprLEM(buffS, _j * 32, F.e(poseidonConstants$1.S[i][_j]));
|
23
|
+
}
|
24
|
+
buffIdx32[i * 5 + 2] = module.alloc(buffS);
|
25
|
+
var N = poseidonConstants$1.M[i].length;
|
26
|
+
var buffM = new Uint8Array(32 * N * N);
|
27
|
+
for (var _j2 = 0; _j2 < N; _j2++) {
|
28
|
+
for (var k = 0; k < N; k++) {
|
29
|
+
F.toRprLEM(buffM, (_j2 * N + k) * 32, F.e(poseidonConstants$1.M[i][k][_j2]));
|
30
|
+
}
|
31
|
+
}
|
32
|
+
buffIdx32[i * 5 + 3] = module.alloc(buffM);
|
33
|
+
var buffP = new Uint8Array(32 * N * N);
|
34
|
+
for (var _j3 = 0; _j3 < N; _j3++) {
|
35
|
+
for (var _k = 0; _k < N; _k++) {
|
36
|
+
F.toRprLEM(buffP, (_j3 * N + _k) * 32, F.e(poseidonConstants$1.P[i][_k][_j3]));
|
37
|
+
}
|
38
|
+
}
|
39
|
+
buffIdx32[i * 5 + 4] = module.alloc(buffP);
|
40
|
+
}
|
41
|
+
var pConstants = module.alloc(buffIdx);
|
42
|
+
var pState = module.alloc(32 * ((NSizes + 1) * 32));
|
43
|
+
function buildAddConstant() {
|
44
|
+
var f = module.addFunction("poseidon_addConstant");
|
45
|
+
f.addParam("t", "i32");
|
46
|
+
f.addParam("pC", "i32");
|
47
|
+
f.setReturnType("i32");
|
48
|
+
f.addLocal("i", "i32");
|
49
|
+
f.addLocal("pState", "i32");
|
50
|
+
var c = f.getCodeBuilder();
|
51
|
+
f.addCode(c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_add", c.getLocal("pC"), c.getLocal("pState"), c.getLocal("pState")), c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.ret(c.getLocal("pC")));
|
52
|
+
}
|
53
|
+
function buildPower5() {
|
54
|
+
var f = module.addFunction("poseidon_power5");
|
55
|
+
f.addParam("p", "i32");
|
56
|
+
var c = f.getCodeBuilder();
|
57
|
+
var AUX = c.i32_const(module.alloc(32));
|
58
|
+
f.addCode(c.call("frm_square", c.getLocal("p"), AUX), c.call("frm_square", AUX, AUX), c.call("frm_mul", c.getLocal("p"), AUX, c.getLocal("p")));
|
59
|
+
}
|
60
|
+
function buildPower5all() {
|
61
|
+
var f = module.addFunction("poseidon_power5all");
|
62
|
+
f.addParam("t", "i32");
|
63
|
+
f.addLocal("i", "i32");
|
64
|
+
f.addLocal("pState", "i32");
|
65
|
+
var c = f.getCodeBuilder();
|
66
|
+
f.addCode(c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("poseidon_power5", c.getLocal("pState")), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
67
|
+
}
|
68
|
+
function buildApplyMatrix() {
|
69
|
+
var f = module.addFunction("poseidon_applyMatrix");
|
70
|
+
f.addParam("t", "i32");
|
71
|
+
f.addParam("pM", "i32");
|
72
|
+
f.addLocal("i", "i32");
|
73
|
+
f.addLocal("j", "i32");
|
74
|
+
f.addLocal("pState", "i32");
|
75
|
+
f.addLocal("pStateAux", "i32");
|
76
|
+
var c = f.getCodeBuilder();
|
77
|
+
var pStateAux = module.alloc(32 * ((NSizes + 1) * 32));
|
78
|
+
var pAux = module.alloc(32);
|
79
|
+
f.addCode(c.setLocal("pStateAux", c.i32_const(pStateAux)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_zero", c.getLocal("pStateAux")), c.setLocal("pState", c.i32_const(pState)), c.setLocal("j", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("j"), c.getLocal("t"))), c.call("frm_mul", c.getLocal("pState"), c.getLocal("pM"), c.i32_const(pAux)), c.call("frm_add", c.i32_const(pAux), c.getLocal("pStateAux"), c.getLocal("pStateAux")), c.setLocal("pM", c.i32_add(c.getLocal("pM"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), c.br(0))), c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("pStateAux", c.i32_const(pStateAux)), c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_copy", c.getLocal("pStateAux"), c.getLocal("pState")), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
80
|
+
}
|
81
|
+
function buildApplySMatrix() {
|
82
|
+
var f = module.addFunction("poseidon_applySMatrix");
|
83
|
+
f.addParam("t", "i32");
|
84
|
+
f.addParam("pS", "i32");
|
85
|
+
f.setReturnType("i32");
|
86
|
+
f.addLocal("i", "i32");
|
87
|
+
f.addLocal("pState", "i32");
|
88
|
+
var c = f.getCodeBuilder();
|
89
|
+
var pS0 = module.alloc(32);
|
90
|
+
var pAux = module.alloc(32);
|
91
|
+
f.addCode(c.call("frm_zero", c.i32_const(pS0)), c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_mul", c.getLocal("pState"), c.getLocal("pS"), c.i32_const(pAux)), c.call("frm_add", c.i32_const(pS0), c.i32_const(pAux), c.i32_const(pS0)), c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("pState", c.i32_const(pState + 32)), c.setLocal("i", c.i32_const(1)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_mul", c.i32_const(pState), c.getLocal("pS"), c.i32_const(pAux)), c.call("frm_add", c.getLocal("pState"), c.i32_const(pAux), c.getLocal("pState")), c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("frm_copy", c.i32_const(pS0), c.i32_const(pState)), c.ret(c.getLocal("pS")));
|
92
|
+
}
|
93
|
+
function buildPoseidon() {
|
94
|
+
var f = module.addFunction("poseidon");
|
95
|
+
f.addParam("pInitState", "i32");
|
96
|
+
f.addParam("pIn", "i32");
|
97
|
+
f.addParam("n", "i32");
|
98
|
+
f.addParam("pOut", "i32");
|
99
|
+
f.addParam("nOut", "i32");
|
100
|
+
f.addLocal("pC", "i32");
|
101
|
+
f.addLocal("pS", "i32");
|
102
|
+
f.addLocal("pM", "i32");
|
103
|
+
f.addLocal("pP", "i32");
|
104
|
+
f.addLocal("t", "i32");
|
105
|
+
f.addLocal("i", "i32");
|
106
|
+
f.addLocal("nRoundsP", "i32");
|
107
|
+
f.addLocal("pAux", "i32");
|
108
|
+
var c = f.getCodeBuilder();
|
109
|
+
f.addCode(c.setLocal("t", c.i32_add(c.getLocal("n"), c.i32_const(1))), c.setLocal("pAux", c.i32_add(c.i32_const(pConstants), c.i32_mul(c.i32_sub(c.getLocal("n"), c.i32_const(1)), c.i32_const(20)))), c.setLocal("nRoundsP", c.i32_load(c.getLocal("pAux"))), c.setLocal("pC", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(4)))), c.setLocal("pS", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(8)))), c.setLocal("pM", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(12)))), c.setLocal("pP", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(16)))),
|
110
|
+
// Initialize state
|
111
|
+
c.call("frm_zero", c.i32_const(pState)), c.call("frm_copy", c.getLocal("pInitState"), c.i32_const(pState)), c.setLocal("i", c.i32_const(1)), c.block(c.loop(c.call("frm_copy", c.i32_add(c.getLocal("pIn"), c.i32_mul(c.i32_sub(c.getLocal("i"), c.i32_const(1)), c.i32_const(32))), c.i32_add(c.i32_const(pState), c.i32_mul(c.getLocal("i"), c.i32_const(32)))), c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))),
|
112
|
+
// Initialize state
|
113
|
+
c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
|
114
|
+
// First full rounds
|
115
|
+
c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pP")), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nRoundsP"))), c.call("poseidon_power5", c.i32_const(pState)), c.call("frm_add", c.i32_const(pState), c.getLocal("pC"), c.i32_const(pState)), c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))), c.setLocal("pS", c.call("poseidon_applySMatrix", c.getLocal("t"), c.getLocal("pS"))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("poseidon_power5all", c.getLocal("t")), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nOut"))), c.call("frm_copy", c.i32_add(c.i32_const(pState), c.i32_mul(c.getLocal("i"), c.i32_const(32))), c.i32_add(c.getLocal("pOut"), c.i32_mul(c.getLocal("i"), c.i32_const(32)))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
116
|
+
}
|
117
|
+
buildAddConstant();
|
118
|
+
buildPower5();
|
119
|
+
buildPower5all();
|
120
|
+
buildApplyMatrix();
|
121
|
+
buildApplySMatrix();
|
122
|
+
buildPoseidon();
|
123
|
+
module.exportFunction("poseidon");
|
124
|
+
}
|
@@ -0,0 +1,220 @@
|
|
1
|
+
"use strict";
|
2
|
+
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
5
|
+
value: true
|
6
|
+
});
|
7
|
+
exports["default"] = getHashes;
|
8
|
+
var ffjavascript = _interopRequireWildcard(require("ffjavascript"));
|
9
|
+
var _poseidonConstants$ = _interopRequireDefault(require("./poseidonConstants$1.js"));
|
10
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
11
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
12
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
13
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { if (r) i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n;else { var o = function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); }; o("next", 0), o("throw", 1), o("return", 2); } }, _regeneratorDefine2(e, r, n, t); }
|
14
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
15
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
16
|
+
function buildPoseidonWasm(module) {
|
17
|
+
var F = new ffjavascript.F1Field(ffjavascript.Scalar.e("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
|
18
|
+
var N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
|
19
|
+
var NSizes = _poseidonConstants$["default"].C.length;
|
20
|
+
var buffIdx = new Uint8Array(NSizes * 5 * 4);
|
21
|
+
var buffIdx32 = new Uint32Array(buffIdx.buffer);
|
22
|
+
for (var i = 0; i < NSizes; i++) {
|
23
|
+
buffIdx32[i * 5] = N_ROUNDS_P[i];
|
24
|
+
var buffC = new Uint8Array(32 * _poseidonConstants$["default"].C[i].length);
|
25
|
+
for (var j = 0; j < _poseidonConstants$["default"].C[i].length; j++) {
|
26
|
+
F.toRprLEM(buffC, j * 32, F.e(_poseidonConstants$["default"].C[i][j]));
|
27
|
+
}
|
28
|
+
buffIdx32[i * 5 + 1] = module.alloc(buffC);
|
29
|
+
var buffS = new Uint8Array(32 * _poseidonConstants$["default"].S[i].length);
|
30
|
+
for (var _j = 0; _j < _poseidonConstants$["default"].S[i].length; _j++) {
|
31
|
+
F.toRprLEM(buffS, _j * 32, F.e(_poseidonConstants$["default"].S[i][_j]));
|
32
|
+
}
|
33
|
+
buffIdx32[i * 5 + 2] = module.alloc(buffS);
|
34
|
+
var N = _poseidonConstants$["default"].M[i].length;
|
35
|
+
var buffM = new Uint8Array(32 * N * N);
|
36
|
+
for (var _j2 = 0; _j2 < N; _j2++) {
|
37
|
+
for (var k = 0; k < N; k++) {
|
38
|
+
F.toRprLEM(buffM, (_j2 * N + k) * 32, F.e(_poseidonConstants$["default"].M[i][k][_j2]));
|
39
|
+
}
|
40
|
+
}
|
41
|
+
buffIdx32[i * 5 + 3] = module.alloc(buffM);
|
42
|
+
var buffP = new Uint8Array(32 * N * N);
|
43
|
+
for (var _j3 = 0; _j3 < N; _j3++) {
|
44
|
+
for (var _k = 0; _k < N; _k++) {
|
45
|
+
F.toRprLEM(buffP, (_j3 * N + _k) * 32, F.e(_poseidonConstants$["default"].P[i][_k][_j3]));
|
46
|
+
}
|
47
|
+
}
|
48
|
+
buffIdx32[i * 5 + 4] = module.alloc(buffP);
|
49
|
+
}
|
50
|
+
var pConstants = module.alloc(buffIdx);
|
51
|
+
var pState = module.alloc(32 * ((NSizes + 1) * 32));
|
52
|
+
function buildAddConstant() {
|
53
|
+
var f = module.addFunction("poseidon_addConstant");
|
54
|
+
f.addParam("t", "i32");
|
55
|
+
f.addParam("pC", "i32");
|
56
|
+
f.setReturnType("i32");
|
57
|
+
f.addLocal("i", "i32");
|
58
|
+
f.addLocal("pState", "i32");
|
59
|
+
var c = f.getCodeBuilder();
|
60
|
+
f.addCode(c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_add", c.getLocal("pC"), c.getLocal("pState"), c.getLocal("pState")), c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.ret(c.getLocal("pC")));
|
61
|
+
}
|
62
|
+
function buildPower5() {
|
63
|
+
var f = module.addFunction("poseidon_power5");
|
64
|
+
f.addParam("p", "i32");
|
65
|
+
var c = f.getCodeBuilder();
|
66
|
+
var AUX = c.i32_const(module.alloc(32));
|
67
|
+
f.addCode(c.call("frm_square", c.getLocal("p"), AUX), c.call("frm_square", AUX, AUX), c.call("frm_mul", c.getLocal("p"), AUX, c.getLocal("p")));
|
68
|
+
}
|
69
|
+
function buildPower5all() {
|
70
|
+
var f = module.addFunction("poseidon_power5all");
|
71
|
+
f.addParam("t", "i32");
|
72
|
+
f.addLocal("i", "i32");
|
73
|
+
f.addLocal("pState", "i32");
|
74
|
+
var c = f.getCodeBuilder();
|
75
|
+
f.addCode(c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("poseidon_power5", c.getLocal("pState")), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
76
|
+
}
|
77
|
+
function buildApplyMatrix() {
|
78
|
+
var f = module.addFunction("poseidon_applyMatrix");
|
79
|
+
f.addParam("t", "i32");
|
80
|
+
f.addParam("pM", "i32");
|
81
|
+
f.addLocal("i", "i32");
|
82
|
+
f.addLocal("j", "i32");
|
83
|
+
f.addLocal("pState", "i32");
|
84
|
+
f.addLocal("pStateAux", "i32");
|
85
|
+
var c = f.getCodeBuilder();
|
86
|
+
var pStateAux = module.alloc(32 * ((NSizes + 1) * 32));
|
87
|
+
var pAux = module.alloc(32);
|
88
|
+
f.addCode(c.setLocal("pStateAux", c.i32_const(pStateAux)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_zero", c.getLocal("pStateAux")), c.setLocal("pState", c.i32_const(pState)), c.setLocal("j", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("j"), c.getLocal("t"))), c.call("frm_mul", c.getLocal("pState"), c.getLocal("pM"), c.i32_const(pAux)), c.call("frm_add", c.i32_const(pAux), c.getLocal("pStateAux"), c.getLocal("pStateAux")), c.setLocal("pM", c.i32_add(c.getLocal("pM"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))), c.br(0))), c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("pStateAux", c.i32_const(pStateAux)), c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_copy", c.getLocal("pStateAux"), c.getLocal("pState")), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
89
|
+
}
|
90
|
+
function buildApplySMatrix() {
|
91
|
+
var f = module.addFunction("poseidon_applySMatrix");
|
92
|
+
f.addParam("t", "i32");
|
93
|
+
f.addParam("pS", "i32");
|
94
|
+
f.setReturnType("i32");
|
95
|
+
f.addLocal("i", "i32");
|
96
|
+
f.addLocal("pState", "i32");
|
97
|
+
var c = f.getCodeBuilder();
|
98
|
+
var pS0 = module.alloc(32);
|
99
|
+
var pAux = module.alloc(32);
|
100
|
+
f.addCode(c.call("frm_zero", c.i32_const(pS0)), c.setLocal("pState", c.i32_const(pState)), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_mul", c.getLocal("pState"), c.getLocal("pS"), c.i32_const(pAux)), c.call("frm_add", c.i32_const(pS0), c.i32_const(pAux), c.i32_const(pS0)), c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("pState", c.i32_const(pState + 32)), c.setLocal("i", c.i32_const(1)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))), c.call("frm_mul", c.i32_const(pState), c.getLocal("pS"), c.i32_const(pAux)), c.call("frm_add", c.getLocal("pState"), c.i32_const(pAux), c.getLocal("pState")), c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))), c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("frm_copy", c.i32_const(pS0), c.i32_const(pState)), c.ret(c.getLocal("pS")));
|
101
|
+
}
|
102
|
+
function buildPoseidon() {
|
103
|
+
var f = module.addFunction("poseidon");
|
104
|
+
f.addParam("pInitState", "i32");
|
105
|
+
f.addParam("pIn", "i32");
|
106
|
+
f.addParam("n", "i32");
|
107
|
+
f.addParam("pOut", "i32");
|
108
|
+
f.addParam("nOut", "i32");
|
109
|
+
f.addLocal("pC", "i32");
|
110
|
+
f.addLocal("pS", "i32");
|
111
|
+
f.addLocal("pM", "i32");
|
112
|
+
f.addLocal("pP", "i32");
|
113
|
+
f.addLocal("t", "i32");
|
114
|
+
f.addLocal("i", "i32");
|
115
|
+
f.addLocal("nRoundsP", "i32");
|
116
|
+
f.addLocal("pAux", "i32");
|
117
|
+
var c = f.getCodeBuilder();
|
118
|
+
f.addCode(c.setLocal("t", c.i32_add(c.getLocal("n"), c.i32_const(1))), c.setLocal("pAux", c.i32_add(c.i32_const(pConstants), c.i32_mul(c.i32_sub(c.getLocal("n"), c.i32_const(1)), c.i32_const(20)))), c.setLocal("nRoundsP", c.i32_load(c.getLocal("pAux"))), c.setLocal("pC", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(4)))), c.setLocal("pS", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(8)))), c.setLocal("pM", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(12)))), c.setLocal("pP", c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(16)))),
|
119
|
+
// Initialize state
|
120
|
+
c.call("frm_zero", c.i32_const(pState)), c.call("frm_copy", c.getLocal("pInitState"), c.i32_const(pState)), c.setLocal("i", c.i32_const(1)), c.block(c.loop(c.call("frm_copy", c.i32_add(c.getLocal("pIn"), c.i32_mul(c.i32_sub(c.getLocal("i"), c.i32_const(1)), c.i32_const(32))), c.i32_add(c.i32_const(pState), c.i32_mul(c.getLocal("i"), c.i32_const(32)))), c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))),
|
121
|
+
// Initialize state
|
122
|
+
c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
|
123
|
+
// First full rounds
|
124
|
+
c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pP")), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nRoundsP"))), c.call("poseidon_power5", c.i32_const(pState)), c.call("frm_add", c.i32_const(pState), c.getLocal("pC"), c.i32_const(pState)), c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))), c.setLocal("pS", c.call("poseidon_applySMatrix", c.getLocal("t"), c.getLocal("pS"))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))), c.call("poseidon_power5all", c.getLocal("t")), c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))), c.call("poseidon_power5all", c.getLocal("t")), c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")), c.setLocal("i", c.i32_const(0)), c.block(c.loop(c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nOut"))), c.call("frm_copy", c.i32_add(c.i32_const(pState), c.i32_mul(c.getLocal("i"), c.i32_const(32))), c.i32_add(c.getLocal("pOut"), c.i32_mul(c.getLocal("i"), c.i32_const(32)))), c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))), c.br(0))));
|
125
|
+
}
|
126
|
+
buildAddConstant();
|
127
|
+
buildPower5();
|
128
|
+
buildPower5all();
|
129
|
+
buildApplyMatrix();
|
130
|
+
buildApplySMatrix();
|
131
|
+
buildPoseidon();
|
132
|
+
module.exportFunction("poseidon");
|
133
|
+
}
|
134
|
+
function buildPoseidon$2() {
|
135
|
+
return _buildPoseidon$.apply(this, arguments);
|
136
|
+
}
|
137
|
+
function _buildPoseidon$() {
|
138
|
+
_buildPoseidon$ = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
|
139
|
+
var bn128, F, pState, pIn, pOut, poseidon;
|
140
|
+
return _regenerator().w(function (_context) {
|
141
|
+
while (1) switch (_context.n) {
|
142
|
+
case 0:
|
143
|
+
_context.n = 1;
|
144
|
+
return ffjavascript.getCurveFromName("bn128", true, buildPoseidonWasm);
|
145
|
+
case 1:
|
146
|
+
bn128 = _context.v;
|
147
|
+
F = bn128.Fr;
|
148
|
+
pState = bn128.tm.alloc(32);
|
149
|
+
pIn = bn128.tm.alloc(32 * 16);
|
150
|
+
pOut = bn128.tm.alloc(32 * 17);
|
151
|
+
poseidon = function poseidon(arr, state, nOut) {
|
152
|
+
var buff;
|
153
|
+
var n;
|
154
|
+
if (Array.isArray(arr)) {
|
155
|
+
n = arr.length;
|
156
|
+
buff = new Uint8Array(n * 32);
|
157
|
+
for (var i = 0; i < n; i++) buff.set(F.e(arr[i]), i * 32);
|
158
|
+
} else {
|
159
|
+
buff = arr;
|
160
|
+
n = buff.byteLength / 32;
|
161
|
+
if (n * 32 != buff.byteLength) throw new Error("Invalid iput buff size");
|
162
|
+
}
|
163
|
+
bn128.tm.setBuff(pIn, buff);
|
164
|
+
if (n < 1 || n > 16) throw new Error("Invalid poseidon size");
|
165
|
+
if (typeof state == "undefined") {
|
166
|
+
state = F.zero;
|
167
|
+
} else {
|
168
|
+
state = F.e(state);
|
169
|
+
}
|
170
|
+
bn128.tm.setBuff(pState, state);
|
171
|
+
nOut = nOut || 1;
|
172
|
+
bn128.tm.instance.exports.poseidon(pState, pIn, n, pOut, nOut);
|
173
|
+
if (nOut == 1) {
|
174
|
+
return bn128.tm.getBuff(pOut, 32);
|
175
|
+
} else {
|
176
|
+
var out = [];
|
177
|
+
for (var _i = 0; _i < nOut; _i++) {
|
178
|
+
out.push(bn128.tm.getBuff(pOut + _i * 32, 32));
|
179
|
+
}
|
180
|
+
return out;
|
181
|
+
}
|
182
|
+
};
|
183
|
+
poseidon.F = F;
|
184
|
+
return _context.a(2, poseidon);
|
185
|
+
}
|
186
|
+
}, _callee);
|
187
|
+
}));
|
188
|
+
return _buildPoseidon$.apply(this, arguments);
|
189
|
+
}
|
190
|
+
function getHashes() {
|
191
|
+
return _getHashes.apply(this, arguments);
|
192
|
+
}
|
193
|
+
function _getHashes() {
|
194
|
+
_getHashes = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
|
195
|
+
var bn128, poseidon;
|
196
|
+
return _regenerator().w(function (_context2) {
|
197
|
+
while (1) switch (_context2.n) {
|
198
|
+
case 0:
|
199
|
+
_context2.n = 1;
|
200
|
+
return ffjavascript.getCurveFromName("bn128", true);
|
201
|
+
case 1:
|
202
|
+
bn128 = _context2.v;
|
203
|
+
_context2.n = 2;
|
204
|
+
return buildPoseidon$2();
|
205
|
+
case 2:
|
206
|
+
poseidon = _context2.v;
|
207
|
+
return _context2.a(2, {
|
208
|
+
hash0: function hash0(left, right) {
|
209
|
+
return poseidon([left, right]);
|
210
|
+
},
|
211
|
+
hash1: function hash1(key, value) {
|
212
|
+
return poseidon([key, value, bn128.Fr.one]);
|
213
|
+
},
|
214
|
+
F: bn128.Fr
|
215
|
+
});
|
216
|
+
}
|
217
|
+
}, _callee2);
|
218
|
+
}));
|
219
|
+
return _getHashes.apply(this, arguments);
|
220
|
+
}
|