Add more tests to bump test coverage (#345)

This commit is contained in:
Chris Stewart 2019-02-19 10:34:23 -06:00 committed by GitHub
parent 8a491022fa
commit 7633bf6179
3 changed files with 53 additions and 13 deletions

View File

@ -61,8 +61,7 @@ class ECDigitalSignatureTest extends BitcoinSUnitTest {
}
it must "must create and verify a digital signature" in {
forAll(CryptoGenerators.doubleSha256Digest,
CryptoGenerators.privateKey) {
forAll(CryptoGenerators.doubleSha256Digest, CryptoGenerators.privateKey) {
case (hash, key) =>
val sig = key.sign(hash)
assert(key.publicKey.verify(hash, sig))
@ -71,8 +70,8 @@ class ECDigitalSignatureTest extends BitcoinSUnitTest {
it must "must not reuse r values" in {
forAll(CryptoGenerators.privateKey,
CryptoGenerators.doubleSha256Digest,
CryptoGenerators.doubleSha256Digest) {
CryptoGenerators.doubleSha256Digest,
CryptoGenerators.doubleSha256Digest) {
case (key, hash1, hash2) =>
val sig1 = key.sign(hash1)
val sig2 = key.sign(hash2)
@ -99,4 +98,14 @@ class ECDigitalSignatureTest extends BitcoinSUnitTest {
}
}
it must "be able to generate valid signatures with bouncy castle" in {
forAll(CryptoGenerators.privateKey, CryptoGenerators.sha256Digest) {
case (privKey: ECPrivateKey, hash: Sha256Digest) =>
val sig = privKey.signWithBouncyCastle(hash.bytes)
val pubKey = privKey.publicKey
assert(pubKey.verify(hash, sig))
}
}
}

View File

@ -0,0 +1,36 @@
package org.bitcoins.core.crypto
import org.bitcoins.core.gen.CryptoGenerators
import org.bitcoins.core.util.BitcoinSUnitTest
import scodec.bits.ByteVector
import scala.concurrent.{ExecutionContext, Future}
class SignTest extends BitcoinSUnitTest {
implicit val ec = ExecutionContext.global
//ECPrivateKey implements the sign interface
//so just use it for testing purposes
val signTestImpl = new Sign {
private val key = ECPrivateKey.freshPrivateKey
override def signFunction: ByteVector => Future[ECDigitalSignature] = {
key.signFunction
}
override def publicKey: ECPublicKey = key.publicKey
}
behavior of "Sign"
it must "sign arbitrary pieces of data correctly" in {
forAll(CryptoGenerators.sha256Digest) {
case hash: Sha256Digest =>
val pubKey = signTestImpl.publicKey
val sigF = signTestImpl.signFunction(hash.bytes)
sigF.map(sig => assert(pubKey.verify(hash.hex, sig)))
}
}
}

View File

@ -62,15 +62,11 @@ sealed abstract class BaseECKey extends NetworkElement with Sign {
implicit ec: ExecutionContext): Future[ECDigitalSignature] =
Future(sign(hash))
@deprecated("Deprecated in favor of signing algorithm inside of secp256k1",
"2/20/2017")
private def oldSign(
dataToSign: ByteVector,
signingKey: BaseECKey): ECDigitalSignature = {
def signWithBouncyCastle(dataToSign: ByteVector): ECDigitalSignature = {
val signer: ECDSASigner = new ECDSASigner(
new HMacDSAKCalculator(new SHA256Digest()))
val privKey: ECPrivateKeyParameters = new ECPrivateKeyParameters(
new BigInteger(1, signingKey.bytes.toArray),
new BigInteger(1, bytes.toArray),
CryptoParams.curve)
signer.init(true, privKey)
val components: Array[BigInteger] =
@ -136,9 +132,8 @@ object ECPrivateKey extends Factory[ECPrivateKey] {
isCompressed: Boolean,
ec: ExecutionContext)
extends ECPrivateKey {
require(
NativeSecp256k1.secKeyVerify(bytes.toArray),
s"Invalid key according to secp256k1, hex: ${bytes.toHex}")
require(NativeSecp256k1.secKeyVerify(bytes.toArray),
s"Invalid key according to secp256k1, hex: ${bytes.toHex}")
}
def apply(bytes: ByteVector, isCompressed: Boolean)(