sp1_curves/uint256.rs
1use typenum::{U32, U63};
2
3use num::{BigUint, One};
4use serde::{Deserialize, Serialize};
5
6use crate::params::{FieldParameters, NumLimbs};
7
8/// Although `U256` is technically not a field, we utilize `FieldParameters` here for compatibility.
9/// This approach is specifically for the `FieldOps` multiplication operation, which employs these
10/// parameters solely as a modulus, rather than enforcing the requirement of being a proper field.
11#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
12pub struct U256Field;
13
14impl FieldParameters for U256Field {
15 /// The modulus of the field. It is represented as a little-endian array of 33 bytes.
16 const MODULUS: &'static [u8] = &[
17 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18 0, 0, 1,
19 ];
20
21 /// A rough witness-offset estimate given the size of the limbs and the size of the field.
22 const WITNESS_OFFSET: usize = 1usize << 14;
23
24 /// The modulus of Uint235 is 2^256.
25 fn modulus() -> BigUint {
26 BigUint::one() << 256
27 }
28}
29
30impl NumLimbs for U256Field {
31 type Limbs = U32;
32 // Note we use one more limb than usual because for mulmod with mod 1<<256, we need an extra
33 // limb.
34 type Witness = U63;
35}