# Double-and-add

## Motivation

Additions (including doubles) are cheap for elliptic curve points. As such, we try to reduce scalar multiplication (or elliptic curve points multiplied by a scalar) to a series of additions.

## Algorithm Process

In practice, Double-and-add is implemented as follows:

```cpp
// Scalar multiplication: Double-and-Add
Point ScalarMultiply(Point p, int k) {
    Point result; // point at infinity
    Point powerOfP = p;
    if (k & 1) {             
        result = result + powerOfP; 
    }
    k >>= 1; 
    while (k > 0) {
        powerOfP = powerOfP.Double(); 
        if (k & 1) {             
            result = result + powerOfP; 
        }
        k >>= 1; 
    }
    return result;
}
```

For example, let's take a look at 6:

$$
6 = \underbrace{0}*{2^0}\underbrace{1}*{2^1}\underbrace{1}\_{2^2}
$$

```
k = 6
result = 0;

// 1st while loop:
powerOfP = powerOfP.Double()  // 1 double : powerOfP = 2p
result += powerOfP            // 1 add    : result = 2p

// 2nd while loop:
powerOfP = powerOfP.Double()  // 1 double : powerOfP = 4p
result += powerOfP            // 1 add    : result = 6p

= 2 adds + 2 doubles
```

> Written by [Ashley Jeong](mailto:undefined) of Fractalyze


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://fractalyze.gitbook.io/intro/primitives/abstract-algebra/elliptic-curve/scalar-multiplication/double-and-add.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
