diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:46:57 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-10-16 01:46:57 +0800 |
commit | f466243417f60531998e8b500f2bb043af5b3d2a (patch) | |
tree | 9f8387b65d2a9d54a94ed26bc6a57ecfde3489f6 /crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java | |
parent | cefe5c80b1cdcab606a169c0be65d9d2ba9bc941 (diff) | |
parent | f32fa075f14d2b3a1213098274e0ba88c7761283 (diff) | |
download | go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar.gz go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar.bz2 go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar.lz go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar.xz go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.tar.zst go-tangerine-f466243417f60531998e8b500f2bb043af5b3d2a.zip |
Merge pull request #1853 from Gustav-Simonsson/libsecp256k1_update
Update libsecp256k1, Go wrapper and tests
Diffstat (limited to 'crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java')
-rw-r--r-- | crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java b/crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java new file mode 100644 index 000000000..90a498eaa --- /dev/null +++ b/crypto/secp256k1/libsecp256k1/src/java/org/bitcoin/NativeSecp256k1.java @@ -0,0 +1,60 @@ +package org.bitcoin; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; + +import com.google.common.base.Preconditions; + + +/** + * 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/sipa/secp256k1 + */ +public class NativeSecp256k1 { + public static final boolean enabled; + static { + boolean isEnabled = true; + try { + System.loadLibrary("javasecp256k1"); + } catch (UnsatisfiedLinkError e) { + isEnabled = false; + } + enabled = isEnabled; + } + + private static ThreadLocal<ByteBuffer> nativeECDSABuffer = new ThreadLocal<ByteBuffer>(); + /** + * Verifies the given secp256k1 signature in native code. + * Calling when enabled == false is undefined (probably library not loaded) + * + * @param data The data which was signed, must be exactly 32 bytes + * @param signature The signature + * @param pub The public key which did the signing + */ + public static boolean verify(byte[] data, byte[] signature, byte[] pub) { + Preconditions.checkArgument(data.length == 32 && signature.length <= 520 && pub.length <= 520); + + ByteBuffer byteBuff = nativeECDSABuffer.get(); + if (byteBuff == null) { + byteBuff = ByteBuffer.allocateDirect(32 + 8 + 520 + 520); + byteBuff.order(ByteOrder.nativeOrder()); + nativeECDSABuffer.set(byteBuff); + } + byteBuff.rewind(); + byteBuff.put(data); + byteBuff.putInt(signature.length); + byteBuff.putInt(pub.length); + byteBuff.put(signature); + byteBuff.put(pub); + return secp256k1_ecdsa_verify(byteBuff) == 1; + } + + /** + * @param byteBuff signature format is byte[32] data, + * native-endian int signatureLength, native-endian int pubkeyLength, + * byte[signatureLength] signature, byte[pubkeyLength] pub + * @returns 1 for valid signature, anything else for invalid + */ + private static native int secp256k1_ecdsa_verify(ByteBuffer byteBuff); +} |