voting.zk 1.1 KB

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