summaryrefslogtreecommitdiff
path: root/luni
diff options
context:
space:
mode:
authorHans Boehm <hboehm@google.com>2020-08-14 15:20:05 -0700
committeralk3pInjection <webmaster@raspii.tech>2021-09-27 21:17:05 +0800
commita927d5f623887f9bf4061e7734d45fc0f6d68d2b (patch)
tree655b0b7afa3930c152374be01548fdd86292968f /luni
parentdd65dc9842c777dea4978f6f2ad9ed83762fbebc (diff)
Fix BigInteger.remainder check and BigInteger TODOsHEADlineage-18.1
Restore the BigInteger.remainder fallback test to check for small results. Use RemainderKnuth if either the divisor or result is short, as was done before. Checking for a short dividend doesn't really make much sense, since that makes the result short as well. Failed BN context allocations used to just pass null to the next operation. Looking at the BN_mul implementation, it doesn't appear that ends well. Explicitly check for that case. Remove some potentially useful but unused methods from NativeBN. Bug: 163898786 Test: Build and boot AOSP Change-Id: I97a46199d8cf69acf29bcfc2f614c2205ff16262
Diffstat (limited to 'luni')
-rw-r--r--luni/src/main/java/libcore/math/NativeBN.java10
-rw-r--r--luni/src/main/native/libcore_math_NativeBN.cpp26
2 files changed, 6 insertions, 30 deletions
diff --git a/luni/src/main/java/libcore/math/NativeBN.java b/luni/src/main/java/libcore/math/NativeBN.java
index fb1cb78a50..8b2ea0f1ee 100644
--- a/luni/src/main/java/libcore/math/NativeBN.java
+++ b/luni/src/main/java/libcore/math/NativeBN.java
@@ -14,9 +14,6 @@
* limitations under the License.
*/
-// TODO: Prune out the methods we no longer need after replacing the BigInteger
-// code.
-
package libcore.math;
/**
@@ -36,13 +33,6 @@ public final class NativeBN {
// word at index 0.
public static native int[] bn2litEndInts(long 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(long b, int n);
- // void BN_set_negative(BIGNUM *b, int n);
-
public static native void BN_mul(long r, long a, long b);
// int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
diff --git a/luni/src/main/native/libcore_math_NativeBN.cpp b/luni/src/main/native/libcore_math_NativeBN.cpp
index a123014bc5..dc4b947358 100644
--- a/luni/src/main/native/libcore_math_NativeBN.cpp
+++ b/luni/src/main/native/libcore_math_NativeBN.cpp
@@ -14,8 +14,6 @@
* limitations under the License.
*/
-// TODO: Check that we handle context allocation failures correctly.
-
#define LOG_TAG "NativeBN"
#include <stdio.h>
@@ -142,36 +140,26 @@ static jintArray NativeBN_bn2litEndInts(JNIEnv* env, jclass, jlong a0) {
return result;
}
-static int NativeBN_sign(JNIEnv*, jclass, jlong a) {
- 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*, jclass, jlong b, int n) {
- BN_set_negative(toBigNum(b), n);
-}
-
static void NativeBN_BN_mul(JNIEnv* env, jclass, jlong r, jlong a, jlong b) {
Unique_BN_CTX ctx(BN_CTX_new());
- if (!BN_mul(toBigNum(r), toBigNum(a), toBigNum(b), ctx.get())) {
+ BN_CTX* ctxp = ctx.get();
+ if (!ctxp || !BN_mul(toBigNum(r), toBigNum(a), toBigNum(b), ctxp)) {
throwException(env);
}
}
static void NativeBN_BN_div(JNIEnv* env, jclass, jlong q, jlong rem, jlong num, jlong divisor) {
Unique_BN_CTX ctx(BN_CTX_new());
- if (!BN_div(toBigNum(q), toBigNum(rem), toBigNum(num), toBigNum(divisor), ctx.get())) {
+ BN_CTX* ctxp = ctx.get();
+ if (!ctxp || !BN_div(toBigNum(q), toBigNum(rem), toBigNum(num), toBigNum(divisor), ctxp)) {
throwException(env);
}
}
static void NativeBN_BN_mod_exp(JNIEnv* env, jclass, jlong r, jlong a, jlong p, jlong m) {
Unique_BN_CTX ctx(BN_CTX_new());
- if (!BN_mod_exp(toBigNum(r), toBigNum(a), toBigNum(p), toBigNum(m), ctx.get())) {
+ BN_CTX* ctxp = ctx.get();
+ if (!ctxp || !BN_mod_exp(toBigNum(r), toBigNum(a), toBigNum(p), toBigNum(m), ctxp)) {
throwException(env);
}
}
@@ -182,10 +170,8 @@ static JNINativeMethod gMethods[] = {
NATIVE_METHOD(NativeBN, BN_mod_exp, "(JJJJ)V"),
NATIVE_METHOD(NativeBN, BN_mul, "(JJJ)V"),
NATIVE_METHOD(NativeBN, BN_new, "()J"),
- NATIVE_METHOD(NativeBN, BN_set_negative, "(JI)V"),
NATIVE_METHOD(NativeBN, bn2litEndInts, "(J)[I"),
NATIVE_METHOD(NativeBN, litEndInts2bn, "([IIZJ)V"),
- NATIVE_METHOD(NativeBN, sign, "(J)I"),
};
void register_libcore_math_NativeBN(JNIEnv* env) {
jniRegisterNativeMethods(env, "libcore/math/NativeBN", gMethods, NELEM(gMethods));