How do I multiply some number by a point G (elliptic curve)?

Viewed 29

How do I multiply a regular integer by a point G? I need code that multiplies a long number by the G point of an eliptic curve represented in sec256k1 format. What library can I use for this in c++?

1 Answers

For a succint introduction to elliptic curve arithmetic check this source. A C++ implementation of the operation is roughly:

class EllipticPoint
{
    double m_x, m_y;

public:
// Omited constructor and other modifiers

    EllipticPoint& operator*=(int rhs) noexcept
    {
        EllipticPoint r;
        EllipticPoint p = *this;

        if(rhs < 0)
        {
            // change p * -rhs to -p * rhs
            rhs = -rhs;
            p = -p;
        }
        
        for (int i = 1; i <= rhs; i <<= 1) 
        {
            if (i & rhs) r += p;
            p.Double();
        }

        *this = r;
        return *this;
    }
};

// Example
EllipticPoint G; 
G *= 3;
Related