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F,kBAAe,eAAe,CAAC","sourcesContent":["import { createCrypto } from './crypto';\nimport { rootRealmGlobal } from '../globalObject';\n\n/**\n * Create a {@link Math} object, with the same properties as the global\n * {@link Math} object, but with the {@link Math.random} method replaced.\n *\n * @returns The {@link Math} object with the {@link Math.random} method\n * replaced.\n */\nfunction createMath() {\n  // `Math` does not work with `Object.keys`, `Object.entries`, etc., so we\n  // need to create a new object with the same properties.\n  const keys = Object.getOwnPropertyNames(\n    rootRealmGlobal.Math,\n  ) as (keyof typeof Math)[];\n\n  const math = keys.reduce<Partial<Math>>((target, key) => {\n    if (key === 'random') {\n      return target;\n    }\n\n    return { ...target, [key]: rootRealmGlobal.Math[key] };\n  }, {});\n\n  // Since the math endowment requires crypto, we can leverage the crypto endowment factory to get a hardened and platform agnostic instance of webcrypto\n  const { crypto: hardenedCrypto } = createCrypto();\n\n  return harden({\n    Math: {\n      ...math,\n      random: () => {\n        // NOTE: This is not intended to be a secure replacement for the weak\n        // random number generator used by `Math.random`. It is only intended to\n        // prevent side channel attacks of `Math.random` by replacing it with an\n        // alternative implementation that is not vulnerable to the same\n        // attacks.\n        //\n        // This does not mean that this implementation is secure. It is not\n        // intended to be used in a security context, and this implementation\n        // may change at any time.\n        //\n        // To securely generate random numbers, use a cryptographically secure\n        // random number generator, such as the one provided by the Web Crypto\n        // API:\n        //\n        // - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey\n        // - https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues\n        return hardenedCrypto.getRandomValues(new Uint32Array(1))[0] / 2 ** 32;\n      },\n    },\n  });\n}\n\nconst endowmentModule = {\n  names: ['Math'] as const,\n  factory: createMath,\n};\n\nexport default endowmentModule;\n"]}