def decode_point(bs, T): """ Decode a string encoded version of a Point into a Point. """ if not bs: raise error("can't decode a blank Point") if bord(bs[0]) == 0: return INFINITY elif bord(bs[0]) == 4: # uncompressed point if len(bs) % 2 == 0: # should be two even strings, plus 1 byte raise error('wrong length for uncompressed point') length = (len(bs) - 1) // 2 + 1 x = number.bytes_to_long(bs[1:length]) y = number.bytes_to_long(bs[length:]) else: x = number.bytes_to_long(bs[1:]) y_prime = (bord(bs[0]) == 3) alpha = (x ** 3 + T.a * x + T.b) % T.p beta = number.sqrt(alpha, T.p) if beta % 2 == y_prime: y = beta else: y = T.p - beta p = Point(x, y, T) if not p.verify(): raise error("decoded an invalid point") return p
def test_sqrt(self): self.assertEqual(number.sqrt(83, 673), 140)