Added PubKey addition functionality (#1051)

This commit is contained in:
Nadav Kohen 2020-01-21 14:30:28 -07:00 committed by GitHub
parent 17c21b3bac
commit 512b23ba63
2 changed files with 25 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package org.bitcoins.core.crypto
import org.bitcoin.NativeSecp256k1
import org.bitcoins.testkit.core.gen.CryptoGenerators
import org.bitcoins.testkit.util.BitcoinSUnitTest
import scodec.bits._
@ -35,4 +36,17 @@ class ECPublicKeyTest extends BitcoinSUnitTest {
assert(pubKey == pub2)
}
}
it must "add keys correctly" in {
forAll(CryptoGenerators.publicKey, CryptoGenerators.privateKey) {
case (pubKey, privKey) =>
val sumKeyBytes = NativeSecp256k1.pubKeyTweakAdd(pubKey.bytes.toArray,
privKey.bytes.toArray,
true)
val sumKeyExpected = ECPublicKey.fromBytes(ByteVector(sumKeyBytes))
val sumKey = pubKey.add(privKey.publicKey)
assert(sumKey == sumKeyExpected)
}
}
}

View file

@ -347,6 +347,17 @@ sealed abstract class ECPublicKey extends BaseECKey {
def toPoint: ECPoint = {
CryptoParams.curve.getCurve.decodePoint(bytes.toArray)
}
/** Adds this ECPublicKey to another as points and returns the resulting ECPublicKey.
*
* Note: if this ever becomes a bottleneck, secp256k1_ec_pubkey_combine should
* get wrapped in NativeSecp256k1 to speed things up.
*/
def add(otherKey: ECPublicKey): ECPublicKey = {
val sumPoint = toPoint.add(otherKey.toPoint)
ECPublicKey.fromPoint(sumPoint)
}
}
object ECPublicKey extends Factory[ECPublicKey] {