aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/curve.py
diff options
context:
space:
mode:
authorJ08nY2020-07-01 23:08:43 +0200
committerJ08nY2020-07-01 23:09:30 +0200
commit2aa191c63c556857ee22e801c19ee9822016f7cc (patch)
tree8824b678714c71979c4f8579c9cf782a6fadcc5e /pyecsca/ec/curve.py
parent3eaaed8ff2dfa36ad31884bc4cb0f45e0bf8f427 (diff)
downloadpyecsca-2aa191c63c556857ee22e801c19ee9822016f7cc.tar.gz
pyecsca-2aa191c63c556857ee22e801c19ee9822016f7cc.tar.zst
pyecsca-2aa191c63c556857ee22e801c19ee9822016f7cc.zip
Diffstat (limited to 'pyecsca/ec/curve.py')
-rw-r--r--pyecsca/ec/curve.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/pyecsca/ec/curve.py b/pyecsca/ec/curve.py
index 20b3902..4e41931 100644
--- a/pyecsca/ec/curve.py
+++ b/pyecsca/ec/curve.py
@@ -129,12 +129,13 @@ class EllipticCurve(object):
data = data[coord_len:]
return Point(self.coordinate_model, **coords)
elif encoded[0] in (0x02, 0x03):
- if isinstance(self.coordinate_model, AffineCoordinateModel) and isinstance(self.model, ShortWeierstrassModel):
+ if isinstance(self.coordinate_model, AffineCoordinateModel):
data = encoded[1:]
if len(data) != coord_len:
raise ValueError("Encoded point has bad length")
x = Mod(int.from_bytes(data, "big"), self.prime)
- rhs = x**3 + self.parameters["a"] * x + self.parameters["b"]
+ loc = {**self.parameters, "x": x}
+ rhs = eval(compile(self.model.ysquared, "", mode="eval"), loc)
if not rhs.is_residue():
raise ValueError("Point not on curve")
sqrt = rhs.sqrt()
@@ -149,6 +150,19 @@ class EllipticCurve(object):
else:
raise ValueError(f"Wrong encoding type: {hex(encoded[0])}, should be one of 0x04, 0x06, 0x02, 0x03 or 0x00")
+ def affine_random(self) -> Point:
+ """Generate a random affine point on the curve."""
+ while True:
+ x = Mod.random(self.prime)
+ loc = {**self.parameters, "x":x}
+ ysquared = eval(compile(self.model.ysquared, "", mode="eval"), loc)
+ if ysquared.is_residue():
+ y = ysquared.sqrt()
+ b = Mod.random(2)
+ if b == 1:
+ y = -y
+ return Point(AffineCoordinateModel(self.model), x=x, y=y)
+
def __eq__(self, other):
if not isinstance(other, EllipticCurve):
return False