The Carlini–Wagner attack, often shortened to the C&W attack, is an optimization-based white-box adversarial attack that searches for the smallest perturbation of an input that causes a neural network classifier to output a chosen wrong label. Introduced by Nicholas Carlini and David Wagner in 2017, it reformulates the search for adversarial examples as a constrained optimization problem solved with gradient descent, and it produces perturbations that are typically far smaller than those of earlier attacks.
In AI security, the C&W attack became a standard benchmark for evaluating defenses. Its original publication showed that defensive distillation, then considered a strong protection, offered almost no robustness against a well-designed optimization attack, and it shaped how researchers test adversarial robustness today.
How the Carlini–Wagner attack formulates the problem
The attack minimizes a sum of two terms: the size of the perturbation and a penalty that becomes zero once the model prefers the target class. Formally, it minimizes the distance between the original input x and the adversarial input x plus delta, plus a constant c multiplied by an objective function f that measures how far the model still is from the desired misclassification.
The most effective objective function in the original work operates on logits rather than softmax probabilities. It compares the largest logit among the non-target classes with the logit of the target class and stops rewarding progress once the target logit exceeds the others by a confidence margin kappa. Working on logits avoids the vanishing gradients that the softmax produces near saturation.
Three technical choices make the attack strong and stable:
- Change of variables with tanh: the perturbation is expressed through a tanh transform, so pixel values always stay inside the valid range without clipping.
- Binary search over c: the constant that balances distance and misclassification is tuned automatically for each input.
- Adam optimizer: an adaptive gradient method converges reliably over hundreds or thousands of iterations.
Distance metrics and attack variants
The C&W paper defined three attack variants, one for each common way of measuring perturbation size. Each norm captures a different notion of how noticeable a change is, and each variant needs a different optimization strategy.
| Variant | Distance measured | Optimization approach | Typical perturbation |
|---|---|---|---|
| L2 attack | Euclidean size of the change | Direct gradient descent with tanh | Small, spread over many pixels |
| L0 attack | Number of changed pixels | Iteratively fixes unimportant pixels | Few pixels, larger changes |
| L-infinity attack | Largest change to any pixel | Penalizes values above a shrinking threshold | Uniform, low-amplitude noise |
The L2 variant is by far the most widely used and is usually what practitioners mean by the C&W attack. Its perturbations on standard image datasets are often imperceptible to human observers, while still driving the misclassification rate on undefended models close to 100 percent.
Comparison with other adversarial attacks
The C&W attack occupies the high-cost, high-precision end of the white-box attack spectrum. Faster attacks produce larger perturbations, whereas C&W spends far more computation to find a near-minimal one.
| Attack | Strategy | Iterations | Perturbation size | Main use |
|---|---|---|---|---|
| Fast gradient sign method | Single signed gradient step | 1 | Large, fixed budget | Quick tests, adversarial training |
| Projected gradient descent | Repeated steps within a norm ball | 10 to 100 | Up to a fixed budget | Robustness evaluation and training |
| DeepFool | Linearizes decision boundary | A few | Small, not always minimal | Measuring boundary distance |
| Carlini–Wagner | Penalized optimization on logits | Hundreds to thousands | Near minimal | Benchmarking defenses |
The key conceptual difference is that projected gradient descent asks for any successful perturbation within a fixed budget, while C&W asks for the smallest successful perturbation. That makes C&W well suited to measuring how close inputs lie to a decision boundary, but too slow for generating adversarial examples during every training step.
Effect on adversarial defenses
The C&W attack demonstrated that many defenses only hide gradients instead of removing vulnerability. Defensive distillation, for example, made softmax outputs extremely saturated so that gradients approached zero, but the logit-based objective of C&W bypassed this effect entirely.
The following table summarizes how common defense families have fared against optimization attacks such as C&W.
| Defense | Mechanism | Outcome against C&W |
|---|---|---|
| Defensive distillation | Softens training labels, saturates softmax | Broken by logit-based loss |
| Gradient masking defenses | Obscure or shatter gradients | Usually broken by adapted attacks |
| Detection classifiers | Flag suspicious inputs | Often evaded when detector is attacked jointly |
| Adversarial training | Trains on attacked examples | Substantially raises required perturbation |
| Certified robustness | Provable bounds such as randomized smoothing | Guaranteed within the certified radius |
A central lesson is the need for adaptive attacks: a defense must be evaluated against an attacker who knows the defense and modifies the objective accordingly. Testing only against the default C&W configuration is no longer considered sufficient evidence of robustness.
Using the attack for robustness evaluation
Security teams use C&W mainly for measurement, not deployment-time attacks. Toolkits such as the Adversarial Robustness Toolbox, Foolbox and CleverHans provide implementations, and the typical parameters are a confidence margin kappa between 0 and about 40, nine or ten binary search steps for c, and 1,000 or more optimization iterations.
Increasing kappa produces high-confidence adversarial examples that transfer better to other models, which matters for estimating black-box risk. The main practical limitations are computational cost, since each input requires its own optimization, and the white-box assumption, which requires access to the model gradients.
A non-technical view
Picture a photo of a cat that an AI system labels correctly. The Carlini–Wagner attack searches for the tiniest possible edit that makes the system call it a dog. It changes pixels so slightly that a person sees no difference at all.
The method works like a careful forger. It tries again and again, each time making the edit smaller while keeping the fake convincing. Researchers use it to test protection. If a defense cannot stop it, the defense is weaker than it appears.
A concrete example
An automotive supplier evaluates a traffic sign classifier before integrating it into a driver assistance system. The security team runs the L2 Carlini–Wagner attack on 1,000 test images with kappa set to 0 and nine binary search steps. On the baseline model, the attack succeeds on every image with an average L2 distance below 0.5 on images scaled to the range 0 to 1, a change invisible on screen.
The team then applies adversarial training with projected gradient descent and repeats the evaluation, adding an adaptive version of the attack. The required perturbation grows several times, and the success rate within the agreed distance budget falls sharply. The measured distance becomes a release metric, and the report documents that the remaining risk applies to white-box attackers only.