Documentation

Go to Portal Website

Arithmetic Encoding

PlanPlatformsMASVS
TeamAndroidMASVS-RESILIENCE-3

Overview

Arithmetic Encoding is a build-time obfuscation control that transforms numeric constants in compiled bytecode into semantically equivalent but syntactically complex arithmetic expressions. Instead of storing literal values like 5 or 0x1234, the control replaces them with expressions such as (0xC ^ 0x9) or compound operations involving XOR, addition, and negation that evaluate to the original value at runtime.

This transformation operates at the Dalvik bytecode (smali) level after compilation but before packaging, ensuring no source code changes are required. The technique defeats constant-propagation optimizations in decompilers (jadx, Procyon, CFR) by forcing them to display complex expressions or partially-reduced forms, significantly increasing the difficulty of reverse engineering business logic, cryptographic keys, API endpoints, and other hard-coded values.

Arithmetic Encoding is probabilistic: each build applies transformations randomly, so repeated compilations produce different bytecode representations of the same constants. The control implements a global cap of 2,000 encodings per build to balance obfuscation strength against APK size inflation.

How It Works

Arithmetic Encoding operates during the AppTego Android protected build pipeline after the app is compiled to Dalvik bytecode but before final APK/AAB assembly. The process modifies .smali files (human-readable Dalvik bytecode representation) directly:

Transformation Patterns

The control applies several encoding strategies depending on the constant size and register constraints:

  1. XOR Split (const/4, const/16, const):
  1. Addition Split:
  1. Negation Chain:
  1. Wide Constant Encoding (const-wide/16):

Each transformation is applied with 33–50% probability per instruction, and the specific arithmetic operation (XOR vs. addition vs. negation) is chosen randomly, ensuring diversity across builds.

Scope and Exclusions

Float Safety

The control includes type-analysis logic to prevent transforming integer constants that are later used in floating-point operations (e.g., cmpl-float, div-float). Applying integer arithmetic (XOR, add) to a constant loaded into a register that will be interpreted as a float causes Android's bytecode verifier to reject the DEX file due to type conflicts at merge points. The float-detection heuristic scans ahead in the method body to identify such usage patterns and skips those constants.

Performance Impact

Arithmetic Encoding introduces minimal runtime overhead (typically <1ms per encoded constant) since the expressions are simple two-instruction sequences that execute at native CPU speed. The primary cost is increased APK size: each encoding adds 2–4 bytes of bytecode. The 2,000-encoding cap typically results in 4–8 KB of additional DEX code, well within the "insignificant" threshold for modern devices.

How to Enable the Control

Navigate to Code Obfuscation from the AppTego portal, and expand the Code And String Protection section. Under this section you will find the Arithmetic Encoding control. Click Enable to apply it to the next protected build.

API Configuration Example

{
  "ArithmeticEncoding": {
    "protection": true
  }
}
FieldPurpose
protectionEnables arithmetic encoding for protected builds.

Threats Mitigated

Caveats

Support Matrix

PlatformMinimum VersionPlan Requirement
iOSNot supported
AndroidAPI 21+ (5.0 Lollipop)TEAM

Build Pipeline Requirement: Android AppTego protected build pipeline (AppTego protected build workers). Apps built locally via gradlew or CI/CD without MobileDefender's AppTego protected build pipeline will not receive this obfuscation.

Plan Requirement

TEAM plan or higher is required to enable Arithmetic Encoding. The control is gated in ObfuscationManager.java via subscription tier check and must be explicitly enabled in the customer's obfuscation_controls configuration array.

To enable, ensure "ArithmeticEncoding" is present in the app's configuration JSON:

{
  "obfuscation_controls": [
    "ArithmeticEncoding"
  ]
}

If the subscription is TEAM or higher and the control is listed, it will be applied automatically during the next app build.