voting.zk 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. k = 13;
  2. field = "pallas";
  3. constant "Vote" {
  4. EcFixedPointShort VALUE_COMMIT_VALUE,
  5. EcFixedPoint VALUE_COMMIT_RANDOM,
  6. EcFixedPointBase NULLIFIER_K,
  7. }
  8. witness "Vote" {
  9. Base process_id_0,
  10. Base process_id_1,
  11. Base secret_key,
  12. Base vote,
  13. Scalar vote_blind,
  14. Uint32 leaf_pos,
  15. MerklePath path,
  16. }
  17. circuit "Vote" {
  18. # Nullifier hash
  19. process_id = poseidon_hash(process_id_0, process_id_1);
  20. nullifier = poseidon_hash(secret_key, process_id);
  21. constrain_instance(nullifier);
  22. # Public key derivation and hashing
  23. public_key = ec_mul_base(secret_key, NULLIFIER_K);
  24. public_x = ec_get_x(public_key);
  25. public_y = ec_get_y(public_key);
  26. pk_hash = poseidon_hash(public_x, public_y);
  27. # Merkle root
  28. root = merkle_root(leaf_pos, path, pk_hash);
  29. constrain_instance(root);
  30. # Pedersen commitment for vote
  31. vcv = ec_mul_short(vote, VALUE_COMMIT_VALUE);
  32. vcr = ec_mul(vote_blind, VALUE_COMMIT_RANDOM);
  33. vote_commit = ec_add(vcv, vcr);
  34. # Since vote_commit is a curve point, we fetch its coordinates
  35. # and constrain_them:
  36. vote_commit_x = ec_get_x(vote_commit);
  37. vote_commit_y = ec_get_y(vote_commit);
  38. constrain_instance(vote_commit_x);
  39. constrain_instance(vote_commit_y);
  40. }