voting.zk 1.1 KB

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