> For the complete documentation index, see [llms.txt](https://thias-organization.gitbook.io/p256-documentation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thias-organization.gitbook.io/p256-documentation/risc-zero-k256-accelerator/field-element-implementation.md).

# Field Element Implementation

`FieldElement` is a low-level representation of field elements used in elliptic curve cryptographic operations. It encapsulates the arithmetic and other operations on elements of the finite field used in the elliptic curve.

Originally, RustCrypto only has `FieldElement` that are catered for 32-bit and 64-bit architectures. The optimised version aims to leverage architecture-specific features to improve performance, especially for the `riscv32` architecture. This is achieved using conditional compilation (`cfg`):

```rust
cfg_if! {
    if #[cfg(all(target_os = "zkvm", target_arch = "riscv32"))] {
        mod field_8x32_risc0;
    } else if #[cfg(target_pointer_width = "32")] {
        mod field_10x26;
    } else if #[cfg(target_pointer_width = "64")] {
        mod field_5x52;
    } else {
        compile_error!("unsupported target word size (i.e. target_pointer_width)");
    }
}
```

## FieldElement8x32 RISC Zero

`FieldElement8x32R0` is the specific `FieldElement` for zkVM architecture. In this implementation, there were several modified functions that uses the accelerated 256-bit modular multiplication `modmul_u256_denormalized`.

For example:

```rust
/// Returns self * rhs mod p
pub fn mul(&self, rhs: &Self) -> Self {
    Self(risc0::modmul_u256_denormalized(&self.0, &rhs.0, &MODULUS))
}

/// Multiplies by a single-limb integer.
pub fn mul_single(&self, rhs: u32) -> Self {
    Self(risc0::modmul_u256_denormalized(
        &self.0,
        &U256::from_words([rhs, 0, 0, 0, 0, 0, 0, 0]),
        &MODULUS,
    ))
}

/// Returns self * self
pub fn square(&self) -> Self {
    Self(risc0::modmul_u256_denormalized(&self.0, &self.0, &MODULUS))
}
```

The rest of the functions in `FieldElement8x32R0` remains largely unchanged compared to `FieldElement10x26`

* Note that zkVM is a software emulator that implements a 32-bit RISC-V instruction set
* More information about its technical specification can be found [here](https://dev.risczero.com/api/zkvm/zkvm-specification#introduction)
