RSA - 8


end;

procedure BN_dec_to_bignum(var s: string; var a: TBigNum);

var i,j,l: Integer;

var n1,n2,n3,n4: TBigNum;

var nten: TBigNum;

begin

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

for i := 0 to BIGNUM_DWORD do nten[i] := 0; nten[0] := 10;

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

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

l := Length(s);

for i := l downto 1 do

begin

j := Ord(s[i])-48;

if (j<0) or (j>9) then Exit;

n2[0] := Cardinal(j);

BN_a_mul_b(n1,n2,n3);

BN_a_add_b(a,n3,n4);

Move(n4,a,sizeof(n4));

BN_a_mul_b(n1,nten,n3);

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

end;

end;

function BN_a_cmp_b(var a,b: TBigNum): Integer;

var i: Integer;

18.2

begin

i := BIGNUM_DWORD;

while (i>=0) and (a[i]=b[i]) do Dec(i);

Result := 0;

if (i>=0) and (a[i]>b[i]) then Result := 1;

if (i>=0) and (a[i]<b[i]) then Result := -1;

end;

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

begin

asm

pushad

mov esi,[a]

mov edi,[b]

mov ebx,[res]

mov ecx,BIGNUM_DWORD

mov eax,[esi]

add eax,[edi]

pushfd

mov [ebx],eax

add esi,4

add edi,4

add ebx,4

@_add_1:

mov eax,[esi]

popfd

adc eax,[edi]

pushfd

mov [ebx],eax

add esi,4

add edi,4

add ebx,4

loop @_add_1

@_add_2:

popfd

popad

end;

end;

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

begin

asm

18.2

pushad

mov esi,[a]

mov edi,[b]

mov ebx,[res]

mov ecx,BIGNUM_DWORD

mov eax,[esi]

sub eax,[edi]

pushfd

mov [ebx],eax

add esi,4

add edi,4

add ebx,4

@_sub_1:

mov eax,[esi]

popfd

sbb eax,[edi]

pushfd

mov [ebx],eax

add esi,4

add edi,4

add ebx,4

loop @_sub_1

@_sub_2:

popfd

popad

end;

end;

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

var i,j: Integer;

begin

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

for i := 0 to BIGNUM_DWORD do

begin

j := i*4;

asm

pushad

mov esi,[a]

mov edi,[b]

add edi,[j]