def test_bytes_to_element(self): b = encodepoint((0, 1)) # order 1, aka Zero self.assertRaises(ValueError, bytes_to_element, b) p = bytes_to_unknown_group_element(b) self.assertFalse(isinstance(p, Element)) self.assertIs(p, Zero) b = encodepoint((0, -1 % Q)) # order 2 self.assertRaises(ValueError, bytes_to_element, b) p = bytes_to_unknown_group_element(b) self.assertFalse(isinstance(p, Element)) # (..,26) is in the right group b = b"\x1a" + b"\x00" * 31 p = bytes_to_element(b) self.assertTrue(isinstance(p, Element))
def test_bytes_to_element(self): b = encodepoint((0,1)) # order 1, aka Zero self.assertRaises(ValueError, bytes_to_element, b) p = bytes_to_unknown_group_element(b) self.assertFalse(isinstance(p, Element)) self.assertIs(p, Zero) b = encodepoint((0,-1%Q)) # order 2 self.assertRaises(ValueError, bytes_to_element, b) p = bytes_to_unknown_group_element(b) self.assertFalse(isinstance(p, Element)) # (..,26) is in the right group b = b"\x1a" + b"\x00"*31 p = bytes_to_element(b) self.assertTrue(isinstance(p, Element))
def test_orders(self): # the point (0,1) is the identity, and has order 1 p0 = xform_affine_to_extended((0, 1)) # p0+p0=p0 # p0+anything=anything # The point (0,-1) has order 2 p2 = xform_affine_to_extended((0, -1)) p3 = add_elements(p2, p2) # p3=p2+p2=p0 p4 = add_elements(p3, p2) # p4=p3+p2=p2 p5 = add_elements(p4, p2) # p5=p4+p2=p0 self.assertBytesEqual( encodepoint(xform_extended_to_affine(p3)), encodepoint(xform_extended_to_affine(p0)), ) self.assertBytesEqual( encodepoint(xform_extended_to_affine(p4)), encodepoint(xform_extended_to_affine(p2)), ) self.assertBytesEqual( encodepoint(xform_extended_to_affine(p5)), encodepoint(xform_extended_to_affine(p0)), ) # now same thing, but with Element p0 = ElementOfUnknownGroup(xform_affine_to_extended((0, 1))) self.assertElementsEqual(p0, Zero) p2 = ElementOfUnknownGroup(xform_affine_to_extended((0, -1))) p3 = p2.add(p2) p4 = p3.add(p2) p5 = p4.add(p2) self.assertElementsEqual(p3, p0) self.assertElementsEqual(p4, p2) self.assertElementsEqual(p5, p0) self.assertFalse(isinstance(p3, Element)) self.assertFalse(isinstance(p4, Element)) self.assertFalse(isinstance(p5, Element)) # and again, with .scalarmult instead of .add p3 = p2.scalarmult(2) # p3=2*p2=p0 p4 = p2.scalarmult(3) # p4=3*p2=p2 p5 = p2.scalarmult(4) # p5=4*p2=p0 self.assertElementsEqual(p3, p0) # TODO: failing self.assertElementsEqual(p4, p2) self.assertElementsEqual(p5, p0) self.assertFalse(isinstance(p3, Element)) self.assertFalse(isinstance(p4, Element)) self.assertFalse(isinstance(p5, Element))
def test_orders(self): # the point (0,1) is the identity, and has order 1 p0 = xform_affine_to_extended((0,1)) # p0+p0=p0 # p0+anything=anything # The point (0,-1) has order 2 p2 = xform_affine_to_extended((0,-1)) p3 = add_elements(p2, p2) # p3=p2+p2=p0 p4 = add_elements(p3, p2) # p4=p3+p2=p2 p5 = add_elements(p4, p2) # p5=p4+p2=p0 self.assertBytesEqual(encodepoint(xform_extended_to_affine(p3)), encodepoint(xform_extended_to_affine(p0))) self.assertBytesEqual(encodepoint(xform_extended_to_affine(p4)), encodepoint(xform_extended_to_affine(p2))) self.assertBytesEqual(encodepoint(xform_extended_to_affine(p5)), encodepoint(xform_extended_to_affine(p0))) # now same thing, but with Element p0 = ElementOfUnknownGroup(xform_affine_to_extended((0,1))) self.assertElementsEqual(p0, Zero) p2 = ElementOfUnknownGroup(xform_affine_to_extended((0,-1))) p3 = p2.add(p2) p4 = p3.add(p2) p5 = p4.add(p2) self.assertElementsEqual(p3, p0) self.assertElementsEqual(p4, p2) self.assertElementsEqual(p5, p0) self.assertFalse(isinstance(p3, Element)) self.assertFalse(isinstance(p4, Element)) self.assertFalse(isinstance(p5, Element)) # and again, with .scalarmult instead of .add p3 = p2.scalarmult(2) # p3=2*p2=p0 p4 = p2.scalarmult(3) # p4=3*p2=p2 p5 = p2.scalarmult(4) # p5=4*p2=p0 self.assertElementsEqual(p3, p0) # TODO: failing self.assertElementsEqual(p4, p2) self.assertElementsEqual(p5, p0) self.assertFalse(isinstance(p3, Element)) self.assertFalse(isinstance(p4, Element)) self.assertFalse(isinstance(p5, Element))
def assertElementsEqual(self, e1, e2): self.assertEqual(hexlify(encodepoint(e1)), hexlify(encodepoint(e2)))