Skip to main content
The types module provides the fundamental data types used throughout snarkVM. All types are generic over an Environment or Network parameter.

Field

Field elements over the base field of the curve.

Structure

E::Field
The underlying field element

Constants

usize
The field size in bits
usize
The field size in bytes
usize
The field capacity for data bits

Methods

fn new(field: E::Field) -> Self
Initializes a new field element
fn new_domain_separator(domain: &str) -> Self
Initializes a field element as a domain separator
fn from_u8(value: u8) -> Self
Creates a field element from a u8
fn from_u16(value: u16) -> Self
Creates a field element from a u16
fn from_u32(value: u32) -> Self
Creates a field element from a u32
fn from_u64(value: u64) -> Self
Creates a field element from a u64
fn from_u128(value: u128) -> Self
Creates a field element from a u128
fn zero() -> Self
Returns the additive identity (0)
fn one() -> Self
Returns the multiplicative identity (1)
fn half() -> Self
Returns 1 * 2^(-1)

Arithmetic Operations

Field supports standard arithmetic:
  • Addition: field1 + field2
  • Subtraction: field1 - field2
  • Multiplication: field1 * field2
  • Division: field1 / field2
  • Negation: -field
  • Inversion: field.inverse()?
  • Square: field.square()
  • Square root: field.square_root()?
  • Power: field.pow(&[u64])

Example

Group

Group elements on the elliptic curve.

Structure

E::Projective
The underlying group element in projective coordinates

Constants

Field<E>
The coefficient A for the twisted Edwards curve equation
Field<E>
The coefficient D for the twisted Edwards curve equation
Field<E>
The coefficient A for the Montgomery curve equation
Field<E>
The coefficient B for the Montgomery curve equation

Methods

fn new(group: E::Affine) -> Self
Initializes a new group element
fn generator() -> Self
Returns the prime subgroup generator
fn zero() -> Self
Returns the additive identity (point at infinity)
fn mul_by_cofactor(&self) -> Self
Returns self * COFACTOR
fn div_by_cofactor(&self) -> Self
Returns self / COFACTOR
fn to_x_coordinate(&self) -> Field<E>
Returns the x-coordinate
fn to_y_coordinate(&self) -> Field<E>
Returns the y-coordinate
fn to_xy_coordinates(&self) -> (Field<E>, Field<E>)
Returns both coordinates as a tuple
fn from_x_coordinate(x: Field<E>) -> Result<Self>
Recovers a group element from an x-coordinate
fn from_xy_coordinates(x: Field<E>, y: Field<E>) -> Result<Self>
Creates a group element from x and y coordinates

Arithmetic Operations

Group supports:
  • Addition: group1 + group2
  • Subtraction: group1 - group2
  • Scalar multiplication: group * scalar
  • Negation: -group
  • Doubling: group.double()

Example

Scalar

Scalar elements over the scalar field (used for private keys and exponents).

Structure

E::Scalar
The underlying scalar element

Constants

usize
The scalar size in bits
usize
The scalar size in bytes
usize
The scalar capacity for data bits

Methods

fn new(scalar: E::Scalar) -> Self
Initializes a new scalar
fn zero() -> Self
Returns the additive identity (0)
fn one() -> Self
Returns the multiplicative identity (1)
fn from_field(field: &Field<E>) -> Result<Self>
Converts a field element to a scalar (fails if out of range)
fn from_field_lossy(field: &Field<E>) -> Self
Converts a field element to a scalar with modular reduction
fn to_field(&self) -> Field<E>
Converts this scalar to a field element

Arithmetic Operations

Scalar supports:
  • Addition: scalar1 + scalar2
  • Subtraction: scalar1 - scalar2
  • Multiplication: scalar1 * scalar2
  • Division: scalar1 / scalar2
  • Negation: -scalar
  • Inversion: scalar.inverse()?
  • Square: scalar.square()
  • Power: scalar.pow(&[u64])

Example

Boolean

Boolean values.

Structure

bool
The underlying boolean value

Methods

fn new(boolean: bool) -> Self
Initializes a new boolean

Logical Operations

Boolean supports:
  • AND: bool1 & bool2
  • OR: bool1 | bool2
  • XOR: bool1 ^ bool2
  • NOT: !bool1

Example

Integer Types

Signed and unsigned integer types.

Available Types

Structure

Constants

Self
Maximum value for this integer type
Self
Minimum value for this integer type

Methods

fn new(integer: I) -> Self
Initializes a new integer
fn zero() -> Self
Returns zero
fn one() -> Self
Returns one
fn to_field(&self) -> Field<E>
Converts to a field element
fn to_scalar(&self) -> Scalar<E>
Converts to a scalar element

Arithmetic Operations

Integers support checked arithmetic:
  • Addition: int1 + int2 (wrapping)
  • Subtraction: int1 - int2 (wrapping)
  • Multiplication: int1 * int2 (wrapping)
  • Division: int1 / int2
  • Modulo: int1 % int2
  • Power: int1.pow(int2)
  • Absolute value: int.abs()

Example

StringType

UTF-8 encoded string type.

Structure

Methods

fn from_str(s: &str) -> Result<Self>
Creates a string from a &str

Example

Common Traits

All types implement common traits:
  • FromStr - Parse from string
  • Display - Convert to string
  • FromBytes / ToBytes - Binary serialization
  • FromBits / ToBits - Bit representation
  • Serialize / Deserialize - Serde support
  • Clone, Debug, PartialEq, Eq, Hash

Example

Type Conversions

Many conversions are available:

See Also