aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/codegen
diff options
context:
space:
mode:
authorJ08nY2025-10-02 13:10:46 +0200
committerJ08nY2025-10-02 13:10:46 +0200
commite179a63fc75055323e2feac09170f7c5cc9f3501 (patch)
tree6d32aa06b54d7e9132b446e787a4731aa8ea125b /pyecsca/codegen
parent5528f8a804be5e0673335e5e843ce8dbe2dd02ba (diff)
downloadpyecsca-codegen-e179a63fc75055323e2feac09170f7c5cc9f3501.tar.gz
pyecsca-codegen-e179a63fc75055323e2feac09170f7c5cc9f3501.tar.zst
pyecsca-codegen-e179a63fc75055323e2feac09170f7c5cc9f3501.zip
Even more tests for bn NAF functions.
Diffstat (limited to 'pyecsca/codegen')
-rw-r--r--pyecsca/codegen/bn/bn.c74
-rw-r--r--pyecsca/codegen/bn/bn.h6
2 files changed, 73 insertions, 7 deletions
diff --git a/pyecsca/codegen/bn/bn.c b/pyecsca/codegen/bn/bn.c
index 02371e2..d3b7968 100644
--- a/pyecsca/codegen/bn/bn.c
+++ b/pyecsca/codegen/bn/bn.c
@@ -472,18 +472,80 @@ wnaf_t *bn_bnaf(const bn_t *bn) {
return bn_wnaf(bn, 2);
}
-void bn_naf_extend(wnaf_t *naf, size_t new_length) {
- if (new_length <= naf->length) {
+void bn_naf_pad_left(wnaf_t *naf, int8_t value, size_t amount) {
+ if (amount == 0) {
return;
}
- int8_t *new_data = calloc(new_length, sizeof(int8_t));
- size_t diff = new_length - naf->length;
+ int8_t *new_data = calloc(naf->length + amount, sizeof(int8_t));
for (size_t i = 0; i < naf->length; i++) {
- new_data[i + diff] = naf->data[i];
+ new_data[i + amount] = naf->data[i];
+ }
+ for (size_t i = 0; i < amount; i++) {
+ new_data[i] = value;
}
free(naf->data);
naf->data = new_data;
- naf->length = new_length;
+ naf->length += amount;
+}
+
+void bn_naf_pad_right(wnaf_t *naf, int8_t value, size_t amount) {
+ if (amount == 0) {
+ return;
+ }
+ naf->data = realloc(naf->data, (naf->length + amount) * sizeof(int8_t));
+ for (size_t i = naf->length; i < naf->length + amount; i++) {
+ naf->data[i] = value;
+ }
+ naf->length += amount;
+}
+
+void bn_naf_strip_left(wnaf_t *naf, int8_t value) {
+ size_t i = 0;
+ while (i < naf->length && naf->data[i] == value) {
+ i++;
+ }
+ if (i == 0) {
+ return;
+ }
+ if (i == naf->length) {
+ free(naf->data);
+ naf->data = NULL;
+ naf->length = 0;
+ return;
+ }
+ int8_t *new_data = calloc(naf->length - i, sizeof(int8_t));
+ for (size_t j = 0; j < naf->length - i; j++) {
+ new_data[j] = naf->data[j + i];
+ }
+ free(naf->data);
+ naf->data = new_data;
+ naf->length -= i;
+}
+
+void bn_naf_strip_right(wnaf_t *naf, int8_t value) {
+ size_t i = naf->length;
+ while (i > 0 && naf->data[i - 1] == value) {
+ i--;
+ }
+ if (i == naf->length) {
+ return;
+ }
+ if (i == 0) {
+ free(naf->data);
+ naf->data = NULL;
+ naf->length = 0;
+ return;
+ }
+ naf->data = realloc(naf->data, i * sizeof(int8_t));
+ naf->length = i;
+}
+
+void bn_naf_reverse(wnaf_t *naf) {
+ for (size_t i = 0; i < naf->length / 2; i++) {
+ int8_t temp = naf->data[i];
+ naf->data[i] = naf->data[naf->length - i - 1];
+ naf->data[naf->length - i - 1] = temp;
+ }
}
wsliding_t *bn_wsliding_ltr(const bn_t *bn, int w) {
diff --git a/pyecsca/codegen/bn/bn.h b/pyecsca/codegen/bn/bn.h
index 4e69474..78d2d94 100644
--- a/pyecsca/codegen/bn/bn.h
+++ b/pyecsca/codegen/bn/bn.h
@@ -138,7 +138,11 @@ 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);
-void bn_naf_extend(wnaf_t *naf, size_t new_length);
+void bn_naf_pad_left(wnaf_t *naf, int8_t value, size_t amount);
+void bn_naf_pad_right(wnaf_t *naf, int8_t value, size_t amount);
+void bn_naf_strip_left(wnaf_t *naf, int8_t value);
+void bn_naf_strip_right(wnaf_t *naf, int8_t value);
+void bn_naf_reverse(wnaf_t *naf);
wsliding_t *bn_wsliding_ltr(const bn_t *bn, int w);
wsliding_t *bn_wsliding_rtl(const bn_t *bn, int w);