Interface Secp256k1
- All Superinterfaces:
AutoCloseable, Closeable
- All Known Implementing Classes:
Bouncy256k1, 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 secp256k1 curve definition using the standard Java typestatic 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 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 secKey) ECDH key agreementecdsaSign(byte[] msg_hash_data, SecpPrivKey seckey) Sign a message hash using the ECDSA algorithmecdsaSignatureParseCompact(byte[] serialized_signature) Parse a Bitcoin compact signature.byte[]Serialize aEcdsaSignatureas a Bitcoin compact signature.ecdsaVerify(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.ecPubKeyCombine(SecpPubKey key1, SecpPubKey key2) Combine two public keys by adding them.ecPubKeyCreate(SecpPrivKey seckey) Create a public key from the given private key.ecPubKeyParse(byte[] inputData) Parse a byte array as a public keybyte[]ecPubKeySerialize(SecpPubKey pubKey, int flags) Serialize a public keyecPubKeyTweakMul(SecpPubKey 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, SecpKeyPair keyPair) 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.
-
Field Details
-
P
The primeP, that defines the secp256k1 field. Note that since the maximum valid value of a field element isP - 1, this constant cannot be represented as aSecpFieldElement, so we useBigIntegerinstead. -
G
The generator pointG(also known as base point) for secp256k1. -
FIELD
The secp256k1 field definitionpusing the standard Java type -
CURVE
The secp256k1 curve definition using the standard Java type -
EC_PARAMS
The secp256k1 domain parameters definition using the standard Java type
-
-
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:
seckey- 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
-
ecdsaSign
Sign a message hash using the ECDSA algorithm- Parameters:
msg_hash_data- hash of message to signseckey- 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 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 signkeyPair- the keypair for signing- 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 partysecKey- secret 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
-