nacl.ristretto

The classes Ristretto255Scalar and Ristretto255Point provide a high-level abstraction around the low-level bindings to libsodium. Several functions are accessible through operator overloading.

See Finite field arithmetic for high-level documentation.

class nacl.ristretto.Ristretto255Scalar(value: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction])[source]

Scalar field modulo prime ORDER. Each element is a scalar value.

Variables
  • ZERO – Scalar with value 0

  • ONE – Scalar with value 1

  • MINUS_ONE – Scalar with value -1 (modulo ORDER)

  • SIZE – Size of Scalars in bytes (32)

  • NONREDUCED_SIZE – Size of non reduced scalar (64); see reduce().

  • ORDER – Group order (2 ** 252 + 27742317777372353535851937790883648493)

__add__(other: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Scalar[source]

Add two scalars.

Parameters

other – Any of the types supported by the constructor

Returns

Sum of self and other reduced modulo ORDER

__bool__() bool[source]

Check if scalar is non-zero.

Returns

True if non-zero, False otherwise

__bytes__() bytes[source]

Get byte representation of scalar.

Returns

Value of scalar in little-endian encoding

__eq__(other: object) bool[source]

Check if two scalars are identical. Comparing with other types such as int will return False.

Returns

True if equal, False otherwise

__init__(value: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) None[source]

Create a new Ristretto255Scalar.

Parameters

value – Value of the scalar. Will be converted according to its type.

Raises

exc.TypeError – Type not supported

Value can be one of:

  • Ristretto255Scalar: Create a new object with the same value.

  • bytes: value must be SIZE bytes in little-endian order.

  • int: value will be reduced modulo ORDER.

  • Fraction: Numerator of value multiplied with the inverse of its denominator.

__int__() int[source]

Get integer representation of scalar.

Returns

Value of scalar reduced modulo ORDER

__mul__(other: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Scalar[source]

Multiply two scalars.

Parameters

other – Any of the types supported by the constructor

Returns

Product of self and other modulo ORDER

__neg__() nacl.ristretto.Ristretto255Scalar[source]

Get the additive inverse such that -x + x == Ristretto255Scalar.ZERO.

Returns

Additive inverse

__sub__(other: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Scalar[source]

Subtract other from self.

Parameters

other – Any of the types supported by the constructor

Returns

Difference of self and other reduced modulo ORDER

__truediv__(other: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Scalar[source]

Divide two scalars.

Parameters

other – Any of the types supported by the constructor

Returns

Product of self and inverse of other modulo ORDER

property complement: nacl.ristretto.Ristretto255Scalar

Get the complement such that x.complement + x == Ristretto255Scalar.ONE.

Note that this is not the two’s complement where ~x + x == -1.

Returns

Complemental value reduced modulo ORDER

property inverse: nacl.ristretto.Ristretto255Scalar

Get multiplicative inverse such that x.inverse * x == Ristretto255Scalar.ONE.

Returns

Multiplicative inverse reduced modulo ORDER

classmethod random() nacl.ristretto.Ristretto255Scalar[source]

Create non-zero random scalar.

Returns

Random scalar

classmethod random_zero() nacl.ristretto.Ristretto255Scalar[source]

Create a random scalar that could be zero.

Returns

Ristretto255Scalar: Random scalar

classmethod reduce(value: bytes) nacl.ristretto.Ristretto255Scalar[source]

Reduce a larger value, e.g. the output of a hash function, to a scalar. There should be at least 317 bits to ensure almost uniformity.

Parameters

valueNONREDUCED_SIZE bytes in little-endian encoding

Returns

Value reduced modulo ORDER

class nacl.ristretto.Ristretto255Point(value: bytes, _assume_valid: bool = False)[source]

Ristretto255 group. Each element is a curve point.

Variables
  • ORDER – Group order

  • SIZE – Size of Points in bytes (32)

  • HASH_SIZE – Size input for from_hash() (64).

  • ZERO – Neutral element

__add__(other: nacl.ristretto.Ristretto255Point) nacl.ristretto.Ristretto255Point[source]

Add two points.

Parameters

other – A group point

Returns

Sum of self and other

__bool__() bool[source]

Check if this is not the zero / neutral / identity point.

Returns

False if zero point, True otherwise

__bytes__() bytes[source]

Get byte representation of point.

Returns

Little-endian byte representation of point

__eq__(other: object) bool[source]

Compare this point to another point.

Parameters

other – Other point to compare to

Returns

True if same point, False otherwise or if not a Ristretto255Scalar

__mul__(other: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Point[source]

Multiply the non-zero scalar other with the point.

Parameters

other – Scalar value, any type supported by Ristretto255Scalar.

Returns

Product of self and other

__neg__() nacl.ristretto.Ristretto255Point[source]

Get inverse element such that -self + self == Ristretto255Point.ZERO.

Returns

Inverse of self

__sub__(other: nacl.ristretto.Ristretto255Point) nacl.ristretto.Ristretto255Point[source]

Subtract two points.

Parameters

other – A group point

Returns

Difference of self and other

classmethod base_mul(n: Union[nacl.ristretto.Ristretto255Scalar, bytes, int, fractions.Fraction]) nacl.ristretto.Ristretto255Point[source]

Multiply the non-zero scalar n with the Ed25519 base point.

Parameters

n – Scalar value, any type supported by Ristretto255Scalar.

Returns

Product of the Ed25519 base point and n

classmethod from_hash(value: bytes) nacl.ristretto.Ristretto255Point[source]

Map 64 bytes of input, e.g. the result of a hash function, to a group point. This might be the zero point, e.g. if hash value is all zeros.

Parameters

valueHASH_SIZE bytes in little-endian encoding

Returns

Point created from value

classmethod random() nacl.ristretto.Ristretto255Point[source]

Generate a random Ristretto255 point. This might be, although astronomically unlikely, the zero point.

Returns

Random point