encrypt.zk 1.8 KB

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