# HG changeset patch
# User Tom Boothby <boothby@u.washington.edu>
# Date 1247872854 25200
# Node ID 814f029deef260021f42f0d1188996a04c8a08a0
# Parent  16a632e998fe41f38736037a5ed4e599ac035edc
#6407 -- slight improvement to double-and-add algorithm

diff --git a/sage/schemes/elliptic_curves/formal_group.py b/sage/schemes/elliptic_curves/formal_group.py
--- a/sage/schemes/elliptic_curves/formal_group.py
+++ b/sage/schemes/elliptic_curves/formal_group.py
@@ -568,6 +568,8 @@
 
         - Hamish Ivey-Law (2009-06): double-and-add algorithm for
           non char 0 field case.
+
+        - Tom Boothby (2009-06): slight improvement to double-and-add
         
         EXAMPLES::
         
@@ -654,11 +656,16 @@
                 g = F(g)        
             return R(g.add_bigoh(orig_prec))
 
-        result = F.parent().base_ring()(0)
+        if n&1:
+            result = g
+        else:
+            result = F.parent().base_ring()(0)
+        n = n >> 1
+
         while n > 0:
+            g = F(g, g)
             if n & 1:
                 result = F(result, g)
-            g = F(g, g)
             n = n >> 1
 
         return R(result.add_bigoh(orig_prec))
