great renaming (cont.)
This commit is contained in:
34
core/c/build.sh
Normal file
34
core/c/build.sh
Normal file
@ -0,0 +1,34 @@
|
||||
#!/bin/sh
|
||||
# linux settings:
|
||||
CC="gcc"
|
||||
ANT="ant"
|
||||
JAVA="java"
|
||||
|
||||
COMPILEFLAGS="-fPIC -Wall"
|
||||
LINKFLAGS="-shared -Wl,-soname,libjbigi.so"
|
||||
|
||||
INCLUDES="-Iinclude -I$JAVA_HOME/include -I$JAVA_HOME/include/linux"
|
||||
INCLUDELIBS="-lgmp"
|
||||
STATICLIBS=""
|
||||
|
||||
LIBFILE="libjbigi.so"
|
||||
|
||||
# jrandom's mingw setup:
|
||||
#INCLUDES="-Iinclude -Ic:/software/j2sdk1.4.2/include/win32/ -Ic:/software/j2sdk1.4.2/include/ -Ic:/dev/gmp-4.1.2/"
|
||||
#LINKFLAGS="-shared -Wl,--kill-at"
|
||||
#LIBFILE="jbigi.dll"
|
||||
#INCLUDELIBS=""
|
||||
#STATICLIBS="c:/dev/libgmp.a"
|
||||
|
||||
rm -f jbigi.o $LIBFILE
|
||||
$CC -c $COMPILEFLAGS $INCLUDES src/jbigi.c
|
||||
$CC $LINKFLAGS $INCLUDES $INCLUDELIBS -o $LIBFILE jbigi.o $STATICLIBS
|
||||
|
||||
echo "built, now testing"
|
||||
(cd ../java/src/ ; $ANT )
|
||||
LD_LIBRARY_PATH=. $JAVA -cp ../java/src/i2p.jar -DloggerConfigLocation=../java/src/logger.config net.i2p.util.NativeBigInteger
|
||||
|
||||
|
||||
echo ""
|
||||
echo ""
|
||||
echo "test complete. please review the lines 'native run time:', 'java run time:', and 'native = '"
|
44
core/c/include/jbigi.h
Normal file
44
core/c/include/jbigi.h
Normal file
@ -0,0 +1,44 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class net_i2p_util_NativeBigInteger */
|
||||
|
||||
#ifndef _Included_net_i2p_util_NativeBigInteger
|
||||
#define _Included_net_i2p_util_NativeBigInteger
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#undef net_i2p_util_NativeBigInteger_serialVersionUID
|
||||
#define net_i2p_util_NativeBigInteger_serialVersionUID -8742448824652078965LL
|
||||
#undef net_i2p_util_NativeBigInteger_LONG_MASK
|
||||
#define net_i2p_util_NativeBigInteger_LONG_MASK 4294967295LL
|
||||
/* Inaccessible static: bitsPerDigit */
|
||||
/* Inaccessible static: SMALL_PRIME_PRODUCT */
|
||||
#undef net_i2p_util_NativeBigInteger_MAX_CONSTANT
|
||||
#define net_i2p_util_NativeBigInteger_MAX_CONSTANT 16L
|
||||
/* Inaccessible static: posConst */
|
||||
/* Inaccessible static: negConst */
|
||||
/* Inaccessible static: ZERO */
|
||||
/* Inaccessible static: ONE */
|
||||
/* Inaccessible static: TWO */
|
||||
/* Inaccessible static: bnExpModThreshTable */
|
||||
/* Inaccessible static: trailingZeroTable */
|
||||
/* Inaccessible static: zeros */
|
||||
/* Inaccessible static: digitsPerLong */
|
||||
/* Inaccessible static: longRadix */
|
||||
/* Inaccessible static: digitsPerInt */
|
||||
/* Inaccessible static: intRadix */
|
||||
#undef net_i2p_util_NativeBigInteger_serialVersionUID
|
||||
#define net_i2p_util_NativeBigInteger_serialVersionUID -8287574255936472291LL
|
||||
/* Inaccessible static: _nativeOk */
|
||||
/*
|
||||
* Class: net_i2p_util_NativeBigInteger
|
||||
* Method: nativeModPow
|
||||
* Signature: ([B[B[B)[B
|
||||
*/
|
||||
JNIEXPORT jbyteArray JNICALL Java_net_i2p_util_NativeBigInteger_nativeModPow
|
||||
(JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
123
core/c/src/jbigi.c
Normal file
123
core/c/src/jbigi.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include <stdio.h>
|
||||
#include <gmp.h>
|
||||
#include "jbigi.h"
|
||||
|
||||
/********/
|
||||
//function prototypes
|
||||
|
||||
//FIXME: should these go into jbigi.h? -- ughabugha
|
||||
|
||||
void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue);
|
||||
void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue);
|
||||
|
||||
/********/
|
||||
/*
|
||||
* Class: net_i2p_util_NativeBigInteger
|
||||
* Method: nativeModPow
|
||||
* Signature: ([B[B[B)[B
|
||||
*
|
||||
* From the javadoc:
|
||||
*
|
||||
* calculate (base ^ exponent) % modulus.
|
||||
* @param curVal big endian twos complement representation of the base (but it must be positive)
|
||||
* @param exponent big endian twos complement representation of the exponent
|
||||
* @param modulus big endian twos complement representation of the modulus
|
||||
* @return big endian twos complement representation of (base ^ exponent) % modulus
|
||||
*/
|
||||
|
||||
JNIEXPORT jbyteArray JNICALL Java_net_i2p_util_NativeBigInteger_nativeModPow
|
||||
(JNIEnv* env, jclass cls, jbyteArray jbase, jbyteArray jexp, jbyteArray jmod) {
|
||||
// convert base, exponent, modulus into the format libgmp understands
|
||||
// call libgmp's modPow
|
||||
// convert libgmp's result into a big endian twos complement number
|
||||
|
||||
mpz_t mbase;
|
||||
mpz_t mexp;
|
||||
mpz_t mmod;
|
||||
//mpz_t mresult;
|
||||
jbyteArray jresult;
|
||||
|
||||
convert_j2mp(env, jbase, &mbase);
|
||||
convert_j2mp(env, jexp, &mexp);
|
||||
convert_j2mp(env, jmod, &mmod);
|
||||
|
||||
//gmp_printf("mbase =%Zd\n", mbase);
|
||||
//gmp_printf("mexp =%Zd\n", mexp);
|
||||
//gmp_printf("mmod =%Zd\n", mmod);
|
||||
|
||||
mpz_powm(mmod, mbase, mexp, mmod);
|
||||
//we use mod for the result because it is always at least as big
|
||||
|
||||
//gmp_printf("mresult=%Zd\n", mmod);
|
||||
|
||||
convert_mp2j(env, mmod, &jresult);
|
||||
//convert_j2mp(env, jresult, &mresult);
|
||||
|
||||
//gmp_printf("", mpz_cmp(mmod, mresult) == 0 ? "true" : "false");
|
||||
|
||||
mpz_clear(mbase);
|
||||
mpz_clear(mexp);
|
||||
mpz_clear(mmod);
|
||||
//mpz_clear(mresult);
|
||||
|
||||
return jresult;
|
||||
}
|
||||
|
||||
/********/
|
||||
/*
|
||||
* Initializes the GMP value with enough preallocated size, and converts the
|
||||
* Java value into the GMP value. The value that mvalue is pointint to
|
||||
* should be uninitialized
|
||||
*/
|
||||
|
||||
void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue)
|
||||
{
|
||||
jsize size;
|
||||
jbyte* jbuffer;
|
||||
|
||||
size = (*env)->GetArrayLength(env, jvalue);
|
||||
jbuffer = (*env)->GetByteArrayElements(env, jvalue, NULL);
|
||||
|
||||
mpz_init2(*mvalue, sizeof(jbyte) * 8 * size); //preallocate the size
|
||||
|
||||
/*
|
||||
* void mpz_import (mpz_t rop, size_t count, int order, int size, int endian, size_t nails, const void *op)
|
||||
* order = 1 - order can be 1 for most significant word first or -1 for least significant first.
|
||||
* endian = 1 - Within each word endian can be 1 for most significant byte first, -1 for least significant first
|
||||
* nails = 0 - The most significant nails bits of each word are skipped, this can be 0 to use the full words
|
||||
*/
|
||||
mpz_import(*mvalue, size, 1, sizeof(jbyte), 1, 0, (void*)jbuffer);
|
||||
(*env)->ReleaseByteArrayElements(env, jvalue, jbuffer, JNI_ABORT);
|
||||
}
|
||||
|
||||
/********/
|
||||
/*
|
||||
* Converts the GMP value into the Java value; Doesn't do anything else.
|
||||
*/
|
||||
|
||||
void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue)
|
||||
{
|
||||
jsize size;
|
||||
jbyte* buffer;
|
||||
jboolean copy;
|
||||
|
||||
copy = JNI_FALSE;
|
||||
|
||||
size = (mpz_sizeinbase(mvalue, 2) + 7) / 8 + sizeof(jbyte); //+7 => ceil division
|
||||
*jvalue = (*env)->NewByteArray(env, size);
|
||||
|
||||
buffer = (*env)->GetByteArrayElements(env, *jvalue, ©);
|
||||
|
||||
buffer[0] = 0;
|
||||
|
||||
/*
|
||||
* void *mpz_export (void *rop, size_t *count, int order, int size, int endian, size_t nails, mpz_t op)
|
||||
*/
|
||||
mpz_export((void*)&buffer[1], &size, 1, sizeof(jbyte), 1, 0, mvalue);
|
||||
|
||||
(*env)->ReleaseByteArrayElements(env, *jvalue, buffer, 0);
|
||||
//mode has (supposedly) no effect if elems is not a copy of the elements in array
|
||||
}
|
||||
|
||||
/********/
|
||||
|
Reference in New Issue
Block a user