Xor-el / CryptoLib4Pascal

Crypto for Modern Object Pascal
MIT License
213 stars 65 forks source link

Tbytes to BigInteger #11

Closed 302ba closed 4 years ago

302ba commented 4 years ago

Hi, Could you please tell me why I get assert:

procedure Test;
var
  arrBytes: array[0..15] of byte = (159,245,94,199,119,153,43,128,135,251,91,211,154,145,184,255);,202,69,245,240,80,20};
  aBytes: TBytes;
  r, s: TBigInteger;
begin
  SetLength(aBytes, Length(arrBytes));
  Move(arrBytes[0], aBytes[0], Length(arrBytes));
  r := TBigInteger.Create(aBytes, 0, 16);
  s := TBigInteger.Create(TConverters.ConvertBytesToHexString(aBytes, false), 16);
  Assert(r.Equals(s), 'BigIntegers are not equal');
end;

S contains the right value, but why R is wrong? Thanks.

Best regards, Alex

Xor-el commented 4 years ago

The reason you get assert is because you didn't specify the sign.

I have fixed it.

  procedure Test;
  var
    arrBytes: array[0..15] of
    byte = (159, 245, 94, 199, 119, 153, 43, 128, 135, 251, 91, 211,
      154, 145, 184, 255);
    aBytes: TBytes;
    r, s: TBigInteger;
  begin
    SetLength(aBytes, Length(arrBytes));
    Move(arrBytes[0], aBytes[0], Length(arrBytes));
    r := TBigInteger.Create(1, aBytes, 0, 16); //<== fixed
    s := TBigInteger.Create(TConverters.ConvertBytesToHexString(aBytes, False), 16);

    Assert(r.Equals(s), 'BigIntegers are not equal');
  end; 
302ba commented 4 years ago

Got it! Thanks for the help