poseidon-h
Version:
Poseidon hash with alt_bn128 implementation in Typescript
33 lines • 1.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.poseidon = exports.P = exports.randomFieldElement = void 0;
const constants_1 = require("./constants");
const operation_1 = require("./operation");
const validation_1 = require("./validation");
var ff_1 = require("./ff");
Object.defineProperty(exports, "randomFieldElement", { enumerable: true, get: function () { return ff_1.randomFieldElement; } });
Object.defineProperty(exports, "P", { enumerable: true, get: function () { return ff_1.P; } });
const poseidon = (inputs) => {
const { length } = inputs;
const t = length + 1;
const { p, m, c } = constants_1.constants[length];
try {
const circuitInputs = (0, validation_1.validateInputs)(inputs);
let state = [BigInt(0), ...circuitInputs];
for (let i = 0; i < constants_1.F + p; i++) {
for (let j = 0; j < t; j++) {
state[j] += c[i * t + j];
if (j === 0 || i < constants_1.HALF_F || i >= constants_1.HALF_F + p)
state[j] = (0, operation_1.pow5)(state[j]);
}
state = (0, operation_1.mix)(state, m);
}
return state[0];
}
catch (error) {
console.error(error);
throw error;
}
};
exports.poseidon = poseidon;
//# sourceMappingURL=index.js.map