 Timestamp:
 Jul 12, 2004 5:12:22 AM (17 years ago)
 Branches:
 master
 Children:
 7b9db07
 Parents:
 0f60ac5a
 gitauthor:
 ugha <ugha> (07/12/04 05:12:22)
 gitcommitter:
 zzz <zzz@…> (07/12/04 05:12:22)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

core/c/src/jbigi.c
r0f60ac5a rf2f2613 3 3 #include "jbigi.h" 4 4 5 /********/ 6 //function prototypes 7 8 //FIXME: should these go into jbigi.h?  ughabugha 5 /******** prototypes */ 9 6 10 7 void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue); 11 8 void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue); 12 9 13 /******** /10 /******** nativeModPow() */ 14 11 /* 15 12 * Class: net_i2p_util_NativeBigInteger … … 28 25 JNIEXPORT jbyteArray JNICALL Java_net_i2p_util_NativeBigInteger_nativeModPow 29 26 (JNIEnv* env, jclass cls, jbyteArray jbase, jbyteArray jexp, jbyteArray jmod) { 30 // convert base, exponent, modulus into the format libgmp understands 31 // call libgmp's modPow 32 // convert libgmp's result into a big endian twos complement number 27 /* 1) Convert base, exponent, modulus into the format libgmp understands 28 * 2) Call libgmp's modPow. 29 * 3) Convert libgmp's result into a big endian twos complement number. 30 * 31 * Luckily we can use GMP's mpz_import() and mpz_export() functions. 32 */ 33 33 34 34 mpz_t mbase; 35 35 mpz_t mexp; 36 36 mpz_t mmod; 37 //mpz_t mresult;38 37 jbyteArray jresult; 39 38 … … 42 41 convert_j2mp(env, jmod, &mmod); 43 42 44 //gmp_printf("mbase =%Zd\n", mbase); 45 //gmp_printf("mexp =%Zd\n", mexp); 46 //gmp_printf("mmod =%Zd\n", mmod); 47 43 /* Perform the actual powmod. We use mmod for the result because it is 44 * always at least as big as the result. 45 */ 48 46 mpz_powm(mmod, mbase, mexp, mmod); 49 //we use mod for the result because it is always at least as big50 51 //gmp_printf("mresult=%Zd\n", mmod);52 47 53 48 convert_mp2j(env, mmod, &jresult); 54 //convert_j2mp(env, jresult, &mresult);55 56 //gmp_printf("", mpz_cmp(mmod, mresult) == 0 ? "true" : "false");57 49 58 50 mpz_clear(mbase); 59 51 mpz_clear(mexp); 60 52 mpz_clear(mmod); 61 //mpz_clear(mresult);62 53 63 54 return jresult; 64 55 } 65 56 66 /******** /57 /******** convert_j2mp() */ 67 58 /* 68 59 * Initializes the GMP value with enough preallocated size, and converts the 69 * Java value into the GMP value. The value that mvalue is pointint to70 * should beuninitialized60 * Java value into the GMP value. The value that mvalue points to should be 61 * uninitialized 71 62 */ 72 63 … … 82 73 83 74 /* 84 * void mpz_import (mpz_t rop, size_t count, int order, int size, int endian, size_t nails, const void *op) 85 * order = 1  order can be 1 for most significant word first or 1 for least significant first. 86 * endian = 1  Within each word endian can be 1 for most significant byte first, 1 for least significant first 87 * nails = 0  The most significant nails bits of each word are skipped, this can be 0 to use the full words 75 * void mpz_import( 76 * mpz_t rop, size_t count, int order, int size, int endian, 77 * size_t nails, const void *op); 78 * 79 * order = 1 80 * order can be 1 for most significant word first or 1 for least 81 * significant first. 82 * endian = 1 83 * Within each word endian can be 1 for most significant byte first, 84 * 1 for least significant first. 85 * nails = 0 86 * The most significant nails bits of each word are skipped, this can 87 * be 0 to use the full words. 88 88 */ 89 89 mpz_import(*mvalue, size, 1, sizeof(jbyte), 1, 0, (void*)jbuffer); … … 91 91 } 92 92 93 /******** /93 /******** convert_mp2j() */ 94 94 /* 95 95 * Converts the GMP value into the Java value; Doesn't do anything else. 96 * Pads the resulting jbyte array with 0, so the twos complement value is always 97 * positive. 96 98 */ 97 99 … … 104 106 copy = JNI_FALSE; 105 107 106 size = (mpz_sizeinbase(mvalue, 2) + 7) / 8 + sizeof(jbyte); //+7 => ceil division 108 /* sizeinbase() + 7 => Ceil division */ 109 size = (mpz_sizeinbase(mvalue, 2) + 7) / 8 + sizeof(jbyte); 107 110 *jvalue = (*env)>NewByteArray(env, size); 108 111 109 112 buffer = (*env)>GetByteArrayElements(env, *jvalue, ©); 110 113 111 buffer[0] = 0 ;114 buffer[0] = 0x00; 112 115 113 116 /* 114 * void *mpz_export (void *rop, size_t *count, int order, int size, int endian, size_t nails, mpz_t op) 117 * void *mpz_export( 118 * void *rop, size_t *count, int order, int size, 119 * int endian, size_t nails, mpz_t op); 115 120 */ 116 121 mpz_export((void*)&buffer[1], &size, 1, sizeof(jbyte), 1, 0, mvalue); 117 122 123 /* mode has (supposedly) no effect if elems is not a copy of the 124 * elements in array 125 */ 118 126 (*env)>ReleaseByteArrayElements(env, *jvalue, buffer, 0); 119 //mode has (supposedly) no effect if elems is not a copy of the elements in array120 127 } 121 128 122 /********/ 123 129 /******** eof */
Note: See TracChangeset
for help on using the changeset viewer.