encrypt.zk 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Verifiable encryption inside ZK
  2. # Normally this algo will be hardened due to malleability attacks
  3. # on the ciphertext, but the ZK proof ensures that the ciphertext
  4. # cannot be modified.
  5. #
  6. # This is basically the el gamal scheme in ZK
  7. contract "Encrypt" {
  8. # We are encrypting values to this public key
  9. Base pub_x,
  10. Base pub_y,
  11. # Emphemeral secret value
  12. Scalar ephem_secret,
  13. # Values we are encrypting
  14. Base value_1,
  15. Base value_2,
  16. Base value_3,
  17. }
  18. circuit "Encrypt" {
  19. ################################################
  20. # 1. Derive shared secret using DH
  21. ################################################
  22. # TODO: get this working {
  23. dest_pub = ec_witness(pub_x, pub_y);
  24. ephem_pub = ec_mul(ephem_secret, dest_pub);
  25. # }
  26. ephem_pub_x = ec_get_x(ephem_pub);
  27. ephem_pub_y = ec_get_y(ephem_pub);
  28. # Used by the receiver to also derive the same shared secret
  29. constrain_instance(ephem_pub_x);
  30. constrain_instance(ephem_pub_y);
  31. shared_secret = poseidon_hash(ephem_pub_x, ephem_pub_y);
  32. ################################################
  33. # 2. Derive blinding factors for witness values
  34. ################################################
  35. N1 = witness_base(1);
  36. N2 = witness_base(2);
  37. N3 = witness_base(3);
  38. blind_1 = poseidon_hash(shared_secret, N1);
  39. blind_2 = poseidon_hash(shared_secret, N2);
  40. blind_3 = poseidon_hash(shared_secret, N3);
  41. ################################################
  42. # 3. Encrypt the values by applying blinds
  43. ################################################
  44. # This could be add or mul
  45. enc_value_1 = base_mul(value_1, blind_1);
  46. enc_value_2 = base_mul(value_2, blind_2);
  47. enc_value_3 = base_mul(value_3, blind_3);
  48. constrain_instance(enc_value_1);
  49. constrain_instance(enc_value_2);
  50. constrain_instance(enc_value_3);
  51. }