mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 10:46:42 +01:00
Make Gen[ECPrivateKey]
in scalacheck to use randomness from scalacheck to preserve reproducibility of test cases (#5285)
* Make Gen[ECPrivateKey] in scalacheck to use randomness from scalacheck to preserve reproducibility of test cases * Revert build.sbt
This commit is contained in:
parent
665727f165
commit
579318cad0
2 changed files with 26 additions and 6 deletions
|
@ -8,7 +8,16 @@ import scodec.bits.ByteVector
|
|||
*/
|
||||
sealed abstract class CryptoGenerators {
|
||||
|
||||
def privateKey: Gen[ECPrivateKey] = Gen.delay(ECPrivateKey())
|
||||
def privateKey: Gen[ECPrivateKey] = {
|
||||
//purposefully don't reach for cryptographically strong
|
||||
//number generation, we want determinism to reproduce failed
|
||||
//test cases. If we don't generate the private key with scalacheck
|
||||
//we won't be able to reproduce the test case with a seed
|
||||
//see: https://github.com/bitcoin-s/bitcoin-s/issues/1339
|
||||
NumberGenerator.bytevector(32).map { vec =>
|
||||
ECPrivateKey.fromBytes(vec)
|
||||
}
|
||||
}
|
||||
|
||||
def fieldElement: Gen[FieldElement] = privateKey.map(_.fieldElement)
|
||||
|
||||
|
@ -169,9 +178,20 @@ sealed abstract class CryptoGenerators {
|
|||
hash = CryptoUtil.sha256Hash160(pubKey.bytes)
|
||||
} yield hash
|
||||
|
||||
def aesKey128Bit: Gen[AesKey] = Gen.delay(AesKey.get128Bit())
|
||||
def aesKey192Bit: Gen[AesKey] = Gen.delay(AesKey.get192Bit())
|
||||
def aesKey256Bit: Gen[AesKey] = Gen.delay(AesKey.get256Bit())
|
||||
def aesKey128Bit: Gen[AesKey] = {
|
||||
val bytesGen = NumberGenerator.bytevector(16)
|
||||
bytesGen.map(AesKey.fromValidBytes(_))
|
||||
}
|
||||
|
||||
def aesKey192Bit: Gen[AesKey] = {
|
||||
val bytesGen = NumberGenerator.bytevector(24)
|
||||
bytesGen.map(AesKey.fromValidBytes(_))
|
||||
}
|
||||
|
||||
def aesKey256Bit: Gen[AesKey] = {
|
||||
val bytesGen = NumberGenerator.bytevector(32)
|
||||
bytesGen.map(AesKey.fromValidBytes(_))
|
||||
}
|
||||
|
||||
def aesKey: Gen[AesKey] =
|
||||
Gen.oneOf(aesKey128Bit, aesKey192Bit, aesKey256Bit)
|
||||
|
|
|
@ -79,9 +79,9 @@ sealed abstract class ScriptGenerators {
|
|||
numKeys <- Gen.choose(1, Consensus.maxPublicKeysPerMultiSig)
|
||||
hash <- CryptoGenerators.doubleSha256Digest
|
||||
hashType <- CryptoGenerators.hashType
|
||||
privKeys <- CryptoGenerators.privateKeySeq(numKeys)
|
||||
} yield for {
|
||||
_ <- 0 until numKeys
|
||||
privKey = ECPrivateKey()
|
||||
privKey <- privKeys
|
||||
} yield ECDigitalSignature.fromBytes(
|
||||
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte))
|
||||
signatures.map(sigs => MultiSignatureScriptSignature(sigs))
|
||||
|
|
Loading…
Add table
Reference in a new issue