From Bitcoin Wiki
Jump to: navigation, search

In cryptography, scrypt (pronounced "ess crypt") is a password-based key derivation function created by Colin Percival, originally for the Tarsnap online backup service. The algorithm was specifically designed to make it costly to perform large-scale custom hardware attacks by requiring large amounts of memory. In 2016, the scrypt algorithm was published by IETF as RFC 7914. A simplified version of scrypt is used as a proof-of-work scheme by a number of cryptocurrencies, first implemented by an anonymous programmer called ArtForz in Tenebrix and followed by Fairbrix and Litecoin soon after.


A password-based key derivation function (password-based KDF) is generally designed to be computationally intensive, so that it takes a relatively long time to compute (say on the order of several hundred milliseconds). Legitimate users only need to perform the function once per operation (e.g., authentication), and so the time required is negligible. However, a brute-force attack would likely need to perform the operation billions of times; at which point the time requirements become significant and, ideally, prohibitive.

Previous password-based KDFs (such as the popular PBKDF2 from RSA Laboratories) have relatively low resource demands, meaning they do not require elaborate hardware or very much memory to perform. They are therefore easily and cheaply implemented in hardware (for instance on an ASIC or even an FPGA). This allows an attacker with sufficient resources to launch a large-scale parallel attack by building hundreds or even thousands of implementations of the algorithm in hardware and having each search a different subset of the key space. This divides the amount of time needed to complete a brute-force attack by the number of implementations available, very possibly bringing it down to a reasonable time frame.

The scrypt function is designed to hinder such attempts by raising the resource demands of the algorithm. Specifically, the algorithm is designed to use a large amount of memory compared to other password-based KDFs, making the size and the cost of a hardware implementation much more expensive, and therefore limiting the amount of parallelism an attacker can use, for a given amount of financial resources.


The large memory requirements of scrypt come from a large vector of pseudorandom bit strings that are generated as part of the algorithm. Once the vector is generated, the elements of it are accessed in a pseudo-random order and combined to produce the derived key. A straightforward implementation would need to keep the entire vector in RAM so that it can be accessed as needed.

Because the elements of the vector are generated algorithmically, each element could be generated on the fly as needed, only storing one element in memory at a time and therefore cutting the memory requirements significantly. However, the generation of each element is intended to be computationally expensive, and the elements are expected to be accessed many times throughout the execution of the function. Thus there is a significant trade-off in speed in order to get rid of the large memory requirements.

This sort of time–memory trade-off often exists in computer algorithms: you can increase speed at the cost of using more memory, or decrease memory requirements at the cost of performing more operations and taking longer. The idea behind scrypt is to deliberately make this trade-off costly in either direction. Thus an attacker could use an implementation that doesn't require many resources (and can therefore be massively parallelized with limited expense) but runs very slowly, or use an implementation that runs more quickly but has very large memory requirements and is therefore more expensive to parallelize.


The algorithm includes the following parameters:

  • Passphrase - The string of characters to be hashed.
  • Salt - A string of characters that modifies the hash to protect against Rainbow table attacks
  • N - CPU/memory cost parameter.
  • p - Parallelization parameter; a positive integer satisfying p ≤ (2<sup>32</sup>− 1) * hLen / MFLen.
  • dkLen - Intended output length in octets of the derived key; a positive integer satisfying dkLen ≤ (2<sup>32</sup>− 1) * hLen.
  • r - The blocksize parameter, which fine-tunes sequential memory read size and performance. 8 is commonly used.
  • hLen - The length in octets of the hash function (32 for SHA256).
  • MFlen - The length in octets of the output of the mixing function (SMix below). Defined as r * 128 in RFC7914.
<span style="color:blue;">Function</span> scrypt
<span style="color:blue;">Inputs:</span>
Passphrase: Bytes <span style="color:green;">string of characters to be hashed</span>
Salt: Bytes <span style="color:green;">random salt</span>
CostFactor (N): Integer <span style="color:green;">CPU/memory cost parameter</span>
BlockSizeFactor (r): Integer <span style="color:green;">blocksize parameter (8 is commonly used)</span>
ParallelizationFactor (p): Integer <span style="color:green;">Parallelization parameter. (1..2<sup>32</sup>-1 * hLen/MFlen)</span>
DesiredKeyLen: Integer <span style="color:green;">Desired key length in bytes</span>
<span style="color:blue;">Output:</span>
DerivedKey: Bytes <span style="color:green;">array of bytes, DesiredKeyLen long</span>

<span style="color:green;">Step 1. Generate expensive salt</span>
blockSize ← 128*BlockSizeFactor <span style="color:green;">//Length (in bytes) of the SMix mixing function output (e.g. 128*8 = 1024 bytes)</span>

<span style="color:green;">Use PBKDF2 to generate initial 128*BlockSizeFactor*p bytes of data (e.g. 128*8*3 = 3072 bytes)</span>
<span style="color:green;">Treat the result as an array of p elements, each entry being blocksize bytes (e.g. 3 elements, each 1024 bytes)</span>
[B<sub>0</sub>...B<sub>p−1</sub>] ← PBKDF2<sub>HMAC-SHA256</sub>(Passphrase, Salt, 1, blockSize*ParallelizationFactor)

<span style="color:green;">Mix each block in B 2<sup>CostFactor</sup> times using ROMix function (each block can be mixed in parallel)</span>
<span style="color:blue;">for</span> i ← 0 <span style="color:blue;">to</span> p-1 <span style="color:blue;">do</span>
B<sub>i</sub> ← ROMix(B<sub>i</sub>, 2<sup>CostFactor</sup>)

<span style="color:green;">All the elements of B is our new "expensive" salt</span>
expensiveSalt ← B<sub>0</sub>∥B<sub>1</sub>∥B<sub>2</sub>∥ ... ∥B<sub>p-1</sub> <span style="color:green;">//where ∥ is concatenation</span>

<span style="color:green;">Step 2. Use PBKDF2 to generate the desired number of bytes, but using the expensive salt we just generated</span>
<span style="color:blue;">return</span> PBKDF2<sub>HMAC-SHA256</sub>(Passphrase, expensiveSalt, 1, DesiredKeyLen);
Function ROMix(Block, Iterations)

<span style="color:green;">Create Iterations copies of X</span>
X ← Block
for i ← 0 to Iterations−1 do
V<sub>i</sub> ← X
X ← BlockMix(X)

for i ← 0 to Iterations−1 do
<span style="color:green;">//Convert first 8-bytes of the last 64-byte block of X to a UInt64, assuming little endian (Intel) format</span>
j ← Integerify(X) mod N 
X ← BlockMix(X xor V<sub>j</sub>)

return X

Where Integerify is a bijective function from {0, 1}<sup>k</sup> to {0,...,2<sup>k</sup>− 1}.

Function BlockMix(B):

<span style="color:green;">The block B is r 128-byte chunks (which is equivalent of 2r 64-byte chunks)</span>
r ← Length(B) / 128;

<span style="color:green;">Treat B as an array of 2r 64-byte chucks</span>
[B<sub>0</sub>...B<sub>2r-1</sub>] ← B

X ← B<sub>2r−1</sub>
for i ← 0 to 2r−1 do
X ← Salsa20/8(X xor B<sub>i</sub>) <span style="color:green;">//Salsa20/8 hashes from 64-bytes to 64-bytes</span>
Y<sub>i</sub> ← X

return ← Y<sub>0</sub>∥Y<sub>2</sub>∥...∥Y<sub>2r−2</sub> ∥ Y<sub>1</sub>∥Y<sub>3</sub>∥...∥Y<sub>2r−1</sub>

Where Salsa20/8 is the 8-round version of Salsa20.

Cryptocurrency uses[edit]

Scrypt is used in many cryptocurrencies as a proof-of-work algorithm. It was first implemented for Tenebrix (released in September 2011) and served as the basis for Litecoin and Dogecoin, which also adopted its scrypt algorithm. Mining of cryptocurrencies that use scrypt is often performed on graphics processing units (GPUs) since GPUs tend to have significantly more processing power compared to the CPU. This led to shortages of high end GPUs due to the rising price of these currencies in the months of November and December 2013.

As of May 2014, specialized ASIC mining hardware is available for scrypt-based cryptocurrencies. As of 2016, InnoSilicon claims to have 14 nm technology with an efficiency of 1.5 watts/megahash-second.

Password Hashing Competition[edit]

In 2013 a Password Hashing Competition was held to develop an improved key derivation function.

See also[edit]