You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

19 lines
540 B

  1. use num_bigint::BigUint;
  2. pub fn u256_to_u64(a: [u8; 32]) -> u64 {
  3. let mut b8: [u8; 8] = [0; 8];
  4. b8.copy_from_slice(&a[32 - 8..32]);
  5. u64::from_be_bytes(b8)
  6. }
  7. pub fn usize_to_u256(i: usize) -> [u8; 32] {
  8. let i_bytes = i.to_be_bytes();
  9. let mut r: [u8; 32] = [0; 32];
  10. r[32 - i_bytes.len()..].copy_from_slice(&i_bytes);
  11. r
  12. }
  13. pub fn str_to_u256(s: &str) -> [u8; 32] {
  14. let bi = s.parse::<BigUint>().unwrap().to_bytes_be();
  15. let mut r: [u8; 32] = [0; 32];
  16. r[32 - bi.len()..].copy_from_slice(&bi[..]);
  17. r
  18. }