removed Math.random replacement with throw pending merge of https://github.com/kripken/emscripten/pull/7096 and replaced Math.random in list rand index in monero_sendingFunds_utils

pull/41/head
Paul Shapiro 6 years ago
parent 1cd6a1dfe6
commit 7de3cfcefa

@ -34,11 +34,6 @@
const JSBigInt = require("./biginteger").BigInteger;
const nettype_utils = require("./nettype");
//
Math.random = function()
{
throw "Math.random calls are disallowed (cryptonote_utils) until emscripten has support to override fallback when (!crypto) - see randomFloat_unit() or remove this in fork";
}
//
var cnUtil = function(currencyConfig)
{
const currency_amount_format_utils = require("../cryptonote_utils/money_format_utils")(currencyConfig)

@ -798,41 +798,8 @@ function new_moneroReadyTargetDescriptions_fromTargetDescriptions(
},
);
}
//
function randomFloat_unit()
{ // https://stackoverflow.com/questions/34575635/cryptographically-secure-float?answertab=oldest#tab-top
// I've produced this function to replace Math.random, which we are black-holing to prevent emscripten from ever being able to call it (not that it is)
let buffer = new ArrayBuffer(8); // A buffer with just the right size to convert to Float64
let ints = new Int8Array(buffer); // View it as an Int8Array and fill it with 8 random ints
window.crypto.getRandomValues(ints);
//
// Set the sign (ints[7][7]) to 0 and the
// exponent (ints[7][6]-[6][5]) to just the right size
// (all ones except for the highest bit)
ints[7] = 63;
ints[6] |= 0xf0;
//
// Now view it as a Float64Array, and read the one float from it
let float = new DataView(buffer).getFloat64(0, true) - 1;
//
return float;
// Probably another way of doing this if the above is not good enough:
/*
if (typeof crypto !== "undefined") {
var randomBuffer = new Uint8Array(1);
crypto.getRandomValues(randomBuffer);
//
return randomBuffer[0] / 256.0;
} else if (ENVIRONMENT_IS_NODE) {
return require("crypto")["randomBytes"](1)[0] / 256.0
} else {
throw "Unable to support randomFloat_unit without window.crypto or a \"crypto\" module"
}
*/
}
function __randomIndex(list) {
return Math.floor(randomFloat_unit() * list.length);
return Math.floor(Math.random() * list.length);
}
function _popAndReturnRandomElementFromList(list) {
var idx = __randomIndex(list);

Loading…
Cancel
Save