From b4a65a184556e43d2fe2273f8c53e505b074bc0b Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Tue, 1 Aug 2023 21:07:15 +0200 Subject: [PATCH] secure-random-js: upgrade sigmajsCryptoFacadeVersion = "0.0.7" and implement SecureRandom for js Platform --- build.sbt | 2 +- .../scala/sigmastate/crypto/Platform.scala | 4 ++-- .../crypto/SigmaJsCryptoFacade.scala | 20 +++++++++++++++++++ .../OracleExamplesSpecification.scala | 4 ++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index 7053fd6555..371536017e 100644 --- a/build.sbt +++ b/build.sbt @@ -186,7 +186,7 @@ lazy val commonDependenies2 = libraryDependencies ++= Seq( "org.scala-lang.modules" %%% "scala-collection-compat" % "2.7.0" ) -val sigmajsCryptoFacadeVersion = "0.0.6" +val sigmajsCryptoFacadeVersion = "0.0.7" lazy val common = crossProject(JVMPlatform, JSPlatform) .in(file("common")) diff --git a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala b/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala index 780b74f9a0..1b963331f2 100644 --- a/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala +++ b/interpreter/js/src/main/scala/sigmastate/crypto/Platform.scala @@ -120,7 +120,7 @@ object Platform { class Curve // TODO JS: Use JS library for secure source of randomness - type SecureRandom = Random + type SecureRandom = sigmastate.crypto.SecureRandomJS /** Opaque point type. */ @js.native @@ -198,7 +198,7 @@ object Platform { } /** Create JS specific source of secure randomness. */ - def createSecureRandom(): Random = new Random() + def createSecureRandom(): SecureRandom = new SecureRandomJS /** Computes HMAC-SHA512 hash of the given data using the specified key. * diff --git a/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala b/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala index 3b19095b6a..908d6ee166 100644 --- a/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala +++ b/interpreter/js/src/main/scala/sigmastate/crypto/SigmaJsCryptoFacade.scala @@ -1,5 +1,7 @@ package sigmastate.crypto +import debox.cfor + import scala.scalajs.js import scala.scalajs.js.annotation.JSImport import scala.scalajs.js.typedarray.Uint8Array @@ -109,6 +111,24 @@ object CryptoFacadeJs extends js.Object { def generatePbkdf2Key( normalizedMnemonic: String, normalizedPass: String): Uint8Array = js.native + + /** Creates a random array of bytes of the given length. */ + def getRandomBytes(length: Int): Uint8Array = js.native +} + +class SecureRandomJS { + /** + * Generates a user-specified number of random bytes. + * + * @param bytes the array to be filled in with random bytes. + */ + def nextBytes(bytes: Array[Byte]): Unit = { + val len = bytes.length + val arr = CryptoFacadeJs.getRandomBytes(len) + cfor(0)(_ < len, _ + 1) { i => + bytes(i) = arr(i).toByte + } + } } /** Represents imported Point class from `sigmajs-crypto-facade` JS libarary. */ diff --git a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala index 5dabe5114a..1e7d4b45b6 100644 --- a/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala +++ b/sc/shared/src/test/scala/sigmastate/utxo/examples/OracleExamplesSpecification.scala @@ -15,7 +15,7 @@ import sigmastate.helpers.TestingHelpers._ import org.ergoplatform._ import org.ergoplatform.dsl.{ContractSpec, SigmaContractSyntax, StdContracts, TestContractSpec} import sigmastate.basics.CryptoConstants -import sigmastate.crypto.CryptoFacade +import sigmastate.crypto.{BigIntegers, CryptoFacade} import sigmastate.eval.Extensions.ArrayOps import sigmastate.interpreter.Interpreter.{ScriptNameProp, emptyEnv} import sigmastate.utxo._ @@ -89,7 +89,7 @@ class OracleExamplesSpecification extends CompilerTestingCommons val temperature: Long = 18 - val r = BigInt.apply(128, CryptoFacade.createSecureRandom()) //128 bits random number + val r = BigInt(BigIntegers.createRandomBigInteger(128, CryptoFacade.createSecureRandom())) //128 bits random number val a = group.exponentiate(group.generator, r.bigInteger) val ts = System.currentTimeMillis()