20 import java.nio.ByteBuffer;
21 import java.nio.ByteOrder;
23 import java.math.BigInteger;
24 import com.google.common.base.Preconditions;
25 import java.util.concurrent.locks.Lock;
26 import java.util.concurrent.locks.ReentrantReadWriteLock;
42 private static final ReentrantReadWriteLock
rwl =
new ReentrantReadWriteLock();
43 private static final Lock
r = rwl.readLock();
44 private static final Lock
w = rwl.writeLock();
55 Preconditions.checkArgument(
data.length == 32 && signature.length <= 520 && pub.length <= 520);
57 ByteBuffer byteBuff = nativeECDSABuffer.get();
58 if (byteBuff == null || byteBuff.capacity() < 520) {
59 byteBuff = ByteBuffer.allocateDirect(520);
61 nativeECDSABuffer.set(byteBuff);
65 byteBuff.put(signature);
68 byte[][] retByteArray;
88 Preconditions.checkArgument(
data.length == 32 && sec.length <= 32);
90 ByteBuffer byteBuff = nativeECDSABuffer.get();
91 if (byteBuff == null || byteBuff.capacity() < 32 + 32) {
92 byteBuff = ByteBuffer.allocateDirect(32 + 32);
94 nativeECDSABuffer.set(byteBuff);
100 byte[][] retByteArray;
109 byte[] sigArr = retByteArray[0];
110 int sigLen =
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue();
111 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
113 assertEquals(sigArr.length, sigLen,
"Got bad signature length.");
115 return retVal == 0 ?
new byte[0] : sigArr;
124 Preconditions.checkArgument(seckey.length == 32);
126 ByteBuffer byteBuff = nativeECDSABuffer.get();
127 if (byteBuff == null || byteBuff.capacity() < seckey.length) {
128 byteBuff = ByteBuffer.allocateDirect(seckey.length);
130 nativeECDSABuffer.set(byteBuff);
133 byteBuff.put(seckey);
154 Preconditions.checkArgument(seckey.length == 32);
156 ByteBuffer byteBuff = nativeECDSABuffer.get();
157 if (byteBuff == null || byteBuff.capacity() < seckey.length) {
158 byteBuff = ByteBuffer.allocateDirect(seckey.length);
160 nativeECDSABuffer.set(byteBuff);
163 byteBuff.put(seckey);
165 byte[][] retByteArray;
174 byte[] pubArr = retByteArray[0];
175 int pubLen =
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue();
176 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
178 assertEquals(pubArr.length, pubLen,
"Got bad pubkey length.");
180 return retVal == 0 ?
new byte[0]: pubArr;
200 }
finally { r.unlock(); }
210 Preconditions.checkArgument(privkey.length == 32);
212 ByteBuffer byteBuff = nativeECDSABuffer.get();
213 if (byteBuff == null || byteBuff.capacity() < privkey.length + tweak.length) {
214 byteBuff = ByteBuffer.allocateDirect(privkey.length + tweak.length);
216 nativeECDSABuffer.set(byteBuff);
219 byteBuff.put(privkey);
222 byte[][] retByteArray;
230 byte[] privArr = retByteArray[0];
232 int privLen = (
byte)
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
233 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
235 assertEquals(privArr.length, privLen,
"Got bad pubkey length.");
237 assertEquals(retVal, 1,
"Failed return value check.");
249 Preconditions.checkArgument(privkey.length == 32);
251 ByteBuffer byteBuff = nativeECDSABuffer.get();
252 if (byteBuff == null || byteBuff.capacity() < privkey.length + tweak.length) {
253 byteBuff = ByteBuffer.allocateDirect(privkey.length + tweak.length);
255 nativeECDSABuffer.set(byteBuff);
258 byteBuff.put(privkey);
261 byte[][] retByteArray;
269 byte[] privArr = retByteArray[0];
271 int privLen = (
byte)
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
272 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
274 assertEquals(privArr.length, privLen,
"Got bad pubkey length.");
276 assertEquals(retVal, 1,
"Failed return value check.");
288 Preconditions.checkArgument(pubkey.length == 33 || pubkey.length == 65);
290 ByteBuffer byteBuff = nativeECDSABuffer.get();
291 if (byteBuff == null || byteBuff.capacity() < pubkey.length + tweak.length) {
292 byteBuff = ByteBuffer.allocateDirect(pubkey.length + tweak.length);
294 nativeECDSABuffer.set(byteBuff);
297 byteBuff.put(pubkey);
300 byte[][] retByteArray;
308 byte[] pubArr = retByteArray[0];
310 int pubLen = (
byte)
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
311 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
313 assertEquals(pubArr.length, pubLen,
"Got bad pubkey length.");
315 assertEquals(retVal, 1,
"Failed return value check.");
327 Preconditions.checkArgument(pubkey.length == 33 || pubkey.length == 65);
329 ByteBuffer byteBuff = nativeECDSABuffer.get();
330 if (byteBuff == null || byteBuff.capacity() < pubkey.length + tweak.length) {
331 byteBuff = ByteBuffer.allocateDirect(pubkey.length + tweak.length);
333 nativeECDSABuffer.set(byteBuff);
336 byteBuff.put(pubkey);
339 byte[][] retByteArray;
347 byte[] pubArr = retByteArray[0];
349 int pubLen = (
byte)
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue() & 0xFF;
350 int retVal =
new BigInteger(
new byte[] { retByteArray[1][1] }).intValue();
352 assertEquals(pubArr.length, pubLen,
"Got bad pubkey length.");
354 assertEquals(retVal, 1,
"Failed return value check.");
366 Preconditions.checkArgument(seckey.length <= 32 && pubkey.length <= 65);
368 ByteBuffer byteBuff = nativeECDSABuffer.get();
369 if (byteBuff == null || byteBuff.capacity() < 32 + pubkey.length) {
370 byteBuff = ByteBuffer.allocateDirect(32 + pubkey.length);
372 nativeECDSABuffer.set(byteBuff);
375 byteBuff.put(seckey);
376 byteBuff.put(pubkey);
378 byte[][] retByteArray;
386 byte[] resArr = retByteArray[0];
387 int retVal =
new BigInteger(
new byte[] { retByteArray[1][0] }).intValue();
389 assertEquals(resArr.length, 32,
"Got bad result length.");
390 assertEquals(retVal, 1,
"Failed return value check.");
400 public static synchronized boolean randomize(
byte[] seed)
throws AssertFailException{
401 Preconditions.checkArgument(seed.length == 32 || seed == null);
403 ByteBuffer byteBuff = nativeECDSABuffer.get();
404 if (byteBuff == null || byteBuff.capacity() < seed.length) {
405 byteBuff = ByteBuffer.allocateDirect(seed.length);
407 nativeECDSABuffer.set(byteBuff);
434 private static native
int secp256k1_ecdsa_verify(ByteBuffer byteBuff,
long context,
int sigLen,
int pubLen);
444 private static native
byte[][]
secp256k1_ecdh(ByteBuffer byteBuff,
long context,
int inputLen);
static native long secp256k1_ctx_clone(long context)
static native byte[][] secp256k1_privkey_tweak_mul(ByteBuffer byteBuff, long context)
ByteOrder
Provides the byte ordering.
static byte[] pubKeyTweakAdd(byte[] pubkey, byte[] tweak)
libsecp256k1 PubKey Tweak-Add - Tweak pubkey by adding to it
static native void secp256k1_destroy_context(long context)
static long cloneContext()
static native byte[][] secp256k1_ecdh(ByteBuffer byteBuff, long context, int inputLen)
static native byte[][] secp256k1_pubkey_tweak_mul(ByteBuffer byteBuff, long context, int pubLen)
static native byte[][] secp256k1_ec_pubkey_create(ByteBuffer byteBuff, long context)
static native int secp256k1_ec_seckey_verify(ByteBuffer byteBuff, long context)
static boolean verify(byte[] data, byte[] signature, byte[] pub)
Verifies the given secp256k1 signature in native code.
static native int secp256k1_context_randomize(ByteBuffer byteBuff, long context)
static byte[] privKeyTweakAdd(byte[] privkey, byte[] tweak)
libsecp256k1 PrivKey Tweak-Add - Tweak privkey by adding to it
static final ReentrantReadWriteLock rwl
static byte[] computePubkey(byte[] seckey)
libsecp256k1 Compute Pubkey - computes public key from secret key
static byte[] pubKeyTweakMul(byte[] pubkey, byte[] tweak)
libsecp256k1 PubKey Tweak-Mul - Tweak pubkey by multiplying to it
static native byte[][] secp256k1_pubkey_tweak_add(ByteBuffer byteBuff, long context, int pubLen)
static byte[] createECDHSecret(byte[] seckey, byte[] pubkey)
libsecp256k1 create ECDH secret - constant time ECDH calculation
static byte[] sign(byte[] data, byte[] sec)
libsecp256k1 Create an ECDSA signature.
static synchronized boolean randomize(byte[] seed)
libsecp256k1 randomize - updates the context randomization
static native byte[][] secp256k1_privkey_tweak_add(ByteBuffer byteBuff, long context)
static boolean secKeyVerify(byte[] seckey)
libsecp256k1 Seckey Verify - returns 1 if valid, 0 if invalid
This class holds the context reference used in native methods to handle ECDSA operations.
static native int secp256k1_ecdsa_verify(ByteBuffer byteBuff, long context, int sigLen, int pubLen)
static byte[] privKeyTweakMul(byte[] privkey, byte[] tweak)
libsecp256k1 PrivKey Tweak-Mul - Tweak privkey by multiplying to it
static ThreadLocal< ByteBuffer > nativeECDSABuffer
static native byte[][] secp256k1_ec_pubkey_parse(ByteBuffer byteBuff, long context, int inputLen)
static native byte[][] secp256k1_ecdsa_sign(ByteBuffer byteBuff, long context)
static synchronized void cleanup()
libsecp256k1 Cleanup - This destroys the secp256k1 context object This should be called at the end of...