Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
252def to_bytes_xonly(self):253"""Convert (the x coordinate of) a non-infinite group element to 32-byte xonly encoding."""254assert not self.infinity255return self.x.to_bytes()257@staticmethod258def lift_x(x):259"""Return group element with specified field element as x coordinate (and even y)."""260y = (FE(x)**3 + 7).sqrt()261if y is None:262return None263if not y.is_even():264y = -y265return GE(x, y)267@staticmethod268def from_bytes(b):269"""Convert a compressed or uncompressed encoding to a group element."""270assert len(b) in (33, 65)271if len(b) == 33:272if b[0] != 2 and b[0] != 3:273return None274x = FE.from_bytes(b[1:])275if x is None:276return None277r = GE.lift_x(x)278if r is None:279return None280if b[0] == 3:281r = -r282return r283else:284if b[0] != 4:285return None286x = FE.from_bytes(b[1:33])287y = FE.from_bytes(b[33:])288if y**2 != x**3 + 7:289return None290return GE(x, y)292@staticmethod293def from_bytes_xonly(b):294"""Convert a point given in xonly encoding to a group element."""295assert_equal(len(b), 32)296x = FE.from_bytes(b)297if x is None:298return None299return GE.lift_x(x)301@staticmethod302def is_valid_x(x):303"""Determine whether the provided field element is a valid X coordinate."""304return (FE(x)**3 + 7).is_square()306def __str__(self):307"""Convert this group element to a string."""308if self.infinity:309return "(inf)"310return f"({self.x},{self.y})"312def __repr__(self):313"""Get a string representation for this group element."""314if self.infinity:315return "GE()"316return f"GE(0x{int(self.x):x},0x{int(self.y):x})"318# The secp256k1 generator point319G = GE.lift_x(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)322class FastGEMul:323"""Table for fast multiplication with a constant group element.325Speed up scalar multiplication with a fixed point P by using a precomputed lookup table with326its powers of 2:328table = [P, 2*P, 4*P, (2^3)*P, (2^4)*P, ..., (2^255)*P]330During multiplication, the points corresponding to each bit set in the scalar are added up,331i.e. on average ~128 point additions take place.332"""334def __init__(self, p):335self.table = [p] # table[i] = (2^i) * p336for _ in range(255):337p = p + p338self.table.append(p)340def mul(self, a):341result = GE()342a = a % GE.ORDER343for bit in range(a.bit_length()):344if a & (1 << bit):345result += self.table[bit]346return result348# Precomputed table with multiples of G for fast multiplication349FAST_G = FastGEMul(G)351class TestFrameworkSecp256k1(unittest.TestCase):352def test_H(self):353H = sha256(G.to_bytes_uncompressed()).digest()354assert GE.lift_x(FE.from_bytes(H)) is not None355self.assertEqual(H.hex(), "50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0")
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.