pub const P521_SCALAR_SIZE: usize = 66;
pub const P521_FIELD_ELEMENT_SIZE: usize = 66;
pub const P521_POINT_UNCOMPRESSED_SIZE: usize = 1 + 2 * P521_FIELD_ELEMENT_SIZE;
pub const P521_POINT_COMPRESSED_SIZE: usize = 1 + P521_FIELD_ELEMENT_SIZE;
pub const P521_KEM_SHARED_SECRET_KDF_OUTPUT_SIZE: usize = 64;
pub(crate) const P521_LIMBS: usize = 17;
pub(crate) fn p521_bytes_to_limbs(bytes_be: &[u8; P521_FIELD_ELEMENT_SIZE]) -> [u32; P521_LIMBS] {
let mut limbs = [0u32; P521_LIMBS];
#[allow(clippy::needless_range_loop)]
for i in 0..16 {
let offset = P521_FIELD_ELEMENT_SIZE - 4 - (i * 4);
limbs[i] = u32::from_be_bytes([
bytes_be[offset],
bytes_be[offset + 1],
bytes_be[offset + 2],
bytes_be[offset + 3],
]);
}
limbs[16] = ((bytes_be[0] as u32) << 8) | (bytes_be[1] as u32);
limbs[16] &= (1 << 9) - 1;
limbs
}
pub(crate) fn p521_limbs_to_bytes(limbs: &[u32; P521_LIMBS]) -> [u8; P521_FIELD_ELEMENT_SIZE] {
let mut bytes_be = [0u8; P521_FIELD_ELEMENT_SIZE];
#[allow(clippy::needless_range_loop)]
for i in 0..16 {
let limb_bytes = limbs[i].to_be_bytes();
let offset = P521_FIELD_ELEMENT_SIZE - 4 - (i * 4);
bytes_be[offset..offset + 4].copy_from_slice(&limb_bytes);
}
let ms_limb_val = limbs[16] & 0x1FF; bytes_be[0] = (ms_limb_val >> 8) as u8; bytes_be[1] = (ms_limb_val & 0xFF) as u8;
bytes_be[0] &= 0x01;
bytes_be
}