Skip to content

Commit

Permalink
Doctests for EC.
Browse files Browse the repository at this point in the history
  • Loading branch information
J08nY committed Jun 1, 2024
1 parent 5ddfea8 commit 3f22cde
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
11 changes: 6 additions & 5 deletions pyecsca/ec/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Action:
An Action.
Can be entered:
>>> with Action() as action:
... print(action.inside)
True
Expand Down Expand Up @@ -243,15 +244,15 @@ class DefaultContext(Context):
... with Action() as yet_another:
... pass
>>> ctx.actions # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
<context.Action ...
<context.ResultAction ...
<context.Action ...
<...Action ...
<...ResultAction ...
<...Action ...
<BLANKLINE>
>>> root, subtree = ctx.actions.get_by_index([0])
>>> for action in subtree: # doctest: +ELLIPSIS
... print(action)
<context.ResultAction ...
<context.Action ...
<...ResultAction ...
<...Action ...
"""

actions: Tree
Expand Down
20 changes: 19 additions & 1 deletion pyecsca/ec/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,25 @@

@public
class CoordinateModel:
"""A coordinate system for a particular model(form) of an elliptic curve."""
"""
A coordinate system for a particular model(form) of an elliptic curve.
>>> from pyecsca.ec.params import get_params
>>> params = get_params("secg", "secp256r1", "projective")
>>> coordinate_model = params.curve.coordinate_model
>>> coordinate_model
EFDCoordinateModel("projective", curve_model=ShortWeierstrass)
>>> coordinate_model.variables
['X', 'Y', 'Z']
>>> coordinate_model.curve_model
ShortWeierstrassModel()
>>> coordinate_model.formulas # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
{'mdbl-2007-bl': DoublingEFDFormula(mdbl-2007-bl for shortw/projective),
'dbl-2007-bl': DoublingEFDFormula(dbl-2007-bl for shortw/projective),
...
'add-2007-bl': AdditionEFDFormula(add-2007-bl for shortw/projective),
...
"""

name: str
"""Name of the coordinate model"""
Expand Down
37 changes: 36 additions & 1 deletion pyecsca/ec/curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,42 @@

@public
class EllipticCurve:
"""Elliptic curve."""
"""
An elliptic curve.
>>> from pyecsca.ec.params import get_params
>>> params = get_params("secg", "secp256r1", "projective")
>>> curve = params.curve
>>> curve.prime
115792089210356248762697446949407573530086143415290314195533631308867097853951
>>> curve.parameters # doctest: +NORMALIZE_WHITESPACE
{'a': 115792089210356248762697446949407573530086143415290314195533631308867097853948,
'b': 41058363725152142129326129780047268409114441015993725554835256314039467401291}
>>> curve.neutral
InfinityPoint(shortw/projective)
You can also use the curve object to operate on affine points.
>>> from pyecsca.ec.coordinates import AffineCoordinateModel
>>> affine = AffineCoordinateModel(curve.model)
>>> points_P = sorted(curve.affine_lift_x(Mod(5, curve.prime)), key=lambda p: int(p.x))
>>> points_P # doctest: +NORMALIZE_WHITESPACE
[Point([x=5, y=84324075564118526167843364924090959423913731519542450286139900919689799730227] in shortw/affine),
Point([x=5, y=31468013646237722594854082025316614106172411895747863909393730389177298123724] in shortw/affine)]
>>> P = points_P[0]
>>> Q = Point(affine, x=Mod(106156966968002564385990772707119429362097710917623193504777452220576981858057, curve.prime), y=Mod(89283496902772247016522581906930535517715184283144143693965440110672128480043, curve.prime))
>>> curve.affine_add(P, Q)
Point([x=47810148756503743072934797587322364123448575767318638174816008618047855704885, y=13254714647685362616794785795476294517294947485316674051531702458991837320158] in shortw/affine)
>>> curve.affine_multiply(P, 10)
Point([x=102258728610797412855984739741975475478412665729440354248608608794190482472287, y=108928182525231985447294771990422379640574982656217795144410067267239526061757] in shortw/affine)
>>> curve.affine_random() # doctest: +ELLIPSIS
Point([x=..., y=...] in shortw/affine)
>>> curve.is_on_curve(P)
True
>>> curve.is_neutral(P)
False
"""

model: CurveModel
"""The model of the curve."""
Expand Down
20 changes: 19 additions & 1 deletion pyecsca/ec/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,25 @@

@public
class DomainParameters:
"""Domain parameters which specify a subgroup on an elliptic curve."""
"""
Domain parameters which specify a subgroup on an elliptic curve.
>>> secp256r1 = get_params("secg", "secp256r1", "projective")
>>> str(secp256r1)
'DomainParameters(secg/secp256r1)'
>>> secp256r1.order
115792089210356248762697446949407573529996955224135760342422259061068512044369
>>> secp256r1.cofactor
1
>>> secp256r1.generator
Point([X=48439561293906451759052585252797914202762949526041747995844080717082404635286, Y=36134250956749795798585127919587881956611106672985015071877198253568414405109, Z=1] in shortw/projective)
>>> secp256r1.curve.prime
115792089210356248762697446949407573530086143415290314195533631308867097853951
>>> secp256r1.curve.parameters # doctest: +NORMALIZE_WHITESPACE
{'a': 115792089210356248762697446949407573530086143415290314195533631308867097853948,
'b': 41058363725152142129326129780047268409114441015993725554835256314039467401291}
"""

curve: EllipticCurve
generator: Point
Expand Down
2 changes: 1 addition & 1 deletion pyecsca/sca/stacked_traces/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ def pearson_corr(self,
The result is equivalent to:
>>> np.corrcoef(self.traces.samples,
np.corrcoef(self.traces.samples,
intermediate_values,
rowvar=False)[-1, :-1]
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@
[tool.setuptools_scm]

[tool.pytest.ini_options]
testpaths = ["test"]
testpaths = ["pyecsca", "test"]
markers = [
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
]
addopts = ["--doctest-modules"]
filterwarnings = [
"ignore:(?s).*pkg_resources is deprecated as an API:DeprecationWarning:chipwhisperer.capture.trace.TraceWhisperer", # ChipWhisperer
"ignore:Deprecated call to `pkg_resources.declare_namespace", # sphinxcontrib
Expand Down

0 comments on commit 3f22cde

Please sign in to comment.