UNPKG

snarkjs

Version:

zkSNARKs implementation in JavaScript

59 lines (46 loc) 1.94 kB
/* Copyright 2022 iden3 association. This file is part of snarkjs. snarkjs is a free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. snarkjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with snarkjs. If not, see <https://www.gnu.org/licenses/>. */ import {BigBuffer} from "ffjavascript"; export class Evaluations { constructor(evaluations, curve, logger) { this.eval = evaluations; this.curve = curve; this.Fr = curve.Fr; this.logger = logger; } static async fromPolynomial(polynomial, extension, curve, logger) { const coefficientsN = new BigBuffer(polynomial.length() * extension * curve.Fr.n8); coefficientsN.set(polynomial.coef, 0); const evaluations = await curve.Fr.fft(coefficientsN); return new Evaluations(evaluations, curve, logger); } getEvaluation(index) { const i_n8 = index * this.Fr.n8; if (i_n8 + this.Fr.n8 > this.eval.byteLength) { throw new Error("Evaluations.getEvaluation() out of bounds"); } return this.eval.slice(i_n8, i_n8 + this.Fr.n8); } length() { let length = this.eval.byteLength / this.Fr.n8; if (length !== Math.floor(this.eval.byteLength / this.Fr.n8)) { throw new Error("Polynomial evaluations buffer has incorrect size"); } if (0 === length) { this.logger.warn("Polynomial has length zero"); } return length; } }