Skip to content

Add Solidity implementation#237

Open
okwme wants to merge 9 commits intonayuki:masterfrom
trifle-labs:master
Open

Add Solidity implementation#237
okwme wants to merge 9 commits intonayuki:masterfrom
trifle-labs:master

Conversation

@okwme
Copy link
Copy Markdown

@okwme okwme commented Mar 6, 2026

Having QR in Solidity will allow onchain visual projects to render QR codes across the various services that render them. For example, an NFT that contains a QR code in its image metadata will be rendered on marketplaces like opensea as well as block explorers like etherscan. The data for the NFT is stored on the blockchain and rendered at runtime when needed.

Copilot AI and others added 9 commits March 5, 2026 12:12
Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
1. Pre-computed lookup tables as bytes constant
   - GF256_EXP / GF256_LOG (256 bytes each) stored in bytecode;
     replaces 8-iteration Russian-peasant GF(256) multiply with
     3 table lookups in _gf256Mul.
   - ALPHA_MAP (59 bytes) replaces 11-branch if-else chain in
     _alphanumericCharIndex with 2 range checks + 1 lookup.
   - _ECPB_* / _NECB_* ECC tables (4 x 41 bytes each) stored as
     library constants; eliminates heap allocation per call.

2. uint256 row packing for the internal module grid
   - Grid kept as uint256[] rows (bit x of rows[y] = module(x,y))
     throughout _buildQrCode; converted to packed bytes only by
     _gridToBytes at the end.
   - _fillRect: 1 OR per row instead of width*height bit sets.
   - _applyMask: per-row 256-bit XOR computed analytically from
     9 precomputed 256-bit constants (_MASK0_EVEN/ODD, _MP0-2,
     _MPA/B/6/C/D) — O(1) per row for all 8 masks.
   - _drawCodewords: no y*size multiplication per module access.

3. Yul inline assembly for hot paths
   - _reedSolomonComputeRemainder: full inner loop in assembly with
     GF(256) multiply inlined via memory-resident log/exp tables;
     eliminates bounds checks and function-call overhead.
   - _getPenaltyScore N2/N4: vectorised 256-bit row operations +
     inline popcnt64/popcnt256 reduce O(n^2) module reads to O(n)
     word operations.

All 22 tests pass with byte-exact output matching the C reference.

Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
Replace the pre-optimisation gas estimates with freshly measured values
after the GF256-table / uint256-grid / Yul-assembly rewrite:

Fixed mask:  v1 byte ~930 k (was ~2.24 M), v5 ~3.3–3.6 M (was ~8.9 M)
Auto mask:   v1 ~5.3 M, v3 ~11 M, v5 ~15.7 M (all now below 30 M limit)
SVG render:  ~2.36 M for version-1 with 4-module border

Also adds a MASK_AUTO table, SVG row, and corrected scaling guide.

Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
…tors

The Solidity port and EVM-specific work belongs to trifle-labs contributors,
not the original author. The MIT license still references Project Nayuki for
the derived algorithmic portions, satisfying the MIT requirement to retain the
original copyright notice in derivatives.

Updated in: QRCode.sol, QRCodeDemo.sol, Readme.markdown

Co-authored-by: okwme <964052+okwme@users.noreply.github.com>
…ation

Add Solidity QR Code generator implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants