pqp
Version:
NodeJS version of libPQP
173 lines (144 loc) • 4 kB
JavaScript
'use strict';
var _fftwJs = require('fftw-js');
var _cryptoJs = require('crypto-js');
var _cryptoJs2 = _interopRequireDefault(_cryptoJs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Float32Array.createZero = function (count) {
var array = new Float32Array(count);
array.fill(0);
return array;
};
Float32Array.prototype.add = function (y) {
var _this = this;
this.forEach(function (value, index) {
return _this[index] += y[index];
});
return this;
};
Float32Array.prototype.max = function () {
var max = this[0];
this.forEach(function (value) {
if (value > max) {
max = value;
}
});
return max;
};
Float32Array.prototype.fft = function () {
var count = this.length;
var fft = new _fftwJs.FFT(count);
var transform = Float32Array.from(fft.forward(this));
fft.dispose();
return transform;
};
function scale(array, scale) {
array.forEach(function (value, index) {
return array[index] *= scale;
});
}
Float32Array.prototype.ifft = function () {
var length = this.length;
var realLength = length - 2;
var fft = new _fftwJs.FFT(realLength);
var temp = Float32Array.from(this);
scale(temp, 1.0 / realLength);
var transform = fft.inverse(temp).slice(0, realLength);
fft.dispose();
return transform;
};
Float32Array.prototype.complexMultiply = function (other) {
var length = this.length;
var result = new Float32Array(length);
for (var idx = 0; idx < length - 1; idx = idx + 2) {
var a = this[idx];
var b = this[idx + 1];
var c = other[idx];
var d = other[idx + 1];
result[idx] = a * c - b * d;
result[idx + 1] = a * d + b * c;
}
if (length % 2 === 1) {
result[length - 1] = this[length - 1] * other[length - 1];
}
return result;
};
Float32Array.prototype.real = function () {
var count = this.length;
var result = this.filter(function (value, idx) {
return idx % 2 === 0 && idx < count - 1;
});
var half = Float32Array.from(result);
half = half.reverse().slice(0, count - result.length);
return result.concat(half);
};
Float32Array.prototype.image = function () {
return this.filter(function (value, idx) {
return idx % 2 === 1;
});
};
Float32Array.prototype.concat = function (second) {
var firstLength = this.length;
var result = new Float32Array(firstLength + second.length);
result.set(this);
result.set(second, firstLength);
return result;
};
Float32Array.prototype.round = function () {
return this.map(function (value) {
return Math.round(value);
});
};
Float32Array.prototype.mod = function (mod) {
return this.map(function (value) {
return value % mod;
});
};
Float32Array.prototype.nonzero = function () {
var indices = [];
this.forEach(function (value, index) {
if (value != 0) {
indices.push(index);
}
});
return Float32Array.from(indices);
};
Float32Array.prototype.multiply = function (number) {
return this.map(function (value) {
return value * number;
});
};
Float32Array.prototype.clone = function () {
return Float32Array.from(this);
};
Float32Array.prototype.toArray = function () {
return Array.prototype.slice.call(this);
};
Float32Array.prototype.toPrintString = function () {
var result = '[';
var count = this.length;
for (var idx = 0; idx < count; ++idx) {
if (idx !== count - 1) {
result += this[idx] + ', ';
} else {
result += this[idx] + ' ';
}
if (idx % 25 === 0 && idx > 0) {
result += '\n';
}
}
result += ']';
return result;
};
Float32Array.prototype.pack = function () {
var data = this.join('');
return _cryptoJs2.default.SHA512(data);
};
// String
String.prototype.toWordArray = function () {
var buffer = Buffer.from(this, 'utf8');
var array = new Uint8Array(buffer);
return _cryptoJs2.default.lib.WordArray.create(array);
};
String.prototype.appendWordArray = function (suffix) {
return this.toWordArray().concat(suffix);
};