| 239 | |
| 240 | |
| 241 | class MPI(long): |
| 242 | def __new__(cls, num): |
| 243 | mpi = num |
| 244 | |
| 245 | if isinstance(num, (bytes, bytearray)): |
| 246 | if isinstance(num, bytes): # pragma: no cover |
| 247 | num = bytearray(num) |
| 248 | |
| 249 | fl = ((MPIs.bytes_to_int(num[:2]) + 7) // 8) |
| 250 | del num[:2] |
| 251 | |
| 252 | mpi = MPIs.bytes_to_int(num[:fl]) |
| 253 | del num[:fl] |
| 254 | |
| 255 | return super(MPI, cls).__new__(cls, mpi) |
| 256 | |
| 257 | def byte_length(self): |
| 258 | return ((self.bit_length() + 7) // 8) |
| 259 | |
| 260 | def to_mpibytes(self): |
| 261 | return MPIs.int_to_bytes(self.bit_length(), 2) + MPIs.int_to_bytes(self, self.byte_length()) |
| 262 | |
| 263 | def __len__(self): |
| 264 | return self.byte_length() + 2 |
| 265 | |
| 266 | |
| 267 | class MPIs(Field): |
no outgoing calls
no test coverage detected