Interface Secp256k1


public interface Secp256k1
Static constants for Secp256k1 operations.
  • Field Details

    • EC_PARAMETERS

      @Deprecated static final org.bouncycastle.asn1.x9.X9ECParameters EC_PARAMETERS
      Deprecated.
      This interface is deprecated in-favor of BcKeyUtils.PARAMS.
      Elliptic Curve parameters for the curve named `secp256k1`.
    • EC_DOMAIN_PARAMETERS

      @Deprecated static final org.bouncycastle.crypto.params.ECDomainParameters EC_DOMAIN_PARAMETERS
      Deprecated.
      This interface is deprecated in-favor of BcKeyUtils.PARAMS.
      Elliptic Curve domain parameters for the curve named `secp256k1`.
    • SECP256K1_PREFIX

      static final UnsignedByte SECP256K1_PREFIX
      Private keys (whether from the ed25519 or secp256k1 curves) have 32 bytes naturally. At the same time, secp256k1 public keys have 33 bytes naturally, whereas ed25519 public keys have 32 bytes naturally. Because of this, in XRPL, ed25519 public keys are prefixed with a one-byte prefix (i.e., 0xED). For consistency, this library (and other XRPL tooling) also prepends all private keys with artificial prefixes (0xED for ed25519 or 0x00 for secp256k1). This value is the one-byte prefix for secp256k1 keys.
  • Method Details

    • toUnsignedByteArray

      static UnsignedByteArray toUnsignedByteArray(BigInteger amount, int minFinalByteLength)
      Creates an UnsignedByteArray from the bytes of a supplied BigInteger. If the length of the resulting array is not at least minFinalByteLength, then the result is prefix padded with `0x00` bytes until the final array length is minFinalByteLength.

      This function exists to ensure that transformation of secp256k1 private keys from a BigInteger to a byte array are done in a consistent manner, always yielding the desired number of bytes. For example, secp256k1 private keys are 32-bytes long naturally. However, when transformed to a byte array via BigInteger.toByteArray(), the result will not always have the same number of leading zero bytes that one might expect. Sometimes the returned array will have 33 bytes, one of which is a zero-byte prefix pad that is meant to ensure the underlying number is not represented as a negative number. Other times, the array will have fewer than 32 bytes, for example 31 or even 30, if the byte array has redundant leading zero bytes.

      Note that this function assumes the supplied amount is always positive, which roughly correlates with the secp256k1 requirement that private key scalar `D` values be in the range [1, N-1].

      Thus, this function can be used to normalize a byte array containing a secp256k1 private key with a desired number of 0-byte padding to ensure that it is always the desired minFinalByteLength (e.g., in this library, secp256k1 private keys should always be comprised of a 32-byte natural private key with a one-byte `0x00` prefix pad).

      Parameters:
      amount - A BigInteger to convert into an UnsignedByteArray.
      minFinalByteLength - The minimum length, in bytes, that the final result must be. If the final byte length is less than this number, the resulting array will be prefix padded to increase its length to this number.
      Returns:
      An UnsignedByteArray with a length of at least minFinalByteLength.
      See Also:
      • "https://github.com/XRPLF/xrpl4j/issues/486"
    • withZeroPrefixPadding

      static UnsignedByteArray withZeroPrefixPadding(UnsignedByteArray bytes, int minFinalByteLength)
      Construct a new UnsignedByteArray that contains the bytes from bytes, but with enough `0x00` prefix padding bytes such that the final length of the returned value is minFinalByteLength.
      Parameters:
      bytes - An UnsignedByteArray to zero-pad.
      minFinalByteLength - The minimum length, in bytes, that the final result must be zero-byte prefix-padded to. If this number is greater-than #length, then this value will be reduced to #length.
      Returns:
      A copy of this UnsignedByteArray that has been zero-byte prefix-padded such that its final length is at least minFinalByteLength.
    • withZeroPrefixPadding

      static UnsignedByteArray withZeroPrefixPadding(byte[] bytes, int minFinalByteLength)
      Construct a new UnsignedByteArray that contains the bytes from bytes, but with enough `0x00` prefix padding bytes such that the final length of the returned value is minFinalByteLength.
      Parameters:
      bytes - A byte array to zero-pad.
      minFinalByteLength - The minimum length, in bytes, that the final result must be zero-byte prefix-padded to. If this number is greater-than #length, then this value will be reduced to #length.
      Returns:
      A copy of this UnsignedByteArray that has been zero-byte prefix-padded such that its final length is at least minFinalByteLength.