source: core/c/jbigi/jbigi/src/jbigi.c @ 566a713

Last change on this file since 566a713 was 566a713, checked in by zzz <zzz@…>, 15 years ago

2005-02-16 jrandom

  • (Merged the 0.5-pre branch back into CVS HEAD)
  • Replaced the old tunnel routing crypto with the one specified in router/doc/tunnel-alt.html, including updates to the web console to view and tweak it.
  • Provide the means for routers to reject tunnel requests with a wider range of responses:

probabalistic rejection, due to approaching overload
transient rejection, due to temporary overload
bandwidth rejection, due to persistent bandwidth overload
critical rejection, due to general router fault (or imminent shutdown)

The different responses are factored into the profiles accordingly.

  • Replaced the old I2CP tunnel related options (tunnels.depthInbound, etc) with a series of new properties, relevent to the new tunnel routing code:

inbound.nickname (used on the console)
inbound.quantity (# of tunnels to use in any leaseSets)
inbound.backupQuantity (# of tunnels to keep in the ready)
inbound.length (# of remote peers in the tunnel)
inbound.lengthVariance (if > 0, permute the length by adding a random #

up to the variance. if < 0, permute the length
by adding or subtracting a random # up to the
variance)

outbound.* (same as the inbound, except for the, uh, outbound tunnels

in that client's pool)

There are other options, and more will be added later, but the above are
the most relevent ones.

  • Replaced Jetty 4.2.21 with Jetty 5.1.2
  • Compress all profile data on disk.
  • Adjust the reseeding functionality to work even when the JVM's http proxy is set.
  • Enable a poor-man's interactive-flow in the streaming lib by choking the max window size.
  • Reduced the default streaming lib max message size to 16KB (though still configurable by the user), also doubling the default maximum window size.
  • Replaced the RouterIdentity? in a Lease with its SHA256 hash.
  • Reduced the overall I2NP message checksum from a full 32 byte SHA256 to the first byte of the SHA256.
  • Added a new "netId" flag to let routers drop references to other routers who we won't be able to talk to.
  • Extended the timestamper to get a second (or third) opinion whenever it wants to actually adjust the clock offset.
  • Replaced that kludge of a timestamp I2NP message with a full blown DateMessage?.
  • Substantial memory optimizations within the router and the SDK to reduce GC churn. Client apps and the streaming libs have not been tuned, however.
  • More bugfixes thank you can shake a stick at.

2005-02-13 jrandom

  • Updated jbigi source to handle 64bit CPUs. The bundled jbigi.jar still only contains 32bit versions, so build your own, placing libjbigi.so in your install dir if necessary. (thanks mule!)
  • Added support for libjbigi-$os-athlon64 to NativeBigInteger? and CPUID (thanks spaetz!)
  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include <stdio.h>
2#include <gmp.h>
3#include "jbigi.h"
4
5/******** prototypes */
6
7void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue);
8void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue);
9
10
11/*****************************************
12 *****Native method implementations*******
13 *****************************************/
14
15/******** nativeModPow() */
16/*
17 * Class:     net_i2p_util_NativeBigInteger
18 * Method:    nativeModPow
19 * Signature: ([B[B[B)[B
20 *
21 * From the javadoc:
22 *
23 * calculate (base ^ exponent) % modulus.
24 * @param curVal big endian twos complement representation of the base (but it must be positive)
25 * @param exponent big endian twos complement representation of the exponent
26 * @param modulus big endian twos complement representation of the modulus
27 * @return big endian twos complement representation of (base ^ exponent) % modulus
28 */
29
30JNIEXPORT jbyteArray JNICALL Java_net_i2p_util_NativeBigInteger_nativeModPow
31        (JNIEnv* env, jclass cls, jbyteArray jbase, jbyteArray jexp, jbyteArray jmod) {
32        /* 1) Convert base, exponent, modulus into the format libgmp understands
33         * 2) Call libgmp's modPow.
34         * 3) Convert libgmp's result into a big endian twos complement number.
35         */
36
37        mpz_t mbase;
38        mpz_t mexp;
39        mpz_t mmod;
40        jbyteArray jresult;
41
42        convert_j2mp(env, jbase, &mbase);
43        convert_j2mp(env, jexp,  &mexp);
44        convert_j2mp(env, jmod,  &mmod);
45 
46                /* Perform the actual powmod. We use mmod for the result because it is
47         * always at least as big as the result.
48         */
49        mpz_powm(mmod, mbase, mexp, mmod);
50
51                convert_mp2j(env, mmod, &jresult);
52
53        mpz_clear(mbase);
54        mpz_clear(mexp);
55        mpz_clear(mmod);
56
57        return jresult;
58}
59
60/******** nativeDoubleValue() */
61/*
62 * Class:     net_i2p_util_NativeBigInteger
63 * Method:    nativeDoubleValue
64 * Signature: ([B)D
65 *
66 * From the Javadoc:
67 *
68 * Converts a BigInteger byte-array to a 'double'
69 * @param ba Big endian twos complement representation of the BigInteger to convert to a double
70 * @return The plain double-value represented by 'ba'
71 */
72JNIEXPORT jdouble JNICALL Java_net_i2p_util_NativeBigInteger_nativeDoubleValue
73(JNIEnv * env, jclass cls, jbyteArray jba){
74            /* 1) Convert the bytearray BigInteger value into the format libgmp understands
75         * 2) Call libgmp's mpz_get_d.
76         * 3) Convert libgmp's result into a big endian twos complement number.
77         */
78        mpz_t mval;
79                jdouble retval;
80        convert_j2mp(env, jba, &mval);
81
82                retval = mpz_get_d(mval);
83                mpz_clear(mval);
84                return retval;
85}
86
87/******************************
88 *****Conversion methods*******
89 ******************************/
90
91/*Luckily we can use GMP's mpz_import() and mpz_export() functions to convert from/to
92 *BigInteger.toByteArray() representation.
93 */
94
95/******** convert_j2mp() */
96/*
97 * Initializes the GMP value with enough preallocated size, and converts the
98 * Java value into the GMP value. The value that mvalue points to should be
99 * uninitialized
100 */
101
102void convert_j2mp(JNIEnv* env, jbyteArray jvalue, mpz_t* mvalue)
103{
104        jsize size;
105        jbyte* jbuffer;
106                //int sign;
107
108        size = (*env)->GetArrayLength(env, jvalue);
109        jbuffer = (*env)->GetByteArrayElements(env, jvalue, NULL);
110
111        mpz_init2(*mvalue, sizeof(jbyte) * 8 * size); //preallocate the size
112
113        /* void mpz_import(
114         *   mpz_t rop, size_t count, int order, int size, int endian,
115         *   size_t nails, const void *op);
116         *
117         * order = 1
118         *   order can be 1 for most significant word first or -1 for least
119         *   significant first.
120         * endian = 1
121         *   Within each word endian can be 1 for most significant byte first,
122         *   -1 for least significant first.
123         * nails = 0
124         *   The most significant nails bits of each word are skipped, this can
125         *   be 0 to use the full words.
126         */
127        mpz_import(*mvalue, size, 1, sizeof(jbyte), 1, 0, (void*)jbuffer);
128                /*Uncomment this to support negative integer values,
129                not tested though..
130                sign = jbuffer[0] < 0?-1:1;
131                if(sign == -1)
132                        mpz_neg(*mvalue,*mvalue);
133                */
134        (*env)->ReleaseByteArrayElements(env, jvalue, jbuffer, JNI_ABORT);
135}
136
137/******** convert_mp2j() */
138/*
139 * Converts the GMP value into the Java value; Doesn't do anything else.
140 * Pads the resulting jbyte array with 0, so the twos complement value is always
141 * positive.
142 */
143
144void convert_mp2j(JNIEnv* env, mpz_t mvalue, jbyteArray* jvalue)
145{
146        // size_t not jsize to work with 64bit CPUs (do we need to update this
147        // elsewhere, and/or adjust memory alloc sizes?)
148        size_t size; 
149        jbyte* buffer;
150        jboolean copy;
151                //int i;
152
153        copy = JNI_FALSE;
154
155        /* sizeinbase() + 7 => Ceil division */
156        size = (mpz_sizeinbase(mvalue, 2) + 7) / 8 + sizeof(jbyte);
157        *jvalue = (*env)->NewByteArray(env, size);
158
159        buffer = (*env)->GetByteArrayElements(env, *jvalue, &copy);
160        buffer[0] = 0x00;
161                //Uncomment the comments below to support negative integer values,
162                //not very well-tested though..
163                //if(mpz_sgn(mvalue) >=0){
164                mpz_export((void*)&buffer[1], &size, 1, sizeof(jbyte), 1, 0, mvalue);
165                //}else{
166                //      mpz_add_ui(mvalue,mvalue,1);
167                //      mpz_export((void*)&buffer[1], &size, 1, sizeof(jbyte), 1, 0, mvalue);
168                //      for(i =0;i<=size;i++){ //This could be done more effectively
169                //              buffer[i]=~buffer[i];
170                //      }
171                //}
172
173                /* mode has (supposedly) no effect if elems is not a copy of the
174         * elements in array
175         */
176        (*env)->ReleaseByteArrayElements(env, *jvalue, buffer, 0);
177        //mode has (supposedly) no effect if elems is not a copy of the elements in array
178}
179
180/******** eof */
Note: See TracBrowser for help on using the repository browser.