aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJ08nY2020-03-02 00:03:57 +0100
committerJ08nY2020-03-02 00:03:57 +0100
commitb3e45bf773cec6ecf3f2a702c0b48c9dbfab0c04 (patch)
tree8a232cf9298772e88bfee7f39b973f0e9009cae3
parent0341d359dc67ced3f1e65d1d11af3590c1f0992f (diff)
downloadpyecsca-codegen-b3e45bf773cec6ecf3f2a702c0b48c9dbfab0c04.tar.gz
pyecsca-codegen-b3e45bf773cec6ecf3f2a702c0b48c9dbfab0c04.tar.zst
pyecsca-codegen-b3e45bf773cec6ecf3f2a702c0b48c9dbfab0c04.zip
Add reduction functions, add global trigger for whole cmd.
-rw-r--r--ext/Makefile4
-rw-r--r--pyecsca/codegen/Makefile.inc1
-rw-r--r--pyecsca/codegen/bn/bn.c168
-rw-r--r--pyecsca/codegen/bn/bn.h42
-rw-r--r--pyecsca/codegen/hal/host/host_hal.h3
-rw-r--r--pyecsca/codegen/hal/stm32f0/stm32f0_hal.c8
-rw-r--r--pyecsca/codegen/hal/stm32f0/stm32f0_hal.h1
-rw-r--r--pyecsca/codegen/hal/stm32f3/stm32f3_hal.c8
-rw-r--r--pyecsca/codegen/hal/stm32f3/stm32f3_hal.h1
-rw-r--r--pyecsca/codegen/render.py8
-rw-r--r--pyecsca/codegen/simpleserial/simpleserial.c17
-rw-r--r--pyecsca/codegen/templates/Makefile2
-rw-r--r--pyecsca/codegen/templates/action.c4
-rw-r--r--pyecsca/codegen/templates/curve.c7
-rw-r--r--pyecsca/codegen/templates/defs.h2
-rw-r--r--pyecsca/codegen/templates/mult.c12
-rw-r--r--pyecsca/codegen/templates/mult_bnaf.c6
-rw-r--r--pyecsca/codegen/templates/mult_coron.c6
-rw-r--r--pyecsca/codegen/templates/mult_diff_ldr.c6
-rw-r--r--pyecsca/codegen/templates/mult_ldr.c6
-rw-r--r--pyecsca/codegen/templates/mult_ltr.c6
-rw-r--r--pyecsca/codegen/templates/mult_rtl.c6
-rw-r--r--pyecsca/codegen/templates/mult_simple_ldr.c6
23 files changed, 264 insertions, 66 deletions
diff --git a/ext/Makefile b/ext/Makefile
index db0a50d..85427f0 100644
--- a/ext/Makefile
+++ b/ext/Makefile
@@ -13,6 +13,8 @@ tommath_dir:
host: LIBNAME=libtommath-HOST.a
host: CFLAGS=-DMP_NO_DEV_URANDOM -DMP_LOW_MEM -DMP_DEFAULT_DIGIT_COUNT=10
+host: COMPILE_SIZE=1
+host: COMPILE_LTO=1
host: tommath_dir
$(MAKE) -C libtommath clean
$(MAKE) -C libtommath
@@ -22,6 +24,7 @@ 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: 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
stm32f0: LIBNAME=libtommath-CW308_STM32F0.a
stm32f0: tommath_dir
$(MAKE) -C libtommath clean
@@ -32,6 +35,7 @@ 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: 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
stm32f3: LIBNAME=libtommath-CW308_STM32F3.a
stm32f3: tommath_dir
$(MAKE) -C libtommath clean
diff --git a/pyecsca/codegen/Makefile.inc b/pyecsca/codegen/Makefile.inc
index bb8a08d..87e85ac 100644
--- a/pyecsca/codegen/Makefile.inc
+++ b/pyecsca/codegen/Makefile.inc
@@ -129,6 +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 += -fshort-enums
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c
index 1247c5d..22e1d49 100644
--- a/pyecsca/codegen/bn/bn.c
+++ b/pyecsca/codegen/bn/bn.c
@@ -135,6 +135,174 @@ bn_err bn_mod(const bn_t *one, const bn_t *mod, bn_t *out) {
return mp_mod(one, mod, out);
}
+bn_err bn_red_init(red_t *out) {
+ #if REDUCTION == RED_MONTGOMERY
+ return bn_init(&out->montgomery_renorm);
+ #elif REDUCTION == RED_BARRETT
+ return bn_init(&out->barret);
+ #endif
+ return BN_OKAY;
+}
+
+bn_err bn_red_setup(const bn_t *mod, red_t *out) {
+ #if REDUCTION == RED_MONTGOMERY
+ bn_err err;
+ if ((err = mp_montgomery_setup(mod, &out->montgomery_digit)) != BN_OKAY) {
+ return err;
+ }
+ if ((err = mp_montgomery_calc_normalization(&out->montgomery_renorm, mod)) != BN_OKAY) {
+ return err;
+ }
+ return mp_sqrmod(&out->montgomery_renorm, mod, &out->montgomery_renorm_sqr);
+ #elif REDUCTION == RED_BARRETT
+ return mp_reduce_setup(mod, &out->barret);
+ #endif
+ return BN_OKAY;
+}
+
+bn_err bn_red_encode(bn_t *one, const bn_t *mod, const red_t *red) {
+ #if REDUCTION == RED_MONTGOMERY
+ return mp_mulmod(one, &red->montgomery_renorm, mod, one);
+ #else
+ return BN_OKAY;
+ #endif
+}
+
+bn_err bn_red_decode(bn_t *one, const bn_t *mod, const red_t *red) {
+ #if REDUCTION == RED_MONTGOMERY
+ return mp_montgomery_reduce(one, mod, red->montgomery_digit);
+ #else
+ return BN_OKAY;
+ #endif
+}
+
+bn_err bn_red_add(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_add(one, other, out)) != BN_OKAY) {
+ return err;
+ }
+ if (mp_cmp(out, mod) == MP_GT) {
+ return mp_sub(out, mod, out);
+ } else {
+ return err;
+ }
+}
+
+bn_err bn_red_sub(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_sub(one, other, out)) != BN_OKAY) {
+ return err;
+ }
+ if (mp_cmp_d(out, 0) == MP_LT) {
+ return mp_add(out, mod, out);
+ }
+ if (mp_cmp(out, mod) == MP_GT) {
+ return mp_sub(out, mod, out);
+ }
+ return err;
+}
+
+bn_err bn_red_neg(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_neg(one, out)) != BN_OKAY) {
+ return err;
+ }
+ if (mp_cmp_d(out, 0) == MP_LT) {
+ return mp_add(out, mod, out);
+ }
+ return err;
+}
+
+bn_err bn_red_mul(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_mul(one, other, out)) != BN_OKAY) {
+ return err;
+ }
+ return bn_red_reduce(mod, red, out);
+}
+
+bn_err bn_red_sqr(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_sqr(one, out)) != BN_OKAY) {
+ return err;
+ }
+ return bn_red_reduce(mod, red, out);
+}
+
+bn_err bn_red_inv(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_err err;
+ if ((err = mp_invmod(one, mod, out)) != BN_OKAY) {
+ return err;
+ }
+ #if REDUCTION == RED_MONTGOMERY
+ return mp_mulmod(out, &red->montgomery_renorm_sqr, mod, out);
+ #else
+ return err;
+ #endif
+}
+
+bn_err bn_red_div(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out) {
+ bn_t inv;
+ bn_err err;
+ if ((err = mp_init(&inv)) != BN_OKAY) {
+ return err;
+ }
+ if ((err = mp_copy(other, &inv)) != BN_OKAY) {
+ goto out;
+ }
+ #if REDUCTION == RED_MONTGOMERY
+ if ((err = mp_montgomery_reduce(&inv, mod, red->montgomery_digit)) != BN_OKAY) {
+ goto out;
+ }
+ #endif
+ if ((err = mp_invmod(&inv, mod, &inv)) != BN_OKAY) {
+ goto out;
+ }
+ if ((err = mp_mulmod(one, &inv, mod, out)) != BN_OKAY) {
+ goto out;
+ }
+out:
+ mp_clear(&inv);
+ return err;
+}
+
+bn_err bn_red_pow(const bn_t *base, const bn_t *exp, const bn_t *mod, const red_t *red, bn_t *out) {
+ int blen = bn_bit_length(exp);
+ bn_t result;
+ bn_err err;
+ if ((err = bn_init(&result)) != BN_OKAY) {
+ return err;
+ }
+ if ((err = bn_copy(base, &result)) != BN_OKAY) {
+ bn_clear(&result);
+ return err;
+ }
+ for (int i = blen - 2; i > 0; --i) {
+ bn_red_sqr(&result, mod, red, &result);
+ if (bn_get_bit(exp, i)) {
+ bn_red_mul(&result, base, mod, red, &result);
+ }
+ }
+ return BN_OKAY;
+}
+
+bn_err bn_red_reduce(const bn_t *mod, const red_t *red, bn_t *what) {
+ #if REDUCTION == RED_MONTGOMERY
+ return mp_montgomery_reduce(what, mod, red->montgomery_digit);
+ #elif REDUCTION == RED_BARRETT
+ return mp_reduce(what, mod, red->barrett);
+ #endif
+ return mp_mod(what, mod, what);
+}
+
+void bn_red_clear(red_t *out) {
+ #if REDUCTION == RED_MONTGOMERY
+ bn_clear(&out->montgomery_renorm);
+ #elif REDUCTION == RED_BARRETT
+ bn_clear(&out->barret);
+ #endif
+}
+
bn_err bn_lsh(const bn_t *one, int amount, bn_t *out) {
return mp_mul_2d(one, amount, out);
}
diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h
index eb6e942..bf64890 100644
--- a/pyecsca/codegen/bn/bn.h
+++ b/pyecsca/codegen/bn/bn.h
@@ -4,6 +4,7 @@
#include <tommath.h>
#define bn_t mp_int
+#define bn_digit mp_digit
#define bn_err mp_err
#define bn_sign mp_sign
@@ -23,6 +24,16 @@
#define BN_GT MP_GT /* greater than */
typedef struct {
+ #if REDUCTION == RED_MONTGOMERY
+ bn_digit montgomery_digit;
+ bn_t montgomery_renorm;
+ bn_t montgomery_renorm_sqr;
+ #elif REDUCTION == RED_BARRETT
+ bn_t barret;
+ #endif
+} red_t;
+
+typedef struct {
char name;
bn_t value;
} named_bn_t;
@@ -33,10 +44,10 @@ typedef struct {
int w;
} wnaf_t;
-bn_err bn_init(bn_t *bn);
+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);
-void bn_clear(bn_t *bn);
+bn_err bn_copy(const bn_t *from, bn_t *to);
+void bn_clear(bn_t *bn);
#define bn_clear_multi mp_clear_multi
bn_err bn_from_bin(const uint8_t *data, size_t size, bn_t *out);
@@ -60,16 +71,31 @@ bn_err bn_mod_inv(const bn_t *one, const bn_t *mod, bn_t *out);
bn_err bn_mod_pow(const bn_t *one, const bn_t *exp, const bn_t *mod, bn_t *out);
bn_err bn_mod(const bn_t *one, const bn_t *mod, bn_t *out);
+bn_err bn_red_init(red_t *out);
+bn_err bn_red_setup(const bn_t *mod, red_t *out);
+bn_err bn_red_encode(bn_t *one, const bn_t *mod, const red_t *red);
+bn_err bn_red_decode(bn_t *one, const bn_t *mod, const red_t *red);
+bn_err bn_red_add(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_sub(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_neg(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_mul(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_sqr(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_inv(const bn_t *one, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_div(const bn_t *one, const bn_t *other, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_pow(const bn_t *base, const bn_t *exp, const bn_t *mod, const red_t *red, bn_t *out);
+bn_err bn_red_reduce(const bn_t *mod, const red_t *red, bn_t *what);
+void bn_red_clear(red_t *out);
+
bn_err bn_lsh(const bn_t *one, int amount, bn_t *out);
bn_err bn_rsh(const bn_t *one, int amount, bn_t *out);
-bool bn_eq(const bn_t *one, const bn_t *other);
-bool bn_is_0(const bn_t *one);
-bool bn_is_1(const bn_t *one);
+bool bn_eq(const bn_t *one, const bn_t *other);
+bool bn_is_0(const bn_t *one);
+bool bn_is_1(const bn_t *one);
bn_sign bn_get_sign(const bn_t *one);
-int bn_get_bit(const bn_t *bn, int which);
-int bn_bit_length(const bn_t *bn);
+int bn_get_bit(const bn_t *bn, int which);
+int bn_bit_length(const bn_t *bn);
wnaf_t *bn_wnaf(const bn_t *bn, int w);
wnaf_t *bn_bnaf(const bn_t *bn);
diff --git a/pyecsca/codegen/hal/host/host_hal.h b/pyecsca/codegen/hal/host/host_hal.h
index ba2fbbe..17b7ff7 100644
--- a/pyecsca/codegen/hal/host/host_hal.h
+++ b/pyecsca/codegen/hal/host/host_hal.h
@@ -1,10 +1,13 @@
#ifndef HOST_HAL_H_
#define HOST_HAL_H_
+#include <stdbool.h>
#include "uart.h"
#define trigger_setup()
#define trigger_high()
+#define trigger_status() false
+#define trigger_flip()
#define trigger_low()
#define init_uart init_uart0
diff --git a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
index 613a20a..f08a340 100644
--- a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
+++ b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.c
@@ -107,6 +107,14 @@ bool trigger_status(void)
return trig;
}
+void trigger_flip(void) {
+ if (trig) {
+ trigger_low();
+ } else {
+ trigger_high();
+ }
+}
+
void trigger_low(void)
{
trig = false;
diff --git a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
index d432304..00df927 100644
--- a/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
+++ b/pyecsca/codegen/hal/stm32f0/stm32f0_hal.h
@@ -10,6 +10,7 @@ char getch(void);
void trigger_setup(void);
void trigger_low(void);
bool trigger_status(void);
+void trigger_flip(void);
void trigger_high(void);
void led_error(unsigned int status);
diff --git a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
index c07fe87..9f40104 100644
--- a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
+++ b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.c
@@ -109,6 +109,14 @@ bool trigger_status(void)
return trig;
}
+void trigger_flip(void) {
+ if (trig) {
+ trigger_low();
+ } else {
+ trigger_high();
+ }
+}
+
void trigger_low(void)
{
trig = false;
diff --git a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
index 156a012..fb13792 100644
--- a/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
+++ b/pyecsca/codegen/hal/stm32f3/stm32f3_hal.h
@@ -30,6 +30,7 @@ char getch(void);
void trigger_setup(void);
void trigger_low(void);
bool trigger_status(void);
+void trigger_flip(void);
void trigger_high(void);
void led_error(unsigned int x);
diff --git a/pyecsca/codegen/render.py b/pyecsca/codegen/render.py
index 281bd9c..0d0bc51 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
+from pyecsca.ec.configuration import HashType, RandomMod, Reduction
from pyecsca.ec.coordinates import CoordinateModel
from pyecsca.ec.formula import (Formula)
from pyecsca.ec.model import CurveModel
@@ -190,9 +190,9 @@ 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) -> str:
+def render_makefile(platform: Platform, hash_type: HashType, mod_rand: RandomMod, reduction: Reduction) -> str:
return env.get_template("Makefile").render(platform=str(platform), hash_type=str(hash_type),
- mod_rand=str(mod_rand))
+ mod_rand=str(mod_rand), reduction=str(reduction))
def save_render(dir: str, fname: str, rendered: str):
@@ -216,7 +216,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))
+ render_makefile(config.platform, config.hash_type, config.mod_rand, config.red))
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/simpleserial/simpleserial.c b/pyecsca/codegen/simpleserial/simpleserial.c
index 4e0f0b3..30cd527 100644
--- a/pyecsca/codegen/simpleserial/simpleserial.c
+++ b/pyecsca/codegen/simpleserial/simpleserial.c
@@ -120,21 +120,6 @@ int simpleserial_get(void)
ascii_buf[i] = c;
}
-// uint8_t ik[4];
-// ik[3] = (uint8_t) i & 0xff;
-// ik[2] = (uint8_t) (i>>8) & 0xff;
-// ik[1] = (uint8_t) (i>>16) & 0xff;
-// ik[0] = (uint8_t) (i>>24) & 0xff;
-// uint8_t ic[4];
-// ic[3] = (uint8_t) c & 0xff;
-// ic[2] = (uint8_t) (c>>8) & 0xff;
-// ic[1] = (uint8_t) (c>>16) & 0xff;
-// ic[0] = (uint8_t) (c>>24) & 0xff;
-// if (commands[cmd].c == 'd') {
-// simpleserial_put('o', 4, ik);
-// simpleserial_put('c', 4, ic);
-// }
-
// ASCII buffer is full: convert to bytes
// Check for illegal characters here
@@ -143,7 +128,9 @@ int simpleserial_get(void)
// Callback
uint8_t ret[1];
+ trigger_high();
ret[0] = commands[cmd].fp(data_buf, i/2);
+ trigger_low();
simpleserial_put('z', 1, ret);
return 1;
diff --git a/pyecsca/codegen/templates/Makefile b/pyecsca/codegen/templates/Makefile
index be1ab08..8721d4c 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 }}
+CDEFS += -DHASH={{ hash_type }} -DMOD_RAND={{ mod_rand }} -DREDUCTION={{ reduction }}
MKDIR_LIST += hash prng asn1 bn gen
diff --git a/pyecsca/codegen/templates/action.c b/pyecsca/codegen/templates/action.c
index 258e599..939c6a0 100644
--- a/pyecsca/codegen/templates/action.c
+++ b/pyecsca/codegen/templates/action.c
@@ -69,13 +69,13 @@ uint32_t action_vector = 0;
void action_start(uint32_t action) {
if (action_vector & action) {
- trigger_high();
+ trigger_flip();
}
}
void action_end(uint32_t action) {
if (action_vector & action) {
- trigger_low();
+ trigger_flip();
}
}
diff --git a/pyecsca/codegen/templates/curve.c b/pyecsca/codegen/templates/curve.c
index a3a6592..f99b8d4 100644
--- a/pyecsca/codegen/templates/curve.c
+++ b/pyecsca/codegen/templates/curve.c
@@ -7,6 +7,8 @@ curve_t* curve_new(void) {
{%- for param in params + ["p", "n", "h"] %}
bn_init(&result->{{ param }});
{%- endfor %}
+ bn_red_init(&result->p_red);
+ bn_red_init(&result->n_red);
result->generator = point_new();
result->neutral = point_new();
@@ -17,6 +19,8 @@ void curve_free(curve_t *curve) {
{%- for param in params + ["p", "n", "h"] %}
bn_clear(&curve->{{ param }});
{%- endfor %}
+ bn_red_clear(&curve->p_red);
+ bn_red_clear(&curve->n_red);
if (curve->generator) {
point_free(curve->generator);
}
@@ -30,6 +34,9 @@ void curve_set_param(curve_t *curve, char name, const bn_t *value) {
switch (name) {
{%- for param in params + ["p", "n", "h"] %}
case '{{ param }}': bn_copy(value, &curve->{{ param }});
+ {% if param in ("p", "n") %}
+ bn_red_setup(value, &curve->{{ param }}_red);
+ {%- endif %}
break;
{%- endfor %}
}
diff --git a/pyecsca/codegen/templates/defs.h b/pyecsca/codegen/templates/defs.h
index 071e8a3..5a3875f 100644
--- a/pyecsca/codegen/templates/defs.h
+++ b/pyecsca/codegen/templates/defs.h
@@ -13,10 +13,12 @@ typedef struct {
typedef struct {
bn_t p;
+ red_t p_red;
{%- for param in params %}
bn_t {{ param }};
{%- endfor %}
bn_t n;
+ red_t n_red;
bn_t h;
point_t *generator;
point_t *neutral;
diff --git a/pyecsca/codegen/templates/mult.c b/pyecsca/codegen/templates/mult.c
index f611e95..0603bc0 100644
--- a/pyecsca/codegen/templates/mult.c
+++ b/pyecsca/codegen/templates/mult.c
@@ -27,4 +27,14 @@
{% include "mult_bnaf.c" %}
-{%- endif -%}
+{%- endif %}
+
+
+#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") }}
+ scalar_mult_inner(scalar, point, curve, out);
+ {{ end_action("mult") }}
+} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_bnaf.c b/pyecsca/codegen/templates/mult_bnaf.c
index 10a93fe..33e7302 100644
--- a/pyecsca/codegen/templates/mult_bnaf.c
+++ b/pyecsca/codegen/templates/mult_bnaf.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+static void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *neg = point_new();
point_neg(point, curve, neg);
point_t *q = point_copy(curve->neutral);
@@ -28,5 +25,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_set(q, out);
point_free(neg);
point_free(q);
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_coron.c b/pyecsca/codegen/templates/mult_coron.c
index a2b1085..05e6804 100644
--- a/pyecsca/codegen/templates/mult_coron.c
+++ b/pyecsca/codegen/templates/mult_coron.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *p0 = point_copy(point);
point_t *p1 = point_new();
@@ -22,5 +19,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_set(p0, out);
point_free(p0);
point_free(p1);
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_diff_ldr.c b/pyecsca/codegen/templates/mult_diff_ldr.c
index 3dd445e..ae74053 100644
--- a/pyecsca/codegen/templates/mult_diff_ldr.c
+++ b/pyecsca/codegen/templates/mult_diff_ldr.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *p0 = point_copy(&curve->neutral);
point_t *p1 = point_copy(point);
{%- if scalarmult.complete %}
@@ -29,5 +26,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_set(p0, out);
point_free(p0);
point_free(p1);
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_ldr.c b/pyecsca/codegen/templates/mult_ldr.c
index b51f3fa..06d1472 100644
--- a/pyecsca/codegen/templates/mult_ldr.c
+++ b/pyecsca/codegen/templates/mult_ldr.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
{%- if scalarmult.complete %}
point_t *p0 = point_copy(curve->neutral);
point_t *p1 = point_copy(point);
@@ -30,5 +27,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_set(p0, out);
point_free(p0);
point_free(p1);
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_ltr.c b/pyecsca/codegen/templates/mult_ltr.c
index 187b536..f8bee19 100644
--- a/pyecsca/codegen/templates/mult_ltr.c
+++ b/pyecsca/codegen/templates/mult_ltr.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
{%- if scalarmult.complete %}
point_t *q = point_copy(point);
point_t *r = point_copy(curve->neutral);
@@ -39,5 +36,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
{%- if scalarmult.always %}
point_free(dummy);
{%- endif %}
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_rtl.c b/pyecsca/codegen/templates/mult_rtl.c
index acddf45..9db12fb 100644
--- a/pyecsca/codegen/templates/mult_rtl.c
+++ b/pyecsca/codegen/templates/mult_rtl.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *q = point_copy(point);
point_t *r = point_copy(curve->neutral);
@@ -37,5 +34,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
{%- if scalarmult.always %}
point_free(dummy);
{%- endif %}
- {{ end_action("mult") }}
} \ No newline at end of file
diff --git a/pyecsca/codegen/templates/mult_simple_ldr.c b/pyecsca/codegen/templates/mult_simple_ldr.c
index 8db5b41..c393290 100644
--- a/pyecsca/codegen/templates/mult_simple_ldr.c
+++ b/pyecsca/codegen/templates/mult_simple_ldr.c
@@ -1,10 +1,7 @@
#include "mult.h"
#include "point.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") }}
+void scalar_mult_inner(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_t *p0 = point_copy(&curve->neutral);
point_t *p1 = point_copy(point);
{%- if scalarmult.complete %}
@@ -29,5 +26,4 @@ void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out) {
point_set(p0, out);
point_free(p0);
point_free(p1);
- {{ end_action("mult") }}
} \ No newline at end of file