use crate::gpu::GpuAffinePoint;
use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::AffinePoint;
#[allow(dead_code)]
pub fn limbs_to_be_bytes(limbs: &[u32; 8]) -> [u8; 32] {
let mut bytes = [0u8; 32];
for i in 0..8 {
let be = limbs[7 - i].to_be_bytes();
bytes[i * 4..(i + 1) * 4].copy_from_slice(&be);
}
bytes
}
pub fn be_bytes_to_limbs(bytes: &[u8; 32]) -> [u32; 8] {
let mut limbs = [0u32; 8];
for i in 0..8 {
limbs[i] = u32::from_be_bytes([
bytes[(7 - i) * 4],
bytes[(7 - i) * 4 + 1],
bytes[(7 - i) * 4 + 2],
bytes[(7 - i) * 4 + 3],
]);
}
limbs
}
#[allow(dead_code)]
pub fn limbs_to_le_bytes(arr: &[u32; 8]) -> [u8; 32] {
let mut bytes = [0u8; 32];
for (i, &val) in arr.iter().enumerate() {
bytes[i * 4..(i + 1) * 4].copy_from_slice(&val.to_le_bytes());
}
bytes
}
pub fn scalar_be_to_limbs(bytes: &[u8; 32]) -> [u32; 8] {
be_bytes_to_limbs(bytes)
}
pub fn affine_to_gpu(point: &AffinePoint) -> GpuAffinePoint {
let encoded = point.to_encoded_point(false);
let x_bytes = encoded.x().unwrap();
let y_bytes = encoded.y().unwrap();
let mut x_arr = [0u8; 32];
let mut y_arr = [0u8; 32];
x_arr.copy_from_slice(x_bytes);
y_arr.copy_from_slice(y_bytes);
GpuAffinePoint {
x: be_bytes_to_limbs(&x_arr),
y: be_bytes_to_limbs(&y_arr),
}
}
#[allow(dead_code)]
pub fn u256_to_u128(val: &[u8; 32]) -> u128 {
let mut bytes = [0u8; 16];
bytes.copy_from_slice(&val[0..16]);
u128::from_le_bytes(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_limbs_to_be_bytes_roundtrip() {
let original: [u32; 8] = [1, 2, 3, 4, 5, 6, 7, 8];
let bytes = limbs_to_be_bytes(&original);
let recovered = be_bytes_to_limbs(&bytes);
assert_eq!(original, recovered);
}
#[test]
fn test_limbs_to_be_bytes_value() {
let mut limbs = [0u32; 8];
limbs[0] = 0x01020304;
let bytes = limbs_to_be_bytes(&limbs);
assert_eq!(bytes[28], 0x01);
assert_eq!(bytes[29], 0x02);
assert_eq!(bytes[30], 0x03);
assert_eq!(bytes[31], 0x04);
}
#[test]
fn test_scalar_be_to_limbs() {
let mut bytes = [0u8; 32];
bytes[31] = 0x42;
let limbs = scalar_be_to_limbs(&bytes);
assert_eq!(limbs[0] & 0xFF, 0x42);
}
#[test]
fn test_u256_to_u128() {
let mut val = [0u8; 32];
val[0] = 0x42;
let result = u256_to_u128(&val);
assert_eq!(result, 0x42);
}
}