ec

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 14, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package ec implements the Edwards curves used by Olvid (spec Section 12): Curve25519 and MDC. All arithmetic is done with math/big; points are represented by their (x, y) affine coordinates, and a point whose x is unknown carries a nil X (the spec's "⊥").

Index

Constants

View Source
const (
	// ErrNotOnCurve is returned when a point is not on the curve.
	ErrNotOnCurve = Error("point is not on the curve")
	// ErrNoSquareRoot is returned when a y-coordinate has no matching x.
	ErrNoSquareRoot = Error("no square root modulo p")
)

Variables

This section is empty.

Functions

func BigInt

func BigInt(rnd prng.PRNG, n *big.Int) (*big.Int, error)

BigInt draws a uniform integer in [0, n) from the PRNG, reproducing Olvid's exact byte-consumption (masked rejection sampling on bitLength(n-1), reading ceil(bitLength(n-1)/8) bytes per attempt). Matching this precisely is required to reproduce signatures/challenges bit-for-bit.

Types

type Curve

type Curve struct {
	// contains filtered or unexported fields
}

Curve is an Edwards curve instance (a concrete subtype of the spec's EdwardsCurve abstract type).

func Curve25519

func Curve25519() *Curve

Curve25519 returns the Curve25519 instance (spec §12.1).

func MDC

func MDC() *Curve

MDC returns the MDC curve instance (spec §12.2).

func (*Curve) ByteLen

func (c *Curve) ByteLen() int

ByteLen returns len(p): the byte-length used to serialize field elements.

func (*Curve) Card

func (c *Curve) Card() *big.Int

func (*Curve) D

func (c *Curve) D() *big.Int

func (*Curve) Equal

func (c *Curve) Equal(o *Curve) bool

Equal reports whether two curves are the same instance type.

func (*Curve) G

func (c *Curve) G() *Point

func (*Curve) GenerateRandomScalarAndPoint

func (c *Curve) GenerateRandomScalarAndPoint(rnd prng.PRNG) (lambda *big.Int, Q *Point, err error)

GenerateRandomScalarAndPoint draws a scalar λ ∈ [2, q-1] and returns (λ, λ*G) (spec §12, curve.generateRandomScalarAndPoint).

NB: the spec writes λ = 2 + prng.bigInt(q-2); the reference implementation (and hence the test vectors) instead uses rejection sampling `do { λ = bigInt(q) } while λ ∈ {0, 1}`. We follow the implementation so the PRNG stream is consumed identically.

func (*Curve) IsOnCurve

func (c *Curve) IsOnCurve(x, y *big.Int) bool

IsOnCurve reports whether (x, y) lies on the curve (spec §12):

x2 + y2 == 1 + d*x2*y2  (mod p)  where x2 = x^2, y2 = y^2.

func (*Curve) MulAdd

func (c *Curve) MulAdd(a *big.Int, p1 *Point, b *big.Int, p2 *Point) (Q, Qp *Point, err error)

MulAdd computes Q = a*P1 + b*P2 (spec §12, curve.mulAdd). When p2.X is nil, only p2.Y is known and there are two candidates for Q, both returned. When p2.X is present, Q == Qp. Fails if a point is not on the curve or p2.Y is not a valid y-coordinate.

func (*Curve) Name

func (c *Curve) Name() string

func (*Curve) Nu

func (c *Curve) Nu() *big.Int

func (*Curve) P

func (c *Curve) P() *big.Int

func (*Curve) PointAddition

func (c *Curve) PointAddition(p1, p2 *Point) *Point

PointAddition returns P1 + P2 on the curve (spec §12, curve.pointAddition). It does NOT check that the inputs are on the curve.

func (*Curve) Q

func (c *Curve) Q() *big.Int

func (*Curve) ScalarMultiplication

func (c *Curve) ScalarMultiplication(n, y *big.Int) *big.Int

ScalarMultiplication returns the y-coordinate of nP given only the y-coordinate of P (spec §12, curve.scalarMultiplication).

The negation of an Edwards-curve point is (x, y) -> (-x, y), so P and its two x-roots (x, -x) are negatives of each other and nP, n(-P) share the same y-coordinate. We therefore recover any x for y and reuse the full Montgomery ladder; the result y is independent of the chosen sign. Returns nil when y is not a valid curve y-coordinate (no x exists).

func (*Curve) ScalarMultiplicationWithX

func (c *Curve) ScalarMultiplicationWithX(n *big.Int, p *Point) (*Point, error)

ScalarMultiplicationWithX returns both coordinates of nP using a Montgomery ladder over point additions (spec §12, curve.scalarMultiplicationWithX). It fails if P is not on the curve.

func (*Curve) XCoordinatesFromY

func (c *Curve) XCoordinatesFromY(y *big.Int) (x1, x2 *big.Int, err error)

XCoordinatesFromY returns the two possible x-coordinates (x, -x mod p) for a point on the curve given its y-coordinate, or an error when y is not the y-coordinate of any curve point (spec §12, curve.xCoordinatesFromY).

type Error

type Error string

func (Error) Error

func (e Error) Error() string

type Point

type Point struct {
	X *big.Int
	Y *big.Int
}

Point is an affine point (x, y). X may be nil when only the y-coordinate is known (the spec's ⊥ for the x-coordinate).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL