aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2024-06-01 15:07:07 +0200
committerJ08nY2024-06-01 15:07:07 +0200
commit3f22cdec33ceff27bfdc6baae456950240936098 (patch)
tree4e8470435546f2b69f8828347a089e830b7412b3
parent5ddfea8c12b2e76fa7aed8eb146c8c385e2abe60 (diff)
downloadpyecsca-3f22cdec33ceff27bfdc6baae456950240936098.tar.gz
pyecsca-3f22cdec33ceff27bfdc6baae456950240936098.tar.zst
pyecsca-3f22cdec33ceff27bfdc6baae456950240936098.zip
Doctests for EC.
-rw-r--r--pyecsca/ec/context.py11
-rw-r--r--pyecsca/ec/coordinates.py20
-rw-r--r--pyecsca/ec/curve.py37
-rw-r--r--pyecsca/ec/params.py20
-rw-r--r--pyecsca/sca/stacked_traces/combine.py2
-rw-r--r--pyproject.toml3
6 files changed, 83 insertions, 10 deletions
diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py
index 175da9d..5b30460 100644
--- a/pyecsca/ec/context.py
+++ b/pyecsca/ec/context.py
@@ -24,6 +24,7 @@ class Action:
An Action.
Can be entered:
+
>>> with Action() as action:
... print(action.inside)
True
@@ -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
diff --git a/pyecsca/ec/coordinates.py b/pyecsca/ec/coordinates.py
index ed347d9..8ce3955 100644
--- a/pyecsca/ec/coordinates.py
+++ b/pyecsca/ec/coordinates.py
@@ -21,7 +21,25 @@ from pyecsca.ec.formula.efd import (
@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"""
diff --git a/pyecsca/ec/curve.py b/pyecsca/ec/curve.py
index bc2276c..0099b2b 100644
--- a/pyecsca/ec/curve.py
+++ b/pyecsca/ec/curve.py
@@ -19,7 +19,42 @@ from pyecsca.ec.point import Point, InfinityPoint
@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."""
diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py
index 5de7877..d9e05e5 100644
--- a/pyecsca/ec/params.py
+++ b/pyecsca/ec/params.py
@@ -32,7 +32,25 @@ from pyecsca.misc.cfg import getconfig
@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
diff --git a/pyecsca/sca/stacked_traces/combine.py b/pyecsca/sca/stacked_traces/combine.py
index a55c18b..8016862 100644
--- a/pyecsca/sca/stacked_traces/combine.py
+++ b/pyecsca/sca/stacked_traces/combine.py
@@ -689,7 +689,7 @@ class CPUTraceManager:
The result is equivalent to:
- >>> np.corrcoef(self.traces.samples,
+ np.corrcoef(self.traces.samples,
intermediate_values,
rowvar=False)[-1, :-1]
diff --git a/pyproject.toml b/pyproject.toml
index cd0fc66..94831b0 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -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