UNPKG

pqp

Version:
60 lines (50 loc) 1.43 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.mul_poly = mul_poly; exports.square_sparse_poly = square_sparse_poly; exports.exp_poly = exp_poly; require('../utils/polyfill'); var _randomgen = require('./randomgen'); function mul_poly(x, y) { var length = x.length; x = x.fft(); y = y.fft(); var temp = x.complexMultiply(y).ifft(length).round().mod(2); return temp; } function square_sparse_poly(x) { var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1; var indices = Float32Array.from(x.nonzero()); var mod = x.length; var result = (0, _randomgen.zeroArray)(mod); indices = indices.multiply(Math.pow(2, times) % mod); indices.forEach(function (index) { var idx = index % mod; result[idx] = result[idx] ^ 1; }); return result; } function exp_poly(x, n) { var length = x.length; var y = (0, _randomgen.zeroArray)(length); y[0] = 1; while (n.gt(1)) { if (n.mod(2) == 0) { x = square_sparse_poly(x); n = n.div(2); } else { // precision does not allow us to stay in FFT domain // hence, interchanging ifft(fft). var X = x.clone().fft(); var Y = y.clone().fft(); var temp = X.complexMultiply(Y).ifft(length); y = temp.round().mod(2); x = square_sparse_poly(x); n = n.sub(1); n = n.div(2); } } return mul_poly(x, y).round().mod(2); }