]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - ecdsa/ecc_chudnovsky.c
switching uart header file
[avr-crypto-lib.git] / ecdsa / ecc_chudnovsky.c
index f5a8f37dfde53ae058552e2a8fa682ef5b70a96d..339c27ea4f676f73a564b7b0db28f1e741cdf9fc 100644 (file)
@@ -1,7 +1,7 @@
 /* ecc_chudnovsky.c */
 /*
     This file is part of the ARM-Crypto-Lib.
-    Copyright (C) 2006-2012 Daniel Otte (daniel.otte@rub.de)
+    Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
 
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -27,7 +27,7 @@
 #if 1
 #include <stdio.h>
 #include "bigint_io.h"
-#include "uart_i.h"
+#include "uart.h"
 #include <avr/pgmspace.h>
 #endif
 
 #define putchar(a)
 #endif
 
+uint8_t ecc_chudnovsky_point_alloc(ecc_chudnovsky_point_t *p, size_t length_B){
+    if(!(p->x.wordv = malloc(length_B))){
+        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
+        return 1;
+    }
+    if(!(p->y.wordv = malloc(length_B))){
+        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
+        free(p->x.wordv);
+        return 1;
+    }
+    if(!(p->z1.wordv = malloc(length_B))){
+        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
+        free(p->x.wordv);
+        free(p->y.wordv);
+        return 1;
+    }
+    if(!(p->z2.wordv = malloc(length_B))){
+        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
+        free(p->x.wordv);
+        free(p->y.wordv);
+        free(p->z1.wordv);
+        return 1;
+    }
+    if(!(p->z3.wordv = malloc(length_B))){
+        printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__);
+        free(p->x.wordv);
+        free(p->y.wordv);
+        free(p->z1.wordv);
+        free(p->z2.wordv);
+        return 1;
+    }
+    bigint_set_zero(&p->x);
+    bigint_set_zero(&p->y);
+    bigint_set_zero(&p->z1);
+    bigint_set_zero(&p->z2);
+    bigint_set_zero(&p->z3);
+    return 0;
+}
+
+void ecc_chudnovsky_point_free(ecc_chudnovsky_point_t *p){
+    free(p->x.wordv);
+    free(p->y.wordv);
+    free(p->z1.wordv);
+    free(p->z2.wordv);
+    free(p->z3.wordv);
+}
+
 /*
  * if (Y == 0)
  *   return POINT_AT_INFINITY
@@ -331,7 +378,7 @@ uint8_t ecc_chudnovsky_point_add_sp(ecc_chudnovsky_point_t *dest,
 uint8_t ecc_chudnovsky_double_and_add(ecc_chudnovsky_point_t *dest,
                                       const bigint_t *k,
                                       const ecc_chudnovsky_point_t *p,
-                                      const ecc_curve_sp_tcurve){
+                                      const ecc_curve_sp_t *curve){
     uint16_t i;
     uint8_t s = 0;
     bigint_word_t v, t;
@@ -357,19 +404,19 @@ uint8_t ecc_chudnovsky_double_and_add(ecc_chudnovsky_point_t *dest,
     return 0;
 }
 
-uint8_t bigint_to_naf(uint8_tdest, uint16_t *length, const bigint_t *src){
+uint8_t bigint_to_naf(uint8_t *dest, uint16_t *length, const bigint_t *src){
     if(src->length_W == 0){
         *dest = 0;
         *length = 2;
         return 0;
     }
 
-    memset(dest, 0, src->length_W * sizeof(bigint_word_t));
+    memset(dest, 0, src->length_W * sizeof(bigint_word_t) * 2 +1);
 
     uint16_t i = 0;
     uint8_t t; /* 3 -> -1 ; 1 -> 1; 0 -> 0 (2 should not happen) */
     bigint_t k, p;
-    bigint_word_t k_w[src->length_W];
+    bigint_word_t k_w[src->length_W + 1];
     bigint_word_t p_w = 1;
     p.wordv = &p_w;
     p.info = 0;
@@ -396,7 +443,7 @@ uint8_t bigint_to_naf(uint8_t* dest, uint16_t *length, const bigint_t *src){
     return 0;
 }
 
-void print_naf(uint8_tnaf, uint16_t length){
+void print_naf(uint8_t *naf, uint16_t length){
     if(!length){
         return;
     }
@@ -419,7 +466,7 @@ void print_naf(uint8_t* naf, uint16_t length){
 uint8_t ecc_chudnovsky_naf_multiplication(ecc_chudnovsky_point_t *dest,
                                           const bigint_t *k,
                                           const ecc_chudnovsky_point_t *p,
-                                          const ecc_curve_sp_tcurve){
+                                          const ecc_curve_sp_t *curve){
     if(k->length_W == 0 || p->y.length_W == 0){
         bigint_set_zero(&dest->y);
         return 0;
@@ -439,11 +486,14 @@ uint8_t ecc_chudnovsky_naf_multiplication(ecc_chudnovsky_point_t *dest,
     bigint_add_s(&p_.y, &p_.y, curve->p);
 
 
-    if(!(t = malloc(k->length_W * sizeof(bigint_word_t) * 2))){
+    if(!(t = calloc(k->length_W * sizeof(bigint_word_t) * 2 + 1, 1))){
         return 1;
     }
     bigint_to_naf(t, &i, k);
 
+ //   printf(" naf: ");
+ //   print_naf(t, i);
+
     --i;
     dest->y.length_W = 0;
     do{
@@ -465,6 +515,17 @@ uint8_t ecc_chudnovsky_naf_multiplication(ecc_chudnovsky_point_t *dest,
 uint8_t ecc_chudnovsky_multiplication(ecc_chudnovsky_point_t *dest,
                                       const bigint_t *k,
                                       const ecc_chudnovsky_point_t *p,
-                                      const ecc_curve_sp_t* curve){
+                                      const ecc_curve_sp_t *curve){
+    return ecc_chudnovsky_naf_multiplication(dest, k, p, curve);
+}
+
+
+
+uint8_t ecc_chudnovsky_multipy_and_sum(ecc_chudnovsky_point_t *dest,
+                                      const bigint_t *k,
+                                      const ecc_chudnovsky_point_t *p,
+                                      const bigint_t *l,
+                                      const ecc_chudnovsky_point_t *q,
+                                      const ecc_curve_sp_t *curve){
     return ecc_chudnovsky_naf_multiplication(dest, k, p, curve);
 }