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.

50 lines
1.4 KiB

  1. use ark_ec::PairingEngine;
  2. use ark_ff::{to_bytes, Zero};
  3. use ark_mnt4_298::MNT4_298;
  4. use ark_mnt6_298::MNT6_298;
  5. use ark_r1cs_std::alloc::AllocVar;
  6. use ark_r1cs_std::fields::nonnative::NonNativeFieldVar;
  7. use ark_r1cs_std::{R1CSVar, ToBitsGadget, ToBytesGadget};
  8. use ark_relations::r1cs::ConstraintSystem;
  9. #[test]
  10. fn to_bytes_test() {
  11. let cs = ConstraintSystem::<<MNT6_298 as PairingEngine>::Fr>::new_ref();
  12. let target_test_elem = <MNT4_298 as PairingEngine>::Fr::from(123456u128);
  13. let target_test_gadget = NonNativeFieldVar::<
  14. <MNT4_298 as PairingEngine>::Fr,
  15. <MNT6_298 as PairingEngine>::Fr,
  16. >::new_witness(cs, || Ok(target_test_elem))
  17. .unwrap();
  18. let target_to_bytes: Vec<u8> = target_test_gadget
  19. .to_bytes()
  20. .unwrap()
  21. .iter()
  22. .map(|v| v.value().unwrap())
  23. .collect();
  24. // 123456 = 65536 + 226 * 256 + 64
  25. assert_eq!(target_to_bytes[0], 64);
  26. assert_eq!(target_to_bytes[1], 226);
  27. assert_eq!(target_to_bytes[2], 1);
  28. for byte in target_to_bytes.iter().skip(3) {
  29. assert_eq!(*byte, 0);
  30. }
  31. assert_eq!(to_bytes!(target_test_elem).unwrap(), target_to_bytes);
  32. }
  33. #[test]
  34. fn to_bits_test() {
  35. type F = ark_bls12_377::Fr;
  36. type CF = ark_bls12_377::Fq;
  37. let cs = ConstraintSystem::<CF>::new_ref();
  38. let f = F::zero();
  39. let f_var = NonNativeFieldVar::<F, CF>::new_input(cs.clone(), || Ok(f)).unwrap();
  40. f_var.to_bits_le().unwrap();
  41. }