Package org.bitcoin

Class NativeSecp256k1

java.lang.Object
org.bitcoin.NativeSecp256k1

public class NativeSecp256k1 extends Object

This class holds native methods to handle ECDSA verification.

You can find an example library that can be used for this at https://github.com/bitcoin/secp256k1

To build secp256k1 for use with bitcoinj, run `./configure --enable-jni --enable-experimental --enable-module-schnorr --enable-module-ecdh` and `make` then copy `.libs/libsecp256k1.so` to your system library path or point the JVM to the folder containing it with -Djava.library.path

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    libsecp256k1 Cleanup - This destroys the secp256k1 context object This should be called at the end of the program for proper cleanup of the context.
    static long
    Clone context
    static byte[]
    computePubkey(byte[] seckey)
    libsecp256k1 Compute Pubkey - computes public key from secret key
    static byte[]
    createECDHSecret(byte[] seckey, byte[] pubkey)
    libsecp256k1 create ECDH secret - constant time ECDH calculation
    static byte[]
    privKeyTweakAdd(byte[] privkey, byte[] tweak)
    libsecp256k1 PrivKey Tweak-Add - Tweak privkey by adding to it
    static byte[]
    privKeyTweakMul(byte[] privkey, byte[] tweak)
    libsecp256k1 PrivKey Tweak-Mul - Tweak privkey by multiplying to it
    static byte[]
    pubKeyTweakAdd(byte[] pubkey, byte[] tweak)
    libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
    static byte[]
    pubKeyTweakMul(byte[] pubkey, byte[] tweak)
    libsecp256k1 PubKey Tweak-Mul - Tweak pubkey by multiplying to it
    static boolean
    randomize(byte[] seed)
    libsecp256k1 randomize - updates the context randomization
    static byte[]
    schnorrSign(byte[] data, byte[] sec)
     
    static boolean
    secKeyVerify(byte[] seckey)
    libsecp256k1 Seckey Verify - returns 1 if valid, 0 if invalid
    static byte[]
    sign(byte[] data, byte[] sec)
    libsecp256k1 Create an ECDSA signature.
    static boolean
    verify(byte[] data, byte[] signature, byte[] pub)
    Verifies the given secp256k1 signature in native code.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NativeSecp256k1

      public NativeSecp256k1()
  • Method Details

    • verify

      public static boolean verify(byte[] data, byte[] signature, byte[] pub) throws NativeSecp256k1Util.AssertFailException
      Verifies the given secp256k1 signature in native code. Calling when enabled == false is undefined (probably library not loaded)
      Parameters:
      data - The data which was signed, must be exactly 32 bytes
      signature - The signature
      pub - The public key which did the signing
      Returns:
      true if correct signature
      Throws:
      NativeSecp256k1Util.AssertFailException - never thrown?
    • sign

      public static byte[] sign(byte[] data, byte[] sec) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 Create an ECDSA signature.
      Parameters:
      data - Message hash, 32 bytes
      sec - Secret key, 32 bytes
      Returns:
      sig byte array of signature
      Throws:
      NativeSecp256k1Util.AssertFailException - on bad signature length
    • secKeyVerify

      public static boolean secKeyVerify(byte[] seckey)
      libsecp256k1 Seckey Verify - returns 1 if valid, 0 if invalid
      Parameters:
      seckey - ECDSA Secret key, 32 bytes
      Returns:
      true if valid, false if invalid
    • computePubkey

      public static byte[] computePubkey(byte[] seckey) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 Compute Pubkey - computes public key from secret key
      Parameters:
      seckey - ECDSA Secret key, 32 bytes
      Returns:
      pubkey ECDSA Public key, 33 or 65 bytes
      Throws:
      NativeSecp256k1Util.AssertFailException - if bad pubkey length
    • cleanup

      public static void cleanup()
      libsecp256k1 Cleanup - This destroys the secp256k1 context object This should be called at the end of the program for proper cleanup of the context.
    • cloneContext

      public static long cloneContext()
      Clone context
      Returns:
      context reference
    • privKeyTweakMul

      public static byte[] privKeyTweakMul(byte[] privkey, byte[] tweak) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 PrivKey Tweak-Mul - Tweak privkey by multiplying to it
      Parameters:
      tweak - some bytes to tweak with
      privkey - 32-byte seckey
      Returns:
      The tweaked private key
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure
    • privKeyTweakAdd

      public static byte[] privKeyTweakAdd(byte[] privkey, byte[] tweak) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 PrivKey Tweak-Add - Tweak privkey by adding to it
      Parameters:
      tweak - some bytes to tweak with
      privkey - 32-byte seckey
      Returns:
      The tweaked private key
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure
    • pubKeyTweakAdd

      public static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
      Parameters:
      tweak - some bytes to tweak with
      pubkey - 32-byte seckey
      Returns:
      The tweaked private key
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure
    • pubKeyTweakMul

      public static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 PubKey Tweak-Mul - Tweak pubkey by multiplying to it
      Parameters:
      tweak - some bytes to tweak with
      pubkey - 32-byte seckey
      Returns:
      The tweaked private key
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure
    • createECDHSecret

      public static byte[] createECDHSecret(byte[] seckey, byte[] pubkey) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 create ECDH secret - constant time ECDH calculation
      Parameters:
      seckey - byte array of secret key used in exponentiation
      pubkey - byte array of public key used in exponentiation
      Returns:
      the secret
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure
    • randomize

      public static boolean randomize(byte[] seed) throws NativeSecp256k1Util.AssertFailException
      libsecp256k1 randomize - updates the context randomization
      Parameters:
      seed - 32-byte random seed
      Returns:
      true if successful, false otherwise
      Throws:
      NativeSecp256k1Util.AssertFailException - never thrown?
    • schnorrSign

      public static byte[] schnorrSign(byte[] data, byte[] sec) throws NativeSecp256k1Util.AssertFailException
      Parameters:
      data - data to sign
      sec - secret key
      Returns:
      Signature or byte[0]
      Throws:
      NativeSecp256k1Util.AssertFailException - assertion failure