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.
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
Floating-Point Tricks
Bitwise Expressions (Lua 5.3+ / Luau)
Modular Arithmetic
For any constantn and chosen k, you can construct:
Table of Common Constants
Boolean and Nil Obfuscation
Boolean Obfuscation
Nil Obfuscation
In Real Conditional Expressions
Table-Based Constant Lookup
Store constants in an encoded table and access them by index.Basic Table Lookup
Encoded Table Built by Decoder
Mathematical Identity Chains
Build a value through a chain of reversible operations.Reversible Operation Chain
Constant Factory Function
Float Precision Exploits
Use float arithmetic that resolves to exact integers at runtime.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.