aboutsummaryrefslogtreecommitdiff
path: root/src/util/binascii.c
diff options
context:
space:
mode:
authorJ08nY2017-08-31 21:13:00 +0200
committerJ08nY2017-08-31 21:13:50 +0200
commit390046e05d5f14f07c6adbe879b51c48bbe27733 (patch)
tree8095406d7c4dfbcfbeb7812890fbe67e9037c85b /src/util/binascii.c
parent98672b5bc2c2897e372892d16124612cd0b1c96a (diff)
downloadecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.gz
ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.tar.zst
ecgen-390046e05d5f14f07c6adbe879b51c48bbe27733.zip
Diffstat (limited to 'src/util/binascii.c')
-rw-r--r--src/util/binascii.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/util/binascii.c b/src/util/binascii.c
new file mode 100644
index 0000000..ae3f1ff
--- /dev/null
+++ b/src/util/binascii.c
@@ -0,0 +1,43 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include "binascii.h"
+#include "util/memory.h"
+
+size_t binascii_blen(GEN i) {
+ pari_sp ltop = avma;
+ size_t result = (size_t)glength(binary_2k_nv(i, 8));
+ avma = ltop;
+ return result;
+}
+
+char *binascii_itob(GEN i, endian_e endianity) {
+ pari_sp ltop = avma;
+ GEN digits = binary_2k_nv(i, 8);
+ if (endianity == ENDIAN_LITTLE) {
+ digits = vecsmall_reverse(digits);
+ }
+ long blen = glength(digits);
+ char *result = try_malloc((size_t)blen);
+ for (long j = 1; j <= blen; ++j) {
+ result[j - 1] = (char)gel(digits, j);
+ }
+
+ avma = ltop;
+ return result;
+}
+
+GEN binascii_btoi(const char *bytes, size_t len, endian_e endianity) {
+ pari_sp ltop = avma;
+ GEN result = gen_0;
+ for (size_t i = 0; i < len; ++i) {
+ size_t index = i;
+ if (endianity == ENDIAN_LITTLE) {
+ index = (len - 1) - i;
+ }
+ result = addis(result, bytes[index]);
+ if (i < len - 1) result = shifti(result, 8);
+ }
+ return gerepileupto(ltop, result);
+}