diff options
| -rw-r--r-- | docs/_static/logo.svg | 12 | ||||
| -rw-r--r-- | pyecsca/ec/mult.py | 17 | ||||
| -rw-r--r-- | pyecsca/ec/point.py | 5 | ||||
| -rw-r--r-- | test/ec/test_mult.py | 17 |
4 files changed, 34 insertions, 17 deletions
diff --git a/docs/_static/logo.svg b/docs/_static/logo.svg index 9f694b6..d4741f5 100644 --- a/docs/_static/logo.svg +++ b/docs/_static/logo.svg @@ -17,8 +17,8 @@ inkscape:version="0.92.2 2405546, 2018-03-11" sodipodi:docname="logo.svg" inkscape:export-filename="/home/johny/bitmap.png" - inkscape:export-xdpi="46.549129" - inkscape:export-ydpi="46.549129" + inkscape:export-xdpi="24.826202" + inkscape:export-ydpi="24.826202" style="enable-background:new"> <defs id="defs2" /> @@ -30,8 +30,8 @@ inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="1.979899" - inkscape:cx="213.66877" - inkscape:cy="92.973704" + inkscape:cx="214.17385" + inkscape:cy="153.58286" inkscape:document-units="mm" inkscape:current-layer="g910" showgrid="false" @@ -80,7 +80,7 @@ transform="translate(-25.307238,-103.83701)" style="display:inline"> <rect - style="opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:21.20100021;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:normal" + style="opacity:1;fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:21.20100021;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;paint-order:normal" id="rect819" width="142.49702" height="142.49702" @@ -169,7 +169,7 @@ inkscape:groupmode="layer" transform="translate(3.7487793e-7,2.477417e-6)"> <path - style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:sans-serif;font-variant-ligatures:normal;font-variant-position:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-feature-settings:normal;text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;text-decoration-style:solid;text-decoration-color:#000000;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-orientation:mixed;dominant-baseline:auto;baseline-shift:baseline;text-anchor:start;white-space:normal;shape-padding:0;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10.89448738;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate" + style="color:#000000;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:medium;line-height:normal;font-family:sans-serif;font-variant-ligatures:normal;font-variant-position:normal;font-variant-caps:normal;font-variant-numeric:normal;font-variant-alternates:normal;font-feature-settings:normal;text-indent:0;text-align:start;text-decoration:none;text-decoration-line:none;text-decoration-style:solid;text-decoration-color:#000000;letter-spacing:normal;word-spacing:normal;text-transform:none;writing-mode:lr-tb;direction:ltr;text-orientation:mixed;dominant-baseline:auto;baseline-shift:baseline;text-anchor:start;white-space:normal;shape-padding:0;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;vector-effect:none;fill:#ffffff;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:10.89448738;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate" d="m 117.09766,54.619141 -11.45313,0.0918 7.30078,8.826171 c 14.54846,17.593485 17.88253,33.613736 16.66602,50.099608 -0.57811,7.83428 -2.32794,15.76997 -4.52539,23.8164 -5.0172,-27.54486 -14.24261,-36.52511 -27.743586,-42.164333 1.626612,8.388513 2.53824,19.222903 2.667416,26.138943 0.17609,11.64808 -7.325299,21.75126 -15.996098,34.49219 -8.665435,12.73307 -18.035828,28.12242 -17.679688,49.42188 0.03385,24.73135 21.713862,53.94988 67.437496,60.22265 -3.41988,-0.90939 -7.07819,-2.44445 -11.6289,-4.96484 -20.27158,-11.22733 -2.18405,-27.82455 8.54883,-41.58789 0.3788,13.38453 24.02295,32.20657 19.40234,9.10351 -3.57142,-17.85713 2.77211,-37.20037 12.93555,-49.35937 -4.02203,18.36592 6.2553,35.77371 18.04101,51.13086 10.31385,13.43928 -14.03094,35.65276 -30.71289,36.60547 -1.32245,0.0755 -2.56676,0.13942 -3.75976,0.18359 2.0414,0.0798 4.1179,0.12386 6.24023,0.11719 41.40679,0.96558 88.67669,-27.76842 88.875,-66.67231 0.19831,-38.90389 -11.63682,-59.20157 -35.42187,-83.25933 l -0.78711,12.09179 c -1.07915,16.58796 -4.16494,27.15512 -7.42579,34.04493 C 201.49442,136.85946 194.43941,112.40727 181.67188,93.5 165.76407,69.942366 141.30687,54.420701 117.09766,54.619141 Z" transform="matrix(0.52916668,0,0,0.52916668,-3.7487793e-7,-2.477417e-6)" id="path906" diff --git a/pyecsca/ec/mult.py b/pyecsca/ec/mult.py index 6a127d3..6bf40ec 100644 --- a/pyecsca/ec/mult.py +++ b/pyecsca/ec/mult.py @@ -42,21 +42,30 @@ class LTRMultiplier(ScalarMultiplier): class RTLMultiplier(ScalarMultiplier): always: bool + scale: bool def __init__(self, curve: EllipticCurve, add: AdditionFormula, dbl: DoublingFormula, scl: ScalingFormula = None, - ctx: Context = None, always: bool = False): + ctx: Context = None, scale: bool = True, always: bool = False): super().__init__(curve, ctx, add=add, dbl=dbl, scl=scl) self.always = always + self.scale = scale def multiply(self, scalar: int, point: Point) -> Point: q = copy(point) r = copy(self.curve.neutral) while scalar > 0: q = self.context.execute(self.formulas["dbl"], q, **self.curve.parameters) + if self.always: + tmp = self.context.execute(self.formulas["add"], r, q, **self.curve.parameters) + else: + if r == self.curve.neutral: + tmp = copy(q) + else: + tmp = self.context.execute(self.formulas["add"], r, q, **self.curve.parameters) if scalar & 1 != 0: - r = self.context.execute(self.formulas["add"], q, r, **self.curve.parameters) - elif self.always: - self.context.execute(self.formulas["add"], q, r, **self.curve.parameters) + r = tmp scalar >>= 1 + if self.scale: + r = self.context.execute(self.formulas["scl"], r, **self.curve.parameters) return r diff --git a/pyecsca/ec/point.py b/pyecsca/ec/point.py index 6c793b8..e81b028 100644 --- a/pyecsca/ec/point.py +++ b/pyecsca/ec/point.py @@ -14,6 +14,11 @@ class Point(object): self.coordinate_model = model self.coords = coords + def __eq__(self, other): + if type(other) is not Point: + return False + return self.coordinate_model == other.coordinate_model and self.coords == other.coords + def __repr__(self): args = ", ".join(["{}={}".format(key, value) for key, value in self.coords.items()]) return "Point([{}] in {})".format(args, self.coordinate_model) diff --git a/test/ec/test_mult.py b/test/ec/test_mult.py index 021a6a3..6fadec7 100644 --- a/test/ec/test_mult.py +++ b/test/ec/test_mult.py @@ -11,12 +11,15 @@ from pyecsca.ec.point import Point class ScalarMultiplierTests(TestCase): def test_rtl_simple(self): - p = 11 + p = 0xfffffffdffffffffffffffffffffffff coords = ShortWeierstrassModel.coordinates["projective"] - curve = EllipticCurve(ShortWeierstrassModel, coords, dict(a=5, b=7), - Point(coords, X=Mod(0, p), Y=Mod(0, p), Z=Mod(1, p))) + curve = EllipticCurve(ShortWeierstrassModel, coords, + dict(a=0xfffffffdfffffffffffffffffffffffc, + b=0xe87579c11079f43dd824993c2cee5ed3), + Point(coords, X=Mod(0, p), Y=Mod(1, p), Z=Mod(0, p))) with Context() as ctx: - mult = RTLMultiplier(curve, coords.formulas["add-2002-bj"], - coords.formulas["dbl-2007-bl"], ctx=ctx) - result = mult.multiply(10, Point(coords, X=Mod(4, p), Y=Mod(3, p), Z=Mod(1, p))) - print(ctx.intermediates) + mult = RTLMultiplier(curve, coords.formulas["add-1998-cmo"], + coords.formulas["dbl-1998-cmo"], coords.formulas["z"], ctx=ctx) + mult.multiply(10, Point(coords, X=Mod(0x161ff7528b899b2d0c28607ca52c5b86, p), + Y=Mod(0xcf5ac8395bafeb13c02da292dded7a83, p), + Z=Mod(1, p))) |
