const LIMB_BITS: usize = 51;
const MASK: u64 = (1u64 << LIMB_BITS) - 1;
pub(crate) const LIMB_COUNT: usize = 5;
pub(crate) const D_LIMBS: [u64; LIMB_COUNT] = [
929_955_233_495_203,
466_365_720_129_213,
1_662_059_464_998_953,
2_033_849_074_728_123,
1_442_794_654_840_575,
];
pub(crate) const TWO_D_LIMBS: [u64; LIMB_COUNT] = [
1_859_910_466_990_425,
932_731_440_258_426,
1_072_319_116_312_658,
1_815_898_335_770_999,
633_789_495_995_903,
];
pub(crate) const SQRT_M1_LIMBS: [u64; LIMB_COUNT] = [
1_718_705_420_411_056,
234_908_883_556_509,
2_233_514_472_574_048,
2_117_202_627_021_982,
765_476_049_583_133,
];
#[derive(Clone, Copy, Debug)]
pub(crate) struct Fe51 {
limbs: [u64; LIMB_COUNT],
}
impl Fe51 {
pub(crate) fn from_limbs_unchecked(limbs: [u64; LIMB_COUNT]) -> Self {
debug_assert!(limbs.iter().all(|&limb| limb < (1u64 << 52)));
Self { limbs }
}
pub(crate) const fn zero() -> Self {
Self {
limbs: [0; LIMB_COUNT],
}
}
pub(crate) const fn one() -> Self {
Self {
limbs: [1, 0, 0, 0, 0],
}
}
pub(crate) const fn two() -> Self {
Self {
limbs: [2, 0, 0, 0, 0],
}
}
pub(crate) fn from_bytes_unchecked(bytes: &[u8; 32]) -> Self {
Self {
limbs: [
load_u64_le(bytes, 0) & MASK,
(load_u64_le(bytes, 6) >> 3) & MASK,
(load_u64_le(bytes, 12) >> 6) & MASK,
(load_u64_le(bytes, 19) >> 1) & MASK,
(load_u64_le(bytes, 24) >> 12) & MASK,
],
}
}
pub(crate) fn loose_limbs(&self) -> [u64; LIMB_COUNT] {
debug_assert!(self.limbs.iter().all(|&limb| limb < (1u64 << 52)));
self.limbs
}
}
fn load_u64_le(bytes: &[u8; 32], offset: usize) -> u64 {
u64::from_le_bytes(bytes[offset..offset + 8].try_into().unwrap())
}