Documentation Index
Fetch the complete documentation index at: https://mintlify.com/provablehq/snarkvm/llms.txt
Use this file to discover all available pages before exploring further.
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
pub struct Field<E: Environment> {
field: E::Field,
}
The underlying field element
Constants
The field capacity for data bits
Methods
new
fn new(field: E::Field) -> Self
Initializes a new field element
new_domain_separator
fn new_domain_separator(domain: &str) -> Self
Initializes a field element as a domain separator
from_u8
fn from_u8(value: u8) -> Self
Creates a field element from a u8
from_u16
fn from_u16(value: u16) -> Self
Creates a field element from a u16
from_u32
fn from_u32(value: u32) -> Self
Creates a field element from a u32
from_u64
fn from_u64(value: u64) -> Self
Creates a field element from a u64
from_u128
fn from_u128(value: u128) -> Self
Creates a field element from a u128
Returns the additive identity (0)
Returns the multiplicative identity (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
use snarkvm_console::types::Field;
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
// Create field elements
let zero = Field::<CurrentNetwork>::zero();
let one = Field::<CurrentNetwork>::one();
let field = Field::<CurrentNetwork>::from_u64(42);
// Arithmetic
let sum = field + one;
let product = field * Field::from_u64(2);
let inverse = field.inverse()?;
// Comparison
assert!(field != zero);
assert!(field > zero);
// Serialization
let bits = field.to_bits_le();
let bytes = field.to_bytes_le()?;
Group
Group elements on the elliptic curve.
Structure
pub struct Group<E: Environment> {
group: E::Projective,
}
The underlying group element in projective coordinates
Constants
The coefficient A for the twisted Edwards curve equation
The coefficient D for the twisted Edwards curve equation
The coefficient A for the Montgomery curve equation
The coefficient B for the Montgomery curve equation
Methods
new
fn new(group: E::Affine) -> Self
Initializes a new group element
Returns the prime subgroup generator
Returns the additive identity (point at infinity)
mul_by_cofactor
fn mul_by_cofactor(&self) -> Self
Returns self * COFACTOR
div_by_cofactor
fn div_by_cofactor(&self) -> Self
Returns self / COFACTOR
to_x_coordinate
fn to_x_coordinate(&self) -> Field<E>
Returns the x-coordinate
to_y_coordinate
fn to_y_coordinate(&self) -> Field<E>
Returns the y-coordinate
to_xy_coordinates
fn to_xy_coordinates(&self) -> (Field<E>, Field<E>)
Returns both coordinates as a tuple
from_x_coordinate
fn from_x_coordinate(x: Field<E>) -> Result<Self>
Recovers a group element from an x-coordinate
from_xy_coordinates
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
use snarkvm_console::types::{Group, Scalar, Field};
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
// Get the generator
let generator = Group::<CurrentNetwork>::generator();
// Scalar multiplication
let scalar = Scalar::<CurrentNetwork>::from_u64(42);
let point = generator * scalar;
// Point addition
let sum = point + generator;
// Access coordinates
let x = point.to_x_coordinate();
let (x, y) = point.to_xy_coordinates();
// Recover from x-coordinate
let recovered = Group::from_x_coordinate(x)?;
Scalar
Scalar elements over the scalar field (used for private keys and exponents).
Structure
pub struct Scalar<E: Environment> {
scalar: E::Scalar,
}
The underlying scalar element
Constants
The scalar capacity for data bits
Methods
new
fn new(scalar: E::Scalar) -> Self
Initializes a new scalar
Returns the additive identity (0)
Returns the multiplicative identity (1)
from_field
fn from_field(field: &Field<E>) -> Result<Self>
Converts a field element to a scalar (fails if out of range)
from_field_lossy
fn from_field_lossy(field: &Field<E>) -> Self
Converts a field element to a scalar with modular reduction
to_field
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
use snarkvm_console::types::Scalar;
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
let scalar = Scalar::<CurrentNetwork>::from_u64(42);
let doubled = scalar * Scalar::from_u64(2);
let inverse = scalar.inverse()?;
Boolean
Boolean values.
Structure
pub struct Boolean<E: Environment> {
boolean: bool,
_phantom: PhantomData<E>,
}
The underlying boolean value
Methods
new
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
use snarkvm_console::types::Boolean;
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
let t = Boolean::<CurrentNetwork>::new(true);
let f = Boolean::<CurrentNetwork>::new(false);
assert!(*t & *t);
assert!(*t | *f);
assert!(!(*f));
Integer Types
Signed and unsigned integer types.
Available Types
pub type I8<E> = Integer<E, i8>;
pub type I16<E> = Integer<E, i16>;
pub type I32<E> = Integer<E, i32>;
pub type I64<E> = Integer<E, i64>;
pub type I128<E> = Integer<E, i128>;
pub type U8<E> = Integer<E, u8>;
pub type U16<E> = Integer<E, u16>;
pub type U32<E> = Integer<E, u32>;
pub type U64<E> = Integer<E, u64>;
pub type U128<E> = Integer<E, u128>;
Structure
pub struct Integer<E: Environment, I: IntegerType> {
integer: I,
_phantom: PhantomData<E>,
}
Constants
Maximum value for this integer type
Minimum value for this integer type
Methods
new
fn new(integer: I) -> Self
Initializes a new integer
to_field
fn to_field(&self) -> Field<E>
Converts to a field element
to_scalar
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
use snarkvm_console::types::{U8, U64, I32};
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
let byte = U8::<CurrentNetwork>::new(255);
let large = U64::<CurrentNetwork>::new(1000000);
let signed = I32::<CurrentNetwork>::new(-42);
assert_eq!(*byte, 255u8);
assert_eq!(*large, 1000000u64);
assert_eq!(*signed, -42i32);
StringType
UTF-8 encoded string type.
Structure
pub struct StringType<N: Network> {
// Internal string representation
}
Methods
from_str
fn from_str(s: &str) -> Result<Self>
Creates a string from a &str
Example
use snarkvm_console::types::StringType;
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
let string = StringType::<CurrentNetwork>::from_str("Hello, Aleo!")?;
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
use std::str::FromStr;
use snarkvm_console::types::Field;
use snarkvm_console::network::MainnetV0;
type CurrentNetwork = MainnetV0;
// Parse from string
let field = Field::<CurrentNetwork>::from_str("123field")?;
// Convert to string
let s = field.to_string();
// Binary serialization
let bytes = field.to_bytes_le()?;
let recovered = Field::from_bytes_le(&bytes)?;
// Bit representation
let bits = field.to_bits_le();
let recovered = Field::from_bits_le(&bits)?;
Type Conversions
Many conversions are available:
// Field ↔ Scalar (with validation)
let scalar = Scalar::from_field(&field)?;
let field = scalar.to_field();
// Integer ↔ Field
let field = integer.to_field();
let integer = Integer::from_field(&field)?;
// Group ↔ Field
let field = group.to_field();
let group = Group::from_field(&field)?;
See Also