Signed mpint.

This commit is contained in:
Andris Raugulis 2016-09-17 19:23:24 +03:00
parent ddc5ea22f5
commit fce491767c
2 changed files with 7 additions and 4 deletions

View File

@ -390,7 +390,7 @@ class WriteBuf(object):
return len(bin(n)) - (2 if n > 0 else 3)
@classmethod
def _create_mpint(cls, n, bits=None):
def _create_mpint(cls, n, signed=True, bits=None):
if bits is None:
bits = cls._bitlength(n)
length = bits // 8 + (1 if n != 0 else 0)
@ -400,14 +400,16 @@ class WriteBuf(object):
v2[ql - i - 1] = (n & 0xffffffffffffffff)
n >>= 64
data = bytes(struct.pack(fmt, *v2)[-length:])
if data.startswith(b'\xff\x80'):
if not signed:
data = data.lstrip(b'\x00')
elif data.startswith(b'\xff\x80'):
data = data[1:]
return data
def write_mpint1(self, n):
# NOTE: Data Type Enc @ http://www.snailbook.com/docs/protocol-1.5.txt
bits = self._bitlength(n)
data = self._create_mpint(n, bits)
data = self._create_mpint(n, False, bits)
self.write(struct.pack('>H', bits))
return self.write(data)

View File

@ -20,7 +20,8 @@ class TestProtocol(object):
mpint1r = lambda x: self.rbuf(x).read_mpint1()
tc = [(0x0, '00 00'),
(0x1234, '00 0d 12 34'),
(0x12345, '00 11 01 23 45')]
(0x12345, '00 11 01 23 45'),
(0xdeadbeef, '00 20 de ad be ef')]
for p in tc:
assert mpint1w(p[0]) == self._b(p[1])
assert mpint1r(self._b(p[1])) == p[0]