#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
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_t* curve){
+ const ecc_curve_sp_t *curve){
uint16_t i;
uint8_t s = 0;
bigint_word_t v, t;
return 0;
}
-uint8_t bigint_to_naf(uint8_t* dest, 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;
return 0;
}
-void print_naf(uint8_t* naf, uint16_t length){
+void print_naf(uint8_t *naf, uint16_t length){
if(!length){
return;
}
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_t* curve){
+ const ecc_curve_sp_t *curve){
if(k->length_W == 0 || p->y.length_W == 0){
bigint_set_zero(&dest->y);
return 0;
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{
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);
}