FieldElement

Mojo struct 🡭

FieldElement

@memory_only
struct FieldElement

An element in POLYVAL’s field.

This type represents an element of the binary field GF(2^128) modulo the irreducible polynomial x^128 + x^127 + x^126 + x^121 + 1 as described in RFC8452 §3.

Arithmetic in POLYVAL’s field has the following properties:

  • All arithmetic operations are performed modulo the polynomial above.
  • Addition is equivalent to the XOR operation applied to the two field elements
  • Multiplication is carryless

Implemented traits

AnyType, Copyable, Deinitable, Equatable, Movable, Writable

Methods

__init__

fn def __init__(out self, var v: Array[UInt8, Int(16)])

Args:

  • v (Array[UInt8, Int(16)])
  • self (Self)

Returns:

Self

fn def __init__(out self, v: SIMD[.uint64, 2])

Build an element from two 64-bit limbs (low limb first).

Args:

  • v (SIMD[.uint64, 2])
  • self (Self)

Returns:

Self

__add__

fn def __add__(self, rhs: Self) -> Self

Adds two POLYVAL field elements.

In POLYVAL’s field, addition is the equivalent operation to XOR.

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

__mul__

fn def __mul__(self, rhs: Self) -> Self

Multiply two POLYVAL field elements mod x^128 + x^127 + x^126 + x^121 + 1.

Dispatches to the 64-bit Karatsuba path on 64-bit platforms and the 32-bit path on 32-bit platforms.

Args:

  • self (Self)
  • rhs (Self)

Returns:

Self

__imul__

fn def __imul__(mut self, rhs: Self)

Args:

  • self (Self)
  • rhs (Self)

zeros

@staticmethod
fn def zeros() -> Self

Returns:

Self

into_bytes

fn def into_bytes(deinit self) -> Array[UInt8, Int(16)]

Consume the element and return its BLOCK_SIZE-byte representation.

Takes deinit self, not var self: transferring _v out dismantles the element, which is only permitted when the callee also takes over responsibility for destroying it.

Args:

  • self (Self)

Returns:

Array[UInt8, Int(16)]

write_to

fn def write_to(self, mut writer: T)

Args:

  • self (Self)
  • writer (T)