All pastes #417773 Raw Edit

BNJR

public c v1 · immutable
#417773 ·published 2007-03-31 03:38 UTC
rendered paste body
#include <openssl/bn.h>#include <stdio.h>BIGNUM * seed;void ab(BIGNUM * a){    printf("%s\n", BN_bn2dec(a));}BIGNUM * BIGNUM_xor(BIGNUM * r ,BIGNUM * a ,BIGNUM * b){    if(a == NULL || b == NULL)        return (BIGNUM *)-1;     BIGNUM * retVal = 0;        if ( 0 != r )       retVal = r;    else    {       retVal = BN_new();       if ( 0 == retVal )           return (BIGNUM *)-1;    }    BN_clear(retVal);        BIGNUM *at, *bt;       if ( a->top < b->top )    {       at = b;       bt = a;    }    else    {       at = a;       bt = b;    }       bn_expand2(retVal, at->top);       int i;    for (i = 0; i < bt->top; i++)       retVal->d[i] = at->d[i] ^ bt->d[i];    for (;i < at->top; i++)       retVal->d[i] = at->d[i];        retVal->top = at->top;    bn_fix_top(retVal);        return retVal;}BIGNUM * BIGNUM_and(BIGNUM * r ,BIGNUM * a ,BIGNUM * b){    if(a == NULL || b == NULL)        return (BIGNUM *)-1;     BIGNUM * retVal = 0;        if ( 0 != r )       retVal = r;    else    {       retVal = BN_new();       if ( 0 == retVal )           return (BIGNUM *)-1;    }    BN_clear(retVal);        BIGNUM *at, *bt;       if ( a->top < b->top )    {       at = b;       bt = a;    }    else    {       at = a;       bt = b;    }       bn_expand2(retVal, at->top);       int i;    for (i = 0; i < bt->top; i++)       retVal->d[i] = at->d[i] & bt->d[i];    for (;i < at->top; i++)       retVal->d[i] = 0;        retVal->top = at->top;    bn_fix_top(retVal);        return retVal;}BIGNUM * next(int bits){    BIGNUM * a = BN_new();    BIGNUM * b = BN_new();        BN_CTX *ctx = BN_CTX_new();    BN_CTX_start(ctx);        BN_dec2bn(&a, "25214903917");    BN_dec2bn(&b, "11");        BN_mul(seed, a, seed, ctx);    BN_add(seed, seed, b);        BN_dec2bn(&b, "1");        BN_lshift(a, b, 48);    BN_sub(a, a, b);    seed = BIGNUM_and(0, seed, a);        BIGNUM * res = BN_new();    res = BN_dup(seed);        if(res->neg == 0)        BN_rshift(res, res, (48 - bits));    else    {        BN_rshift(res, res, (48 - bits));        BN_dec2bn(&b, "2");        BN_lshift(b, b, ~(48 - bits));        BN_add(res, res, b);    }        return res;}BIGNUM * nextInt(BIGNUM * n){    BN_CTX *ctx = BN_CTX_new();    BN_CTX_start(ctx);        BIGNUM * a = BN_new();        if(n->neg == 1) return NULL;        /*    BIGNUM * neg = BN_new();    BN_copy(neg ,n);    BN_set_negative(neg,1);        if(BN_cmp(BIGNUM_and(0, n, neg), n) == 0) // is power of 2.    {        BN_mul(n, next(31), n, ctx);        BN_rshift(a, a, 31);        printf("caca\n");        return a;    }*/        BIGNUM * bits = BN_new();    BIGNUM * val = BN_new();    do {        bits = next(31);        BN_mod(val, bits, n, ctx);        BIGNUM * b = BN_new();                BN_dec2bn(&a, "1");        BN_sub(b, n, a);        BN_sub(a, bits, val);        BN_add(a, b, a);    } while(BN_is_negative(a) != 0);    return val;}BIGNUM * nextLong(void){    BIGNUM * a = BN_new();    BIGNUM * b = BN_new();    a = next(32);    BN_lshift(a, a, 32);    b = next(32);    BN_add(a, a, b);    return a;}void setSeed(BIGNUM * seed_init){    BIGNUM * a = BN_new();    BIGNUM * b = BN_new();        BN_dec2bn(&a, "25214903917");    BN_dec2bn(&b, "1");        seed_init = BIGNUM_xor(0, seed_init, a);    BN_lshift(a, b, 48);    BN_sub(a, a, b);    seed = BIGNUM_and(0, seed_init, a);}char * makes1(){       seed = BN_new();    BN_dec2bn(&seed, "5406");    setSeed(seed);        BIGNUM * a = BN_new();    BN_dec2bn(&a, "99999999");    a = nextInt(a);        return BN_bn2dec(a);}char * makes2(){    return "00014";}static const char H18[]="0123456789ABCDEFGH";/* FONCTION EN TRAVAUX */char *BN_bn2h18(const BIGNUM *a)	{	int i,j,v,z=0;	char *buf;	char *p;	buf=(char *)malloc(a->top*BN_BYTES*2+2);	p=buf;	if (a->neg) *(p++)='-';	if (BN_is_zero(a)) *(p++)='0';	for (i=a->top-1; i >=0; i--)		{		for (j=BN_BITS2-8; j >= 0; j-=8)			{			/* strip leading zeros */			v=((int)(a->d[i]>>(long)j))&0xff;			if (z || (v != 0))				{				*(p++)=H18[v>>4];				*(p++)=H18[v&0x11];				z=1;				}			}		}	*p='\0';	return(buf);}char * makes3(char * s1, char * s2){    BN_CTX *ctx = BN_CTX_new();    BN_CTX_start(ctx);        BIGNUM * a = BN_new();    BIGNUM * b = BN_new();    BIGNUM * c = BN_new();        BN_dec2bn(&a, s1);    BN_dec2bn(&b, s2);    BN_dec2bn(&c, "123456789");        BN_mul(b, b, c, ctx);    BN_add(a, a, b);    setSeed(a);        a = nextLong(); // \o/ BUG : ca tombe pas loin, mais c'est pas encore ça \o/        printf("%s\n", BN_bn2h18(a));        return BN_bn2dec(a);}int main(int argc, char *argv[]){      BN_CTX *ctx = BN_CTX_new();    BN_CTX_start(ctx);        char * s1 = makes1();    char * s2 = makes2();       char * s3 = makes3(s1, s2);        BIGNUM * a = BN_new();    BN_dec2bn(&a, "20");    printf("%s\n", BN_bn2h18(a));        return 0;}