RSA - 11


begin

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

Move(b,n2,sizeof(a));

end

else

begin

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

Move(a,n2,sizeof(a));

end;

while (BN_a_cmp_b(n2,nzero)<>0) do

begin

BN_a_mod_b(n1,n2,n3);

Move(n2,n1,sizeof(n1));

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

end;

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

end;

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

var i: Integer;

var n1,n2,n3,n4,n5,n6,n7: TBigNum;

var nzero,none: TBigNum;

18.2

begin

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

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

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

BN_ab_GCD(a,b,n4);

if (BN_a_cmp_b(n4,none)<>0) then Exit;

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

Move(a,n2,sizeof(a));

Move(none,n7,sizeof(a));

repeat

BN_a_div_b(n1,n2,n3);

BN_a_mod_b(n1,n2,n4);

Move(n2,n1,sizeof(n2));

Move(n4,n2,sizeof(n2));

BN_a_mul_b(n3,n7,n5);

BN_a_sub_b(res,n5,n6);

Move(n7,res,sizeof(n7));

Move(n6,n7,sizeof(n6));

until (BN_a_cmp_b(n4,nzero)=0);

if (res[BIGNUM_DWORD] and $80000000 <> 0) then

begin

BN_a_add_b(res,b,n7);

Move(n7,res,sizeof(n6));

end;

end;

function BN_PrimeTest(var a: TBigNum): Integer;

var i,j: Integer;

var oldseed: LongInt;

var nzero,none,nn: TBigNum;

var n1,n2,n3,n4: TBigNum;

begin

Result := 0;

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

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

for i := 0 to BIGNUM_DWORD do nn[i] := 0; nn[0] := 256;

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

if (BN_a_cmp_b(a,none)=0) then begin Result := 1; Exit; end;

if (BN_a_cmp_b(a,nn)<=0) then

begin

i := 0;

18.2

while (i<=53) and (Cardinal(primes[i])<>a[0]) do Inc(i);

if (i>53) then Exit;

Result := 1;

Exit;

end;

Move(nzero,n1,sizeof(nzero));

i := 0;

n1[0] := primes[i];

BN_a_mod_b(a,n1,n2);

while (i<=53) and (BN_a_cmp_b(n2,nzero)>0) do

begin

Inc(i);

if (i>53) then Break;

n1[0] := primes[i];

BN_a_mod_b(a,n1,n2);