From 9bd89c836b1b53fc3ad77333def0ec70a86d7fae Mon Sep 17 00:00:00 2001 From: Russell O'Connor Date: Sun, 12 Aug 2018 23:47:38 -0400 Subject: [PATCH] Optimize secp256k1_fe_normalize_weak calls. Move secp256k1_fe_normalize_weak calls out of ECMULT_TABLE_GET_GE and ECMULT_TABLE_GET_GE_STORAGE and into secp256k1_ge_globalz_set_table_gej instead. --- src/ecmult_impl.h | 5 +++-- src/group_impl.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h index d5fb6c5b61dd2..fe87c4fe87513 100644 --- a/src/ecmult_impl.h +++ b/src/ecmult_impl.h @@ -166,7 +166,8 @@ static void secp256k1_ecmult_odd_multiples_table_storage_var(int n, secp256k1_ge if ((n) > 0) { \ *(r) = (pre)[((n)-1)/2]; \ } else { \ - secp256k1_ge_neg((r), &(pre)[(-(n)-1)/2]); \ + *(r) = (pre)[(-(n)-1)/2]; \ + secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \ } \ } while(0) @@ -178,7 +179,7 @@ static void secp256k1_ecmult_odd_multiples_table_storage_var(int n, secp256k1_ge secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \ } else { \ secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \ - secp256k1_ge_neg((r), (r)); \ + secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \ } \ } while(0) diff --git a/src/group_impl.h b/src/group_impl.h index b1ace87b6ffd0..a075c80a44762 100644 --- a/src/group_impl.h +++ b/src/group_impl.h @@ -178,6 +178,8 @@ static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp /* The z of the final point gives us the "global Z" for the table. */ r[i].x = a[i].x; r[i].y = a[i].y; + /* Ensure all y values are in weak normal form for fast negation of points */ + secp256k1_fe_normalize_weak(&r[i].y); *globalz = a[i].z; r[i].infinity = 0; zs = zr[i];