Skip to content

Lyd/underflow constraints#494

Open
lydiagarms wants to merge 5 commits into
masterfrom
lyd/underflowConstraints
Open

Lyd/underflow constraints#494
lydiagarms wants to merge 5 commits into
masterfrom
lyd/underflowConstraints

Conversation

@lydiagarms

@lydiagarms lydiagarms commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

Currently in the circuits in Starlight, there are no checks to prevent underflows. Say we have a decrementation:
c -= a, we do not currently check that c > a first in the circuits. This means that c could underflow to the field modulus minus a. In contracts like Escrow.zol, this will lead to a user having a very high balance.

Changes

  • Changes to the circuit boilerplate for decrementation of unknown variables.
  • Changes to toCircuit.ts to add in underflow guards.

Checklist

  • Tests added/updated
  • CI passes
  • No secrets/keys committed
  • Docs updated (if needed)
  • Backwards compatible (or noted breaking change)

How to test

  1. Compile the Underflows.zol contract and check the circuits for underflow guards

Screenshots / Evidence (if applicable)

NA

@lydiagarms lydiagarms force-pushed the lyd/underflowConstraints branch 2 times, most recently from 7ca3ae7 to 9b4e2b6 Compare July 1, 2026 17:08
@lydiagarms lydiagarms force-pushed the lyd/underflowConstraints branch from 9b4e2b6 to 8df4208 Compare July 2, 2026 13:26
@lydiagarms lydiagarms marked this pull request as ready for review July 2, 2026 13:26
@lydiagarms lydiagarms requested review from SwatiEY and Wei-257 July 2, 2026 13:55
@lydiagarms lydiagarms force-pushed the lyd/underflowConstraints branch from 1fd1727 to 62d98ca Compare July 2, 2026 19:29
@lydiagarms lydiagarms force-pushed the lyd/underflowConstraints branch from 62d98ca to 590e8e6 Compare July 13, 2026 12:25
@lydiagarms lydiagarms force-pushed the lyd/underflowConstraints branch from 325cdbe to dfc7067 Compare July 13, 2026 16:07
node.expression?.leftHandSide?.typeName === 'bool'
? 'bool'
: 'field';
return withUnderflowGuardAssertions(

@Wei-257 Wei-257 Jul 14, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 390, case 'InternalFunctionCall', is there any reason that we do not wrap it with "withUnderflowGuardAssertions(...)"?

for example, what if in solidity, we have something like "helper(a - amount);" which rejects when amount is greater than a. but in the circuit, this would not be enforced based on the current behaviour.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get an unrelated compiler error for inputs to internal function calls such as these. In commit d011d87 I have therefore added this as unsupported.

@lydiagarms lydiagarms requested a review from AdarshRon July 15, 2026 09:22
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.

2 participants