Hello! I am having a problem using the openSSL BigNum. See this code: 

#include <stdio.h>
#include <openssl/bn.h>

BIGNUM ee(BIGNUM *a, BIGNUM *b) {

BIGNUM *a0 = BN_new();
BIGNUM *b0 = BN_new();
BIGNUM *c = BN_new();
BIGNUM *p = BN_new();
BIGNUM *q = BN_new();
BIGNUM *r = BN_new();
BIGNUM *s = BN_new();
BIGNUM *quot = BN_new();
BIGNUM *new_r  = BN_new();
BIGNUM *new_s = BN_new();
BIGNUM *zero = BN_new();
BN_zero(zero);
BIGNUM *resultadoEe = BN_new();
BIGNUM *qb0 = BN_new();

*a0 = *a;
*b0 = *b;
BN_one(p);
BN_zero(q);
BN_zero(r);
BN_one(s);

printf("p: %s * a0: %s + q %s * b0: %s || a: %s, b: %s, c: %s, r: %s, s: %s\n", 
BN_bn2dec(p), BN_bn2dec(a0), BN_bn2dec(q), BN_bn2dec(b0), BN_bn2dec(a),         
BN_bn2dec(b), BN_bn2dec(c), BN_bn2dec(r), BN_bn2dec(s)); getchar();

while (BN_cmp(zero, b) != 0) { 
BN_div(quot, c, a, b, BN_CTX_new());        printf("teste b: %s\n", 
BN_bn2dec(b)); // c é o resto de a/b. quot é a divisão de a/b
*a = *b;
*b = *c;
//        new_r  = p - quot * r;
printf("tabós1 new_r : %s. quot: %s. r: %s\n", BN_bn2dec(new_r ), 
BN_bn2dec(quot), BN_bn2dec(r));
BN_mul(new_r , quot, r, BN_CTX_new());
        PRINTF(&QUOT;TABóS2 NEW_R : %S. QUOT: %S. R: %S\N&QUOT;, 
BN_BN2DEC(NEW_R ), BN_BN2DEC(QUOT), BN_BN2DEC(R));
BN_sub(new_r , p, new_r ); //O problema está aqui.........na verdade
 PRINTF(&QUOT;TABóS3 NEW_R : %S. QUOT: %S. R: %S\N&QUOT;, BN_BN2DEC(NEW_R ), 
BN_BN2DEC(QUOT), BN_BN2DEC(R));
//        new_s = q - quot * s;
BN_mul(new_s, quot, s, BN_CTX_new()); //Erro aqui também !!
BN_sub(new_s, q, new_s);
*p = *r;
*q = *s;
*r = *new_r ;
*s = *new_s;
printf("p: %s * a0: %s + q: %s * b0: %s || a: %s, b: %s, c: %s, r: %s, s: 
%s\n", BN_bn2dec(p), BN_bn2dec(a0), BN_bn2dec(q), BN_bn2dec(b0),BN_bn2dec    
(a), BN_bn2dec(b), BN_bn2dec(c), BN_bn2dec(r), BN_bn2dec(s)); getchar();
}

printf("p: %s * a0: %s + q %s * b0: %s\n", BN_bn2dec(p), BN_bn2dec(a0), 
BN_bn2dec(q), BN_bn2dec(b0));
BN_mul(resultadoEe, p, a0, BN_CTX_new());
BN_mul(qb0, q, b0, BN_CTX_new());
BN_add(resultadoEe, resultadoEe, qb0);
return (*resultadoEe);
}

int main () {

BIGNUM *retorno = BN_new();
BIGNUM *um = BN_new();
BN_one(um);
BIGNUM *tres = BN_new();
BN_one(tres);
BN_add(tres, tres, um);
BN_add(tres, tres, um);
printf("tres: %s\n", BN_bn2dec(tres));    

*retorno = ee(um,tres);

printf("resultado: %s\n", BN_bn2dec(retorno));    

getchar ();
return 0;
}

When the while is executed for the second time, the black printf prints the 
variables new_r and r as below: And this is wrong: 

teste b: 0
tabós1 new_r : 1. quot: 3. r: 1
tabós2 new_r : 474297672. quot: 3. r: 158099224
tabós3 new_r : -474297672. quot: 3. r: 158072883
p: 158072883 * a0: 1 + q: 0 * b0: 3 || a: 0, b: 0, c: 0, r: -474297672, s: 1

Can someone help me please? 

Thanks.

Reply via email to