Interface Secp256k1
- All Superinterfaces:
AutoCloseable, Closeable
- All Known Implementing Classes:
Secp256k1Foreign
Main interface providing Elliptic Curve Cryptography functions using the SECG curve.
secp256k1.
The API is based on the C-language API of libsecp256k1, but
is here adapted to modern, idiomatic, functional-style Java and use Elliptic Curve types from the Java Class Library,
such as ECPublicKey via the specialized SecpPubKey subclass.
Two implementations are being developed.
-
Module
org.bitcoinj.secp.ffm: Using a Java Foreign Function and Memory API interface to the secp256k1 library. -
Module
org.bitcoinj.secp.bouncy: Using the Bouncy Castle Java library.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceProvider interface for implementations ofSecp256k1.static enumStandard, well-known providers -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final EllipticCurveThe curve definition for secp256k1 using the standard Java typeEllipticCurve.static final ECParameterSpecThe secp256k1 domain parameters definition using the standard Java typestatic final ECFieldFpThe secp256k1 field definitionpusing the standard Java typestatic final SecpPoint.UncompressedThe generator pointG(also known as base point) for secp256k1.static final SecpFieldElementEqual toEC_PARAMS.getOrder().shiftRight(1), used for canonicalizing the S value of a signature.static final BigIntegerThe primeN, that represents the order of the generator point, i.e. the number of points on the curve.static final BigIntegerThe primeP, that defines the secp256k1 field. -
Method Summary
Modifier and TypeMethodDescriptionstatic Stream<Secp256k1.Provider> all()Get a stream of all known providersvoidclose()Override close and declare that no checked exceptions are thrownecdh(SecpPubKey pubKey, SecpPrivKey privKey) ECDH key agreementecdsaSign(byte[] msg_hash_data, SecpPrivKey privKey) Sign a message hash using the ECDSA algorithmecdsaSignatureParseCompact(byte[] serialized_signature) Parse a Bitcoin compact signature.byte[]Serialize aEcdsaSignatureas a Bitcoin compact signature.ecdsaSignLowR(byte[] messageHashData, SecpPrivKey privKey) Sign a message hash using the ECDSA algorithm and Low-R signature gridingecdsaVerify(EcdsaSignature sig, byte[] msg_hash_data, SecpPubKey pubKey) Verify an ECDSA signature.Create a new, randomly-generated private key and return it with its matching public keyecKeyPairCreate(SecpPrivKey privKey) Create a key pair structure from a known private keydefault SecpPoint.UncompressedecPointUncompress(SecpPoint.Compressed compressedPoint) Calculate an uncompressed point from a compressed point.Create a new, randomly-generated private key.Combine two public keys by adding them.ecPubKeyCreate(SecpPrivKey privKey) Create a public key from the given private key.default SecpPubKeyecPubKeyFromXOnly(SecpXOnlyPubKey xOnlyPubKey) Convert an x-only public key to a (uncompressed point) public keyecPubKeyParse(byte[] inputData) Parse a byte array as a public keybyte[]ecPubKeySerialize(SecpPubKey pubKey, int flags) Serialize a public keyecPubKeyTweakMul(SecpPoint.Uncompressed pubKey, BigInteger scalarMultiplier) Multiply a public key by a scalar, this is known as key "tweaking"static Stream<Secp256k1.Provider> findAll(Predicate<Secp256k1.Provider> filter) Get a stream of all providers that match a filterstatic Secp256k1get()Get the default implementationstatic Secp256k1Get implementation by IDstatic Secp256k1getById(Secp256k1.ProviderId idEnum) Get implementation by ID enumschnorrSigSign32(byte[] msg_hash, SecpPrivKey privKey) Create a Schnorr signature for a message.schnorrSigSign32(byte[] messageHash, SecpPrivKey privKey, byte[] auxiliaryRandom) Create a Schnorr signature for a message.default SecpResult<Boolean> schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpPubKey pubKey) Verify a Schnorr signature.schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpXOnlyPubKey pubKey) Verify a Schnorr signature.byte[]taggedSha256(byte[] tag, byte[] message) Generate a tagged SHA-256 hash.default byte[]taggedSha256(String tag, String message) Generate a tagged SHA-256 hash.xOnlyPubKeyParse(byte[] inputData) Parse a byte array as an x-only public key
-
Field Details
-
P
The primeP, that defines the secp256k1 field.P = 2²⁵⁶ − 2³² − 2⁹ − 2⁸ − 2⁷ − 2⁶ − 2⁴ − 1Note that since the maximum valid value of a field element is
P - 1, this constant cannot be represented as aSecpFieldElement, so we useBigIntegerinstead. -
N
The primeN, that represents the order of the generator point, i.e. the number of points on the curve. -
G
The generator pointG(also known as base point) for secp256k1. -
FIELD
The secp256k1 field definitionpusing the standard Java type -
CURVE
The curve definition for secp256k1 using the standard Java typeEllipticCurve.The curve equation is
y² = x³ + ax + b, where:a = 0,b = 7, resulting in:y² = x³ + 7 -
EC_PARAMS
The secp256k1 domain parameters definition using the standard Java type -
HALF_CURVE_ORDER
Equal toEC_PARAMS.getOrder().shiftRight(1), used for canonicalizing the S value of a signature. If you aren't sure what this is about, you can ignore it.
-
-
Method Details
-
all
Get a stream of all known providers- Returns:
- stream of all known providers
-
findAll
Get a stream of all providers that match a filter- Parameters:
filter- filter function to select providers- Returns:
- stream of matching providers
-
ecPrivKeyCreate
SecpPrivKey ecPrivKeyCreate()Create a new, randomly-generated private key.- Returns:
- the private key
-
ecPubKeyCreate
Create a public key from the given private key.- Parameters:
privKey- the private key- Returns:
- derived public key
-
ecKeyPairCreate
SecpKeyPair ecKeyPairCreate()Create a new, randomly-generated private key and return it with its matching public key- Returns:
- newly generated key pair
-
ecKeyPairCreate
Create a key pair structure from a known private key- Parameters:
privKey- the private key- Returns:
- object containing both public and private key
-
ecPubKeyTweakMul
Multiply a public key by a scalar, this is known as key "tweaking"- Parameters:
pubKey- public key representing a point on the curvescalarMultiplier- scalar multiplier- Returns:
- the product
-
ecPubKeyCombine
Combine two public keys by adding them.- Parameters:
key1- first keykey2- second key- Returns:
- the sum
-
ecPubKeySerialize
Serialize a public key- Parameters:
pubKey- public key to serializeflags- serialization flags- Returns:
- pubKey serialized as a byte array
-
ecPointUncompress
Calculate an uncompressed point from a compressed point.- Parameters:
compressedPoint- a compressed point- Returns:
- The same point, in uncompressed format
-
ecPubKeyParse
Parse a byte array as a public key- Parameters:
inputData- raw data to parse as public key- Returns:
- public key result or error
-
ecPubKeyFromXOnly
Convert an x-only public key to a (uncompressed point) public key- Parameters:
xOnlyPubKey- x-only public key- Returns:
- public key
-
xOnlyPubKeyParse
Parse a byte array as an x-only public key- Parameters:
inputData- raw data to parse as an x-only public key- Returns:
- an x-only public key result or error
-
ecdsaSign
Sign a message hash using the ECDSA algorithm- Parameters:
msg_hash_data- 32-byte hash of message to signprivKey- private key- Returns:
- the signature
-
ecdsaSignLowR
Sign a message hash using the ECDSA algorithm and Low-R signature gridingThis uses the same nonce-incrementing algorithm as Bitcoin Core to ensure a low-R signature.
- Parameters:
messageHashData- 32-byte hash of message to signprivKey- private key- Returns:
- the signature
-
ecdsaSignatureSerializeCompact
Serialize aEcdsaSignatureas a Bitcoin compact signature. A compact signature is the two signature component field integers (known asrands) serialized in-order as binary data in big-endian format.- Parameters:
sig- signature object- Returns:
- compact signature bytes
-
ecdsaSignatureParseCompact
Parse a Bitcoin compact signature. A compact signature is the two signature component field integers (known asrands) serialized in-order as binary data in big-endian format.- Parameters:
serialized_signature- compact signature bytes- Returns:
- signature object
-
ecdsaVerify
Verify an ECDSA signature.- Parameters:
sig- The signature to verify.msg_hash_data- A 32-byte hash of the message to verify.pubKey- The pubkey that must have signed the message- Returns:
- true, false, or error
-
taggedSha256
-
taggedSha256
byte[] taggedSha256(byte[] tag, byte[] message) Generate a tagged SHA-256 hash.- Parameters:
tag- a tag specifying the context of usagemessage- the message itself- Returns:
- the SHA-256 HASH
-
schnorrSigSign32
Create a Schnorr signature for a message.- Parameters:
msg_hash- a hash of a message to signprivKey- private key for signing- Returns:
- the signature
-
schnorrSigSign32
Create a Schnorr signature for a message.- Parameters:
messageHash- a hash of a message to signprivKey- private key for signingauxiliaryRandom- auxiliary randomness (typically from a test vector)- Returns:
- the signature
-
schnorrSigVerify
SecpResult<Boolean> schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpXOnlyPubKey pubKey) Verify a Schnorr signature.- Parameters:
signature- the signature to verifymsg_hash- hash of the messagepubKey- x-only pubkey that must have signed the message- Returns:
- true, false, or error
-
schnorrSigVerify
default SecpResult<Boolean> schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpPubKey pubKey) Verify a Schnorr signature.- Parameters:
signature- the signature to verifymsg_hash- hash of the messagepubKey- pubkey that must have signed the message- Returns:
- true, false, or error
-
ecdh
ECDH key agreement- Parameters:
pubKey- pubkey of the other partyprivKey- private key- Returns:
- ecdh key agreement
-
close
void close()Override close and declare that no checked exceptions are thrown- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
get
Get the default implementation- Returns:
- A Secp256k1 instance using the default implementation
-
getById
-
getById
Get implementation by ID enum- Parameters:
idEnum- implementation ID enum- Returns:
- A Secp256k1 instance using the default implementation
-