RSA - 10


end;

procedure BN_a_mod_b(var a,b,res: TBigNum);

var k: Integer;

var n1,n2,n3: TBigNum;

begin

FillChar(n3,sizeof(n3),0);

if (BN_a_cmp_b(b,n3)=0) then Exit;

Move(a,n1,sizeof(a));

while (BN_a_cmp_b(n1,b)>=0) do

begin

k := BN_a_upbit(n1) - BN_a_upbit(b);

BN_a_shl_k(b,k,n2);

if (BN_a_cmp_b(n2,n1)>0) then

begin

BN_a_shr_k(n2,1,n3);

Move(n3,n2,sizeof(n3));

end;

BN_a_sub_b(n1,n2,n3);

Move(n3,n1,sizeof(n3));

end;

Move(n1,res,sizeof(n1));

end;

procedure BN_a_div_b(var a,b,res: TBigNum);

var k: Integer;

var n1,n2,n3: TBigNum;

begin

18.2

FillChar(res,sizeof(res),0);

FillChar(n3,sizeof(n3),0);

if (BN_a_cmp_b(b,n3)=0) then Exit;

Move(a,n1,sizeof(a));

while (BN_a_cmp_b(n1,b)>=0) do

begin

k := BN_a_upbit(n1) - BN_a_upbit(b);

BN_a_shl_k(b,k,n2);

if (BN_a_cmp_b(n2,n1)>0) then

begin

BN_a_shr_k(n2,1,n3);

Move(n3,n2,sizeof(n3));

Dec(k);

end;

BN_a_sub_b(n1,n2,n3);

Move(n3,n1,sizeof(n3));

BN_a_setbit_k(res,k);

end;

end;

procedure BN_a_exp_b_mod_c(var a,b,c,res: TBigNum);

var i,n: Integer;

var n1,n2,n3: TBigNum;

begin

FillChar(n3,sizeof(n3),0);

if (BN_a_cmp_b(c,n3)=0) then Exit;

for i := 0 to BIGNUM_DWORD do res[i] := 0;

if (BN_a_cmp_b(b,n3)=0) then

begin

res[0] := 1;

Exit;

end;

Move(a,n1,sizeof(a));

for i := 0 to BIGNUM_DWORD do n2[i] := 0;

n2[0] := 1;

n := BN_a_upbit(b)-1;

i := 0;

while (i<=n) do

begin

if ( (b[i shr 5] shr (i and 31)) and 1 = 1 ) then

begin

18.2

BN_a_mul_b(n2,n1,n3);

BN_a_mod_b(n3,c,n2);

end;

BN_a_mul_b(n1,n1,n3);

BN_a_mod_b(n3,c,n1);

Inc(i);

end;

Move(n2,res,sizeof(n2));

end;

procedure BN_ab_GCD(var a,b,res: TBigNum);

var i: Integer;

var n1,n2,n3,nzero: TBigNum;

begin

res[0] := 1;

for i := 1 to BIGNUM_DWORD do res[i] := 0;

for i := 0 to BIGNUM_DWORD do nzero[i] := 0;

if (BN_a_cmp_b(a,nzero)=0) or (BN_a_cmp_b(b,nzero)=0) then Exit;

if (BN_a_cmp_b(a,b)>0) then