Skip to content

Commit

Permalink
secure-random-js: upgrade sigmajsCryptoFacadeVersion = "0.0.7" and im…
Browse files Browse the repository at this point in the history
…plement SecureRandom for js Platform
  • Loading branch information
aslesarenko committed Aug 1, 2023
1 parent 1c7b9dc commit b4a65a1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b4a65a1

Please sign in to comment.