Remove Point.from_affine(), add a check for calling to_affine() on the identity point

pull/267/head
Bogdan Opanchuk 2021-05-18 13:51:55 -07:00
parent 48c3441344
commit 4e0b6a54fe
4 changed files with 23 additions and 60 deletions

View File

@ -24,16 +24,6 @@ def test_generator_point():
assert g1 == g2
def test_to_and_from_affine():
x = 17004608369308732328368332205668001941491834793934321461466076545247324070015
y = 69725941631324401609944843130171147910924748427773762412028916504484868631573
p = CurvePoint.from_affine(x, y)
assert p.to_affine() == (x, y)
def test_invalid_serialized_points():
field_order = 2**256 - 0x1000003D1
@ -72,19 +62,15 @@ def test_serialize_point_at_infinity():
assert bytes_point_at_infinity == b'\x00'
def test_coords_with_special_characteristics():
def test_to_affine():
p = CurvePoint.generator()
x_ref = 0x79BE667E_F9DCBBAC_55A06295_CE870B07_029BFCDB_2DCE28D9_59F2815B_16F81798
y_ref = 0x483ADA77_26A3C465_5DA4FBFC_0E1108A8_FD17B448_A6855419_9C47D08F_FB10D4B8
assert p.to_affine() == (x_ref, y_ref)
# Testing that a point with x coordinate greater than the curve order is still valid.
# In particular, we will test the last valid point from the default curve (secp256k1)
# whose x coordinate is `field_order - 3` and is greater than the order of the curve
field_order = 2**256 - 0x1000003D1
compressed = b'\x02' + (field_order-3).to_bytes(32, 'big')
last_point = CurvePoint.from_bytes(compressed)
# The same point, but obtained through the from_affine method
x = 115792089237316195423570985008687907853269984665640564039457584007908834671660
y = 109188863561374057667848968960504138135859662956057034999983532397866404169138
assert last_point == CurvePoint.from_affine(x, y)
def test_identity_to_affine():
p = CurvePoint.generator()
identity = p - p
with pytest.raises(ValueError):
identity.to_affine()

View File

@ -26,15 +26,6 @@ class CurvePoint(Serializable):
"""
return cls.generator() * CurveScalar.random_nonzero()
@classmethod
def from_affine(cls, affine_x: int, affine_y: int) -> 'CurvePoint':
"""
Returns a CurvePoint object from the given affine coordinates in a tuple in
the format of (x, y) and a given curve.
"""
backend_point = openssl.point_from_affine_coords(CURVE, affine_x, affine_y)
return cls(backend_point)
def to_affine(self) -> Tuple[int, int]:
"""
Returns a tuple of Python ints in the format of (x, y) that represents

View File

@ -233,7 +233,7 @@ def _bn_size(bn):
return BACKEND_LIB.BN_num_bytes(bn)
def bn_to_int(bn):
def bn_to_int(bn) -> int:
return backend._bn_to_int(bn)
@ -316,23 +316,6 @@ def _point_new(ec_group):
return new_point
def point_from_affine_coords(curve: Curve, affine_x: int, affine_y: int):
"""
Returns an EC_POINT given the group of a curve and the affine coordinates
provided.
"""
bn_affine_x = bn_from_int(affine_x)
bn_affine_y = bn_from_int(affine_y)
new_point = _point_new(curve.ec_group)
with tmp_bn_ctx() as bn_ctx:
res = BACKEND_LIB.EC_POINT_set_affine_coordinates_GFp(
curve.ec_group, new_point, bn_affine_x, bn_affine_y, bn_ctx
)
backend.openssl_assert(res == 1)
return new_point
def point_to_affine_coords(curve: Curve, point) -> Tuple[int, int]:
"""
Returns the affine coordinates of a given point on the provided ec_group.
@ -340,11 +323,14 @@ def point_to_affine_coords(curve: Curve, point) -> Tuple[int, int]:
affine_x = _bn_new()
affine_y = _bn_new()
with tmp_bn_ctx() as bn_ctx:
res = BACKEND_LIB.EC_POINT_get_affine_coordinates_GFp(
curve.ec_group, point, affine_x, affine_y, bn_ctx
)
backend.openssl_assert(res == 1)
try:
with tmp_bn_ctx() as bn_ctx:
res = BACKEND_LIB.EC_POINT_get_affine_coordinates_GFp(
curve.ec_group, point, affine_x, affine_y, bn_ctx
)
backend.openssl_assert(res == 1)
except InternalError as e:
raise ValueError("Cannot get affine coordinates of an identity point")
return bn_to_int(affine_x), bn_to_int(affine_y)

View File

@ -1,7 +1,9 @@
import json
import os
from umbral import SecretKey, PublicKey, Signer, KeyFrag, CapsuleFrag, encrypt, generate_kfrags, reencrypt
from umbral import (
SecretKey, PublicKey, Signer, KeyFrag, CapsuleFrag,
encrypt, generate_kfrags, reencrypt)
from umbral.curve_scalar import CurveScalar
from umbral.curve_point import CurvePoint
from umbral.hashing import Hash, unsafe_hash_to_point
@ -15,9 +17,7 @@ from umbral.dem import DEM, kdf
def hexlify(data):
if isinstance(data, int):
return hex(data)[2:]
try:
return data.to_bytes().hex()
except AttributeError:
else:
return bytes(data).hex()