nist

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package nist allows simple and abstracted operations in the NIST P-256, P-384, and P-521 groups, wrapping filippo.io/nistec.

Index

Constants

View Source
const (
	// H2CP256 is the hash-to-curve ciphersuite identifier for P-256.
	H2CP256 = nistP256.H2CP256
	// E2CP256 is the encode-to-curve ciphersuite identifier for P-256.
	E2CP256 = nistP256.E2CP256
	// H2CP384 is the hash-to-curve ciphersuite identifier for P-384.
	H2CP384 = nistP384.H2CP384
	// E2CP384 is the encode-to-curve ciphersuite identifier for P-384.
	E2CP384 = nistP384.E2CP384
	// H2CP521 is the hash-to-curve ciphersuite identifier for P-521.
	H2CP521 = nistP521.H2CP521
	// E2CP521 is the encode-to-curve ciphersuite identifier for P-521.
	E2CP521 = nistP521.E2CP521

	// IdentifierP256 identifies the P-256 group internally.
	IdentifierP256 = byte(3)
	// IdentifierP384 identifies the P-384 group internally.
	IdentifierP384 = byte(4)
	// IdentifierP521 identifies the P-521 group internally.
	IdentifierP521 = byte(5)
)

Variables

View Source
var (
	// ErrDecodeElementP256 is returned when input data could not be decoded to a P256 element.
	ErrDecodeElementP256 = errors.New("invalid P256 element encoding")

	// ErrDecodeElementP384 is returned when input data could not be decoded to a P384 element.
	ErrDecodeElementP384 = errors.New("invalid P384 element encoding")

	// ErrDecodeElementP521 is returned when input data could not be decoded to a P521 element.
	ErrDecodeElementP521 = errors.New("invalid P521 element encoding")
)

Functions

func P256

func P256() internal.Group

P256 returns the singleton P-256 group instance.

func P384

func P384() internal.Group

P384 returns the singleton P-384 group instance.

func P521

func P521() internal.Group

P521 returns the singleton P-521 group instance.

Types

type Element

type Element[Point nistECPoint[Point]] struct {
	// contains filtered or unexported fields
}

Element implements the Element interface for group elements over NIST curves.

func (*Element[Point]) Add

func (e *Element[Point]) Add(element internal.Element) internal.Element

Add sets the receiver to the sum of the input and the receiver, and returns the receiver.

func (*Element[Point]) Base

func (e *Element[Point]) Base() internal.Element

Base sets the element to the group's base point a.k.a. canonical generator.

func (*Element[P]) Copy

func (e *Element[P]) Copy() internal.Element

Copy returns a copy of the receiver.

func (*Element[P]) Decode

func (e *Element[P]) Decode(data []byte) error

Decode sets the receiver to a decoding of the input data, and returns an error on failure.

func (*Element[P]) DecodeHex

func (e *Element[P]) DecodeHex(h string) error

DecodeHex sets e to the decoding of the hex encoded element.

func (*Element[Point]) Double

func (e *Element[Point]) Double() internal.Element

Double sets the receiver to its double, and returns it.

func (*Element[P]) Encode

func (e *Element[P]) Encode() []byte

Encode returns the compressed byte encoding of the element.

func (*Element[Point]) Equal

func (e *Element[Point]) Equal(element internal.Element) int

Equal returns 1 if the elements are equivalent, and 0 otherwise.

func (*Element[Point]) Group

func (e *Element[Point]) Group() byte

Group returns the group's Identifier.

func (*Element[P]) Hex

func (e *Element[P]) Hex() string

Hex returns the fixed-sized hexadecimal encoding of e.

func (*Element[Point]) Identity

func (e *Element[Point]) Identity() internal.Element

Identity sets the element to the point at infinity of the Group's underlying curve.

func (*Element[P]) IsIdentity

func (e *Element[P]) IsIdentity() bool

IsIdentity returns whether the Element is the point at infinity of the Group's underlying curve.

func (*Element[P]) Multiply

func (e *Element[P]) Multiply(scalar internal.Scalar) internal.Element

Multiply sets the receiver to the scalar multiplication of the receiver with the given Scalar, and returns it.

func (*Element[P]) Negate

func (e *Element[P]) Negate() internal.Element

Negate sets the receiver to its negation, and returns it.

func (*Element[P]) Set

func (e *Element[P]) Set(element internal.Element) internal.Element

Set sets the receiver to the value of the argument, and returns the receiver.

func (*Element[P]) Subtract

func (e *Element[P]) Subtract(element internal.Element) internal.Element

Subtract subtracts the input from the receiver, and returns the receiver.

func (*Element[P]) XCoordinate

func (e *Element[P]) XCoordinate() []byte

XCoordinate returns the encoded x coordinate of the element.

type Group

type Group[Point nistECPoint[Point]] struct {
	NewPoint func() Point
	// contains filtered or unexported fields
}

Group exposes the prime-order API for a NIST curve backed by nistec points.

func (Group[P]) Base

func (g Group[P]) Base() internal.Element

Base returns the canonical generator.

func (Group[P]) Ciphersuite

func (g Group[P]) Ciphersuite() string

Ciphersuite returns the RFC 9380 ciphersuite identifier.

func (Group[P]) ElementLength

func (g Group[P]) ElementLength() int

ElementLength returns the encoded element length in bytes.

func (Group[P]) EncodeToGroup

func (g Group[P]) EncodeToGroup(input, dst []byte) (internal.Element, error)

EncodeToGroup encodes arbitrary input to a non-uniform group element.

func (Group[P]) HashFunc

func (g Group[P]) HashFunc() crypto.Hash

HashFunc returns the RFC9380 associated hash function of the group.

func (Group[P]) HashToGroup

func (g Group[P]) HashToGroup(input, dst []byte) (internal.Element, error)

HashToGroup hashes arbitrary input to a group element.

func (Group[P]) HashToScalar

func (g Group[P]) HashToScalar(input, dst []byte) (internal.Scalar, error)

HashToScalar hashes arbitrary input to a scalar.

func (Group[P]) NewElement

func (g Group[P]) NewElement() internal.Element

NewElement returns the identity element.

func (Group[P]) NewScalar

func (g Group[P]) NewScalar() internal.Scalar

NewScalar returns a new zero scalar.

func (Group[P]) Order

func (g Group[P]) Order() []byte

Order returns the canonical scalar field order encoding.

func (Group[P]) ScalarLength

func (g Group[P]) ScalarLength() int

ScalarLength returns the encoded scalar length in bytes.

type Scalar

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

Scalar implements the Scalar interface for group scalars.

func (*Scalar) Add

func (s *Scalar) Add(scalar internal.Scalar) internal.Scalar

Add sets the receiver to the sum of the input and the receiver, and returns the receiver.

func (*Scalar) Copy

func (s *Scalar) Copy() internal.Scalar

Copy returns a copy of the Scalar.

func (*Scalar) Decode

func (s *Scalar) Decode(data []byte) error

Decode sets s to a big-endian byte decoding of x. If x is not a canonical encoding of s, Decode returns an error.

func (*Scalar) DecodeHex

func (s *Scalar) DecodeHex(h string) error

DecodeHex sets s to the decoding of the hex encoded scalar.

func (*Scalar) DecodeWithReduction added in v0.10.0

func (s *Scalar) DecodeWithReduction(data []byte) error

DecodeWithReduction sets s to x modulo the group order. If x is nil or not of the correct input length, DecodeWithReduction returns an error.

func (*Scalar) Encode

func (s *Scalar) Encode() []byte

Encode returns the compressed byte encoding of the scalar.

func (*Scalar) Equal

func (s *Scalar) Equal(scalar internal.Scalar) int

Equal returns 1 if the scalars are equal, and 0 otherwise.

func (*Scalar) Group

func (s *Scalar) Group() byte

Group returns the group's Identifier.

func (*Scalar) Hex

func (s *Scalar) Hex() string

Hex returns the fixed-sized hexadecimal encoding of s.

func (*Scalar) Invert

func (s *Scalar) Invert() internal.Scalar

Invert sets the receiver to its modular inverse ( 1 / s ), and returns it.

func (*Scalar) IsZero

func (s *Scalar) IsZero() bool

IsZero returns whether the scalar is 0.

func (*Scalar) LessOrEqual

func (s *Scalar) LessOrEqual(scalar internal.Scalar) int

LessOrEqual returns 1 if s <= scalar, and 0 otherwise.

func (*Scalar) MinusOne

func (s *Scalar) MinusOne() internal.Scalar

MinusOne sets the scalar to order-1, and returns it.

func (*Scalar) Multiply

func (s *Scalar) Multiply(scalar internal.Scalar) internal.Scalar

Multiply multiplies the receiver with the input, and returns the receiver.

func (*Scalar) One

func (s *Scalar) One() internal.Scalar

One sets s to 1, and returns it.

func (*Scalar) Pow

func (s *Scalar) Pow(scalar internal.Scalar) internal.Scalar

Pow sets s to s**scalar modulo the group order, and returns s. If scalar is nil, it returns 1.

func (*Scalar) Random

func (s *Scalar) Random() internal.Scalar

Random sets s to a new random scalar and returns it. The random source is crypto/rand, and this functions is guaranteed to return a non-zero scalar.

func (*Scalar) Set

func (s *Scalar) Set(scalar internal.Scalar) internal.Scalar

Set sets the receiver to the value of the argument scalar, and returns the receiver.

func (*Scalar) SetUInt64

func (s *Scalar) SetUInt64(i uint64) internal.Scalar

SetUInt64 sets s to i modulo the field order, and returns an error if one occurs.

func (*Scalar) Subtract

func (s *Scalar) Subtract(scalar internal.Scalar) internal.Scalar

Subtract subtracts the input from the receiver, and returns the receiver.

func (*Scalar) UInt64

func (s *Scalar) UInt64() (uint64, error)

UInt64 returns the uint64 representation of the scalar, or an error if its value is higher than the authorized limit for uint64.

func (*Scalar) Zero

func (s *Scalar) Zero() internal.Scalar

Zero sets s to 0, and returns it.

Directories

Path Synopsis
Package sswu implements the shared constant-time Simplified SWU engine used by the NIST curve wrappers.
Package sswu implements the shared constant-time Simplified SWU engine used by the NIST curve wrappers.

Jump to

Keyboard shortcuts

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