> ## Documentation Index
> Fetch the complete documentation index at: https://compact-by-example.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Primitive Types

> Compact's primitive types explained: bounded Uint, Field, Bytes, Boolean, and Opaque — with disclosure rules and safe casting on Midnight.

## The Contract

<Accordion title="View Contract Code">
  ```compact theme={null}
  pragma language_version >= 0.23;

  import CompactStandardLibrary;

  // Unsigned integers - bounded type
  export ledger counter: Uint<0..1000>;

  // Unsigned integers - sized type (32 bits)
  export ledger balance: Uint<32>;

  // Field element for ZK computations
  export ledger commitment: Field;

  // Fixed-length byte array
  export ledger hash: Bytes<32>;

  // Opaque type - hashed inside circuits, full value visible off-chain
  export ledger secretValue: Opaque<"string">;

  export circuit updateCounter(newValue: Uint<0..1000>): [] {
    counter = disclose(newValue);
  }

  export circuit updateBalance(amount: Uint<32>): [] {
    // a + b widens; assert no overflow, then cast back to Uint<32>
    assert(balance <= (4294967295 as Uint<32>) - amount, "balance overflow");
    balance = disclose((balance + amount) as Uint<32>);
  }

  export circuit storeCommitment(value: Field): [] {
    commitment = disclose(value);
  }

  export circuit storeHash(data: Bytes<32>): [] {
    hash = disclose(data);
  }

  export circuit updateSecret(secret: Opaque<"string">): [] {
    secretValue = disclose(secret);
  }
  ```
</Accordion>

## How It Works

### Unsigned Integer Types - Bounded

```compact theme={null}
export ledger counter: Uint<0..1000>;
```

`Uint<m..n>` represents values from `m` to `n` (inclusive). Lower bound must be `0`.

### Unsigned Integer Types - Sized

```compact theme={null}
export ledger balance: Uint<32>;
```

`Uint<n>` uses up to `n` bits, equivalent to `Uint<0..(2^n - 1)>`. The maximum width is `Uint<248>`.

### Field Type

```compact theme={null}
export ledger commitment: Field;
```

Elements of the scalar prime field used by the ZK proof system. Arithmetic on `Field` wraps modulo the prime. Comparison operators (`<`, `<=`) are **not** defined on `Field`; convert to a `Uint` first if you need ordering.

### Bytes Type

```compact theme={null}
export ledger hash: Bytes<32>;
```

Fixed-length byte arrays. `Bytes<n>` for exactly `n` bytes.

### Opaque Types

```compact theme={null}
export ledger secretValue: Opaque<"string">;
```

`Opaque<"label">` is a value whose internal representation is **opaque to circuit logic**: the circuit can hash it, store it, pass it around, and compare it for equality (`==`, `!=`), but cannot do arithmetic on it or inspect individual bytes. The witness layer holds the real bytes; inside the circuit the value is represented by its hash.

### Disclosure Rule for Circuit Parameters

```compact theme={null}
counter = disclose(newValue);
```

Compact's privacy model is conservative: any circuit parameter is treated as **potentially witness-derived** at compile time. Writing a parameter directly to ledger state without `disclose()` fails with:

```
potential witness-value disclosure must be declared but is not
```

This applies to every type — `Uint`, `Field`, `Bytes`, and `Opaque` alike. `disclose(x)` is a compiler annotation that says "I know this value flows from a (potentially) private source to a public location and that's intentional."

See [Privacy & Disclosure](/basics/hello-world) for more on the disclosure rule.

### Arithmetic Widening and Casts

Compact has no silent overflow. Arithmetic on bounded integers produces a **wider** result type:

```compact theme={null}
balance = disclose((balance + amount) as Uint<32>);   // assert first, then cast
```

See [Overflow Protection](/basics/overflow-protection) for the full pattern.

### Boolean Type

```compact theme={null}
export ledger flag: Boolean;
```

Two values: `true` and `false`. Standard operators: `&&`, `||`, `!`, `==`, `!=`.

## What's Next

<CardGroup cols={2}>
  <Card title="Hello World" icon="hand-wave" href="/basics/hello-world">
    Apply types in your first contract
  </Card>

  <Card title="First App" icon="rocket" href="/basics/first-app">
    Build a counter with type safety
  </Card>
</CardGroup>
