mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 18:47:38 +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 {
|
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)
|
def fieldElement: Gen[FieldElement] = privateKey.map(_.fieldElement)
|
||||||
|
|
||||||
|
@ -169,9 +178,20 @@ sealed abstract class CryptoGenerators {
|
||||||
hash = CryptoUtil.sha256Hash160(pubKey.bytes)
|
hash = CryptoUtil.sha256Hash160(pubKey.bytes)
|
||||||
} yield hash
|
} yield hash
|
||||||
|
|
||||||
def aesKey128Bit: Gen[AesKey] = Gen.delay(AesKey.get128Bit())
|
def aesKey128Bit: Gen[AesKey] = {
|
||||||
def aesKey192Bit: Gen[AesKey] = Gen.delay(AesKey.get192Bit())
|
val bytesGen = NumberGenerator.bytevector(16)
|
||||||
def aesKey256Bit: Gen[AesKey] = Gen.delay(AesKey.get256Bit())
|
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] =
|
def aesKey: Gen[AesKey] =
|
||||||
Gen.oneOf(aesKey128Bit, aesKey192Bit, aesKey256Bit)
|
Gen.oneOf(aesKey128Bit, aesKey192Bit, aesKey256Bit)
|
||||||
|
|
|
@ -79,9 +79,9 @@ sealed abstract class ScriptGenerators {
|
||||||
numKeys <- Gen.choose(1, Consensus.maxPublicKeysPerMultiSig)
|
numKeys <- Gen.choose(1, Consensus.maxPublicKeysPerMultiSig)
|
||||||
hash <- CryptoGenerators.doubleSha256Digest
|
hash <- CryptoGenerators.doubleSha256Digest
|
||||||
hashType <- CryptoGenerators.hashType
|
hashType <- CryptoGenerators.hashType
|
||||||
|
privKeys <- CryptoGenerators.privateKeySeq(numKeys)
|
||||||
} yield for {
|
} yield for {
|
||||||
_ <- 0 until numKeys
|
privKey <- privKeys
|
||||||
privKey = ECPrivateKey()
|
|
||||||
} yield ECDigitalSignature.fromBytes(
|
} yield ECDigitalSignature.fromBytes(
|
||||||
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte))
|
privKey.sign(hash).bytes ++ ByteVector.fromByte(hashType.byte))
|
||||||
signatures.map(sigs => MultiSignatureScriptSignature(sigs))
|
signatures.map(sigs => MultiSignatureScriptSignature(sigs))
|
||||||
|
|
Loading…
Add table
Reference in a new issue