Core
ecutils.core
CoordinateSystem
Bases: Enum
Coordinate system used for internal arithmetic.
Two systems are supported:
- AFFINE — points are (x, y). Each operation requires one modular inversion, which is simple but relatively slow.
- JACOBIAN — points are (X, Y, Z) with x = X/Z², y = Y/Z³. Avoids inversions during addition/doubling (~3x faster for scalar multiplication) at the cost of a single inversion when converting back to affine form.
Source code in ecutils/core/curve.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
CurveParams dataclass
Immutable parameters defining an elliptic curve y² = x³ + ax + b (mod p).
A valid (non-singular) elliptic curve requires a non-zero discriminant:
Δ = -16(4a³ + 27b²) ≠ 0 (mod p)
This is verified automatically at construction time; attempting to create a CurveParams with 4a³ + 27b² ≡ 0 (mod p) raises ValueError.
.. note::
For cryptographic security the group order n should be at least 2¹⁶⁰ (see NIST SP 800-57). This library does not enforce a minimum order so that small "toy" curves can be used for educational purposes.
Attributes:
| Name | Type | Description |
|---|---|---|
p | int | Prime order of the finite field. |
a | int | Coefficient 'a' in the curve equation. |
b | int | Coefficient 'b' in the curve equation. |
n | int | Order of the generator point. |
h | int | Cofactor. |
coord | CoordinateSystem | Coordinate system used for internal computations. |
Source code in ecutils/core/curve.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
__post_init__()
Validate that the curve is non-singular: 4a³ + 27b² ≠ 0 (mod p).
Source code in ecutils/core/curve.py
63 64 65 66 67 68 69 70 71 72 | |
Point dataclass
A point on an elliptic curve that supports arithmetic operators.
Usage
curve = CurveParams(p=23, a=1, b=1, n=28, h=1) P = Point(x=0, y=1, curve=curve) Q = Point(x=6, y=19, curve=curve) P + Q # point addition 5 * P # scalar multiplication -P # point negation P == Q # equality
To switch coordinate systems, create a new CurveParams with a different coord field:
affine_curve = CurveParams(p=23, a=1, b=1, n=28, coord=CoordinateSystem.AFFINE) P_affine = Point(x=0, y=1, curve=affine_curve)
Source code in ecutils/core/point.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
is_identity: bool property
True when this point represents the point at infinity (identity element).
__add__(other)
Add two points on the same curve using the group law.
Delegates to affine or Jacobian arithmetic depending on curve.coord. The chord-and-tangent formulas are:
Addition (P ≠ Q): λ = (y₂ - y₁) · (x₂ - x₁)⁻¹ x₃ = λ² - x₁ - x₂ y₃ = λ(x₁ - x₃) - y₁
Doubling (P = Q): λ = (3x₁² + a) · (2y₁)⁻¹ x₃ = λ² - 2x₁ y₃ = λ(x₁ - x₃) - y₁
Source code in ecutils/core/point.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
__mul__(k)
Scalar multiplication: k · P via double-and-add in O(log k).
Source code in ecutils/core/point.py
275 276 277 278 279 280 281 282 283 284 285 | |
__neg__()
Return the additive inverse: -P = (x, -y mod p).
Source code in ecutils/core/point.py
237 238 239 240 241 242 | |
__rmul__(k)
Scalar multiplication: k * Point.
Source code in ecutils/core/point.py
287 288 289 | |
__sub__(other)
Subtract: self + (-other).
Source code in ecutils/core/point.py
271 272 273 | |
compress()
Compress this point to its x-coordinate and parity bit.
Returns:
| Type | Description |
|---|---|
tuple[int, int] | A tuple |
Raises:
| Type | Description |
|---|---|
ValueError | If this point is the identity (point at infinity). |
Source code in ecutils/core/point.py
106 107 108 109 110 111 112 113 114 115 116 117 | |
compress_sec1()
Compress this point to SEC 1 / X9.62 format.
The output is a single byte prefix (0x02 for even y, 0x03 for odd y) followed by the x-coordinate as a big-endian unsigned integer, zero-padded to the field size.
Returns:
| Type | Description |
|---|---|
bytes | Compressed point as bytes. |
Raises:
| Type | Description |
|---|---|
ValueError | If this point is the identity or has no curve params. |
Source code in ecutils/core/point.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
decompress(x, parity, curve) classmethod
Reconstruct a point from its compressed form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | int | The x-coordinate. | required |
parity | int | The parity bit (0 or 1) indicating which y to select. | required |
curve | CurveParams | The curve parameters. | required |
Returns:
| Type | Description |
|---|---|
Point | The decompressed |
Raises:
| Type | Description |
|---|---|
ValueError | If x does not correspond to a valid point on the curve. |
Source code in ecutils/core/point.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
from_sec1(data, curve) classmethod
Deserialize a point from SEC 1 / X9.62 format.
Supports both compressed (0x02/0x03 prefix) and uncompressed (0x04 prefix) encodings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | bytes | The SEC 1 encoded point bytes. | required |
curve | CurveParams | The curve parameters. | required |
Returns:
| Type | Description |
|---|---|
Point | The deserialized |
Raises:
| Type | Description |
|---|---|
ValueError | If the data is malformed or the point is invalid. |
Source code in ecutils/core/point.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
is_on_curve()
Check whether this point satisfies y² ≡ x³ + ax + b (mod p).
Source code in ecutils/core/point.py
77 78 79 80 81 82 83 84 85 | |
to_uncompressed_sec1()
Serialize this point to SEC 1 / X9.62 uncompressed format.
The output is 0x04 || x || y, where x and y are big-endian unsigned integers zero-padded to the field size.
Returns:
| Type | Description |
|---|---|
bytes | Uncompressed point as bytes. |
Raises:
| Type | Description |
|---|---|
ValueError | If this point is the identity or has no curve params. |
Source code in ecutils/core/point.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
affine_add(p1x, p1y, p2x, p2y, curve) cached
Add two distinct points in affine coordinates.
Given P₁ = (x₁, y₁) and P₂ = (x₂, y₂) with P₁ ≠ P₂, the chord-line formula is:
λ = (y₂ - y₁) · (x₂ - x₁)⁻¹ (mod p)
x₃ = λ² - x₁ - x₂ (mod p)
y₃ = λ(x₁ - x₃) - y₁ (mod p)
If P₁ = P₂ the call is forwarded to :func:affine_double.
Example (E: y² = x³ + x + 1 over F₂₃, P(0,1) + Q(6,19)):
>>> curve = CurveParams(p=23, a=1, b=1, n=28, h=1, coord=CoordinateSystem.AFFINE)
>>> affine_add(0, 1, 6, 19, curve)
(3, 13)
Source code in ecutils/core/arithmetic/affine.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
affine_double(px, py, curve) cached
Double a point in affine coordinates.
Computes 2P using the tangent-line formula:
λ = (3x₁² + a) · (2y₁)⁻¹ (mod p)
x₃ = λ² - 2x₁ (mod p)
y₃ = λ(x₁ - x₃) - y₁ (mod p)
Example (E: y² = x³ + x + 1 over F₂₃):
>>> curve = CurveParams(p=23, a=1, b=1, n=28, h=1, coord=CoordinateSystem.AFFINE)
>>> affine_double(0, 1, curve)
(6, 19)
Source code in ecutils/core/arithmetic/affine.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
affine_mul(k, px, py, curve)
Scalar multiplication in affine coordinates (double-and-add).
Computes k·P by scanning the bits of k from LSB to MSB, accumulating the result and doubling the base at each step. Runs in O(log k) doublings and at most O(log k) additions.
.. warning::
The double-and-add algorithm is not constant-time: the number of additions depends on the Hamming weight of k. For constant-time requirements see RFC 6090, Section 4.
Source code in ecutils/core/arithmetic/affine.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
jac_add(jp1, jp2, curve) cached
Add two points in Jacobian coordinates.
Uses the standard Jacobian addition formulas (see RFC 6090 Section 4):
U₁ = X₁·Z₂², U₂ = X₂·Z₁²
S₁ = Y₁·Z₂³, S₂ = Y₂·Z₁³
H = U₂ - U₁, R = 2·(S₂ - S₁)
X' = R² - H³ - 2·U₁·H²
Y' = R·(U₁·H² - X') - 2·S₁·H³
Z' = ((Z₁ + Z₂)² - Z₁² - Z₂²)·H
If U₁ = U₂ and S₁ ≠ S₂ the points are inverses → identity. If U₁ = U₂ and S₁ = S₂ the points are equal → delegates to :func:jac_double.
Source code in ecutils/core/arithmetic/jacobian.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
jac_double(jp, curve) cached
Double a point in Jacobian coordinates.
Uses the standard Jacobian doubling formulas (see RFC 6090 Section 4):
S = 4·X·Y²
M = 3·X² + a·Z⁴
X' = M² - 2·S
Y' = M·(S - X') - 8·Y⁴
Z' = 2·Y·Z
Cost: 1S + 4M (no field inversions).
Source code in ecutils/core/arithmetic/jacobian.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
jac_mul(k, jp, curve)
Scalar multiplication in Jacobian coordinates (double-and-add).
Computes k·P using the binary expansion of k. Runs in O(log k) doublings and at most O(log k) additions, all without field inversions until the final conversion back to affine.
Source code in ecutils/core/arithmetic/jacobian.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
to_affine(jp, curve)
Convert a _JacobianPoint back to affine (x, y) coordinates.
Source code in ecutils/core/arithmetic/jacobian.py
55 56 57 58 59 60 61 62 63 | |
to_jacobian(pt)
Convert an affine Point to Jacobian coordinates.
Source code in ecutils/core/arithmetic/jacobian.py
48 49 50 51 52 | |