summaryrefslogtreecommitdiffstats
path: root/luni
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-04-23 00:37:11 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2013-04-23 00:37:11 +0000
commit60d77a525c05c0d0a5d70d2a903ab3a9029dd58d (patch)
tree705d135a5b9550e1f594daab832c90316e0869da /luni
parent18abc83d66f090abb1243519d97652d975cf3f6c (diff)
parenta125dded8ab0490d05e2fa9ec2e821ef1ae6facd (diff)
downloadlibcore-60d77a525c05c0d0a5d70d2a903ab3a9029dd58d.zip
libcore-60d77a525c05c0d0a5d70d2a903ab3a9029dd58d.tar.gz
libcore-60d77a525c05c0d0a5d70d2a903ab3a9029dd58d.tar.bz2
Merge "64-bit fixes for BigInteger."
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/java/math/BigInt.java13
-rw-r--r--luni/src/main/java/java/math/NativeBN.java100
-rw-r--r--luni/src/main/native/java_math_NativeBN.cpp778
3 files changed, 429 insertions, 462 deletions
diff --git a/luni/src/main/java/java/math/BigInt.java b/luni/src/main/java/java/math/BigInt.java
index 614dbb4..e3b329c 100644
--- a/luni/src/main/java/java/math/BigInt.java
+++ b/luni/src/main/java/java/math/BigInt.java
@@ -24,7 +24,7 @@ package java.math;
final class BigInt {
/* Fields used for the internal representation. */
- transient int bignum = 0;
+ transient long bignum = 0;
@Override protected void finalize() throws Throwable {
try {
@@ -42,15 +42,15 @@ final class BigInt {
return this.decString();
}
- int getNativeBIGNUM() {
+ long getNativeBIGNUM() {
return this.bignum;
}
static int consumeErrors(StringBuilder sb) {
int cnt = 0;
- int e, reason;
+ long e;
while ((e = NativeBN.ERR_get_error()) != 0) {
- reason = e & 255;
+ int reason = ((int) e) & 0xff;
if (reason == 103) {
throw new ArithmeticException("BigInteger division by zero");
}
@@ -335,9 +335,8 @@ final class BigInt {
// int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx);
}
- static void division(BigInt dividend, BigInt divisor,
- BigInt quotient, BigInt remainder) {
- int quot, rem;
+ static void division(BigInt dividend, BigInt divisor, BigInt quotient, BigInt remainder) {
+ long quot, rem;
if (quotient != null) {
quotient.makeValid();
quot = quotient.bignum;
diff --git a/luni/src/main/java/java/math/NativeBN.java b/luni/src/main/java/java/math/NativeBN.java
index 95bb2a2..0bd87b5 100644
--- a/luni/src/main/java/java/math/NativeBN.java
+++ b/luni/src/main/java/java/math/NativeBN.java
@@ -18,148 +18,124 @@ package java.math;
final class NativeBN {
- public static native int ERR_get_error();
+ public static native long ERR_get_error();
// unsigned long ERR_get_error(void);
- public static native String ERR_error_string(int e);
+ public static native String ERR_error_string(long e);
// char *ERR_error_string(unsigned long e, char *buf);
- public static native int BN_new();
+ public static native long BN_new();
// BIGNUM *BN_new(void);
- public static native void BN_free(int a);
+ public static native void BN_free(long a);
// void BN_free(BIGNUM *a);
- public static native int BN_cmp(int a, int b);
+ public static native int BN_cmp(long a, long b);
// int BN_cmp(const BIGNUM *a, const BIGNUM *b);
- public static native boolean BN_copy(int to, int from);
+ public static native boolean BN_copy(long to, long from);
// Returns boolean success AND NOT result BIGNUM handle!
// BIGNUM *BN_copy(BIGNUM *to, const BIGNUM *from);
-// public static native int BN_dup(int from);
- // BIGNUM *BN_dup(const BIGNUM *a);
- public static native boolean putLongInt(int a, long dw);
+ public static native boolean putLongInt(long a, long dw);
- public static native boolean putULongInt(int a, long dw, boolean neg);
+ public static native boolean putULongInt(long a, long dw, boolean neg);
- public static native int BN_dec2bn(int a, String str);
+ public static native int BN_dec2bn(long a, String str);
// int BN_dec2bn(BIGNUM **a, const char *str);
- public static native int BN_hex2bn(int a, String str);
+ public static native int BN_hex2bn(long a, String str);
// int BN_hex2bn(BIGNUM **a, const char *str);
- public static native boolean BN_bin2bn(byte[] s, int len, boolean neg, int ret);
+ public static native boolean BN_bin2bn(byte[] s, int len, boolean neg, long ret);
// Returns boolean success AND NOT result BIGNUM handle!
// BIGNUM * BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
// BN-Docu: s is taken as unsigned big endian;
// Additional parameter: neg.
- public static native boolean litEndInts2bn(int[] ints, int len, boolean neg, int ret);
+ public static native boolean litEndInts2bn(int[] ints, int len, boolean neg, long ret);
- public static native boolean twosComp2bn(byte[] s, int len, int ret);
+ public static native boolean twosComp2bn(byte[] s, int len, long ret);
- public static native long longInt(int a);
+ public static native long longInt(long a);
// unsigned long BN_get_word(BIGNUM *a);
- public static native String BN_bn2dec(int a);
+ public static native String BN_bn2dec(long a);
// char * BN_bn2dec(const BIGNUM *a);
- public static native String BN_bn2hex(int a);
+ public static native String BN_bn2hex(long a);
// char * BN_bn2hex(const BIGNUM *a);
- public static native byte[] BN_bn2bin(int a);
+ public static native byte[] BN_bn2bin(long a);
// Returns result byte[] AND NOT length.
// int BN_bn2bin(const BIGNUM *a, unsigned char *to);
- public static native int[] bn2litEndInts(int a);
+ public static native int[] bn2litEndInts(long a);
- public static native int sign(int a);
+ public static native int sign(long a);
// Returns -1, 0, 1 AND NOT boolean.
// #define BN_is_negative(a) ((a)->neg != 0)
- public static native void BN_set_negative(int b, int n);
+ public static native void BN_set_negative(long b, int n);
// void BN_set_negative(BIGNUM *b, int n);
- public static native int bitLength(int a);
+ public static native int bitLength(long a);
- public static native boolean BN_is_bit_set(int a, int n);
+ public static native boolean BN_is_bit_set(long a, int n);
// int BN_is_bit_set(const BIGNUM *a, int n);
- public static native boolean modifyBit(int a, int n, int op);
- // Returns boolean success.
- // op: 0 = reset; 1 = set; -1 = flip
- // uses BN_set_bit(), BN_clear_bit() and BN_is_bit_set()
-
- public static native boolean BN_shift(int r, int a, int n);
+ public static native boolean BN_shift(long r, long a, int n);
// int BN_shift(BIGNUM *r, const BIGNUM *a, int n);
- public static native boolean BN_add_word(int a, int w);
+ public static native boolean BN_add_word(long a, int w);
// ATTENTION: w is treated as unsigned.
// int BN_add_word(BIGNUM *a, BN_ULONG w);
- public static native boolean BN_sub_word(int a, int w);
- // ATTENTION: w is treated as unsigned.
- // int BN_sub_word(BIGNUM *a, BN_ULONG w);
-
- public static native boolean BN_mul_word(int a, int w);
+ public static native boolean BN_mul_word(long a, int w);
// ATTENTION: w is treated as unsigned.
// int BN_mul_word(BIGNUM *a, BN_ULONG w);
- public static native int BN_div_word(int a, int w);
- // ATTENTION: w is treated as unsigned.
- // BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
-
- public static native int BN_mod_word(int a, int w);
+ public static native int BN_mod_word(long a, int w);
// ATTENTION: w is treated as unsigned.
// BN_ULONG BN_mod_word(BIGNUM *a, BN_ULONG w);
- public static native boolean BN_add(int r, int a, int b);
+ public static native boolean BN_add(long r, long a, long b);
// int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
- public static native boolean BN_sub(int r, int a, int b);
+ public static native boolean BN_sub(long r, long a, long b);
// int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
- public static native boolean BN_gcd(int r, int a, int b);
+ public static native boolean BN_gcd(long r, long a, long b);
// int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
- public static native boolean BN_mul(int r, int a, int b);
+ public static native boolean BN_mul(long r, long a, long b);
// int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
- public static native boolean BN_exp(int r, int a, int p);
+ public static native boolean BN_exp(long r, long a, long p);
// int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
- // OPTIONAL:
-// public static native int BN_sqr(BigInteger r, BigInteger a, BN_CTX ctx);
- // int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx);
-
- public static native boolean BN_div(int dv, int rem, int m, int d);
+ public static native boolean BN_div(long dv, long rem, long m, long d);
// int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
- public static native boolean BN_nnmod(int r, int a, int m);
+ public static native boolean BN_nnmod(long r, long a, long m);
// int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
- public static native boolean BN_mod_exp(int r, int a, int p, int m);
+ public static native boolean BN_mod_exp(long r, long a, long p, long m);
// int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);
- // OPTIONAL:
-// public static native boolean BN_mod_sqr(BigInteger r, BigInteger a, BigInteger m, BN_CTX ctx);
- // int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
-
-
- public static native boolean BN_mod_inverse(int ret, int a, int n);
+ public static native boolean BN_mod_inverse(long ret, long a, long n);
// Returns boolean success AND NOT result BIGNUM handle!
// BIGNUM * BN_mod_inverse(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
- public static native boolean BN_generate_prime_ex(int ret, int bits, boolean safe,
- int add, int rem, int cb);
+ public static native boolean BN_generate_prime_ex(long ret, int bits, boolean safe,
+ long add, long rem, long cb);
// int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
// const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb);
- public static native boolean BN_is_prime_ex(int p, int nchecks, int cb);
+ public static native boolean BN_is_prime_ex(long p, int nchecks, long cb);
// int BN_is_prime_ex(const BIGNUM *p, int nchecks, BN_CTX *ctx, BN_GENCB *cb);
}
diff --git a/luni/src/main/native/java_math_NativeBN.cpp b/luni/src/main/native/java_math_NativeBN.cpp
index 5e111d7..3669bcf 100644
--- a/luni/src/main/native/java_math_NativeBN.cpp
+++ b/luni/src/main/native/java_math_NativeBN.cpp
@@ -39,71 +39,72 @@ struct BN_CTX_Deleter {
};
typedef UniquePtr<BN_CTX, BN_CTX_Deleter> Unique_BN_CTX;
-static int isValidHandle (JNIEnv* env, void* handle, const char* message) {
- if (handle == NULL) {
- jniThrowNullPointerException(env, message);
- return JNI_FALSE;
- }
- return JNI_TRUE;
+static BIGNUM* toBigNum(jlong address) {
+ return reinterpret_cast<BIGNUM*>(static_cast<uintptr_t>(address));
}
-static int oneValidHandle (JNIEnv* env, void* a)
-{
- return isValidHandle(env, a, "Mandatory handle (first) passed as null");
+
+static int isValidHandle(JNIEnv* env, jlong handle, const char* message) {
+ if (handle == 0) {
+ jniThrowNullPointerException(env, message);
+ return JNI_FALSE;
+ }
+ return JNI_TRUE;
}
-static int twoValidHandles (JNIEnv* env, void* a, void* b)
-{
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return isValidHandle(env, b, "Mandatory handle (second) passed as null");
+static int oneValidHandle(JNIEnv* env, jlong a) {
+ return isValidHandle(env, a, "Mandatory handle (first) passed as null");
}
-static int threeValidHandles (JNIEnv* env, void* a, void* b, void* c)
-{
- if (!twoValidHandles(env, a, b)) return JNI_FALSE;
- return isValidHandle(env, c, "Mandatory handle (third) passed as null");
+static int twoValidHandles(JNIEnv* env, jlong a, jlong b) {
+ if (!oneValidHandle(env, a)) return JNI_FALSE;
+ return isValidHandle(env, b, "Mandatory handle (second) passed as null");
}
-static int fourValidHandles (JNIEnv* env, void* a, void* b, void* c, void* d)
-{
- if (!threeValidHandles(env, a, b, c)) return JNI_FALSE;
- return isValidHandle(env, d, "Mandatory handle (fourth) passed as null");
+static int threeValidHandles(JNIEnv* env, jlong a, jlong b, jlong c) {
+ if (!twoValidHandles(env, a, b)) return JNI_FALSE;
+ return isValidHandle(env, c, "Mandatory handle (third) passed as null");
+}
+
+static int fourValidHandles(JNIEnv* env, jlong a, jlong b, jlong c, jlong d) {
+ if (!threeValidHandles(env, a, b, c)) return JNI_FALSE;
+ return isValidHandle(env, d, "Mandatory handle (fourth) passed as null");
}
static unsigned long NativeBN_ERR_get_error(JNIEnv*, jclass) {
- return ERR_get_error();
+ return ERR_get_error();
}
static jstring NativeBN_ERR_error_string(JNIEnv* env, jclass, unsigned long e) {
- char* errStr = ERR_error_string(e, NULL);
- return env->NewStringUTF(errStr);
+ return env->NewStringUTF(ERR_error_string(e, NULL));
}
-static BIGNUM* NativeBN_BN_new(JNIEnv*, jclass) {
- return BN_new();
+static jlong NativeBN_BN_new(JNIEnv*, jclass) {
+ return static_cast<jlong>(reinterpret_cast<uintptr_t>(BN_new()));
}
-static void NativeBN_BN_free(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return;
- BN_free(a);
+static void NativeBN_BN_free(JNIEnv* env, jclass, jlong a) {
+ if (!oneValidHandle(env, a)) return;
+ BN_free(toBigNum(a));
}
-static int NativeBN_BN_cmp(JNIEnv* env, jclass, BIGNUM* a, BIGNUM* b) {
- if (!twoValidHandles(env, a, b)) return 1;
- return BN_cmp(a, b);
+static int NativeBN_BN_cmp(JNIEnv* env, jclass, jlong a, jlong b) {
+ if (!twoValidHandles(env, a, b)) return 1;
+ return BN_cmp(toBigNum(a), toBigNum(b));
}
-static jboolean NativeBN_BN_copy(JNIEnv* env, jclass, BIGNUM* to, BIGNUM* from) {
- if (!twoValidHandles(env, to, from)) return JNI_FALSE;
- return (BN_copy(to, from) != NULL);
+static jboolean NativeBN_BN_copy(JNIEnv* env, jclass, jlong to, jlong from) {
+ if (!twoValidHandles(env, to, from)) return JNI_FALSE;
+ return (BN_copy(toBigNum(to), toBigNum(from)) != NULL);
}
-static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, BIGNUM* a, unsigned long long dw, jboolean neg) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
+static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, jlong a0, unsigned long long dw, jboolean neg) {
+ if (!oneValidHandle(env, a0)) return JNI_FALSE;
unsigned int hi = dw >> 32; // This shifts without sign extension.
- int lo = (int)dw; // This truncates implicitely.
+ int lo = (int)dw; // This truncates implicitly.
// cf. litEndInts2bn:
+ BIGNUM* a = toBigNum(a0);
bn_check_top(a);
if (bn_wexpand(a, 2) != NULL) {
a->d[0] = lo;
@@ -116,40 +117,45 @@ static jboolean NativeBN_putULongInt(JNIEnv* env, jclass, BIGNUM* a, unsigned lo
else return JNI_FALSE;
}
-static jboolean NativeBN_putLongInt(JNIEnv* env, jclass cls, BIGNUM* a, long long dw) {
- if (dw >= 0) return NativeBN_putULongInt(env, cls, a, dw, JNI_FALSE);
- else return NativeBN_putULongInt(env, cls, a, -dw, JNI_TRUE);
+static jboolean NativeBN_putLongInt(JNIEnv* env, jclass cls, jlong a, long long dw) {
+ if (dw >= 0) {
+ return NativeBN_putULongInt(env, cls, a, dw, JNI_FALSE);
+ } else {
+ return NativeBN_putULongInt(env, cls, a, -dw, JNI_TRUE);
+ }
}
-static int NativeBN_BN_dec2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) {
- if (!oneValidHandle(env, a)) return -1;
- ScopedUtfChars chars(env, str);
- if (chars.c_str() == NULL) {
- return -1;
- }
- return BN_dec2bn(&a, chars.c_str());
+static int NativeBN_BN_dec2bn(JNIEnv* env, jclass, jlong a0, jstring str) {
+ if (!oneValidHandle(env, a0)) return -1;
+ ScopedUtfChars chars(env, str);
+ if (chars.c_str() == NULL) {
+ return -1;
+ }
+ BIGNUM* a = toBigNum(a0);
+ return BN_dec2bn(&a, chars.c_str());
}
-static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, BIGNUM* a, jstring str) {
- if (!oneValidHandle(env, a)) return -1;
- ScopedUtfChars chars(env, str);
- if (chars.c_str() == NULL) {
- return -1;
- }
- return BN_hex2bn(&a, chars.c_str());
+static int NativeBN_BN_hex2bn(JNIEnv* env, jclass, jlong a0, jstring str) {
+ if (!oneValidHandle(env, a0)) return -1;
+ ScopedUtfChars chars(env, str);
+ if (chars.c_str() == NULL) {
+ return -1;
+ }
+ BIGNUM* a = toBigNum(a0);
+ return BN_hex2bn(&a, chars.c_str());
}
-static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len, jboolean neg, BIGNUM* ret) {
- if (!oneValidHandle(env, ret)) return JNI_FALSE;
- ScopedByteArrayRO bytes(env, arr);
- if (bytes.get() == NULL) {
- return -1;
- }
- jboolean success = (BN_bin2bn(reinterpret_cast<const unsigned char*>(bytes.get()), len, ret) != NULL);
- if (success && neg) {
- BN_set_negative(ret, 1);
- }
- return success;
+static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len, jboolean neg, jlong ret) {
+ if (!oneValidHandle(env, ret)) return JNI_FALSE;
+ ScopedByteArrayRO bytes(env, arr);
+ if (bytes.get() == NULL) {
+ return -1;
+ }
+ jboolean success = (BN_bin2bn(reinterpret_cast<const unsigned char*>(bytes.get()), len, toBigNum(ret)) != NULL);
+ if (success && neg) {
+ BN_set_negative(toBigNum(ret), 1);
+ }
+ return success;
}
/**
@@ -160,133 +166,142 @@ static jboolean NativeBN_BN_bin2bn(JNIEnv* env, jclass, jbyteArray arr, int len,
* Also note that the same representation is used in the Cordoba Java Implementation of BigIntegers,
* whereof certain functionality is still being used.
*/
-static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int len, jboolean neg, BIGNUM* ret) {
- if (!oneValidHandle(env, ret)) return JNI_FALSE;
- bn_check_top(ret);
- if (len > 0) {
- ScopedIntArrayRO scopedArray(env, arr);
- if (scopedArray.get() == NULL) {
- return JNI_FALSE;
- }
+static jboolean NativeBN_litEndInts2bn(JNIEnv* env, jclass, jintArray arr, int len, jboolean neg, jlong ret0) {
+ if (!oneValidHandle(env, ret0)) return JNI_FALSE;
+ BIGNUM* ret = toBigNum(ret0);
+ bn_check_top(ret);
+ if (len > 0) {
+ ScopedIntArrayRO scopedArray(env, arr);
+ if (scopedArray.get() == NULL) {
+ return JNI_FALSE;
+ }
- STATIC_ASSERT(sizeof(BN_ULONG) == sizeof(jint), BN_ULONG_not_32_bit);
- const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get());
- if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) {
- int i = len; do { i--; ret->d[i] = tmpInts[i]; } while (i > 0);
- ret->top = len;
- ret->neg = neg;
- // need to call this due to clear byte at top if avoiding
- // having the top bit set (-ve number)
- // Basically get rid of top zero ints:
- bn_correct_top(ret);
- return JNI_TRUE;
- } else {
- return JNI_FALSE;
- }
- } else { // (len = 0) means value = 0 and sign will be 0, too.
- ret->top = 0;
- return JNI_TRUE;
+ STATIC_ASSERT(sizeof(BN_ULONG) == sizeof(jint), BN_ULONG_not_32_bit);
+ const BN_ULONG* tmpInts = reinterpret_cast<const BN_ULONG*>(scopedArray.get());
+ if ((tmpInts != NULL) && (bn_wexpand(ret, len) != NULL)) {
+ int i = len; do { i--; ret->d[i] = tmpInts[i]; } while (i > 0);
+ ret->top = len;
+ ret->neg = neg;
+ // need to call this due to clear byte at top if avoiding
+ // having the top bit set (-ve number)
+ // Basically get rid of top zero ints:
+ bn_correct_top(ret);
+ return JNI_TRUE;
+ } else {
+ return JNI_FALSE;
}
+ } else { // (len = 0) means value = 0 and sign will be 0, too.
+ ret->top = 0;
+ return JNI_TRUE;
+ }
}
#define BYTES2INT(bytes, k) \
- ( (bytes[k + 3] & 0xFF) \
- | (bytes[k + 2] & 0xFF) << 8 \
- | (bytes[k + 1] & 0xFF) << 16 \
- | (bytes[k + 0] & 0xFF) << 24 )
-
-static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes, int bytesLen, BIGNUM* ret) {
- // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes:
- bn_check_top(ret);
- // FIXME: assert bytesLen > 0
- int intLen = (bytesLen + 3) / 4;
- int firstNonzeroDigit = -2;
- if (bn_wexpand(ret, intLen) != NULL) {
- BN_ULONG* d = ret->d;
- BN_ULONG di;
- ret->top = intLen;
- int highBytes = bytesLen % 4;
- int k = bytesLen;
- // Put bytes to the int array starting from the end of the byte array
- int i = 0;
+ ((bytes[k + 3] & 0xff) | (bytes[k + 2] & 0xff) << 8 | (bytes[k + 1] & 0xff) << 16 | (bytes[k + 0] & 0xff) << 24)
+
+static jboolean negBigEndianBytes2bn(JNIEnv*, jclass, const unsigned char* bytes, int bytesLen, jlong ret0) {
+ BIGNUM* ret = toBigNum(ret0);
+
+ // We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes:
+ bn_check_top(ret);
+ // FIXME: assert bytesLen > 0
+ int intLen = (bytesLen + 3) / 4;
+ int firstNonzeroDigit = -2;
+ if (bn_wexpand(ret, intLen) != NULL) {
+ BN_ULONG* d = ret->d;
+ BN_ULONG di;
+ ret->top = intLen;
+ int highBytes = bytesLen % 4;
+ int k = bytesLen;
+ // Put bytes to the int array starting from the end of the byte array
+ int i = 0;
+ while (k > highBytes) {
+ k -= 4;
+ di = BYTES2INT(bytes, k);
+ if (di != 0) {
+ d[i] = -di;
+ firstNonzeroDigit = i;
+ i++;
while (k > highBytes) {
- k -= 4;
- di = BYTES2INT(bytes, k);
- if (di != 0) {
- d[i] = -di;
- firstNonzeroDigit = i;
- i++;
- while (k > highBytes) {
- k -= 4;
- d[i] = ~BYTES2INT(bytes, k);
- i++;
- }
- break;
- } else {
- d[i] = 0;
- i++;
- }
+ k -= 4;
+ d[i] = ~BYTES2INT(bytes, k);
+ i++;
}
- if (highBytes != 0) {
- di = -1;
- // Put the first bytes in the highest element of the int array
- if (firstNonzeroDigit != -2) {
- for (k = 0; k < highBytes; k++) {
- di = (di << 8) | (bytes[k] & 0xFF);
- }
- d[i] = ~di;
- } else {
- for (k = 0; k < highBytes; k++) {
- di = (di << 8) | (bytes[k] & 0xFF);
- }
- d[i] = -di;
- }
- }
- return JNI_TRUE;
+ break;
+ } else {
+ d[i] = 0;
+ i++;
+ }
}
- else return JNI_FALSE;
-}
-
-static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, int bytesLen, BIGNUM* ret) {
- if (!oneValidHandle(env, ret)) return JNI_FALSE;
- ScopedByteArrayRO bytes(env, arr);
- if (bytes.get() == NULL) {
- return -1;
- }
- jboolean success;
- const unsigned char* s = reinterpret_cast<const unsigned char*>(bytes.get());
- if ((bytes[0] & 0X80) == 0) { // Positive value!
- //
- // We can use the existing BN implementation for unsigned big endian bytes:
- //
- success = (BN_bin2bn(s, bytesLen, ret) != NULL);
- BN_set_negative(ret, JNI_FALSE);
- } else { // Negative value!
- //
- // We need to apply two's complement:
- //
- success = negBigEndianBytes2bn(env, cls, s, bytesLen, ret);
- BN_set_negative(ret, JNI_TRUE);
+ if (highBytes != 0) {
+ di = -1;
+ // Put the first bytes in the highest element of the int array
+ if (firstNonzeroDigit != -2) {
+ for (k = 0; k < highBytes; k++) {
+ di = (di << 8) | (bytes[k] & 0xFF);
+ }
+ d[i] = ~di;
+ } else {
+ for (k = 0; k < highBytes; k++) {
+ di = (di << 8) | (bytes[k] & 0xFF);
+ }
+ d[i] = -di;
+ }
}
- return success;
-}
-
-static long long NativeBN_longInt(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return -1;
- bn_check_top(a);
- int intLen = a->top;
- BN_ULONG* d = a->d;
- switch (intLen) {
+ return JNI_TRUE;
+ }
+ return JNI_FALSE;
+}
+
+static jboolean NativeBN_twosComp2bn(JNIEnv* env, jclass cls, jbyteArray arr, int bytesLen, jlong ret0) {
+ if (!oneValidHandle(env, ret0)) return JNI_FALSE;
+ BIGNUM* ret = toBigNum(ret0);
+
+ ScopedByteArrayRO bytes(env, arr);
+ if (bytes.get() == NULL) {
+ return -1;
+ }
+ jboolean success;
+ const unsigned char* s = reinterpret_cast<const unsigned char*>(bytes.get());
+ if ((bytes[0] & 0X80) == 0) { // Positive value!
+ //
+ // We can use the existing BN implementation for unsigned big endian bytes:
+ //
+ success = (BN_bin2bn(s, bytesLen, ret) != NULL);
+ BN_set_negative(ret, JNI_FALSE);
+ } else { // Negative value!
+ //
+ // We need to apply two's complement:
+ //
+ success = negBigEndianBytes2bn(env, cls, s, bytesLen, ret0);
+ BN_set_negative(ret, JNI_TRUE);
+ }
+ return success;
+}
+
+static long long NativeBN_longInt(JNIEnv* env, jclass, jlong a0) {
+ if (!oneValidHandle(env, a0)) return -1;
+ BIGNUM* a = toBigNum(a0);
+ bn_check_top(a);
+ int intLen = a->top;
+ BN_ULONG* d = a->d;
+ switch (intLen) {
case 0:
- return 0;
+ return 0;
case 1:
- if (!a->neg) return d[0] & 0X00000000FFFFFFFFLL;
- else return -(d[0] & 0X00000000FFFFFFFFLL);
+ if (!a->neg) {
+ return d[0] & 0X00000000FFFFFFFFLL;
+ } else {
+ return -(d[0] & 0X00000000FFFFFFFFLL);
+ }
default:
- if (!a->neg) return ((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL);
- else return -(((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL));
- }
+ if (!a->neg) {
+ return ((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL);
+ } else {
+ return -(((long long)d[1] << 32) | (d[0] & 0XFFFFFFFFLL));
+ }
+ }
}
static char* leadingZerosTrimmed(char* s) {
@@ -302,256 +317,233 @@ static char* leadingZerosTrimmed(char* s) {
return p;
}
-static jstring NativeBN_BN_bn2dec(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return NULL;
- char* tmpStr;
- char* retStr;
- tmpStr = BN_bn2dec(a);
- if (tmpStr != NULL) {
- retStr = leadingZerosTrimmed(tmpStr);
- jstring returnJString = env->NewStringUTF(retStr);
- OPENSSL_free(tmpStr);
- return returnJString;
- }
- else return NULL;
-}
-
-static jstring NativeBN_BN_bn2hex(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return NULL;
- char* tmpStr;
- char* retStr;
- tmpStr = BN_bn2hex(a);
- if (tmpStr != NULL) {
- retStr = leadingZerosTrimmed(tmpStr);
- jstring returnJString = env->NewStringUTF(retStr);
- OPENSSL_free(tmpStr);
- return returnJString;
- }
- else return NULL;
-}
-
-static jbyteArray NativeBN_BN_bn2bin(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return NULL;
- jbyteArray result = env->NewByteArray(BN_num_bytes(a));
- if (result == NULL) {
- return NULL;
- }
- ScopedByteArrayRW bytes(env, result);
- if (bytes.get() == NULL) {
- return NULL;
- }
- BN_bn2bin(a, reinterpret_cast<unsigned char*>(bytes.get()));
- return result;
-}
-
-static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return NULL;
- bn_check_top(a);
- int len = a->top;
- if (len == 0) {
- return NULL;
- }
- jintArray result = env->NewIntArray(len);
- if (result == NULL) {
- return NULL;
- }
- ScopedIntArrayRW ints(env, result);
- if (ints.get() == NULL) {
- return NULL;
- }
- BN_ULONG* ulongs = reinterpret_cast<BN_ULONG*>(ints.get());
- if (ulongs == NULL) {
- return NULL;
- }
- int i = len; do { i--; ulongs[i] = a->d[i]; } while (i > 0);
- return result;
-}
-
-static int NativeBN_sign(JNIEnv* env, jclass, BIGNUM* a) {
- if (!oneValidHandle(env, a)) return -2;
- if (BN_is_zero(a)) return 0;
- else if (BN_is_negative(a)) return -1;
- else return 1;
-}
-
-static void NativeBN_BN_set_negative(JNIEnv* env, jclass, BIGNUM* b, int n) {
- if (!oneValidHandle(env, b)) return;
- BN_set_negative(b, n);
-}
-
-static int NativeBN_bitLength(JNIEnv* env, jclass, BIGNUM* a) {
+static jstring NativeBN_BN_bn2dec(JNIEnv* env, jclass, jlong a) {
+ if (!oneValidHandle(env, a)) return NULL;
+ char* tmpStr = BN_bn2dec(toBigNum(a));
+ if (tmpStr == NULL) {
+ return NULL;
+ }
+ char* retStr = leadingZerosTrimmed(tmpStr);
+ jstring returnJString = env->NewStringUTF(retStr);
+ OPENSSL_free(tmpStr);
+ return returnJString;
+}
+
+static jstring NativeBN_BN_bn2hex(JNIEnv* env, jclass, jlong a) {
+ if (!oneValidHandle(env, a)) return NULL;
+ char* tmpStr = BN_bn2hex(toBigNum(a));
+ if (tmpStr == NULL) {
+ return NULL;
+ }
+ char* retStr = leadingZerosTrimmed(tmpStr);
+ jstring returnJString = env->NewStringUTF(retStr);
+ OPENSSL_free(tmpStr);
+ return returnJString;
+}
+
+static jbyteArray NativeBN_BN_bn2bin(JNIEnv* env, jclass, jlong a0) {
+ if (!oneValidHandle(env, a0)) return NULL;
+ BIGNUM* a = toBigNum(a0);
+ jbyteArray result = env->NewByteArray(BN_num_bytes(a));
+ if (result == NULL) {
+ return NULL;
+ }
+ ScopedByteArrayRW bytes(env, result);
+ if (bytes.get() == NULL) {
+ return NULL;
+ }
+ BN_bn2bin(a, reinterpret_cast<unsigned char*>(bytes.get()));
+ return result;
+}
+
+static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, jlong a0) {
+ if (!oneValidHandle(env, a0)) return NULL;
+ BIGNUM* a = toBigNum(a0);
+ bn_check_top(a);
+ int len = a->top;
+ if (len == 0) {
+ return NULL;
+ }
+ jintArray result = env->NewIntArray(len);
+ if (result == NULL) {
+ return NULL;
+ }
+ ScopedIntArrayRW ints(env, result);
+ if (ints.get() == NULL) {
+ return NULL;
+ }
+ BN_ULONG* ulongs = reinterpret_cast<BN_ULONG*>(ints.get());
+ if (ulongs == NULL) {
+ return NULL;
+ }
+ int i = len; do { i--; ulongs[i] = a->d[i]; } while (i > 0);
+ return result;
+}
+
+static int NativeBN_sign(JNIEnv* env, jclass, jlong a) {
+ if (!oneValidHandle(env, a)) return -2;
+ if (BN_is_zero(toBigNum(a))) {
+ return 0;
+ } else if (BN_is_negative(toBigNum(a))) {
+ return -1;
+ }
+ return 1;
+}
+
+static void NativeBN_BN_set_negative(JNIEnv* env, jclass, jlong b, int n) {
+ if (!oneValidHandle(env, b)) return;
+ BN_set_negative(toBigNum(b), n);
+}
+
+static int NativeBN_bitLength(JNIEnv* env, jclass, jlong a0) {
// We rely on: (BN_BITS2 == 32), i.e. BN_ULONG is unsigned int and has 4 bytes:
//
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- bn_check_top(a);
- int intLen = a->top;
- if (intLen == 0) return 0;
- BN_ULONG* d = a->d;
- int i = intLen - 1;
- BN_ULONG msd = d[i]; // most significant digit
- if (a->neg) {
- // Handle negative values correctly:
- // i.e. decrement the msd if all other digits are 0:
- // while ((i > 0) && (d[i] != 0)) { i--; }
- do { i--; } while (!((i < 0) || (d[i] != 0)));
- if (i < 0) msd--; // Only if all lower significant digits are 0 we decrement the most significant one.
- }
- return (intLen - 1) * 32 + BN_num_bits_word(msd);
-}
-
-static jboolean NativeBN_BN_is_bit_set(JNIEnv* env, jclass, BIGNUM* a, int n) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return (jboolean)BN_is_bit_set(a, n);
-}
-
-static jboolean NativeBN_modifyBit(JNIEnv* env, jclass, BIGNUM* a, int n, int op) {
-// ALOGD("NativeBN_BN_modifyBit");
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- switch (op) {
- case 1: return BN_set_bit(a, n);
- case 0: return BN_clear_bit(a, n);
- case -1:
- if (BN_is_bit_set(a, n)) return BN_clear_bit(a, n);
- else return BN_set_bit(a, n);
- }
- return JNI_FALSE;
-}
-
-static jboolean NativeBN_BN_shift(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, int n) {
- if (!twoValidHandles(env, r, a)) return JNI_FALSE;
- return (n >= 0) ? BN_lshift(r, a, n) : BN_rshift(r, a, -n);
+ if (!oneValidHandle(env, a0)) return JNI_FALSE;
+ BIGNUM* a = toBigNum(a0);
+ bn_check_top(a);
+ int intLen = a->top;
+ if (intLen == 0) return 0;
+ BN_ULONG* d = a->d;
+ int i = intLen - 1;
+ BN_ULONG msd = d[i]; // most significant digit
+ if (a->neg) {
+ // Handle negative values correctly:
+ // i.e. decrement the msd if all other digits are 0:
+ // while ((i > 0) && (d[i] != 0)) { i--; }
+ do { i--; } while (!((i < 0) || (d[i] != 0)));
+ if (i < 0) msd--; // Only if all lower significant digits are 0 we decrement the most significant one.
+ }
+ return (intLen - 1) * 32 + BN_num_bits_word(msd);
}
-static jboolean NativeBN_BN_add_word(JNIEnv* env, jclass, BIGNUM* a, BN_ULONG w) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return BN_add_word(a, w);
+static jboolean NativeBN_BN_is_bit_set(JNIEnv* env, jclass, jlong a, int n) {
+ if (!oneValidHandle(env, a)) return JNI_FALSE;
+ return BN_is_bit_set(toBigNum(a), n);
}
-static jboolean NativeBN_BN_sub_word(JNIEnv* env, jclass, BIGNUM* a, BN_ULONG w) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return BN_sub_word(a, w);
+static jboolean NativeBN_BN_shift(JNIEnv* env, jclass, jlong r, jlong a, int n) {
+ if (!twoValidHandles(env, r, a)) return JNI_FALSE;
+ return (n >= 0) ? BN_lshift(toBigNum(r), toBigNum(a), n) : BN_rshift(toBigNum(r), toBigNum(a), -n);
}
-static jboolean NativeBN_BN_mul_word(JNIEnv* env, jclass, BIGNUM* a, BN_ULONG w) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return BN_mul_word(a, w);
+static jboolean NativeBN_BN_add_word(JNIEnv* env, jclass, jlong a, BN_ULONG w) {
+ if (!oneValidHandle(env, a)) return JNI_FALSE;
+ return BN_add_word(toBigNum(a), w);
}
-static BN_ULONG NativeBN_BN_div_word(JNIEnv* env, jclass, BIGNUM* a, BN_ULONG w) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return BN_div_word(a, w);
+static jboolean NativeBN_BN_mul_word(JNIEnv* env, jclass, jlong a, BN_ULONG w) {
+ if (!oneValidHandle(env, a)) return JNI_FALSE;
+ return BN_mul_word(toBigNum(a), w);
}
-static BN_ULONG NativeBN_BN_mod_word(JNIEnv* env, jclass, BIGNUM* a, BN_ULONG w) {
- if (!oneValidHandle(env, a)) return JNI_FALSE;
- return BN_mod_word(a, w);
+static BN_ULONG NativeBN_BN_mod_word(JNIEnv* env, jclass, jlong a, BN_ULONG w) {
+ if (!oneValidHandle(env, a)) return JNI_FALSE;
+ return BN_mod_word(toBigNum(a), w);
}
-static jboolean NativeBN_BN_add(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) {
- if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
- return BN_add(r, a, b);
+static jboolean NativeBN_BN_add(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
+ if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
+ return BN_add(toBigNum(r), toBigNum(a), toBigNum(b));
}
-static jboolean NativeBN_BN_sub(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) {
- if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
- return BN_sub(r, a, b);
+static jboolean NativeBN_BN_sub(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
+ if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
+ return BN_sub(toBigNum(r), toBigNum(a), toBigNum(b));
}
-static jboolean NativeBN_BN_gcd(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) {
- if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_gcd(r, a, b, ctx.get());
+static jboolean NativeBN_BN_gcd(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
+ if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_gcd(toBigNum(r), toBigNum(a), toBigNum(b), ctx.get());
}
-static jboolean NativeBN_BN_mul(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* b) {
- if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_mul(r, a, b, ctx.get());
+static jboolean NativeBN_BN_mul(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
+ if (!threeValidHandles(env, r, a, b)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_mul(toBigNum(r), toBigNum(a), toBigNum(b), ctx.get());
}
-static jboolean NativeBN_BN_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p) {
- if (!threeValidHandles(env, r, a, p)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_exp(r, a, p, ctx.get());
+static jboolean NativeBN_BN_exp(JNIEnv* env, jclass, jlong r, jlong a, jlong p) {
+ if (!threeValidHandles(env, r, a, p)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_exp(toBigNum(r), toBigNum(a), toBigNum(p), ctx.get());
}
-static jboolean NativeBN_BN_div(JNIEnv* env, jclass, BIGNUM* dv, BIGNUM* rem, BIGNUM* m, BIGNUM* d) {
- if (!fourValidHandles(env, (rem ? rem : dv), (dv ? dv : rem), m, d)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_div(dv, rem, m, d, ctx.get());
+static jboolean NativeBN_BN_div(JNIEnv* env, jclass, jlong dv, jlong rem, jlong m, jlong d) {
+ if (!fourValidHandles(env, (rem ? rem : dv), (dv ? dv : rem), m, d)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_div(toBigNum(dv), toBigNum(rem), toBigNum(m), toBigNum(d), ctx.get());
}
-static jboolean NativeBN_BN_nnmod(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* m) {
- if (!threeValidHandles(env, r, a, m)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_nnmod(r, a, m, ctx.get());
+static jboolean NativeBN_BN_nnmod(JNIEnv* env, jclass, jlong r, jlong a, jlong m) {
+ if (!threeValidHandles(env, r, a, m)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_nnmod(toBigNum(r), toBigNum(a), toBigNum(m), ctx.get());
}
-static jboolean NativeBN_BN_mod_exp(JNIEnv* env, jclass, BIGNUM* r, BIGNUM* a, BIGNUM* p, BIGNUM* m) {
- if (!fourValidHandles(env, r, a, p, m)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_mod_exp(r, a, p, m, ctx.get());
+static jboolean NativeBN_BN_mod_exp(JNIEnv* env, jclass, jlong r, jlong a, jlong p, jlong m) {
+ if (!fourValidHandles(env, r, a, p, m)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_mod_exp(toBigNum(r), toBigNum(a), toBigNum(p), toBigNum(m), ctx.get());
}
-static jboolean NativeBN_BN_mod_inverse(JNIEnv* env, jclass, BIGNUM* ret, BIGNUM* a, BIGNUM* n) {
- if (!threeValidHandles(env, ret, a, n)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return (BN_mod_inverse(ret, a, n, ctx.get()) != NULL);
+static jboolean NativeBN_BN_mod_inverse(JNIEnv* env, jclass, jlong ret, jlong a, jlong n) {
+ if (!threeValidHandles(env, ret, a, n)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return (BN_mod_inverse(toBigNum(ret), toBigNum(a), toBigNum(n), ctx.get()) != NULL);
}
-static jboolean NativeBN_BN_generate_prime_ex(JNIEnv* env, jclass, BIGNUM* ret, int bits, jboolean safe,
- BIGNUM* add, BIGNUM* rem, jint cb) {
- if (!oneValidHandle(env, ret)) return JNI_FALSE;
- return BN_generate_prime_ex(ret, bits, safe, add, rem, reinterpret_cast<BN_GENCB*>(cb));
+static jboolean NativeBN_BN_generate_prime_ex(JNIEnv* env, jclass, jlong ret, int bits,
+ jboolean safe, jlong add, jlong rem, jlong cb) {
+ if (!oneValidHandle(env, ret)) return JNI_FALSE;
+ return BN_generate_prime_ex(toBigNum(ret), bits, safe, toBigNum(add), toBigNum(rem),
+ reinterpret_cast<BN_GENCB*>(cb));
}
-static jboolean NativeBN_BN_is_prime_ex(JNIEnv* env, jclass, BIGNUM* p, int nchecks, jint cb) {
- if (!oneValidHandle(env, p)) return JNI_FALSE;
- Unique_BN_CTX ctx(BN_CTX_new());
- return BN_is_prime_ex(p, nchecks, ctx.get(), reinterpret_cast<BN_GENCB*>(cb));
+static jboolean NativeBN_BN_is_prime_ex(JNIEnv* env, jclass, jlong p, int nchecks, BN_GENCB* cb) {
+ if (!oneValidHandle(env, p)) return JNI_FALSE;
+ Unique_BN_CTX ctx(BN_CTX_new());
+ return BN_is_prime_ex(toBigNum(p), nchecks, ctx.get(), cb);
}
static JNINativeMethod gMethods[] = {
- NATIVE_METHOD(NativeBN, BN_add, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_add_word, "(II)Z"),
- NATIVE_METHOD(NativeBN, BN_bin2bn, "([BIZI)Z"),
- NATIVE_METHOD(NativeBN, BN_bn2bin, "(I)[B"),
- NATIVE_METHOD(NativeBN, BN_bn2dec, "(I)Ljava/lang/String;"),
- NATIVE_METHOD(NativeBN, BN_bn2hex, "(I)Ljava/lang/String;"),
- NATIVE_METHOD(NativeBN, BN_cmp, "(II)I"),
- NATIVE_METHOD(NativeBN, BN_copy, "(II)Z"),
- NATIVE_METHOD(NativeBN, BN_dec2bn, "(ILjava/lang/String;)I"),
- NATIVE_METHOD(NativeBN, BN_div, "(IIII)Z"),
- NATIVE_METHOD(NativeBN, BN_div_word, "(II)I"),
- NATIVE_METHOD(NativeBN, BN_exp, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_free, "(I)V"),
- NATIVE_METHOD(NativeBN, BN_gcd, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_generate_prime_ex, "(IIZIII)Z"),
- NATIVE_METHOD(NativeBN, BN_hex2bn, "(ILjava/lang/String;)I"),
- NATIVE_METHOD(NativeBN, BN_is_bit_set, "(II)Z"),
- NATIVE_METHOD(NativeBN, BN_is_prime_ex, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_mod_exp, "(IIII)Z"),
- NATIVE_METHOD(NativeBN, BN_mod_inverse, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_mod_word, "(II)I"),
- NATIVE_METHOD(NativeBN, BN_mul, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_mul_word, "(II)Z"),
- NATIVE_METHOD(NativeBN, BN_new, "()I"),
- NATIVE_METHOD(NativeBN, BN_nnmod, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_set_negative, "(II)V"),
- NATIVE_METHOD(NativeBN, BN_shift, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_sub, "(III)Z"),
- NATIVE_METHOD(NativeBN, BN_sub_word, "(II)Z"),
- NATIVE_METHOD(NativeBN, ERR_error_string, "(I)Ljava/lang/String;"),
- NATIVE_METHOD(NativeBN, ERR_get_error, "()I"),
- NATIVE_METHOD(NativeBN, bitLength, "(I)I"),
- NATIVE_METHOD(NativeBN, bn2litEndInts, "(I)[I"),
- NATIVE_METHOD(NativeBN, litEndInts2bn, "([IIZI)Z"),
- NATIVE_METHOD(NativeBN, longInt, "(I)J"),
- NATIVE_METHOD(NativeBN, modifyBit, "(III)Z"),
- NATIVE_METHOD(NativeBN, putLongInt, "(IJ)Z"),
- NATIVE_METHOD(NativeBN, putULongInt, "(IJZ)Z"),
- NATIVE_METHOD(NativeBN, sign, "(I)I"),
- NATIVE_METHOD(NativeBN, twosComp2bn, "([BII)Z"),
+ NATIVE_METHOD(NativeBN, BN_add, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_add_word, "(JI)Z"),
+ NATIVE_METHOD(NativeBN, BN_bin2bn, "([BIZJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_bn2bin, "(J)[B"),
+ NATIVE_METHOD(NativeBN, BN_bn2dec, "(J)Ljava/lang/String;"),
+ NATIVE_METHOD(NativeBN, BN_bn2hex, "(J)Ljava/lang/String;"),
+ NATIVE_METHOD(NativeBN, BN_cmp, "(JJ)I"),
+ NATIVE_METHOD(NativeBN, BN_copy, "(JJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_dec2bn, "(JLjava/lang/String;)I"),
+ NATIVE_METHOD(NativeBN, BN_div, "(JJJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_exp, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_free, "(J)V"),
+ NATIVE_METHOD(NativeBN, BN_gcd, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_generate_prime_ex, "(JIZJJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_hex2bn, "(JLjava/lang/String;)I"),
+ NATIVE_METHOD(NativeBN, BN_is_bit_set, "(JI)Z"),
+ NATIVE_METHOD(NativeBN, BN_is_prime_ex, "(JIJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_mod_exp, "(JJJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_mod_inverse, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_mod_word, "(JI)I"),
+ NATIVE_METHOD(NativeBN, BN_mul, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_mul_word, "(JI)Z"),
+ NATIVE_METHOD(NativeBN, BN_new, "()J"),
+ NATIVE_METHOD(NativeBN, BN_nnmod, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, BN_set_negative, "(JI)V"),
+ NATIVE_METHOD(NativeBN, BN_shift, "(JJI)Z"),
+ NATIVE_METHOD(NativeBN, BN_sub, "(JJJ)Z"),
+ NATIVE_METHOD(NativeBN, ERR_error_string, "(J)Ljava/lang/String;"),
+ NATIVE_METHOD(NativeBN, ERR_get_error, "()J"),
+ NATIVE_METHOD(NativeBN, bitLength, "(J)I"),
+ NATIVE_METHOD(NativeBN, bn2litEndInts, "(J)[I"),
+ NATIVE_METHOD(NativeBN, litEndInts2bn, "([IIZJ)Z"),
+ NATIVE_METHOD(NativeBN, longInt, "(J)J"),
+ NATIVE_METHOD(NativeBN, putLongInt, "(JJ)Z"),
+ NATIVE_METHOD(NativeBN, putULongInt, "(JJZ)Z"),
+ NATIVE_METHOD(NativeBN, sign, "(J)I"),
+ NATIVE_METHOD(NativeBN, twosComp2bn, "([BIJ)Z"),
};
void register_java_math_NativeBN(JNIEnv* env) {
jniRegisterNativeMethods(env, "java/math/NativeBN", gMethods, NELEM(gMethods));