From 7de3cfcefa3b589ccdce0adae3bfd5922fec8567 Mon Sep 17 00:00:00 2001 From: Paul Shapiro Date: Tue, 4 Sep 2018 15:06:31 -0400 Subject: [PATCH] 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 --- cryptonote_utils/cryptonote_utils.js | 5 ---- monero_utils/monero_sendingFunds_utils.js | 35 +---------------------- 2 files changed, 1 insertion(+), 39 deletions(-) diff --git a/cryptonote_utils/cryptonote_utils.js b/cryptonote_utils/cryptonote_utils.js index c5129ce..746782a 100644 --- a/cryptonote_utils/cryptonote_utils.js +++ b/cryptonote_utils/cryptonote_utils.js @@ -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) diff --git a/monero_utils/monero_sendingFunds_utils.js b/monero_utils/monero_sendingFunds_utils.js index 8aa267c..2dfa2c9 100644 --- a/monero_utils/monero_sendingFunds_utils.js +++ b/monero_utils/monero_sendingFunds_utils.js @@ -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);