UltrafastSecp256k1 3.50.0
Ultra high-performance secp256k1 elliptic curve cryptography library
Loading...
Searching...
No Matches
glv.hpp
Go to the documentation of this file.
1// GLV endomorphism optimization for secp256k1
2// phi(x,y) = (beta*x, y) where beta^3 == 1 (mod p)
3// lambda*P = phi(P) where lambda^2 + lambda + 1 == 0 (mod n)
4
5#pragma once
6
7#include "scalar.hpp"
8#include "point.hpp"
9#include <array>
10#include <cstdint>
11
12namespace secp256k1::fast {
13
14// GLV constants for secp256k1
15namespace glv_constants {
16 // lambda -- endomorphism eigenvalue: lambda * G equals phi(G)
17 // lambda = 0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72
18 constexpr std::array<uint8_t, 32> LAMBDA = {
19 0x53, 0x63, 0xad, 0x4c, 0xc0, 0x5c, 0x30, 0xe0,
20 0xa5, 0x26, 0x1c, 0x02, 0x88, 0x12, 0x64, 0x5a,
21 0x12, 0x2e, 0x22, 0xea, 0x20, 0x81, 0x66, 0x78,
22 0xdf, 0x02, 0x96, 0x7c, 0x1b, 0x23, 0xbd, 0x72
23 };
24
25 // beta (beta) - cube root of unity mod p
26 // beta = 0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee
27 constexpr std::array<uint8_t, 32> BETA = {
28 0x7a, 0xe9, 0x6a, 0x2b, 0x65, 0x7c, 0x07, 0x10,
29 0x6e, 0x64, 0x47, 0x9e, 0xac, 0x34, 0x34, 0xe9,
30 0x9c, 0xf0, 0x49, 0x75, 0x12, 0xf5, 0x89, 0x95,
31 0xc1, 0x39, 0x6c, 0x28, 0x71, 0x95, 0x01, 0xee
32 };
33
34 // Precomputed values for GLV decomposition
35 // These are used to split k -> (k1, k2) such that k = k1 + k2*lambda (mod n)
36 // Using the lattice basis vectors from secp256k1 optimization
37
38 // a1 = 0x3086d221a7d46bcde86c90e49284eb15
39 constexpr std::array<uint8_t, 16> A1 = {
40 0x30, 0x86, 0xd2, 0x21, 0xa7, 0xd4, 0x6b, 0xcd,
41 0xe8, 0x6c, 0x90, 0xe4, 0x92, 0x84, 0xeb, 0x15
42 };
43
44 // -b1 = 0xe4437ed6010e88286f547fa90abfe4c3
45 constexpr std::array<uint8_t, 16> MINUS_B1 = {
46 0xe4, 0x43, 0x7e, 0xd6, 0x01, 0x0e, 0x88, 0x28,
47 0x6f, 0x54, 0x7f, 0xa9, 0x0a, 0xbf, 0xe4, 0xc3
48 };
49
50 // a2 = 0xe4437ed6010e88286f547fa90abfe4c4
51 constexpr std::array<uint8_t, 16> A2 = {
52 0xe4, 0x43, 0x7e, 0xd6, 0x01, 0x0e, 0x88, 0x28,
53 0x6f, 0x54, 0x7f, 0xa9, 0x0a, 0xbf, 0xe4, 0xc4
54 };
55
56 // b2 = 0x3086d221a7d46bcde86c90e49284eb15
57 constexpr std::array<uint8_t, 16> B2 = {
58 0x30, 0x86, 0xd2, 0x21, 0xa7, 0xd4, 0x6b, 0xcd,
59 0xe8, 0x6c, 0x90, 0xe4, 0x92, 0x84, 0xeb, 0x15
60 };
61}
62
63// GLV decomposition result
67 bool k1_neg; // true if k1 should be negated
68 bool k2_neg; // true if k2 should be negated
69};
70
71// Decompose scalar k into k1, k2 such that k = k1 + k2*lambda (mod n)
72// The resulting k1, k2 are roughly half the bit length of k (~128 bits each)
74
75// Apply endomorphism to point: phi(x,y) = (beta*x, y)
76// This is very cheap - just one field multiplication
78
79// Verify endomorphism properties (for testing)
80// Should verify: phi(phi(P)) + P = O (point at infinity)
82
83} // namespace secp256k1::fast
constexpr std::array< uint8_t, 16 > MINUS_B1
Definition glv.hpp:45
constexpr std::array< uint8_t, 16 > A1
Definition glv.hpp:39
constexpr std::array< uint8_t, 16 > A2
Definition glv.hpp:51
constexpr std::array< uint8_t, 16 > B2
Definition glv.hpp:57
constexpr std::array< uint8_t, 32 > LAMBDA
Definition glv.hpp:18
constexpr std::array< uint8_t, 32 > BETA
Definition glv.hpp:27
GLVDecomposition glv_decompose(const Scalar &k)
Point apply_endomorphism(const Point &P)
bool verify_endomorphism(const Point &P)