356 lines
10 KiB
C
356 lines
10 KiB
C
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
struct bn {
|
|
unsigned int* _p;
|
|
unsigned long _i;
|
|
};
|
|
|
|
struct fn {
|
|
struct bn numerator;
|
|
struct bn denominator;
|
|
};
|
|
|
|
#define DECIMAL_PLACES 1024
|
|
|
|
#define BN_ZERO (struct bn){NULL,0}
|
|
|
|
bool bn_bigger(struct bn _bn,unsigned int _i){
|
|
for(int i=1;i<_bn._i;i++){
|
|
if(_bn._p[i]!=0){
|
|
return true;
|
|
}
|
|
}
|
|
return _bn._p[0]>_i;
|
|
}
|
|
|
|
bool bn_smaller(struct bn _bn,unsigned int _i){
|
|
for(int i=1;i<_bn._i;i++){
|
|
if(_bn._p[i]!=0){
|
|
return false;
|
|
}
|
|
}
|
|
return _bn._p[0]<_i;
|
|
}
|
|
|
|
struct bn bn_add_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int _nn){
|
|
if(output->_p==NULL){
|
|
output->_p=malloc(_bn._i*sizeof(unsigned int));
|
|
output->_i=_bn._i;
|
|
}
|
|
if(output->_i<_bn._i){
|
|
unsigned int* _np = realloc(output->_p,_bn._i*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = _bn._i;
|
|
}
|
|
}
|
|
unsigned long res = (unsigned long)_bn._p[offset]+_nn;
|
|
if(res>UINT32_MAX){
|
|
if(offset>=_bn._i-1){
|
|
unsigned int* _np = realloc(output->_p,(output->_i+1)*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i +=1;
|
|
}
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
output->_p[offset+1] = 1;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
bn_add_nn(_bn,output,offset+1,1);
|
|
}
|
|
}else{
|
|
if(offset==_bn._i-1){
|
|
output->_p[offset] = res;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res;
|
|
bn_add_nn(_bn,output,offset+1,0);
|
|
}
|
|
}
|
|
}
|
|
|
|
struct bn bn_add_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset,unsigned int carry){
|
|
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i:_bn2._i;
|
|
if(output->_p==NULL){
|
|
output->_p=malloc(_ni*sizeof(unsigned int));
|
|
output->_i=_ni;
|
|
}
|
|
if(output->_i<_ni){
|
|
unsigned int* _np = realloc(output->_p,_ni*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = _ni;
|
|
}
|
|
}
|
|
unsigned long res;
|
|
if(offset<_bn1._i&&offset<_bn2._i){
|
|
res = (unsigned long)_bn1._p[offset]+(unsigned long)_bn2._p[offset]+carry;
|
|
}else if(offset>=_bn1._i){
|
|
res = (unsigned long)_bn2._p[offset]+carry;
|
|
}else{
|
|
res = (unsigned long)_bn1._p[offset]+carry;
|
|
}
|
|
if(res>UINT32_MAX){
|
|
if(offset>=output->_i-1){
|
|
unsigned int* _np = realloc(output->_p,output->_i+1);
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i +=1;
|
|
}
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
output->_p[offset+1] = 1;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
bn_add_bn(_bn1,_bn2,output,offset+1,1);
|
|
}
|
|
}else{
|
|
if(offset==output->_i-1){
|
|
output->_p[offset] = res;
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res;
|
|
bn_add_bn(_bn1,_bn2,output,offset+1,0);
|
|
}
|
|
}
|
|
}
|
|
|
|
struct bn bn_muti_nn(struct bn _bn,struct bn* output,unsigned long offset,unsigned int carry,unsigned int _nn){
|
|
if(output->_p==NULL){
|
|
output->_p = malloc((_bn._i)*sizeof(unsigned int));
|
|
output->_i = _bn._i;
|
|
}
|
|
if(output->_i<_bn._i){
|
|
unsigned int* _np = realloc(output->_p,_bn._i*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = _bn._i;
|
|
}
|
|
}
|
|
if(bn_bigger(*output,0)&&offset==0){
|
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
|
}
|
|
unsigned long res = ((unsigned long)_bn._p[offset])*_nn+carry;
|
|
if(offset<_bn._i-1){
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
bn_muti_nn(_bn,output,offset+1,res/((unsigned long)UINT32_MAX+1),_nn);
|
|
return *output;
|
|
}else{
|
|
if(res>UINT32_MAX){
|
|
unsigned int* _np = realloc(output->_p,(_bn._i+1)*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = _bn._i+1;
|
|
}
|
|
output->_p[offset] = res%((unsigned long)UINT32_MAX+1);
|
|
output->_p[offset+1] = res/((unsigned long)UINT32_MAX+1);
|
|
return *output;
|
|
}else{
|
|
output->_p[offset] = res;
|
|
return *output;
|
|
}
|
|
}
|
|
}
|
|
|
|
struct bn bn_left_move(struct bn* _bn,unsigned long offset){
|
|
if(offset==0){
|
|
return *_bn;
|
|
}
|
|
unsigned int* _np = realloc(_bn->_p,(_bn->_i+offset)*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
_bn->_p = _np;
|
|
_bn->_i += offset;
|
|
}
|
|
for(int i=_bn->_i-offset-1;i>=0;i--){
|
|
_bn->_p[i+offset] = _bn->_p[i];
|
|
}
|
|
memset(_bn->_p,0,offset*sizeof(unsigned int));
|
|
return *_bn;
|
|
}
|
|
|
|
struct bn bn_muti_bn(struct bn _bn1,struct bn _bn2,struct bn* output,unsigned long offset){
|
|
unsigned long _ni = _bn1._i>_bn2._i?_bn1._i*2:_bn2._i*2;
|
|
if(output->_p==NULL){
|
|
output->_p = malloc(_ni*sizeof(unsigned int));
|
|
output->_i = _ni;
|
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
|
}
|
|
if(output->_i < _ni){
|
|
unsigned long old_i = output->_i;
|
|
unsigned int* _np = realloc(output->_p, _ni * sizeof(unsigned int));
|
|
if(_np != NULL){
|
|
if(_ni > old_i){
|
|
memset(_np + old_i, 0, (_ni - old_i) * sizeof(unsigned int));
|
|
}
|
|
output->_p = _np;
|
|
output->_i = _ni;
|
|
}
|
|
}
|
|
if(bn_bigger(*output,0)&&offset==0){
|
|
memset(output->_p,0,output->_i*sizeof(unsigned int));
|
|
}
|
|
struct bn res = BN_ZERO,carry = BN_ZERO;
|
|
if(offset<_bn2._i-1){
|
|
bn_muti_nn(_bn1,&res,0,0,_bn2._p[offset]);
|
|
bn_left_move(&res,offset);
|
|
bn_add_bn(*output,res,&carry,0,0);
|
|
unsigned int* _np = realloc(output->_p,carry._i*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = carry._i;
|
|
}
|
|
memcpy(output->_p,carry._p,output->_i*sizeof(unsigned int));
|
|
bn_muti_bn(_bn1,_bn2,output,offset+1);
|
|
return *output;
|
|
}else{
|
|
bn_muti_nn(_bn1,&res,0,0,_bn2._p[offset]);
|
|
bn_left_move(&res,offset);
|
|
bn_add_bn(*output,res,&carry,0,0);
|
|
unsigned int* _np = realloc(output->_p,carry._i*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
output->_p = _np;
|
|
output->_i = carry._i;
|
|
}
|
|
memcpy(output->_p,carry._p,output->_i*sizeof(unsigned int));
|
|
return *output;
|
|
}
|
|
}
|
|
|
|
void bn_copy(struct bn* _des,struct bn _src){
|
|
if(_des->_p==NULL){
|
|
_des->_p = malloc(_src._i*sizeof(unsigned int));
|
|
_des->_i=_src._i;
|
|
}else{
|
|
unsigned int* _np = realloc(_des->_p,_src._i*sizeof(unsigned int));
|
|
if(_np!=NULL){
|
|
_des->_p=_np;
|
|
_des->_i=_src._i;
|
|
return;
|
|
}
|
|
}
|
|
memcpy(_des->_p,_src._p,_des->_i*sizeof(unsigned int));
|
|
return;
|
|
}
|
|
|
|
struct bn bn_power(struct bn* _bn,unsigned long index){
|
|
if(index<0){
|
|
return BN_ZERO;
|
|
}
|
|
if(index==0){
|
|
struct bn res = nn2bn(1);
|
|
bn_copy(_bn,res);
|
|
free(res._p);
|
|
return nn2bn(1);
|
|
}
|
|
if(index==1){
|
|
return *_bn;
|
|
}
|
|
struct bn res = BN_ZERO,copy = BN_ZERO;
|
|
bn_copy(&res,*_bn);bn_copy(©,*_bn);
|
|
for(int i=0;i<index-1;i++){
|
|
bn_muti_bn(*_bn,copy,&res,0);
|
|
bn_copy(_bn,res);
|
|
}
|
|
free(res._p);free(copy._p);
|
|
return *_bn;
|
|
}
|
|
|
|
struct bn nn2bn(unsigned long _i){
|
|
if(_i>UINT32_MAX){
|
|
unsigned int* _p=malloc(2*sizeof(int));
|
|
_p[1]=_i>>32;
|
|
_p[0]=_i%((unsigned long)UINT32_MAX+1);
|
|
return (struct bn){_p,2};
|
|
}else{
|
|
unsigned int* _p=malloc(sizeof(int));
|
|
_p[0]=_i%((unsigned long)UINT32_MAX+1);
|
|
return (struct bn){_p,1};
|
|
}
|
|
}
|
|
|
|
unsigned int bn_remainder(struct bn _bn,unsigned long offset,unsigned int last_remainder,unsigned int divisor){
|
|
unsigned long res;
|
|
if(offset!=0){
|
|
res = _bn._p[_bn._i-1-offset]+((unsigned long)last_remainder<<32);
|
|
}else{
|
|
res = _bn._p[_bn._i-1-offset];
|
|
}
|
|
if(offset+1==_bn._i){
|
|
return res%divisor;
|
|
}else{
|
|
return bn_remainder(_bn,offset+1,res%divisor,divisor);
|
|
}
|
|
}
|
|
|
|
struct bn bn_division(struct bn _bn,struct bn last_result,unsigned long offset,unsigned int last_remainder,unsigned int divisor){
|
|
unsigned long res;
|
|
if(offset!=0){
|
|
res = _bn._p[_bn._i-1-offset]+((unsigned long)last_remainder<<32);
|
|
}else{
|
|
res = _bn._p[_bn._i-1-offset];
|
|
}
|
|
if(last_result._p==NULL){
|
|
last_result._p=malloc(_bn._i*sizeof(unsigned int));
|
|
memset(last_result._p,0,_bn._i*sizeof(unsigned int));
|
|
last_result._i=_bn._i;
|
|
}
|
|
last_result._p[last_result._i-offset-1] = res/divisor;
|
|
if(offset+1==_bn._i){
|
|
return last_result;
|
|
}else{
|
|
return bn_division(_bn,last_result,offset+1,res%divisor,divisor);
|
|
}
|
|
}
|
|
|
|
char* bn2string(struct bn _i){//don't forget free this!!!
|
|
char* res = malloc(_i._i*10);
|
|
struct bn n1,n2;
|
|
n1._p = malloc(_i._i*sizeof(unsigned int));n1._i=_i._i;n2._p = malloc(_i._i*sizeof(unsigned int));n2._i=_i._i;
|
|
memcpy(n1._p,_i._p,n1._i*sizeof(unsigned int));
|
|
memset(n2._p,0,n2._i*sizeof(unsigned int));
|
|
unsigned long char_offset = 0;
|
|
while(true){
|
|
res[char_offset] = bn_remainder(n1,0,0,10);
|
|
if(bn_smaller(n1,10)){
|
|
break;
|
|
}
|
|
char_offset++;
|
|
bn_division(n1,n2,0,0,10);
|
|
memset(n1._p,0,n1._i*sizeof(unsigned int));
|
|
res[char_offset] = bn_remainder(n2,0,0,10);
|
|
if(bn_smaller(n2,10)){
|
|
break;
|
|
}
|
|
char_offset++;
|
|
bn_division(n2,n1,0,0,10);
|
|
memset(n2._p,0,n2._i*sizeof(unsigned int));
|
|
}
|
|
free(n1._p);free(n2._p);
|
|
char* out = malloc(char_offset+2);
|
|
memset(out,0,char_offset+2);
|
|
for(int i=0;i<char_offset+1;i++){
|
|
out[i]='0'+res[char_offset-i];
|
|
}
|
|
free(res);
|
|
return out;
|
|
}
|
|
|
|
struct fn Chudnovsky(unsigned long _k){
|
|
|
|
}
|
|
|
|
int main(){
|
|
struct bn n1 = nn2bn(0xFFFFFFFFFFFFFFFFULL);
|
|
struct bn n2 = bn_muti_nn(n1,&BN_ZERO,0,0,0xffffffff);
|
|
struct bn n3 = bn_muti_bn(n1,n2,&n3,0);
|
|
char* out = bn2string(n3);
|
|
printf("%s\n",out);
|
|
free(out);
|
|
} |