Interface Secp256k1

All Superinterfaces:
AutoCloseable, Closeable
All Known Implementing Classes:
Bouncy256k1, Secp256k1Foreign

public interface Secp256k1 extends Closeable
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.

  • Field Details

    • P

      static final BigInteger P
      The prime P, that defines the secp256k1 field. Note that since the maximum valid value of a field element is P - 1, this constant cannot be represented as a SecpFieldElement, so we use BigInteger instead.
    • G

      static final SecpPoint.Uncompressed G
      The generator point G (also known as base point) for secp256k1.
    • FIELD

      static final ECFieldFp FIELD
      The secp256k1 field definition p using the standard Java type
    • CURVE

      static final EllipticCurve CURVE
      The secp256k1 curve definition using the standard Java type
    • EC_PARAMS

      static final ECParameterSpec EC_PARAMS
      The secp256k1 domain parameters definition using the standard Java type
  • Method Details

    • all

      static Stream<Secp256k1.Provider> 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

      SecpPubKey ecPubKeyCreate(SecpPrivKey seckey)
      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

      SecpKeyPair ecKeyPairCreate(SecpPrivKey privKey)
      Create a key pair structure from a known private key
      Parameters:
      privKey - the private key
      Returns:
      object containing both public and private key
    • ecPubKeyTweakMul

      SecpPubKey ecPubKeyTweakMul(SecpPubKey pubKey, BigInteger scalarMultiplier)
      Multiply a public key by a scalar, this is known as key "tweaking"
      Parameters:
      pubKey - public key representing a point on the curve
      scalarMultiplier - scalar multiplier
      Returns:
      the product
    • ecPubKeyCombine

      SecpPubKey ecPubKeyCombine(SecpPubKey key1, SecpPubKey key2)
      Combine two public keys by adding them.
      Parameters:
      key1 - first key
      key2 - second key
      Returns:
      the sum
    • ecPubKeySerialize

      byte[] ecPubKeySerialize(SecpPubKey pubKey, int flags)
      Serialize a public key
      Parameters:
      pubKey - public key to serialize
      flags - serialization flags
      Returns:
      pubKey serialized as a byte array
    • ecPointUncompress

      default SecpPoint.Uncompressed ecPointUncompress(SecpPoint.Compressed compressedPoint)
      Calculate an uncompressed point from a compressed point.
      Parameters:
      compressedPoint - a compressed point
      Returns:
      The same point, in uncompressed format
    • ecPubKeyParse

      SecpResult<SecpPubKey> ecPubKeyParse(byte[] inputData)
      Parse a byte array as a public key
      Parameters:
      inputData - raw data to parse as public key
      Returns:
      public key result or error
    • ecdsaSign

      SecpResult<EcdsaSignature> ecdsaSign(byte[] msg_hash_data, SecpPrivKey seckey)
      Sign a message hash using the ECDSA algorithm
      Parameters:
      msg_hash_data - hash of message to sign
      seckey - private key
      Returns:
      the signature
    • ecdsaSignatureSerializeCompact

      byte[] ecdsaSignatureSerializeCompact(EcdsaSignature sig)
      Serialize a EcdsaSignature as a Bitcoin compact signature. A compact signature is the two signature component field integers (known as r and s) serialized in-order as binary data in big-endian format.
      Parameters:
      sig - signature object
      Returns:
      compact signature bytes
    • ecdsaSignatureParseCompact

      SecpResult<EcdsaSignature> ecdsaSignatureParseCompact(byte[] serialized_signature)
      Parse a Bitcoin compact signature. A compact signature is the two signature component field integers (known as r and s) serialized in-order as binary data in big-endian format.
      Parameters:
      serialized_signature - compact signature bytes
      Returns:
      signature object
    • ecdsaVerify

      SecpResult<Boolean> ecdsaVerify(EcdsaSignature sig, byte[] msg_hash_data, SecpPubKey pubKey)
      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

      default byte[] taggedSha256(String tag, String message)
      Generate a tagged SHA-256 hash.
      Parameters:
      tag - a tag specifying the context of usage
      message - the message itself
      Returns:
      the SHA-256 HASH
    • taggedSha256

      byte[] taggedSha256(byte[] tag, byte[] message)
      Generate a tagged SHA-256 hash.
      Parameters:
      tag - a tag specifying the context of usage
      message - the message itself
      Returns:
      the SHA-256 HASH
    • schnorrSigSign32

      SchnorrSignature schnorrSigSign32(byte[] msg_hash, SecpKeyPair keyPair)
      Create a Schnorr signature for a message.
      Parameters:
      msg_hash - a hash of a message to sign
      keyPair - 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 verify
      msg_hash - hash of the message
      pubKey - 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 verify
      msg_hash - hash of the message
      pubKey - pubkey that must have signed the message
      Returns:
      true, false, or error
    • ecdh

      ECDH key agreement
      Parameters:
      pubKey - pubkey of the other party
      secKey - secret key
      Returns:
      ecdh key agreement
    • close

      void close()
      Override close and declare that no checked exceptions are thrown
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • get

      static Secp256k1 get()
      Get the default implementation
      Returns:
      A Secp256k1 instance using the default implementation
    • getById

      static Secp256k1 getById(String id)
      Get implementation by ID
      Parameters:
      id - implementation ID
      Returns:
      A Secp256k1 instance using the default implementation
    • getById

      static Secp256k1 getById(Secp256k1.ProviderId idEnum)
      Get implementation by ID enum
      Parameters:
      idEnum - implementation ID enum
      Returns:
      A Secp256k1 instance using the default implementation