Puzzles · 5 min read ·

Cracking the Alpaca Crypto Coding Challenge

How one cryptic string on a waitlist page turned into a five-step tour through Base64, hex, financial history, HTTP headers, and prime numbers.

By Paden Roder

Editorial illustration of four linked stages on a navy grid: a folded cipher ribbon, nested hexagons, a cube with a newspaper fold, and a framed jewel.

Back in September 2021, I got an email from Alpaca inviting me to join the waitlist for their crypto beta. At the bottom of the invite page sat a small coding challenge offering a shoutout to anyone who could crack it using their Alpaca API credentials. All it gave were two lines of pseudocode:

python
s = decode('MzEzMTMwMmQ2NDYxNzk3MzJkNjE2Njc0NjU3MjJkNmM2NTY4NmQ2MTZlMmQ2MzZmNmM2YzYxNzA3MzY1NjQyZTZhNzA2Nwo=')
try('https://api.alpaca.markets/v2/puzzle/202108/hint/{s}')

What looked like a quick decoding warm-up turned into a five-stage API treasure hunt spanning nested encodings, the 2008 financial crisis, a hidden HTTP header, Bitcoin’s Genesis block, and a prime-number scan inspired by Google’s famous 2004 billboard puzzle. I documented my original solve in a Jupyter notebook in my public AlpacaPuzzle repository on GitHub; here is the full story of how the pieces clicked together, plus a few neat math and code details that stand out even more in hindsight.

Five-step puzzle pipeline showing Base64 decoding to hex, hex decoding to 110-days-after-lehman-collapsed.jpg, the GET hint request returning the X-Alpaca-Puzzle header, Bitcoin’s Genesis block hash, and the prime scan ending in POST solve.
The full five-stage chain from the waitlist string to Alpaca's verification endpoint.

Peeling two layers of encoding

The trailing = and mixed-case alphanumeric alphabet immediately pointed to Base64. Running Python’s base64.b64decode() on the string produced a 70-character result (plus a trailing newline, the classic fingerprint of someone piping echo into base64):

text
3131302d646179732d61667465722d6c65686d616e2d636f6c6c61707365642e6a7067

That looked suspiciously encoded, though I tried passing it straight to the hint endpoint on my paper-trading API just to be sure. Alpaca bounced it right back:

json
{"code": 40010001, "message": "given image does not exist - try again."}

Looking closer at the 70-character string gave the second layer away. Every single character was a digit 0 through 9 or a letter a through f, and paired bytes sat neatly in the printable ASCII range (0x2d for -, 0x61 through 0x7a for lowercase letters). Decoding from hexadecimal with bytes.fromhex() peeled off the second layer:

python
import base64

raw = "MzEzMTMwMmQ2NDYxNzk3MzJkNjE2Njc0NjU3MjJkNmM2NTY4NmQ2MTZlMmQ2MzZmNmM2YzYxNzA3MzY1NjQyZTZhNzA2Nwo="
hex_layer = base64.b64decode(raw).decode("ascii").strip()
filename = bytes.fromhex(hex_layer).decode("ascii")
# "110-days-after-lehman-collapsed.jpg"

110 days after Lehman Brothers

Even before hitting the API again, the filename itself was a clue. Lehman Brothers filed for bankruptcy on September 15, 2008. Count 110 days forward and you land on January 3, 2009, the exact date Satoshi Nakamoto mined the first block of the Bitcoin blockchain.

Sending that decoded filename to the hint endpoint worked:

text
GET /v2/puzzle/202108/hint/110-days-after-lehman-collapsed.jpg

The JSON response returned a signed S3 link to a photograph of the front page of The Times No. 69523 from Saturday, January 3, 2009, carrying the headline “Chancellor on brink of second bailout for banks.”

At first glance that looked like a dead end: just an image URL and no instructions. Checking the HTTP response headers uncovered the next clue tucked inside a custom X-Alpaca-Puzzle header:

text
X-Alpaca-Puzzle: h = this.GetHash(); x = {last 10-digit prime found in consecutive digits of h in base-10}; requests.post('https://api.alpaca.markets/v2/puzzle/202108/solve', json={'answer': account.id.int * x, 'email': your_waitlist_email})

Following this.GetHash() to block zero

Figuring out what this.GetHash() meant took the longest of any step in the challenge. Searching for The Times No. 69523 turned up a 2021 article noting that original print copies of that issue had become collector’s items because Satoshi embedded its front-page headline inside the coinbase data of Bitcoin’s Genesis block, both as proof that the block was not mined before January 3, 2009 and as a pointed comment on bank bailouts.

That led straight to the Bitcoin Wiki entry for the Genesis block, which quotes the block dump from early Bitcoin C++ source code verbatim:

text
GetHash()      = 0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f
hashMerkleRoot = 0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b

There it was: this.GetHash() was literally the method call on Bitcoin’s CBlock struct, and h was the 256-bit hash of block zero (complete with ten leading hex zeros, or 40 zero bits, two hex zeros tighter than Bitcoin’s initial proof-of-work target even required).

Scanning 56 windows for a 10-digit prime

The next piece of the header, {last 10-digit prime found in consecutive digits of h in base-10}, is a playful nod to Google’s 2004 Silicon Valley billboard puzzle ({first 10-digit prime found in consecutive digits of e}.com).

Converting the 64-character hex hash to base 10 yields a 65-digit integer:

text
10628944869218562084050143519444549580389464591454674019345556079

A 10-digit sliding window across 65 digits produces 65 - 10 + 1 = 56 candidates. Before running any code, the Prime Number Theorem gives a quick sanity check on what to expect: near 10^10, roughly 1 / ln(10^10) (about 4.3%) of integers are prime, so 56 consecutive windows should turn up around 2.4 primes on average.

Testing every window with trial division up to its square root finds exactly two: 4050143519 starting at index 19, and 9345556079 starting at index 55, sitting right on the final ten digits of the entire hash.

Two-panel chart showing the 65-digit base-10 Bitcoin Genesis block hash with windows 19 and 55 highlighted, above a log-scale bar chart of the smallest prime factor across all 56 windows.
Every 10-digit sliding window of the Genesis hash plotted by its smallest prime factor. Forty-three windows are divisible by 2, 3, or 5; only windows 19 and 55 are prime.

Looking back at the data in hindsight, it is easy to see why even a straightforward trial-division loop finishes in milliseconds:

  • 77% of the candidates (43 of 56) fail immediately on divisors 2, 3, or 5 (29 are even, 6 fail on 3, and 8 fail on 5).
  • Only 11 composites need a larger odd factor. The toughest near-miss is window #3 (2894486921 = 27,803 × 104,107), which still takes only about 14,000 odd divisions to rule out.
  • One subtle edge case worth catching in a general solver: six windows (at indices 1, 17, 20, 22, 37, and 53) begin with 0, so calling int() turns them into 9-digit numbers. All six are composite, so skipping leading zeros does not change the result here, though checking window[0] != "0" makes the solver strictly faithful to “10-digit prime.”
python
from math import isqrt

genesis_hex = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
digits = str(int(genesis_hex, 16))


def is_prime(n: int) -> bool:
    if n < 2 or n % 2 == 0:
        return n == 2
    return all(n % d != 0 for d in range(3, isqrt(n) + 1, 2))


primes = [
    int(w)
    for i in range(len(digits) - 9)
    if (w := digits[i : i + 10])[0] != "0" and is_prime(int(w))
]
# [4050143519, 9345556079]

Turning a UUID into the final answer

The last detail in X-Alpaca-Puzzle was account.id.int * x. Calling GET /v2/account returns your Alpaca account ID as a standard hyphenated UUID string. Python’s standard library converts that 128-bit UUID straight into a base-10 integer (in my 2021 notebook, I did the same thing by hand by stripping the hyphens and parsing the remaining hex digits in base 16):

python
import uuid

account_int = uuid.UUID(account_id).int
answer = account_int * 9345556079
# POST {"answer": answer, "email": your_waitlist_email}
# to https://api.alpaca.markets/v2/puzzle/202108/solve

Posting that product to Alpaca’s solve endpoint returned their verification confirmation, wrapping up the hunt. You can browse the original 2021 workbook and raw API screenshots in the AlpacaPuzzle repository on GitHub.