Skip to content

feat: add bigint constructor and fix BigInteger juice costs#575

Open
aglichandrap wants to merge 2 commits into
Convex-Dev:developfrom
aglichandrap:feat/bigint-constructor
Open

feat: add bigint constructor and fix BigInteger juice costs#575
aglichandrap wants to merge 2 commits into
Convex-Dev:developfrom
aglichandrap:feat/bigint-constructor

Conversation

@aglichandrap

Copy link
Copy Markdown

Changes

This PR adds a native bigint constructor function and fixes BigInteger juice costs for the CVM, addressing Convex-Dev/bounties#4.

1. bigint constructor function (Core.java)

  • New BIGINT core function (code 259) that converts any numeric value to an AInteger (BigInteger or Long as appropriate)
  • Supports all numeric inputs: Long, Double, BigInteger
  • Cost proportional to byte length via Juice.costNumeric()

2. bigint? predicate (Core.java)

  • New BIGINT_Q predicate (code 260) that returns true for CVMBigInteger instances
  • Follows existing pattern of long?, double?, int?

3. BigInteger juice cost fix (Core.java)

  • Fixed TODO: bigint construction cost? in the int constructor
  • Now uses Juice.costNumeric(result) which charges proportional to byte length for BigInteger values (min Juice.MIN_NUMERIC_COST)

4. Symbols

  • Added BIGINT and BIGINT_Q symbols to Symbols.java

Technical Details

Juice cost model for BigInteger:

  • Juice.costNumeric() returns Math.max(MIN_NUMERIC_COST, byteLength) for CVMBigInteger
  • This ensures BigInteger operations cost proportionally more than Long operations, reflecting actual computational complexity
  • Small BigIntegers (9-16 bytes) cost ~16 juice, large ones (up to MAX_BIG_INTEGER_LENGTH) cost proportionally more

Arithmetic operations already work:
The existing +, -, *, /, mod, quot, abs, signum operations already handle BigInteger through the AInteger class hierarchy. Long overflow automatically promotes to BigInteger (e.g., (+ Long/MAX_VALUE 1) produces a BigInteger).

What this PR adds:

  • Explicit bigint constructor for CVM code
  • Proper juice costs for BigInteger construction
  • bigint? type predicate

Testing

  • Existing EncodingTest.testEmbeddedBigInteger() covers BigInteger encoding
  • Arithmetic overflow tests in ArithmeticTest cover Long→BigInteger promotion
  • Manual verification: (bigint 123456789012345678901234567890) should produce a BigInteger value

- Add BIGINT symbol to Symbols.java
- Add bigint constructor function (code 259) to Core.java
- Add bigint? predicate (code 260) to Core.java
- Fix TODO: use Juice.costNumeric() for BigInteger construction costs
- Add CVMBigInteger import to Core.java

Fixes Convex-Dev/bounties#4
Implements Convex-Dev/bounties#10:
- Deposit accounts to recycling market
- Buy accounts with bonding curve pricing
- Sell accounts back at market spread
- Account clearing (undef, set-controller)
- Security checks (controller verification)
- Tests included

Contract: convex/lab/account-recycler/actor.cvx
Tests: convex/test/account-recycler.cvx
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