From 9cc950fad37cb1473d485ea8512ebce617e2d65a Mon Sep 17 00:00:00 2001 From: juanelas Date: Wed, 8 Apr 2020 17:10:22 +0200 Subject: [PATCH] Examples of code --- examples/browser.esm.html | 54 ++++++++++++++++++++++++++++++++++++++ examples/browser.iife.html | 54 ++++++++++++++++++++++++++++++++++++++ examples/node.cjs.js | 36 +++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 examples/browser.esm.html create mode 100644 examples/browser.iife.html create mode 100644 examples/node.cjs.js diff --git a/examples/browser.esm.html b/examples/browser.esm.html new file mode 100644 index 0000000..0dcb3ec --- /dev/null +++ b/examples/browser.esm.html @@ -0,0 +1,54 @@ + + + + + bigint-crypto-utils + + + + +
Still computing in background...
+

Look for the results in the JS console (Developer Tools)

+ + + + \ No newline at end of file diff --git a/examples/browser.iife.html b/examples/browser.iife.html new file mode 100644 index 0000000..a9faa31 --- /dev/null +++ b/examples/browser.iife.html @@ -0,0 +1,54 @@ + + + + + bigint-crypto-utils + + + + + +
Still computing in background...
+

Look for the results in the JS console (Developer Tools)

+ + + + \ No newline at end of file diff --git a/examples/node.cjs.js b/examples/node.cjs.js new file mode 100644 index 0000000..54f3de2 --- /dev/null +++ b/examples/node.cjs.js @@ -0,0 +1,36 @@ +const bigintCryptoUtils = require('../lib/index.node') +// const bigintCryptoUtils = require('bigint-crypto-utils') + +/* A BigInt with value 666 can be declared calling the bigint constructor as +BigInt('666') or with the shorter 666n. +Notice that you can also pass a number to the constructor, e.g. BigInt(666). +However, it is not recommended since values over 2**53 - 1 won't be safe but +no warning will be raised. +*/ +const a = BigInt('5') +const b = BigInt('2') +const n = 19n + +console.log(bigintCryptoUtils.modPow(a, b, n)) // prints 6 + +console.log(bigintCryptoUtils.modInv(2n, 5n)) // prints 3 + +console.log(bigintCryptoUtils.modInv(BigInt('3'), BigInt('5'))) // prints 2 + +console.log(bigintCryptoUtils.randBetween(2n ** 256n)) // Prints a cryptographically secure random number between 1 and 2**256 bits. + +async function primeTesting () { + // Output of a probable prime of 2048 bits + console.log(await bigintCryptoUtils.prime(2048)) + + // Testing if a number is a probable prime (Miller-Rabin) + const number = 13139188972124309083000292697519085211422620620787723340749020496498012413131881656428777288953095338604061035790562501399090389032827482643578651715752317n + const isPrime = await bigintCryptoUtils.isProbablyPrime(number) + if (isPrime) { + console.log(`${number} is prime`) + } else { + console.log(`${number} is composite`) + } +} + +primeTesting()