encrypt.zk 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. k = 13;
  8. constant "Encrypt" {}
  9. witness "Encrypt" {
  10. # We are encrypting values to this public key
  11. EcNiPoint pubkey,
  12. # Emphemeral secret value
  13. Base ephem_secret,
  14. # Values we are encrypting
  15. Base value_1,
  16. Base value_2,
  17. Base value_3,
  18. }
  19. circuit "Encrypt" {
  20. ################################################
  21. # 1. Derive shared secret using DH
  22. ################################################
  23. ephem_pub = ec_mul_var_base(ephem_secret, pubkey);
  24. ephem_pub_x = ec_get_x(ephem_pub);
  25. ephem_pub_y = ec_get_y(ephem_pub);
  26. # Used by the receiver to also derive the same shared secret
  27. constrain_instance(ephem_pub_x);
  28. constrain_instance(ephem_pub_y);
  29. shared_secret = poseidon_hash(ephem_pub_x, ephem_pub_y);
  30. ################################################
  31. # 2. Derive blinding factors for witness values
  32. ################################################
  33. N1 = witness_base(1);
  34. N2 = witness_base(2);
  35. N3 = witness_base(3);
  36. blind_1 = poseidon_hash(shared_secret, N1);
  37. blind_2 = poseidon_hash(shared_secret, N2);
  38. blind_3 = poseidon_hash(shared_secret, N3);
  39. ################################################
  40. # 3. Encrypt the values by applying blinds
  41. ################################################
  42. # This could be add or mul
  43. enc_value_1 = base_mul(value_1, blind_1);
  44. enc_value_2 = base_mul(value_2, blind_2);
  45. enc_value_3 = base_mul(value_3, blind_3);
  46. constrain_instance(enc_value_1);
  47. constrain_instance(enc_value_2);
  48. constrain_instance(enc_value_3);
  49. }