Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions CIPs/cip-166.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
CIP No.: 166
Title: CLZ Opcode
Author: PanaW (@PanaW)
Status: Draft
Type: Spec Breaking
Created: 2026-01-07
Required CIPs:
---

## Simple Summary

Introduce the `CLZ (0x1e)` instruction for counting leading zeros.

## Abstract

Introduce the `CLZ (0x1e)` instruction as [EIP-7939](https://eips.ethereum.org/EIPS/eip-7939), which pops x from the stack and pushes the number of leading zero bits in x to the stack. If x is zero, pushes 256.

## Motivation

Count leading zeros (CLZ) is a native opcode in many processor architectures (even in RISC architectures like ARM).

It is a basic building block used in math operations, byte operations, compression algorithms, data structures:

* lnWad
* powWad
* lambertW0Wad
* sqrt
* cbrt
* byte string comparisons
* generalized calldata compression/decompression
* bitmaps (for finding the next/previous set/unset bit)
* post quantum signature schemes

Adding a CLZ opcode will:

* Lead to cheaper compute.
* Lead to cheaper ZK proving costs. The fastest known Solidity implementation uses several dynamic bitwise right shifts shr, which are very expensive to prove. In SP1 rv32im, a 32-bit right shift costs 1.6x more than a 32-bit mul.
* Lead to smaller bytecode size. The fastest known Solidity implementation contains several large constants and is often inlined for performance.

## Specification

Same as [EIP-7939](https://eips.ethereum.org/EIPS/eip-7939).

## Rationale
N/A

## Backwards Compatibility
This CIP is Spec Breaking.

## Test Cases
<!--Test cases for an implementation are mandatory for CIPs that are affecting consensus changes. Other CIPs can choose to include links to test cases if applicable.-->
N/A

## Implementation
<!--The implementations must be completed before any CIP is given status "Final", but it need not be completed before the CIP is accepted. While there is merit to the approach of reaching consensus on the specification and rationale before writing code, the principle of "rough consensus and running code" is still useful when it comes to resolving many discussions of API details.-->
N/A

## Security Considerations
<!--All CIPs must contain a section that discusses the security implications/considerations relevant to the proposed change. Include information that might be important for security discussions, surfaces risks and can be used throughout the life cycle of the proposal. E.g. include security-relevant design decisions, concerns, important discussions, implementation-specific guidance and pitfalls, an outline of threats and risks and how they are being addressed. CIP submissions missing the "Security Considerations" section will be rejected. a CIP cannot proceed to status "Final" without a Security Considerations discussion deemed sufficient by the reviewers.-->
N/A

## Copyright
Copyright and related rights waived via [CC0](https://creativecommons.org/publicdomain/zero/1.0/).