Changeset f2f2613 for core/c


Ignore:
Timestamp:
Jul 12, 2004 5:12:22 AM (16 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
7b9db07
Parents:
0f60ac5a
git-author:
ugha <ugha> (07/12/04 05:12:22)
git-committer:
zzz <zzz@…> (07/12/04 05:12:22)
Message:

Minior cleanups — removed commented out debugging code, wrote better
comments.
(ugha)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • core/c/src/jbigi.c

    r0f60ac5a rf2f2613  
    33#include "jbigi.h"
    44
    5 /********/
    6 //function prototypes
    7 
    8 //FIXME: should these go into jbigi.h? -- ughabugha
     5/******** prototypes */
    96
    107void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue);
    118void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue);
    129
    13 /********/
     10/******** nativeModPow() */
    1411/*
    1512 * Class:     net_i2p_util_NativeBigInteger
     
    2825JNIEXPORT jbyteArray JNICALL Java_net_i2p_util_NativeBigInteger_nativeModPow
    2926        (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         */
    3333
    3434        mpz_t mbase;
    3535        mpz_t mexp;
    3636        mpz_t mmod;
    37         //mpz_t mresult;
    3837        jbyteArray jresult;
    3938
     
    4241        convert_j2mp(env, jmod,  &mmod);
    4342
    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         */
    4846        mpz_powm(mmod, mbase, mexp, mmod);
    49         //we use mod for the result because it is always at least as big
    50 
    51         //gmp_printf("mresult=%Zd\n", mmod);
    5247
    5348        convert_mp2j(env, mmod, &jresult);
    54         //convert_j2mp(env, jresult, &mresult);
    55 
    56         //gmp_printf("", mpz_cmp(mmod, mresult) == 0 ? "true" : "false");
    5749
    5850        mpz_clear(mbase);
    5951        mpz_clear(mexp);
    6052        mpz_clear(mmod);
    61         //mpz_clear(mresult);
    6253
    6354        return jresult;
    6455}
    6556
    66 /********/
     57/******** convert_j2mp() */
    6758/*
    6859 * Initializes the GMP value with enough preallocated size, and converts the
    69  * Java value into the GMP value. The value that mvalue is pointint to
    70  * should be uninitialized
     60 * Java value into the GMP value. The value that mvalue points to should be
     61 * uninitialized
    7162 */
    7263
     
    8273
    8374        /*
    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.
    8888         */
    8989        mpz_import(*mvalue, size, 1, sizeof(jbyte), 1, 0, (void*)jbuffer);
     
    9191}
    9292
    93 /********/
     93/******** convert_mp2j() */
    9494/*
    9595 * 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.
    9698 */
    9799
     
    104106        copy = JNI_FALSE;
    105107
    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);
    107110        *jvalue = (*env)->NewByteArray(env, size);
    108111
    109112        buffer = (*env)->GetByteArrayElements(env, *jvalue, &copy);
    110113
    111         buffer[0] = 0;
     114        buffer[0] = 0x00;
    112115
    113116        /*
    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);
    115120         */
    116121        mpz_export((void*)&buffer[1], &size, 1, sizeof(jbyte), 1, 0, mvalue);
    117122
     123        /* mode has (supposedly) no effect if elems is not a copy of the
     124         * elements in array
     125         */
    118126        (*env)->ReleaseByteArrayElements(env, *jvalue, buffer, 0);
    119         //mode has (supposedly) no effect if elems is not a copy of the elements in array
    120127}
    121128
    122 /********/
    123 
     129/******** eof */
Note: See TracChangeset for help on using the changeset viewer.