diff options
| author | J08nY | 2020-03-02 17:23:20 +0100 |
|---|---|---|
| committer | J08nY | 2020-03-02 17:23:20 +0100 |
| commit | 49018aa37f44b56292193b763ed4a8bb15389c30 (patch) | |
| tree | 9d62101f699075d18c9406a2a175bdd5c377954a /pyecsca/codegen | |
| parent | 81e5f24363cf8f50099b7fbed0fd3bcfaac9d5ea (diff) | |
| download | pyecsca-codegen-49018aa37f44b56292193b763ed4a8bb15389c30.tar.gz pyecsca-codegen-49018aa37f44b56292193b763ed4a8bb15389c30.tar.zst pyecsca-codegen-49018aa37f44b56292193b763ed4a8bb15389c30.zip | |
Add support for specifying multiplication and square algos.
Diffstat (limited to 'pyecsca/codegen')
| -rw-r--r-- | pyecsca/codegen/Makefile.inc | 2 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.c | 25 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.h | 26 | ||||
| -rw-r--r-- | pyecsca/codegen/client.py | 3 | ||||
| -rw-r--r-- | pyecsca/codegen/render.py | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/Makefile | 2 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/main.c | 1 |
7 files changed, 56 insertions, 13 deletions
diff --git a/pyecsca/codegen/Makefile.inc b/pyecsca/codegen/Makefile.inc index f5454c7..87e85ac 100644 --- a/pyecsca/codegen/Makefile.inc +++ b/pyecsca/codegen/Makefile.inc @@ -129,7 +129,7 @@ CFLAGS += -funsigned-char CFLAGS += -funsigned-bitfields # Note: -fpack-struct is dangerous! This is only included in XMEGA/AVR HAL #CFLAGS += -fpack-struct -#CFLAGS += -flto +CFLAGS += -flto CFLAGS += -fshort-enums CFLAGS += -Wall CFLAGS += -Wstrict-prototypes diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c index 71b71a7..0d83731 100644 --- a/pyecsca/codegen/bn/bn.c +++ b/pyecsca/codegen/bn/bn.c @@ -1,6 +1,31 @@ #include "bn.h" #include <string.h> #include <stdlib.h> +#include <limits.h> + +void math_init(void) { + #if MUL == MUL_TOOM_COOK + MP_MUL_TOOM_CUTOFF = 4; + MP_MUL_KARATSUBA_CUTOFF = INT_MAX; + #elif MUL == MUL_KARATSUBA + MP_MUL_TOOM_CUTOFF = INT_MAX; + MP_MUL_KARATSUBA_CUTOFF = 2; + #else + MP_MUL_TOOM_CUTOFF = INT_MAX; + MP_MUL_KARATSUBA_CUTOFF = INT_MAX; + #endif //TODO: COMBA + + #if SQR == SQR_TOOM_COOK + MP_SQR_TOOM_CUTOFF = 4; + MP_SQR_KARATSUBA_CUTOFF = INT_MAX; + #elif SQR == SQR_KARATSUBA + MP_SQR_TOOM_CUTOFF = INT_MAX; + MP_SQR_KARATSUBA_CUTOFF = 2; + #else + MP_SQR_TOOM_CUTOFF = INT_MAX; + MP_SQR_KARATSUBA_CUTOFF = INT_MAX; + #endif //TODO: COMBA +} bn_err bn_init(bn_t *bn) { return mp_init(bn); diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h index cb0be48..a3d653e 100644 --- a/pyecsca/codegen/bn/bn.h +++ b/pyecsca/codegen/bn/bn.h @@ -3,9 +3,19 @@ #include <tommath.h> -#define RED_MONTGOMERY 1 -#define RED_BARRET 2 -#define RED_BASE 3 +#define RED_MONTGOMERY 1 //TODO: Make Montgomery work! +#define RED_BARRET 2 +#define RED_BASE 3 + +#define MUL_TOOM_COOK 1 +#define MUL_KARATSUBA 2 +#define MUL_COMBA 3 +#define MUL_BASE 4 + +#define SQR_TOOM_COOK 1 +#define SQR_KARATSUBA 2 +#define SQR_COMBA 3 +#define SQR_BASE 4 #define bn_t mp_int #define bn_digit mp_digit @@ -29,11 +39,11 @@ typedef struct { #if REDUCTION == RED_MONTGOMERY - bn_digit montgomery_digit; - bn_t montgomery_renorm; - bn_t montgomery_renorm_sqr; + bn_digit montgomery_digit; + bn_t montgomery_renorm; + bn_t montgomery_renorm_sqr; #elif REDUCTION == RED_BARRETT - bn_t barrett; + bn_t barrett; #endif } red_t; @@ -48,6 +58,8 @@ typedef struct { int w; } wnaf_t; +void math_init(void); + bn_err bn_init(bn_t *bn); #define bn_init_multi mp_init_multi bn_err bn_copy(const bn_t *from, bn_t *to); diff --git a/pyecsca/codegen/client.py b/pyecsca/codegen/client.py index 6eaec79..07feb93 100644 --- a/pyecsca/codegen/client.py +++ b/pyecsca/codegen/client.py @@ -3,6 +3,7 @@ import re from binascii import hexlify, unhexlify from enum import IntFlag from os import path +from time import time from typing import Mapping, Union, Optional, Tuple import chipwhisperer as cw @@ -316,7 +317,9 @@ def generate(ctx: click.Context, timeout, curve): target.timeout = timeout target.connect() target.set_params(curve) + start = time() click.echo(target.generate()) + click.echo(time() - start) target.disconnect() diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py index 8350146..50d15af 100644 --- a/pyecsca/codegen/render.py +++ b/pyecsca/codegen/render.py @@ -9,7 +9,7 @@ from typing import Optional, List, Set, Mapping, MutableMapping, Any, Tuple from jinja2 import Environment, PackageLoader from pkg_resources import resource_filename from public import public -from pyecsca.ec.configuration import HashType, RandomMod, Reduction +from pyecsca.ec.configuration import HashType, RandomMod, Reduction, Multiplication, Squaring from pyecsca.ec.coordinates import CoordinateModel from pyecsca.ec.formula import (Formula) from pyecsca.ec.model import CurveModel @@ -190,9 +190,11 @@ def render_main(model: CurveModel, coords: CoordinateModel, keygen: bool, ecdh: keygen=keygen, ecdh=ecdh, ecdsa=ecdsa) -def render_makefile(platform: Platform, hash_type: HashType, mod_rand: RandomMod, reduction: Reduction) -> str: +def render_makefile(platform: Platform, hash_type: HashType, mod_rand: RandomMod, + reduction: Reduction, mul: Multiplication, sqr: Squaring) -> str: return env.get_template("Makefile").render(platform=str(platform), hash_type=str(hash_type), - mod_rand=str(mod_rand), reduction=str(reduction)) + mod_rand=str(mod_rand), reduction=str(reduction), + mul=str(mul), sqr=str(sqr)) def save_render(dir: str, fname: str, rendered: str): @@ -216,7 +218,7 @@ def render(config: DeviceConfiguration) -> Tuple[str, str, str]: os.mkdir(gen_dir) save_render(temp, "Makefile", - render_makefile(config.platform, config.hash_type, config.mod_rand, config.red)) + render_makefile(config.platform, config.hash_type, config.mod_rand, config.red, config.mult, config.sqr)) save_render(temp, "main.c", render_main(config.model, config.coords, config.keygen, config.ecdh, config.ecdsa)) save_render(gen_dir, "defs.h", render_defs(config.model, config.coords)) diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile index 8721d4c..1c661c2 100644 --- a/pyecsca/codegen/templates/Makefile +++ b/pyecsca/codegen/templates/Makefile @@ -4,7 +4,7 @@ SRC += main.c bn/bn.c asn1/asn1.c hash/hash.c prng/prng.c $(wildcard gen/*.c) PLATFORM = {{ platform }} -CDEFS += -DHASH={{ hash_type }} -DMOD_RAND={{ mod_rand }} -DREDUCTION={{ reduction }} +CDEFS += -DHASH={{ hash_type }} -DMOD_RAND={{ mod_rand }} -DREDUCTION={{ reduction }} -DMUL={{ mul }} -DSQR={{ sqr }} MKDIR_LIST += hash prng asn1 bn gen diff --git a/pyecsca/codegen/templates/main.c b/pyecsca/codegen/templates/main.c index b30020f..f3c6de6 100644 --- a/pyecsca/codegen/templates/main.c +++ b/pyecsca/codegen/templates/main.c @@ -471,6 +471,7 @@ int main(void) { prng_init(); formulas_init(); + math_init(); curve = curve_new(); pubkey = point_new(); |
