1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package java.math;
18
19final class NativeBN {
20
21    public static native long BN_new();
22    // BIGNUM *BN_new(void);
23
24    public static native void BN_free(long a);
25    // void BN_free(BIGNUM *a);
26
27    public static native int BN_cmp(long a, long b);
28    // int BN_cmp(const BIGNUM *a, const BIGNUM *b);
29
30    public static native void BN_copy(long to, long from);
31    // BIGNUM *BN_copy(BIGNUM *to, const BIGNUM *from);
32
33    public static native void putLongInt(long a, long dw);
34    public static native void putULongInt(long a, long dw, boolean neg);
35
36    public static native int BN_dec2bn(long a, String str);
37    // int BN_dec2bn(BIGNUM **a, const char *str);
38
39    public static native int BN_hex2bn(long a, String str);
40    // int BN_hex2bn(BIGNUM **a, const char *str);
41
42    public static native void BN_bin2bn(byte[] s, int len, boolean neg, long ret);
43    // BIGNUM * BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret);
44    // BN-Docu: s is taken as unsigned big endian;
45    // Additional parameter: neg.
46
47    public static native void litEndInts2bn(int[] ints, int len, boolean neg, long ret);
48
49    public static native void twosComp2bn(byte[] s, int len, long ret);
50
51
52    public static native long longInt(long a);
53    // unsigned long BN_get_word(BIGNUM *a);
54
55    public static native String BN_bn2dec(long a);
56    // char * BN_bn2dec(const BIGNUM *a);
57
58    public static native String BN_bn2hex(long a);
59    // char * BN_bn2hex(const BIGNUM *a);
60
61    public static native byte[] BN_bn2bin(long a);
62    // Returns result byte[] AND NOT length.
63    // int BN_bn2bin(const BIGNUM *a, unsigned char *to);
64
65    public static native int[] bn2litEndInts(long a);
66
67    public static native int sign(long a);
68    // Returns -1, 0, 1 AND NOT boolean.
69    // #define BN_is_negative(a) ((a)->neg != 0)
70
71    public static native void BN_set_negative(long b, int n);
72    // void BN_set_negative(BIGNUM *b, int n);
73
74    public static native int bitLength(long a);
75
76    public static native boolean BN_is_bit_set(long a, int n);
77    // int BN_is_bit_set(const BIGNUM *a, int n);
78
79    public static native void BN_shift(long r, long a, int n);
80    // int BN_shift(BIGNUM *r, const BIGNUM *a, int n);
81
82    public static native void BN_add_word(long a, int w);
83    // ATTENTION: w is treated as unsigned.
84    // int BN_add_word(BIGNUM *a, BN_ULONG w);
85
86    public static native void BN_mul_word(long a, int w);
87    // ATTENTION: w is treated as unsigned.
88    // int BN_mul_word(BIGNUM *a, BN_ULONG w);
89
90    public static native int BN_mod_word(long a, int w);
91    // ATTENTION: w is treated as unsigned.
92    // BN_ULONG BN_mod_word(BIGNUM *a, BN_ULONG w);
93
94    public static native void BN_add(long r, long a, long b);
95    // int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
96
97    public static native void BN_sub(long r, long a, long b);
98    // int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
99
100    public static native void BN_gcd(long r, long a, long b);
101    // int BN_gcd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
102
103    public static native void BN_mul(long r, long a, long b);
104    // int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
105
106    public static native void BN_exp(long r, long a, long p);
107    // int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
108
109    public static native void BN_div(long dv, long rem, long m, long d);
110    // int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
111
112    public static native void BN_nnmod(long r, long a, long m);
113    // int BN_nnmod(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
114
115    public static native void BN_mod_exp(long r, long a, long p, long m);
116    // int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);
117
118    public static native void BN_mod_inverse(long ret, long a, long n);
119    // BIGNUM * BN_mod_inverse(BIGNUM *ret, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
120
121
122    public static native void BN_generate_prime_ex(long ret, int bits, boolean safe,
123                                                   long add, long rem);
124    // int BN_generate_prime_ex(BIGNUM *ret, int bits, int safe,
125    //         const BIGNUM *add, const BIGNUM *rem, BN_GENCB *cb);
126
127    public static native boolean BN_primality_test(long candidate, int checks,
128                                                   boolean do_trial_division);
129    // int BN_primality_test(int *is_probably_prime, const BIGNUM *candidate, int checks,
130    //                       BN_CTX *ctx, int do_trial_division, BN_GENCB *cb);
131    // Returns *is_probably_prime on success and throws an exception on error.
132
133    public static native long getNativeFinalizer();
134    // &BN_free
135
136    /** Returns the expected size of the native allocation for a BIGNUM.
137     */
138    public static long size() {
139        // 36 bytes is an empirically determined approximation.
140        return 36;
141    }
142}
143