Class Secp256k1Foreign
- All Implemented Interfaces:
Closeable, AutoCloseable, Secp256k1
Implementation of Secp256k1 using the secp256k1 C-language library and the Java Foreign Function & Memory API.
Memory management
Every API method that needs off-heap (native) memory creates a temporary, thread-scoped "confined" Arena (via
Arena.ofConfined()) for the duration of that call, so all native allocations are deterministically released
(via Arena.close()]) when the call returns.
We use ta as the name for these temporary/thread arenas.
Helper methods never create an arena of their own -- they take a SegmentAllocator so that the caller
retains control over the lifetime of everything that is allocated on its behalf. Any MemorySegment
returned by a helper is therefore only valid until the caller's arena is closed.
The only native resource with a lifetime longer than a single call is the secp256k1_context, which is
allocated and freed by the C library itself (see close()).
-
Nested Class Summary
Nested classes/interfaces inherited from interface Secp256k1
Secp256k1.Provider, Secp256k1.ProviderId -
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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[] msg_hash_data, SecpPrivKey privKey) ECDSA signing with Low-R grinding.static booleanecdsaVerify(MemorySegment sig, MemorySegment msg_hash, MemorySegment pubkey) TBD: Static verify method that doesn't require a class instance.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 keyCreate a new, randomly-generated private key.Combine two public keys by adding them.ecPubKeyCombine(SecpPubKey key1) ecPubKeyCreate(SecpPrivKey privkey) 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) SincePubKeyDatais serializable without using the native lib, this method serialized without a native call.ecPubKeyTweakMul(SecpPoint.Uncompressed pubKey, BigInteger scalarMultiplier) Multiply a public key by a scalar, this is known as key "tweaking"schnorrSigSign32(byte[] messageHash, SecpPrivKey privKey) Create a Schnorr signature for a message.schnorrSigSign32(byte[] messageHash, SecpPrivKey privKey, byte[] auxiliaryRandom) schnorrSigSign32 using provided randomness.schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpXOnlyPubKey pubKey) Verify a Schnorr signature.byte[]taggedSha256(byte[] tag, byte[] message) Generate a tagged SHA-256 hash.toString()xOnlyPubKeyParse(byte[] inputData) Parse a byte array as an x-only public keyMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Secp256k1
ecPointUncompress, ecPubKeyFromXOnly, schnorrSigVerify, taggedSha256
-
Constructor Details
-
Secp256k1Foreign
public Secp256k1Foreign() -
Secp256k1Foreign
public Secp256k1Foreign(int flags, boolean randomize)
-
-
Method Details
-
ecdsaVerify
TBD: Static verify method that doesn't require a class instance. -
close
-
ecPrivKeyCreate
Description copied from interface:Secp256k1Create a new, randomly-generated private key.- Specified by:
ecPrivKeyCreatein interfaceSecp256k1- Returns:
- the private key
-
ecPubKeyCreate
Description copied from interface:Secp256k1Create a public key from the given private key.- Specified by:
ecPubKeyCreatein interfaceSecp256k1- Parameters:
privkey- the private key- Returns:
- derived public key
-
ecKeyPairCreate
Description copied from interface:Secp256k1Create a new, randomly-generated private key and return it with its matching public key- Specified by:
ecKeyPairCreatein interfaceSecp256k1- Returns:
- newly generated key pair
-
ecKeyPairCreate
Description copied from interface:Secp256k1Create a key pair structure from a known private key- Specified by:
ecKeyPairCreatein interfaceSecp256k1- Parameters:
privKey- the private key- Returns:
- object containing both public and private key
-
ecPubKeyTweakMul
Description copied from interface:Secp256k1Multiply a public key by a scalar, this is known as key "tweaking"- Specified by:
ecPubKeyTweakMulin interfaceSecp256k1- Parameters:
pubKey- public key representing a point on the curvescalarMultiplier- scalar multiplier- Returns:
- the product
-
ecPubKeyCombine
Description copied from interface:Secp256k1Combine two public keys by adding them.- Specified by:
ecPubKeyCombinein interfaceSecp256k1- Parameters:
key1- first keykey2- second key- Returns:
- the sum
-
ecPubKeyCombine
-
ecPubKeySerialize
SincePubKeyDatais serializable without using the native lib, this method serialized without a native call.- Specified by:
ecPubKeySerializein interfaceSecp256k1- Parameters:
pubKey-flags-- Returns:
-
ecPubKeyParse
Description copied from interface:Secp256k1Parse a byte array as a public key- Specified by:
ecPubKeyParsein interfaceSecp256k1- Parameters:
inputData- raw data to parse as public key- Returns:
- public key result or error
-
xOnlyPubKeyParse
Description copied from interface:Secp256k1Parse a byte array as an x-only public key- Specified by:
xOnlyPubKeyParsein interfaceSecp256k1- Parameters:
inputData- raw data to parse as an x-only public key- Returns:
- an x-only public key result or error
-
ecdsaSign
Description copied from interface:Secp256k1Sign a message hash using the ECDSA algorithm -
ecdsaSignLowR
ECDSA signing with Low-R grinding. Will potentially sign multiple times until a low-R signature is generated.- Specified by:
ecdsaSignLowRin interfaceSecp256k1- Parameters:
msg_hash_data- hashed message dataprivKey- private key- Returns:
- A result, which on success contains a valid signature with a low R value.
-
ecdsaSignatureSerializeCompact
Description copied from interface:Secp256k1Serialize 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.- Specified by:
ecdsaSignatureSerializeCompactin interfaceSecp256k1- Parameters:
sig- signature object- Returns:
- compact signature bytes
-
ecdsaSignatureParseCompact
Description copied from interface:Secp256k1Parse 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.- Specified by:
ecdsaSignatureParseCompactin interfaceSecp256k1- Parameters:
serialized_signature- compact signature bytes- Returns:
- signature object
-
ecdsaVerify
Description copied from interface:Secp256k1Verify an ECDSA signature.- Specified by:
ecdsaVerifyin interfaceSecp256k1- 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
public byte[] taggedSha256(byte[] tag, byte[] message) Description copied from interface:Secp256k1Generate a tagged SHA-256 hash.- Specified by:
taggedSha256in interfaceSecp256k1- Parameters:
tag- a tag specifying the context of usagemessage- the message itself- Returns:
- the SHA-256 HASH
-
schnorrSigSign32
Description copied from interface:Secp256k1Create a Schnorr signature for a message.- Specified by:
schnorrSigSign32in interfaceSecp256k1- Parameters:
messageHash- a hash of a message to signprivKey- private key for signing- Returns:
- the signature
-
schnorrSigSign32
public SchnorrSignature schnorrSigSign32(byte[] messageHash, SecpPrivKey privKey, byte[] auxiliaryRandom) schnorrSigSign32 using provided randomness. This is not part of the API and is intended for testing.- Specified by:
schnorrSigSign32in interfaceSecp256k1- Parameters:
messageHash- message hashprivKey- private keyauxiliaryRandom- auxiliary randomness (typically from a test vector)- Returns:
- the signature
-
schnorrSigVerify
public SecpResult<Boolean> schnorrSigVerify(SchnorrSignature signature, byte[] msg_hash, SecpXOnlyPubKey pubKey) Description copied from interface:Secp256k1Verify a Schnorr signature.- Specified by:
schnorrSigVerifyin interfaceSecp256k1- 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
-
ecdh
Description copied from interface:Secp256k1ECDH key agreement -
toString
-