From 4402971348d442d24693f1a7298662c476b9dcd3 Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 3 Dec 2023 16:21:18 +0100 Subject: Use more-CT libtommath. --- .gitmodules | 2 +- ext/Makefile | 8 ++++---- ext/libtommath | 2 +- pyecsca/codegen/formulas.h | 2 ++ pyecsca/codegen/point.h | 7 +++++++ pyecsca/codegen/templates/formula_add.c | 4 +++- pyecsca/codegen/templates/formula_dadd.c | 4 +++- pyecsca/codegen/templates/formula_dbl.c | 4 +++- pyecsca/codegen/templates/formula_ladd.c | 4 +++- pyecsca/codegen/templates/formula_neg.c | 4 +++- pyecsca/codegen/templates/formula_scl.c | 4 +++- pyecsca/codegen/templates/formula_tpl.c | 4 +++- pyecsca/codegen/templates/formulas.c | 6 ++++++ pyecsca/codegen/templates/mult.c | 3 ++- pyecsca/codegen/templates/ops.c | 9 +++++++++ 15 files changed, 53 insertions(+), 14 deletions(-) diff --git a/.gitmodules b/.gitmodules index b9e9e5b..3a2134c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "ext/libtommath"] path = ext/libtommath - url = https://github.com/libtom/libtommath + url = https://github.com/J08nY/libtommath diff --git a/ext/Makefile b/ext/Makefile index dcc7bc3..ffd53d5 100644 --- a/ext/Makefile +++ b/ext/Makefile @@ -15,7 +15,7 @@ tommath_headers: tommath_dir cp libtommath/*.h ../pyecsca/codegen/tommath/ host: LIBNAME=libtommath-HOST.a -host: CFLAGS=-DMP_NO_DEV_URANDOM -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 +host: CFLAGS=-DMP_NO_DEV_URANDOM -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 -DMP_MIN_DIGIT_COUNT=10 host: COMPILE_SIZE=1 host: COMPILE_LTO=1 host: tommath_dir tommath_headers @@ -24,7 +24,7 @@ host: tommath_dir tommath_headers cp libtommath/$(LIBNAME) ../pyecsca/codegen/tommath/$(LIBNAME) nano: CROSS_COMPILE=arm-none-eabi- -nano: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 +nano: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 -DMP_MIN_DIGIT_COUNT=10 nano: LDFLAGS=--specs=nano.specs --specs=nosys.specs -T ../pyecsca/codegen/hal/stm32f0_nano/LinkerScript.ld -Wl,--gc-sections -lm -mthumb -mcpu=cortex-m0 nano: COMPILE_SIZE=1 nano: COMPILE_LTO=1 @@ -35,7 +35,7 @@ nano: tommath_dir tommath_headers cp libtommath/$(LIBNAME) ../pyecsca/codegen/tommath/$(LIBNAME) stm32f0: CROSS_COMPILE=arm-none-eabi- -stm32f0: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 +stm32f0: CFLAGS=-mcpu=cortex-m0 -mthumb -mfloat-abi=soft -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 -DMP_MIN_DIGIT_COUNT=10 stm32f0: LDFLAGS=--specs=nano.specs --specs=nosys.specs -T ../pyecsca/codegen/hal/stm32f0/LinkerScript.ld -Wl,--gc-sections -lm -mthumb -mcpu=cortex-m0 stm32f0: COMPILE_SIZE=1 stm32f0: COMPILE_LTO=1 @@ -46,7 +46,7 @@ stm32f0: tommath_dir tommath_headers cp libtommath/$(LIBNAME) ../pyecsca/codegen/tommath/$(LIBNAME) stm32f3: CROSS_COMPILE=arm-none-eabi- -stm32f3: CFLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 +stm32f3: CFLAGS=-mcpu=cortex-m4 -mthumb -mfloat-abi=soft -mfpu=fpv4-sp-d16 -fmessage-length=0 -ffunction-sections -DMP_NO_DEV_URANDOM -DMP_32BIT -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10 -DMP_MIN_DIGIT_COUNT=10 stm32f3: LDFLAGS=--specs=nano.specs -T ../pyecsca/codegen/hal/stm32f3/LinkerScript.ld -Wl,--gc-sections -lm -mthumb -mcpu=cortex-m4 stm32f3: COMPILE_SIZE=1 stm32f3: COMPILE_LTO=1 diff --git a/ext/libtommath b/ext/libtommath index 8314bde..b696099 160000 --- a/ext/libtommath +++ b/ext/libtommath @@ -1 +1 @@ -Subproject commit 8314bde5e5c8e5d9331460130a9d1066e324f091 +Subproject commit b6960992aa41046350cef0e35c0cbe243999c275 diff --git a/pyecsca/codegen/formulas.h b/pyecsca/codegen/formulas.h index 763b85d..9e3152b 100644 --- a/pyecsca/codegen/formulas.h +++ b/pyecsca/codegen/formulas.h @@ -3,6 +3,8 @@ void formulas_init(void); +void formulas_zero(void); + void formulas_clear(void); #endif //FORMULAS_H_ \ No newline at end of file diff --git a/pyecsca/codegen/point.h b/pyecsca/codegen/point.h index fd3736e..f3a3ba2 100644 --- a/pyecsca/codegen/point.h +++ b/pyecsca/codegen/point.h @@ -25,30 +25,37 @@ void point_from_affine(bn_t *x, bn_t *y, const curve_t *curve, point_t *out); void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one); bool point_add_init(void); +void point_add_zero(void); void point_add_clear(void); void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one); bool point_dbl_init(void); +void point_dbl_zero(void); void point_dbl_clear(void); void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one); bool point_tpl_init(void); +void point_tpl_zero(void); void point_tpl_clear(void); void point_neg(const point_t *one, const curve_t *curve, point_t *out_one); bool point_neg_init(void); +void point_neg_zero(void); void point_neg_clear(void); void point_scl(const point_t *one, const curve_t *curve, point_t *out_one); bool point_scl_init(void); +void point_scl_zero(void); void point_scl_clear(void); void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one); bool point_dadd_init(void); +void point_dadd_zero(void); void point_dadd_clear(void); void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other); bool point_ladd_init(void); +void point_ladd_zero(void); void point_ladd_clear(void); void point_accumulate(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one); diff --git a/pyecsca/codegen/templates/formula_add.c b/pyecsca/codegen/templates/formula_add.c index 39d12b6..6026601 100644 --- a/pyecsca/codegen/templates/formula_add.c +++ b/pyecsca/codegen/templates/formula_add.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out_one) { {{ start_action("add") }} //NOP_128(); {%- if short_circuit %} diff --git a/pyecsca/codegen/templates/formula_dadd.c b/pyecsca/codegen/templates/formula_dadd.c index 855cf5d..de9c977 100644 --- a/pyecsca/codegen/templates/formula_dadd.c +++ b/pyecsca/codegen/templates/formula_dadd.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_dadd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one) { {{ start_action("dadd") }} //NOP_128(); // TODO: short-circuits diff --git a/pyecsca/codegen/templates/formula_dbl.c b/pyecsca/codegen/templates/formula_dbl.c index 3c10bd7..451b0ee 100644 --- a/pyecsca/codegen/templates/formula_dbl.c +++ b/pyecsca/codegen/templates/formula_dbl.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_dbl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("dbl") }} //NOP_128(); {%- if short_circuit %} diff --git a/pyecsca/codegen/templates/formula_ladd.c b/pyecsca/codegen/templates/formula_ladd.c index 2eaa531..2656708 100644 --- a/pyecsca/codegen/templates/formula_ladd.c +++ b/pyecsca/codegen/templates/formula_ladd.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other) { +__attribute__((noinline)) void point_ladd(const point_t *one, const point_t *other, const point_t *diff, const curve_t *curve, point_t *out_one, point_t *out_other) { {{ start_action("ladd") }} //NOP_128(); // TODO: short-circuits diff --git a/pyecsca/codegen/templates/formula_neg.c b/pyecsca/codegen/templates/formula_neg.c index 4eaf62d..93fbe20 100644 --- a/pyecsca/codegen/templates/formula_neg.c +++ b/pyecsca/codegen/templates/formula_neg.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_neg(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("neg") }} //NOP_128(); {%- if short_circuit %} diff --git a/pyecsca/codegen/templates/formula_scl.c b/pyecsca/codegen/templates/formula_scl.c index dfcfcbe..48ac52e 100644 --- a/pyecsca/codegen/templates/formula_scl.c +++ b/pyecsca/codegen/templates/formula_scl.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_scl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("scl") }} //NOP_128(); {%- if short_circuit %} diff --git a/pyecsca/codegen/templates/formula_tpl.c b/pyecsca/codegen/templates/formula_tpl.c index 40cf114..d280bad 100644 --- a/pyecsca/codegen/templates/formula_tpl.c +++ b/pyecsca/codegen/templates/formula_tpl.c @@ -6,9 +6,11 @@ {{ ops.render_static_init(allocations, formula.shortname) }} +{{ ops.render_static_zero(allocations, formula.shortname) }} + {{ ops.render_static_clear(frees, formula.shortname) }} -void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { +__attribute__((noinline)) void point_tpl(const point_t *one, const curve_t *curve, point_t *out_one) { {{ start_action("tpl") }} //NOP_128(); {%- if short_circuit %} diff --git a/pyecsca/codegen/templates/formulas.c b/pyecsca/codegen/templates/formulas.c index 7135727..f17ea13 100644 --- a/pyecsca/codegen/templates/formulas.c +++ b/pyecsca/codegen/templates/formulas.c @@ -8,6 +8,12 @@ void formulas_init(void) { {%- endfor %} } +void formulas_zero(void) { + {%- for name in names %} + point_{{ name }}_zero(); + {%- endfor %} +} + void formulas_clear(void) { {%- for name in names %} point_{{ name }}_clear(); diff --git a/pyecsca/codegen/templates/mult.c b/pyecsca/codegen/templates/mult.c index 22a385d..0144e36 100644 --- a/pyecsca/codegen/templates/mult.c +++ b/pyecsca/codegen/templates/mult.c @@ -53,12 +53,13 @@ {%- endif %} - +#include "formulas.h" #include "action.h" {% from "action.c" import start_action, end_action %} void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) { {{ start_action("mult") }} + formulas_zero(); scalar_mult_inner(scalar, point, curve, out); {{ end_action("mult") }} } \ No newline at end of file diff --git a/pyecsca/codegen/templates/ops.c b/pyecsca/codegen/templates/ops.c index ee322a8..30a7622 100644 --- a/pyecsca/codegen/templates/ops.c +++ b/pyecsca/codegen/templates/ops.c @@ -55,6 +55,15 @@ } {%- endmacro %} +{% macro render_static_zero(allocations, name) -%} + void point_{{ name }}_zero(void) { + {%- for alloc in allocations -%} + bn_from_int(0, &{{alloc}}); + {%- endfor -%} + } +{%- endmacro %} + + {% macro render_static_clear(frees, name) -%} void point_{{ name }}_clear(void) { {{ render_frees(frees) }} -- cgit v1.3.1