mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-22 14:33:06 +01:00
Added PubKey addition functionality (#1051)
This commit is contained in:
parent
17c21b3bac
commit
512b23ba63
2 changed files with 25 additions and 0 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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] {
|
||||
|
|
Loading…
Add table
Reference in a new issue