aboutsummaryrefslogtreecommitdiff
path: root/libcmix-bignum/gmp/gmp_bignum.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcmix-bignum/gmp/gmp_bignum.c')
-rw-r--r--libcmix-bignum/gmp/gmp_bignum.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/libcmix-bignum/gmp/gmp_bignum.c b/libcmix-bignum/gmp/gmp_bignum.c
new file mode 100644
index 0000000..cc40b48
--- /dev/null
+++ b/libcmix-bignum/gmp/gmp_bignum.c
@@ -0,0 +1,49 @@
+#include "bignum.h"
+
+#include "gmp.h"
+
+void from_bignum(mpz_t i, struct Bignum b) {
+ mpz_import(i, b.len, -1, 1, 0, 0, b.data);
+}
+
+void to_bignum(struct Bignum* b, mpz_t i) {
+ mpz_export(b->data, &b->len, -1, 1, 0, 0, i);
+}
+
+enum bignum_error get_curve25519_mod(struct Bignum* result) {
+ mpz_t mod;
+ mpz_init(mod);
+ mpz_ui_pow_ui(mod, 2, 255);
+ mpz_sub_ui(mod, mod, 19);
+
+ to_bignum(result, mod);
+ return NoError;
+}
+
+enum bignum_error multiply_mod(struct Bignum* result, struct Bignum const lh, struct Bignum const rh, struct Bignum const mod) {
+ mpz_t lh_;
+ mpz_init(lh_);
+ from_bignum(lh_, lh);
+
+ mpz_t rh_;
+ mpz_init(rh_);
+ from_bignum(rh_, rh);
+
+ mpz_t result_;
+ mpz_init(result_);
+ mpz_mul(result_, lh_, rh_);
+
+ mpz_t mod_;
+ mpz_init(mod_);
+ from_bignum(mod_, mod);
+ mpz_mod(result_, result_, mod_);
+
+ to_bignum(result, result_);
+
+ mpz_clear(result_);
+ mpz_clear(lh_);
+ mpz_clear(rh_);
+ mpz_clear(mod_);
+
+ return NoError;
+}