]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Update invert calls for curve25519_eh_to_x and curve448_eh_to_x
authorNiels Möller <nisse@lysator.liu.se>
Sat, 14 Nov 2020 16:32:12 +0000 (17:32 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Sat, 14 Nov 2020 16:32:12 +0000 (17:32 +0100)
ChangeLog
curve25519-eh-to-x.c
curve448-eh-to-x.c

index 5d774a5fafcfc633e8067aa2239cc792fc49b12c..3bb77d847cabc5a7693e9ede7566370e72058918 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,9 @@
        * ecc-eh-to-a.c (ecc_eh_to_a): Likewise.
        * ecc-j-to-a.c (ecc_j_to_a): Likewise.
        * ecc-gostdsa-verify.c (ecc_gostdsa_verify): Likewise.
+       * curve25519-eh-to-x.c (curve25519_eh_to_x): Likewise.
+       * curve448-eh-to-x.c (curve448_eh_to_x): Update invert call, and
+       reduce scratch need from 9*size to 5*size.
        * ecc-internal.h (ECC_MOD_INV_ITCH, ECC_J_TO_A_ITCH)
        (ECC_EH_TO_A_ITCH): Update accordingly, but no change in total
        scratch need.
index 08ad3d91aa9e41587cd6a6870fc33850ef3e6725..f0f1920d2798fe46f7a677edbbbf680437e3a501 100644 (file)
@@ -50,12 +50,12 @@ curve25519_eh_to_x (mp_limb_t *xp, const mp_limb_t *p,
 #define wp (p + 2*ecc->p.size)
 #define t0 scratch
 #define t1 (scratch + ecc->p.size)
-#define t2 (scratch + 2*ecc->p.size)
+#define tp (scratch + 2*ecc->p.size)
 
   const struct ecc_curve *ecc = &_nettle_curve25519;
   mp_limb_t cy;
 
-  /* If u = U/W and v = V/W are the coordiantes of the point on the
+  /* If u = U/W and v = V/W are the coordinates of the point on the
      Edwards curve we get the curve25519 x coordinate as
 
      x = (1+v) / (1-v) = (W + V) / (W - V)
@@ -65,17 +65,17 @@ curve25519_eh_to_x (mp_limb_t *xp, const mp_limb_t *p,
      x = 0, and we should be fine, since ecc_mod_inv for ecc->p returns 0
      in this case. */
   ecc_mod_sub (&ecc->p, t0, wp, vp);
-  /* Needs a total of 5*size storage. */
-  ecc->p.invert (&ecc->p, t1, t0, t2 + ecc->p.size);
+  /* Needs a total of 6*size storage. */
+  ecc->p.invert (&ecc->p, t1, t0, tp);
   
   ecc_mod_add (&ecc->p, t0, wp, vp);
-  ecc_mod_mul (&ecc->p, t2, t0, t1, t2);
+  ecc_mod_mul (&ecc->p, t0, t0, t1, tp);
 
-  cy = mpn_sub_n (xp, t2, ecc->p.m, ecc->p.size);
-  cnd_copy (cy, xp, t2, ecc->p.size);
+  cy = mpn_sub_n (xp, t0, ecc->p.m, ecc->p.size);
+  cnd_copy (cy, xp, t0, ecc->p.size);
 #undef vp
 #undef wp
 #undef t0
 #undef t1
-#undef t2
+#undef tp
 }
index 6e3367ee4f2881cb12aa3d05f914ba167f883762..8f3f8c4514c51df82cdb75921d1d6bb3f430fd9b 100644 (file)
 void
 curve448_eh_to_x (mp_limb_t *xp, const mp_limb_t *p, mp_limb_t *scratch)
 {
+#define up p
 #define vp (p + ecc->p.size)
 #define t0 scratch
-#define t1 (scratch + ecc->p.size)
-#define t2 (scratch + 2*ecc->p.size)
+#define tp (scratch + ecc->p.size)
 
   const struct ecc_curve *ecc = &_nettle_curve448;
   mp_limb_t cy;
@@ -59,15 +59,15 @@ curve448_eh_to_x (mp_limb_t *xp, const mp_limb_t *p, mp_limb_t *scratch)
 
      x = v^2 / u^2 = (V/W)^2 / (U/W)^2 = (V/U)^2
   */
-  /* Needs a total of 9*size storage. */
-  ecc->p.invert (&ecc->p, t0, p, t1 + ecc->p.size);
-  ecc_mod_mul (&ecc->p, t1, t0, vp, t1);
-  ecc_mod_mul (&ecc->p, t2, t1, t1, t2);
-
-  cy = mpn_sub_n (xp, t2, ecc->p.m, ecc->p.size);
-  cnd_copy (cy, xp, t2, ecc->p.size);
+  /* Needs a total of 5*size storage. */
+  ecc->p.invert (&ecc->p, t0, up, tp);
+  ecc_mod_mul (&ecc->p, t0, t0, vp, tp);
+  ecc_mod_sqr (&ecc->p, t0, t0, tp);
+
+  cy = mpn_sub_n (xp, t0, ecc->p.m, ecc->p.size);
+  cnd_copy (cy, xp, t0, ecc->p.size);
+#undef up
 #undef vp
 #undef t0
-#undef t1
-#undef t2
+#undef tp
 }