Skip to content

Quadratic voting example showcasing libraries#30

Open
mohammadfawaz wants to merge 1 commit intomainfrom
mohammadfawaz/quadratic_voting
Open

Quadratic voting example showcasing libraries#30
mohammadfawaz wants to merge 1 commit intomainfrom
mohammadfawaz/quadratic_voting

Conversation

@mohammadfawaz
Copy link
Copy Markdown
Collaborator

@mohammadfawaz mohammadfawaz commented Mar 27, 2026

Summary

quadratic_vote is a governance example that demonstrates Leo library packages and library submodules.

Two reusable libraries handle all the math:

Package Kind What it provides
sqrt_math library isqrt — integer square root via Newton's method
vote_math library QV weights, credit costs, voter profiles (depends on sqrt_math)
governance.aleo program Private vote records, proposal outcome checks

Under Quadratic Voting (QV), a participant's vote weight equals ⌊√(token_balance)⌋ and casting k votes costs credits. This makes influence grow with the square root of wealth rather than linearly — fairer to smaller holders.

Noteworthy Features

  • Library packagessqrt_math and vote_math have no .aleo program name, are never deployed on their own, and only take effect when included by a program package.
  • Library submodules — each extra .leo file under src/ becomes a named module; no declaration needed.
  • Cross-submodule calls within a libraryvote_math::cost::build_profile calls vote_math::weight::compute; sqrt_math::checks::is_perfect_square calls sqrt_math::newton::isqrt.
  • Cross-library submodule callsvote_math::weight::compute reaches directly into sqrt_math::newton::isqrt, demonstrating the full dependency chain (governance → vote_math → sqrt_math).
  • Top-level library constantssqrt_math::ISQRT_MAX, vote_math::MAX_WEIGHT, and vote_math::MIN_BALANCE are defined in lib.leo and referenced by name from within submodules.
  • Library struct exportvote_math::cost::VoterProfile is defined in a submodule and used as a variable type inside governance.aleo.
  • Private vote receipt — the Vote record is private; the token balance that drove the weight computation is never revealed on-chain.

Test plan

  • cd quadratic_vote/sqrt_math && leo build
  • cd quadratic_vote/vote_math && leo build
  • cd quadratic_vote/governance && leo build
  • cd quadratic_vote/governance && leo test → 18/18 pass
  • cd quadratic_vote && bash run.sh → all steps produce expected output

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/quadratic_voting branch 3 times, most recently from 67ae9f8 to 2c8312f Compare March 30, 2026 15:38
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/quadratic_voting branch 3 times, most recently from f192ea7 to d512558 Compare April 14, 2026 03:26
@mohammadfawaz mohammadfawaz self-assigned this Apr 14, 2026
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/quadratic_voting branch from d512558 to 8fd99ff Compare April 14, 2026 03:31
@mohammadfawaz mohammadfawaz marked this pull request as ready for review April 14, 2026 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant