diff options
| author | J08nY | 2019-11-27 20:34:58 +0100 |
|---|---|---|
| committer | J08nY | 2019-11-27 20:34:58 +0100 |
| commit | 32a4874abfbaff8cb4ee2d31fe71bcc3499e52dc (patch) | |
| tree | 1203a322d7488ef51b4de2d34c6e0c2871ea68a5 /pyecsca/codegen/bn | |
| parent | bb00fee9702155586e674b9d6a3b838bd54baac2 (diff) | |
| download | pyecsca-codegen-32a4874abfbaff8cb4ee2d31fe71bcc3499e52dc.tar.gz pyecsca-codegen-32a4874abfbaff8cb4ee2d31fe71bcc3499e52dc.tar.zst pyecsca-codegen-32a4874abfbaff8cb4ee2d31fe71bcc3499e52dc.zip | |
Reorganize files, implement proper main.
Diffstat (limited to 'pyecsca/codegen/bn')
| -rw-r--r-- | pyecsca/codegen/bn/bn.c | 119 | ||||
| -rw-r--r-- | pyecsca/codegen/bn/bn.h | 51 |
2 files changed, 170 insertions, 0 deletions
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c new file mode 100644 index 0000000..9b805d1 --- /dev/null +++ b/pyecsca/codegen/bn/bn.c @@ -0,0 +1,119 @@ +#include "bn.h" + +bn_err bn_init(bn_t *bn) { + return mp_init(bn); +} + +void bn_copy(const bn_t *from, bn_t *to) { + mp_copy(from, to); +} + +void bn_clear(bn_t *bn) { + mp_clear(bn); +} + +int bn_from_bin(const uint8_t *data, size_t size, bn_t *out) { + return mp_from_ubin(out, data, size); +} + +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_to_binpad(const bn_t *one, uint8_t *data, size_t size) { + size_t ubin_size = mp_ubin_size(one); + size_t offset = size - ubin_size; + mp_to_ubin(one, data + offset, ubin_size, NULL); +} + +void bn_to_bin(const bn_t *one, uint8_t *data) { + mp_to_ubin(one, data, mp_ubin_size(one), NULL); +} + +size_t bn_to_bin_size(const bn_t *one) { + return mp_ubin_size(one); +} + +void bn_rand_mod_sample(bn_t *out, const bn_t *mod) { + int mod_len = bn_bit_length(mod); + + bn_t mask; bn_init(&mask); + mp_2expt(&mask, mod_len + 1); + mp_decr(&mask); + while (1) { + mp_rand(out, (mod_len / (sizeof(mp_digit) * 8)) + 1); + mp_and(out, &mask, out); + if (mp_cmp_mag(out, mod) == MP_LT) { + bn_clear(&mask); + break; + } + } +} + +void bn_rand_mod_reduce(bn_t *out, const bn_t *mod) { + int mod_len = bn_bit_length(mod); + mp_rand(out, (mod_len / (sizeof(mp_digit) * 8)) + 2); + mp_mod(out, mod, out); +} + +void bn_mod_add(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out) { + mp_addmod(one, other, mod, out); +} + +void bn_mod_sub(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out) { + mp_submod(one, other, mod, out); +} + +void bn_mod_mul(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out) { + mp_mulmod(one, other, mod, out); +} + +void bn_mod_sqr(const bn_t *one, const bn_t *mod, bn_t *out) { + mp_sqrmod(one, mod, out); +} + +void bn_mod_div(const bn_t *one, const bn_t *other, const 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(const bn_t *one, const bn_t *mod, bn_t *out) { + mp_invmod(one, mod, out); +} + +void bn_mod(const bn_t *one, const bn_t *mod, bn_t *out) { + mp_mod(one, mod, out); +} + +void bn_lsh(const bn_t *one, int amount, bn_t *out) { + mp_mul_2d(one, amount, out); +} + +void bn_rsh(const bn_t *one, int amount, bn_t *out) { + mp_div_2d(one, amount, out, NULL); +} + +bool bn_eq(const bn_t *one, const bn_t *other) { + return mp_cmp_mag(one, other) == MP_EQ; +} + +int bn_get_bit(const 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(const bn_t *bn) { + return mp_count_bits(bn); +}
\ No newline at end of file diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h new file mode 100644 index 0000000..2ff723f --- /dev/null +++ b/pyecsca/codegen/bn/bn.h @@ -0,0 +1,51 @@ +#ifndef BN_H_ +#define BN_H_ + +#include <tommath.h> + +#define bn_t mp_int +#define bn_err mp_err + +typedef struct { + char name; + bn_t value; +} named_bn_t; + +bn_err bn_init(bn_t *bn); +void bn_copy(const bn_t *from, bn_t *to); +void bn_clear(bn_t *bn); + +int bn_from_bin(const uint8_t *data, size_t size, bn_t *out); +int bn_from_hex(const char *data, bn_t *out); +int bn_from_int(uint64_t value, bn_t *out); + +void bn_to_binpad(const bn_t *one, uint8_t *data, size_t size); +void bn_to_bin(const bn_t *one, uint8_t *data); +size_t bn_to_bin_size(const bn_t *one); + +void bn_rand_mod_sample(bn_t *out, const bn_t *mod); +void bn_rand_mod_reduce(bn_t *out, const bn_t *mod); + +#if MOD_RAND == MOD_RAND_SAMPLE +#define bn_rand_mod bn_rand_mod_sample +#elif MOD_RAND == MOD_RAND_REDUCE +#define bn_rand_mod bn_rand_mod_reduce +#endif + +void bn_mod_add(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); +void bn_mod_sub(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); +void bn_mod_mul(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); +void bn_mod_sqr(const bn_t *one, const bn_t *mod, bn_t *out); +void bn_mod_div(const bn_t *one, const bn_t *other, const bn_t *mod, bn_t *out); +void bn_mod_inv(const bn_t *one, const bn_t *mod, bn_t *out); +void bn_mod(const bn_t *one, const bn_t *mod, bn_t *out); + +void bn_lsh(const bn_t *one, int amount, bn_t *out); +void bn_rsh(const bn_t *one, int amount, bn_t *out); + +bool bn_eq(const bn_t *one, const bn_t *other); + +int bn_get_bit(const bn_t *bn, int which); +int bn_bit_length(const bn_t *bn); + +#endif //BN_H_
\ No newline at end of file |
