Skip to content

Support arrays, structs, and identifiers in ternary operators#3222

Open
mohammadfawaz wants to merge 1 commit into
stagingfrom
mohammadfawaz/complex_ternary
Open

Support arrays, structs, and identifiers in ternary operators#3222
mohammadfawaz wants to merge 1 commit into
stagingfrom
mohammadfawaz/complex_ternary

Conversation

@mohammadfawaz
Copy link
Copy Markdown
Collaborator

@mohammadfawaz mohammadfawaz commented Apr 17, 2026

Summary

Extends the ternary instruction to accept array, struct, and identifier operands (in addition to the pre-existing literal set), gated behind ConsensusVersion::V15.

  • console and circuit: new Ternary impls for Literal and Plaintext (array/struct aware, recursive on fields/elements); new Ternary impl for IdentifierLiteral.
  • synthesizer-program: ternary operation now handles non-literal operands; type-checking ensures both branches have matching array shape / struct layout.
  • synthesizer: cost model updated; deployment validation via check_no_non_literal_ternary rejects non-literal / non-pre-V15-supported ternary before V15.

Consensus gating

Before V15, deployments containing ternary on arrays, structs, or identifier operands are aborted — in function, finalize, closure, and constructor bodies. At V15 and later, they are accepted and executable.

String is intentionally not supported

The string literal type is not a permitted ternary operand at any version:

  • Pre-V15: check_no_non_literal_ternary's pre-V15 allow-list excludes string (it was never in the original macro-based list).
  • V15+: the new ensure_no_string_leaves check in Ternary::output_types recursively walks the branch plaintext type and rejects any string leaf (including inside arrays or structs, local or external).

Rationale: Aleo strings are variable-length byte arrays, but the byte-wise MUX that underlies circuit ternary requires both branches to have matching byte-lengths. During deployment, circuit synthesis samples strings with Literal::sample(String, rng), which yields random lengths; supporting strings would require either a padded sampling strategy or a graceful length-check path, neither of which is a small change. If we ever want strings, it would be a follow-up with its own consensus gate.

Tests

synthesizer/src/vm/tests/test_v15/ternary_plaintext.rs covers:

  • V14 rejection / V15 acceptance for array, struct, identifier, finalize, closure, and constructor cases.
  • Execution: arrays of various sizes, nested structs, identifiers, structs containing external records.
  • Type-mismatch rejection (array size mismatch, struct field mismatch).
  • ternary inside finalize.
  • String rejection at V15 (both plain string and structs containing a string field).

Unit tests in console/program/src/data/literal/ternary.rs and circuit/program/src/data/literal/ternary.rs exercise Literal::ternary dispatch across all supported variants, including Identifier.

@mohammadfawaz mohammadfawaz changed the title Support arrays and structs and ternary operators Support arrays and structs in ternary operators Apr 17, 2026
@mohammadfawaz mohammadfawaz self-assigned this Apr 17, 2026
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/complex_ternary branch 2 times, most recently from be16d8f to f333d2e Compare April 17, 2026 03:14
@mohammadfawaz mohammadfawaz marked this pull request as ready for review April 17, 2026 14:46
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/complex_ternary branch from f333d2e to f6e5a6b Compare April 17, 2026 16:42
@mohammadfawaz mohammadfawaz requested review from d0cd and vicsn April 17, 2026 16:42
@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/complex_ternary branch from f6e5a6b to d794d30 Compare April 17, 2026 16:45
@mohammadfawaz mohammadfawaz changed the title Support arrays and structs in ternary operators Support arrays, structs, and identifiers in ternary operators Apr 17, 2026
@mohammadfawaz
Copy link
Copy Markdown
Collaborator Author

mohammadfawaz commented Apr 18, 2026

Support for this in Leo is here: ProvableHQ/leo#29349

Shows on average 7.8% drop in Aleo program sizes across the Leo test suite.

@mohammadfawaz mohammadfawaz force-pushed the mohammadfawaz/complex_ternary branch from d794d30 to fa9c2c2 Compare May 12, 2026 17:36
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