diff options
| author | J08nY | 2019-11-25 21:25:29 +0100 |
|---|---|---|
| committer | J08nY | 2019-11-25 21:25:29 +0100 |
| commit | de491a6191b465edb7bd9a01a5177ac9bf836747 (patch) | |
| tree | 18459bc2cb101782e2157b0e9313d288d1b6a43b /pyecsca/codegen | |
| parent | 8a56c7a95e662862cfe78b834ccb091e95d5372f (diff) | |
| download | pyecsca-codegen-de491a6191b465edb7bd9a01a5177ac9bf836747.tar.gz pyecsca-codegen-de491a6191b465edb7bd9a01a5177ac9bf836747.tar.zst pyecsca-codegen-de491a6191b465edb7bd9a01a5177ac9bf836747.zip | |
Start Python codegen impl.
Diffstat (limited to 'pyecsca/codegen')
| -rw-r--r-- | pyecsca/codegen/.gitignore | 4 | ||||
| -rw-r--r-- | pyecsca/codegen/Makefile | 6 | ||||
| -rw-r--r-- | pyecsca/codegen/__init__.py | 33 | ||||
| -rw-r--r-- | pyecsca/codegen/bn.c | 58 | ||||
| -rw-r--r-- | pyecsca/codegen/bn.h | 3 | ||||
| -rw-r--r-- | pyecsca/codegen/coords.h | 12 | ||||
| -rw-r--r-- | pyecsca/codegen/curve.h | 10 | ||||
| -rw-r--r-- | pyecsca/codegen/defs.h | 18 | ||||
| -rw-r--r-- | pyecsca/codegen/formulas.h | 3 | ||||
| -rw-r--r-- | pyecsca/codegen/mult/double_and_add.c | 3 | ||||
| -rw-r--r-- | pyecsca/codegen/mult/mult.c | 6 | ||||
| -rw-r--r-- | pyecsca/codegen/mult/mult.h | 5 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/__init__.py | 0 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/coords.c | 33 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/coords.h | 5 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/curve.c | 28 | ||||
| -rw-r--r-- | pyecsca/codegen/templates/curve.h | 8 |
17 files changed, 209 insertions, 26 deletions
diff --git a/pyecsca/codegen/.gitignore b/pyecsca/codegen/.gitignore index eb85fff..76ee88f 100644 --- a/pyecsca/codegen/.gitignore +++ b/pyecsca/codegen/.gitignore @@ -6,4 +6,6 @@ *.map *.sym .dep/ -objdir/
\ No newline at end of file +objdir/ + +__pycache__
\ No newline at end of file diff --git a/pyecsca/codegen/Makefile b/pyecsca/codegen/Makefile index d99a1ce..73a6eca 100644 --- a/pyecsca/codegen/Makefile +++ b/pyecsca/codegen/Makefile @@ -1,12 +1,12 @@ TARGET = pyecsca-codegen -SRC += main.c bn.c ecdh.c ecdsa.c hash/hash.c prng/prng.c +SRC += main.c bn.c ecdh.c ecdsa.c hash/hash.c mult/mult.c prng/prng.c CDEFS += -DHASH=HASH_SHA224 -MKDIR_LIST += hash prng +MKDIR_LIST += hash prng mult -EXTRAINCDIRS += hash prng ../../ext/libtommath/ +EXTRAINCDIRS += hash prng mult ../../ext/libtommath/ LDFLAGS += ../../ext/libtommath/libtommath.a diff --git a/pyecsca/codegen/__init__.py b/pyecsca/codegen/__init__.py index e69de29..894c843 100644 --- a/pyecsca/codegen/__init__.py +++ b/pyecsca/codegen/__init__.py @@ -0,0 +1,33 @@ +from jinja2 import Environment, PackageLoader +from pyecsca.ec.model import CurveModel, ShortWeierstrassModel +from pyecsca.ec.coordinates import CoordinateModel + +env = Environment( + loader=PackageLoader("pyecsca.codegen") +) + + +def get_curve_definition(model: CurveModel): + return env.get_template("curve.h").render(params=model.parameter_names) + +def get_curve_impl(model: CurveModel): + return env.get_template("curve.c").render(params=model.parameter_names) + +def get_coords_definition(coords: CoordinateModel): + return env.get_template("coords.h").render(variables=coords.variables) + +def get_coords_impl(coords: CoordinateModel): + print(coords.satisfying) + return env.get_template("coords.c").render(variables=coords.variables) + +if __name__ == "__main__": + model = ShortWeierstrassModel() + s = get_curve_definition(model) + + s = get_curve_impl(model) + + coords = model.coordinates["projective"] + + s = get_coords_definition(coords) + + s = get_coords_impl(coords)
\ No newline at end of file diff --git a/pyecsca/codegen/bn.c b/pyecsca/codegen/bn.c index dfd433a..26149ba 100644 --- a/pyecsca/codegen/bn.c +++ b/pyecsca/codegen/bn.c @@ -1,9 +1,63 @@ #include "bn.h" bn_err bn_init(bn_t *bn) { - return mp_init(bn); + return mp_init(bn); +} + +void bn_copy(bn_t *from, bn_t *to) { + mp_copy(from, to); } void bn_clear(bn_t *bn) { - mp_clear(bn); + mp_clear(bn); +} + +int bn_from_hex(const char *data, bn_t *out) { + return mp_read_radix(out, data, 16); +} + +int bn_from_int(uint64_t value, bn_t *out) { + mp_set_u64(out, value); + return MP_OKAY; +} + +void bn_mod_add(bn_t *one, bn_t *other, bn_t *mod, bn_t *out) { + mp_addmod(one, other, mod, out); +} + +void bn_mod_sub(bn_t *one, bn_t *other, bn_t *mod, bn_t *out) { + mp_submod(one, other, mod, out); +} + +void bn_mod_mul(bn_t *one, bn_t *other, bn_t *mod, bn_t *out) { + mp_mulmod(one, other, mod, out); +} + +void bn_mod_sqr(bn_t *one, bn_t *mod, bn_t *out) { + mp_sqrmod(one, mod, out); +} + +void bn_mod_div(bn_t *one, bn_t *other, bn_t *mod, bn_t *out) { + bn_t inv; + mp_init(&inv); + mp_invmod(other, mod, &inv); + mp_mulmod(one, &inv, mod, out); + mp_clear(&inv); +} + +void bn_mod_inv(bn_t *one, bn_t *mod, bn_t *out) { + mp_invmod(one, mod, out); +} + +int bn_get_bit(bn_t *bn, int which) { + int which_digit = which / (sizeof(mp_digit) * 8); + int which_bit = which % (sizeof(mp_digit) * 8); + if (bn->used <= which_digit) { + return 0; + } + return (bn->dp[which_digit] >> which_bit) & 1; +} + +int bn_bit_length(bn_t *bn) { + return mp_count_bits(bn); }
\ No newline at end of file diff --git a/pyecsca/codegen/bn.h b/pyecsca/codegen/bn.h index ff0ba37..2cad2a0 100644 --- a/pyecsca/codegen/bn.h +++ b/pyecsca/codegen/bn.h @@ -12,6 +12,7 @@ typedef struct { } named_bn_t; bn_err bn_init(bn_t *bn); +void bn_copy(bn_t *from, bn_t *to); void bn_clear(bn_t *bn); int bn_from_hex(const char *data, bn_t *out); @@ -19,10 +20,10 @@ int bn_from_int(uint64_t value, bn_t *out); void bn_mod_add(bn_t *one, bn_t *other, bn_t *mod, bn_t *out); void bn_mod_sub(bn_t *one, bn_t *other, bn_t *mod, bn_t *out); void bn_mod_mul(bn_t *one, bn_t *other, bn_t *mod, bn_t *out); +void bn_mod_sqr(bn_t *one, bn_t *mod, bn_t *out); void bn_mod_div(bn_t *one, bn_t *other, bn_t *mod, bn_t *out); void bn_mod_inv(bn_t *one, bn_t *mod, bn_t *out); int bn_get_bit(bn_t *bn, int which); -void bn_set_bit(bn_t *bn, int which, int value); int bn_bit_length(bn_t *bn); #endif //BN_H_
\ No newline at end of file diff --git a/pyecsca/codegen/coords.h b/pyecsca/codegen/coords.h index 751aeb6..6a1af7e 100644 --- a/pyecsca/codegen/coords.h +++ b/pyecsca/codegen/coords.h @@ -1,16 +1,10 @@ #ifndef COORDS_H_ #define COORDS_H_ -//point_t definition is variable -/* -typedef struct { - bn_t X; - bn_t Y; - bn_t Z; -} point_t; -*/ +#include "bn.h" +#include "defs.h" -point_t *point_new(); +point_t *point_new(void); point_t *point_copy(const point_t *from); diff --git a/pyecsca/codegen/curve.h b/pyecsca/codegen/curve.h index 81eb526..ab962e7 100644 --- a/pyecsca/codegen/curve.h +++ b/pyecsca/codegen/curve.h @@ -1,15 +1,9 @@ #ifndef CURVE_H_ #define CURVE_H_ -//curve_t definition is variable -/* -typedef struct { - bn_t n; - point_t *neutral; -} curve_t; -*/ +#include "defs.h" -curve_t* curve_new(named_bn_t **params, int num_params); +curve_t* curve_new(const named_bn_t **params, int num_params); void curve_free(curve_t *curve); diff --git a/pyecsca/codegen/defs.h b/pyecsca/codegen/defs.h new file mode 100644 index 0000000..df81226 --- /dev/null +++ b/pyecsca/codegen/defs.h @@ -0,0 +1,18 @@ +#ifndef DEFS_H_ +#define DEFS_H_ +#include "bn.h" + +//point_t definition is variable +typedef struct { + bn_t X; + bn_t Y; + bn_t Z; +} point_t; + +//curve_t definition is variable +typedef struct { + bn_t n; + point_t *neutral; +} curve_t; + +#endif //DEFS_H_
\ No newline at end of file diff --git a/pyecsca/codegen/formulas.h b/pyecsca/codegen/formulas.h index 6cd6120..c8ba6d4 100644 --- a/pyecsca/codegen/formulas.h +++ b/pyecsca/codegen/formulas.h @@ -1,6 +1,9 @@ #ifndef FORMULAS_H_ #define FORMULAS_H_ +#include "coords.h" +#include "defs.h" + int point_add(const point_t *one, const point_t *other, const curve_t *curve, point_t *out); int point_dbl(const point_t *one, const curve_t *curve, point_t *out); diff --git a/pyecsca/codegen/mult/double_and_add.c b/pyecsca/codegen/mult/double_and_add.c index 1fd3c9e..a795654 100644 --- a/pyecsca/codegen/mult/double_and_add.c +++ b/pyecsca/codegen/mult/double_and_add.c @@ -1,10 +1,11 @@ #include "mult.h" +#include "formulas.h" void scalar_mult(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); - int nbits = bn_bit_length(curve->n); + int nbits = bn_bit_length(&curve->n); for (int i = nbits; i >= 0; i--) { point_dbl(r, curve, r); if (bn_get_bit(scalar, i) == 1) { diff --git a/pyecsca/codegen/mult/mult.c b/pyecsca/codegen/mult/mult.c new file mode 100644 index 0000000..d0f60ae --- /dev/null +++ b/pyecsca/codegen/mult/mult.c @@ -0,0 +1,6 @@ + +#include "mult.h" + +#if MULT == MULT_DOUBLE_AND_ADD +#include "double_and_add.c" +#endif
\ No newline at end of file diff --git a/pyecsca/codegen/mult/mult.h b/pyecsca/codegen/mult/mult.h index dcf6767..d2817b7 100644 --- a/pyecsca/codegen/mult/mult.h +++ b/pyecsca/codegen/mult/mult.h @@ -1,7 +1,10 @@ #ifndef MULT_H_ #define MULT_H_ -#include "formulas.h" +#include "defs.h" + +#define MULT_NONE 0 +#define MULT_DOUBLE_AND_ADD 1 void scalar_mult(bn_t *scalar, point_t *point, curve_t *curve, point_t *out); diff --git a/pyecsca/codegen/templates/__init__.py b/pyecsca/codegen/templates/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/pyecsca/codegen/templates/__init__.py diff --git a/pyecsca/codegen/templates/coords.c b/pyecsca/codegen/templates/coords.c new file mode 100644 index 0000000..00f6086 --- /dev/null +++ b/pyecsca/codegen/templates/coords.c @@ -0,0 +1,33 @@ +point_t *point_new(void) { + point_t *result = malloc(sizeof(point_t)); + {%- for variable in variables %} + bn_init(&result->{{ variable }}); + {%- endfor %} + + return result; +} + +point_t *point_copy(const point_t *from) { + point_t *result = point_new(); + point_set(from, result); + return result; +} + +void point_set(const point_t *from, point_t *out) { + {%- for variable in variables %} + bn_copy(&from->{{ variable }}, &out->{{ variable }}); + {%- endfor %} +} + +void point_free(point_t *point) { + {%- for variable in variables %} + bn_clear(&point->{{ variable }}); + {%- endfor %} + free(point); +} + +int point_to_affine(point_t *point, const char coord, curve_t *curve, bn_t *out) { + +} + +int point_from_affine(bn_t *x, bn_t *y, curve_t *curve, point_t *out); diff --git a/pyecsca/codegen/templates/coords.h b/pyecsca/codegen/templates/coords.h new file mode 100644 index 0000000..6890c11 --- /dev/null +++ b/pyecsca/codegen/templates/coords.h @@ -0,0 +1,5 @@ +typedef struct { + {%- for variable in variables %} + bn_t {{ variable }}; + {%- endfor %} +} point_t;
\ No newline at end of file diff --git a/pyecsca/codegen/templates/curve.c b/pyecsca/codegen/templates/curve.c new file mode 100644 index 0000000..a49adde --- /dev/null +++ b/pyecsca/codegen/templates/curve.c @@ -0,0 +1,28 @@ +curve_t* curve_new(const named_bn_t **params, int num_params) { + curve_t *result = malloc(sizeof(curve_t)); + {%- for param in params %} + bn_init(&result->{{ param }}); + {%- endfor %} + bn_init(&result->n); + + for (int i = 0; i < num_params; ++i) { + switch (params[i]->name) { + {%- for param in params %} + case '{{ param }}': bn_copy(params[i]->value, result->{{ param }}); + break; + {%- endfor %} + default: + curve_free(result); + return NULL; + } + } + return result; +} + +void curve_free(curve_t *curve) { + {%- for param in params %} + bn_clear(&curve->{{ param }}); + {%- endfor %} + bn_clear(&curve->n); + free(curve); +}
\ No newline at end of file diff --git a/pyecsca/codegen/templates/curve.h b/pyecsca/codegen/templates/curve.h new file mode 100644 index 0000000..ff4a078 --- /dev/null +++ b/pyecsca/codegen/templates/curve.h @@ -0,0 +1,8 @@ +typedef struct { + {%- for param in params %} + bn_t {{ param }}; + {%- endfor %} + bn_t n; + bn_t h; + point_t neutral; +} curve_t;
\ No newline at end of file |
