> ## Documentation Index
> Fetch the complete documentation index at: https://starcryptdocs.starsolenterprise.com/lua-docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Constant Folding and Constant Obfuscation in Lua/Luau

> Obfuscate numeric and boolean constants in Lua and Luau by replacing literals with equivalent computed expressions, table lookups, and arithmetic identities.

Constant values like numbers, booleans, and `nil` in source code are trivially readable by anyone who opens the script. Constant obfuscation replaces these literals with runtime-computed equivalents that resolve to the same value. This page covers the difference between compiler constant folding (an optimization) and constant obfuscation (the reverse), then shows you how to apply every major technique to Lua and Luau.

> [!NOTE]
>
> **Constant Folding vs. Constant Obfuscation**: Constant folding is a compiler optimization where expressions like `2 + 3` are evaluated at compile time and replaced with `5`. Constant obfuscation is the reverse: replacing `5` with `2 + 3` so the literal value is not visible in source. We are doing the reverse.

## Numeric Constant Expansion

Replace integer and float literals with mathematically equivalent expressions.

### Integer Arithmetic Tricks

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- 42 → (6 * 7)
local answer = (6 * 7)

-- 255 → (2^8 - 1)
local maxByte = (2 ^ 8 - 1)

-- 1024 → (2^10)
local kilobyte = (2 ^ 10)

-- 100 → (10 ^ 2)
local hundred = (10 ^ 2)

-- 16 → (2 ^ 4)
local hexBase = (2 ^ 4)

-- 7 → (1 + 2 + 4)
local lucky = (1 + 2 + 4)
```

### Floating-Point Tricks

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- 3.14159 → (math.pi - 0.00000265)
local approxPi = (math.pi - 0.00000265)

-- 2.71828 → math.exp(1)
local approxE = math.exp(1)

-- 1.41421 → math.sqrt(2)
local approxSqrt2 = math.sqrt(2)
```

### Bitwise Expressions (Lua 5.3+ / Luau)

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- 15 → (0x0F)
local fifteen = (0x0F)

-- 255 → (0xFF & 0xFF)
local maxByte = (0xFF & 0xFF)

-- 128 → (0x80 | 0x00)
local halfByte = (0x80 | 0x00)

-- 1 → (0x01 << 0)
local one = (0x01 << 0)
```

### Modular Arithmetic

For any constant `n` and chosen `k`, you can construct:

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- n → ((n + k) % (k + n) + n) simplifies to n
local n = 42
local k = 7
local obfuscated = ((n + k) % (k + n) + n)  -- = 42 + 7 % 49 + 42 = 42
```

A cleaner pattern:

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- n → ((n + k) - k)
local value = ((42 + 13) - 13)  -- = 42
```

### Table of Common Constants

| Constant | Obfuscated Form |
| - | - |
| `0` | `math.floor(0.9)` |
| `1` | `(0 + 1)` |
| `2` | `(1 + 1)` |
| `10` | `(5 * 2)` |
| `42` | `(6 * 7)` |
| `100` | `(10 ^ 2)` |
| `255` | `(2 ^ 8 - 1)` |
| `256` | `(2 ^ 8)` |
| `1000` | `(10 ^ 3)` |
| `1024` | `(2 ^ 10)` |

## Boolean and Nil Obfuscation

### Boolean Obfuscation

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- true → (1 == 1)
local enabled = (1 == 1)

-- true → (not false)
local active = (not false)

-- true → (type(nil) == "nil")
local valid = (type(nil) == "nil")

-- false → (1 == 2)
local disabled = (1 == 2)

-- false → (not true)
local inactive = (not true)

-- false → (nil == 1)
local invalid = (nil == 1)
```

### Nil Obfuscation

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- nil → ({})[1]
local nothing = ({})[1]

-- nil → ({})._nonexistent
local empty = ({})._nonexistent

-- nil → next({})
local none = next({})
```

### In Real Conditional Expressions

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- Before
if config.enabled and user ~= nil then

-- After
if (config.enabled == (1 == 1)) and (user ~= (({})[1])) then
```

## Table-Based Constant Lookup

Store constants in an encoded table and access them by index.

### Basic Table Lookup

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
local K = {[1] = 42, [2] = 255, [3] = 1024, [4] = 0}

-- Instead of: local maxHealth = 42
local maxHealth = K[1]

-- Instead of: local maxByte = 255
local maxByte = K[2]

-- Instead of: local bufferSize = 1024
local bufferSize = K[3]
```

### Encoded Table Built by Decoder

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
local function decodeConstants(seed)
    local K = {}
    K[1] = seed * 6 + 12  -- if seed = 5: 42
    K[2] = seed * 51 - 0  -- if seed = 5: 255
    K[3] = seed * 204 + 4 -- if seed = 5: 1024
    return K
end

local K = decodeConstants(5)
local maxHealth = K[1]  -- 42
```

## Mathematical Identity Chains

Build a value through a chain of reversible operations.

### Reversible Operation Chain

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- ((x + 7) * 3 - 21) / 3 == x
local function obfuscateConstant(x)
    return ((x + 7) * 3 - 21) / 3
end

-- Usage
local value = obfuscateConstant(42)  -- returns 42
```

### Constant Factory Function

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
local function constantFactory(index)
    if index == 1 then
        return ((6 * 7))
    elseif index == 2 then
        return ((2 ^ 8 - 1))
    elseif index == 3 then
        return ((2 ^ 10))
    elseif index == 4 then
        return ((10 ^ 2))
    elseif index == 5 then
        return ((math.floor(0.9)))
    else
        return 0
    end
end

-- Usage
local maxHealth = constantFactory(1)     -- 42
local maxByte = constantFactory(2)      -- 255
local bufferSize = constantFactory(3)   -- 1024
```

## Float Precision Exploits

Use float arithmetic that resolves to exact integers at runtime.

```lua theme={"theme":{"light":"ayu-dark","dark":"night-owl"}}
-- 42 → math.floor(math.sqrt(1764))
local fortyTwo = math.floor(math.sqrt(1764))

-- 7 → math.floor(math.log(1096.63) / math.log(2))
local seven = math.floor(math.log(1096.63) / math.log(2))

-- 16 → math.floor(math.exp(2.772588722239781))
local sixteen = math.floor(math.exp(2.772588722239781))

-- 100 → math.floor(math.pow(10, 2))
local hundred = math.floor(math.pow(10, 2))

-- 3 → math.floor(math.pi)
local three = math.floor(math.pi)

-- 1 → math.ceil(math.sin(1.5707963267948966) - 0.1)
local one = math.ceil(math.sin(math.pi / 2) - 0.1)
```

> [!TIP]
>
> Combine constant obfuscation with string encoding: encode a string key as a number, then decode it via the constant pipeline. For example, store `key = 42` obfuscated, then use it to index into a table of encoded strings.

> [!NOTE]
>
> Luau's type checker may warn about some constant expressions that do not narrow to the expected type. If you see type errors, use type casts or annotations to suppress them. For example, `local x: number = ((6 * 7))` explicitly tells Luau the result is a number.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.