CryptoNight 算法白皮书

图方便。

CRYPTONOTE STANDARD 008 Seigen
Category: Main Track Max Jameson
Tuomo Nieminen
Neocortex
Antonio M. Juarez
CryptoNote
March 2013

CryptoNight Hash Function

Abstract

This document is part of the CryptoNote Standards describing a peer-
to-peer anonymous payment system. It defines the CryptoNote's default
proof-of-work hash function, CryptoNight.

Copyright and License Notice

Copyright (c) 2013 CryptoNote. This document is available under the
Creative Commons Attribution 3.0 License (international). To view a
copy of the license visit http://creativecommons.org/licenses/by/3.0/

Table of Contents

  1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . 2
  2. Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . 2
  3. Scratchpad Initialization . . . . . . . . . . . . . . . . . . . 2
  4. Memory-Hard Loop . . . . . . . . . . . . . . . . . . . . . . . 4
  5. Result Calculation . . . . . . . . . . . . . . . . . . . . . . 6
  6. References . . . . . . . . . . . . . . . . . . . . . . . . . . 8

1. Introduction

CryptoNight is a memory-hard hash function. It is designed to be
inefficiently computable on GPU, FPGA and ASIC architectures. The
CryptoNight algorithm's first step is initializing large scratchpad
with pseudo-random data. The next step is numerous read/write
operations at pseudo-random addresses contained in the scratchpad.
The final step is hashing the entire scratchpad to produce the
resulting value.

2. Definitions

hash function: an efficiently computable function which maps data of
arbitrary size to data of fixed size and behaves similarly to a
random function

scratchpad: a large area of memory used to store intermediate values
during the evaluation of a memory-hard function

3. Scratchpad Initialization

First, the input is hashed using Keccak [KECCAK] with parameters b =
1600 and c = 512. The bytes 0..31 of the Keccak final state are
interpreted as an AES-256 key [AES] and expanded to 10 round keys. A
scratchpad of 2097152 bytes (2 MiB) is allocated. The bytes 64..191
are extracted from the Keccak final state and split into 8 blocks of
16 bytes each. Each block is encrypted using the following procedure:

  for i = 0..9 do:
      block = aes_round(block, round_keys[i])

Where aes_round function performs a round of AES encryption, which
means that SubBytes, ShiftRows and MixColumns steps are performed on
the block, and the result is XORed with the round key. Note that
unlike in the AES encryption algorithm, the first and the last rounds
are not special. The resulting blocks are written into the first 128
bytes of the scratchpad. Then, these blocks are encrypted again in
the same way, and the result is written into the second 128 bytes of
the scratchpad. Each time 128 bytes are written, they represent the
result of the encryption of the previously written 128 bytes. The
process is repeated until the scratchpad is fully initialized.

This diagram illustrates scratchpad initialization:

                           +-----+
                           |Input|
                           +-----+
                              |
                              V
                         +--------+
                         | Keccak |
                         +--------+
                              |
                              V

+-------------------------------------------------------------+
| Final state |
+-------------+--------------+---------------+----------------+
| Bytes 0..31 | Bytes 32..63 | Bytes 64..191 | Bytes 192..199 |
+-------------+--------------+---------------+----------------+
| |
V |
+-------------+ V
| Round key 0 |------------+---+->+-----+
+-------------+ | | | |
| . | | | | |
| . | | | | AES |
| . | | | | |
+-------------+ | | | |
| Round key 9 |----------+-|-+-|->+-----+ +---+
+-------------+ | | | | | | |
| | | | +------------------->| |
| | | | | | |
| | | | V | |
| | | +->+-----+ | |
| | | | | | S |
| | | | | | |
| | | | AES | | c |
| | | | | | |
| | | | | | r |
| | +--->+-----+ | |
| | | | a |
| | +------------------->| |
| | . | t |
| | . | |
| | . | c |
| | +------------------->| |
| | | | h |
| | V | |
| +----->+-----+ | p |
| | | | |
| | | | a |
| | AES | | |
| | | | d |
| | | | |
+------->+-----+ | |
| | |
+------------------->| |
| |
+---+

          Figure 3: Scratchpad initialization diagram

4. Memory-Hard Loop

Prior to the main loop, bytes 0..31 and 32..63 of the Keccak state
are XORed, and the resulting 32 bytes are used to initialize
variables a and b, 16 bytes each. These variables are used in the
main loop. The main loop is iterated 524,288 times. When a 16-byte
value needs to be converted into an address in the scratchpad, it is
interpreted as a little-endian integer, and the 21 low-order bits are
used as a byte index. However, the 4 low-order bits of the index are
cleared to ensure the 16-byte alignment. The data is read from and
written to the scratchpad in 16-byte blocks. Each iteration can be
expressed with the following pseudo-code:

  scratchpad_address = to_scratchpad_address(a)
  scratchpad[scratchpad_address] = aes_round(scratchpad 
    [scratchpad_address], a)
  b, scratchpad[scratchpad_address] = scratchpad[scratchpad_address],
    b xor scratchpad[scratchpad_address]
  scratchpad_address = to_scratchpad_address(b)
  a = 8byte_add(a, 8byte_mul(b, scratchpad[scratchpad_address]))
  a, scratchpad[scratchpad_address] = a xor 
    scratchpad[scratchpad_address], a

Where, the 8byte_add function represents each of the arguments as a
pair of 64-bit little-endian values and adds them together,
component-wise, modulo 2^64. The result is converted back into 16
bytes.

The 8byte_mul function, however, uses only the first 8 bytes of each
argument, which are interpreted as unsigned 64-bit little-endian
integers and multiplied together. The result is converted into 16
bytes, and finally the two 8-byte halves of the result are swapped.

This diagram illustrates the memory-hard loop:

+-------------------------------------------------------------+
| Final state |
+-------------+--------------+---------------+----------------+
| Bytes 0..31 | Bytes 32..63 | Bytes 64..191 | Bytes 192..199 |
+-------------+--------------+---------------+----------------+
| |
| +-----+ |
+-->| XOR |<--+
+-----+
| |
+----+ +----+
| |
V V
+---+ +---+
| a | | b |
+---+ +---+
| |
--------------------- REPEAT 524288 TIMES ---------------------
| | address +---+
+-------------|----------------------------------->| |
| +-----+ | read | |
+-->| AES |<--|------------------------------------| |
| +-----+ V | |
| | +-----+ | S |
| +-->| XOR | | |
| | +-----+ write | c |
| | | +------------------------------>| |
| | +----+ address | r |
| +------------------------------------------>| |
| | +-----------+ read | a |
| +->| 8byte_mul |<--+------------------------| |
| | +-----------+ | | t |
| | | | | |
| | V | | c |
| | +-----------+ | | |
+------|->| 8byte_add | | | h |
| +-----------+ | | |
| | | write | p |
| +---------|----------------------->| |
| | | | a |
| V | | |
| +-----+ | | d |
| | XOR |<-----+ | |
| +-----+ | |
+------+ | | |
+-------------|-+ | |
| | +---+
-------------------------- END REPEAT ------------------------
| |

               Figure 4: Memory-hard loop diagram

5. Result Calculation

After the memory-hard part, bytes 32..63 from the Keccak state are
expanded into 10 AES round keys in the same manner as in the first
part.

Bytes 64..191 are extracted from the Keccak state and XORed with the
first 128 bytes of the scratchpad. Then the result is encrypted in
the same manner as in the first part, but using the new keys. The
result is XORed with the second 128 bytes from the scratchpad,
encrypted again, and so on.

After XORing with the last 128 bytes of the scratchpad, the result is
encrypted the last time, and then the bytes 64..191 in the Keccak
state are replaced with the result. Then, the Keccak state is passed
through Keccak-f (the Keccak permutation) with b = 1600.

Then, the 2 low-order bits of the first byte of the state are used to
select a hash function: 0=BLAKE-256 [BLAKE], 1=Groestl-256 [GROESTL],
2=JH-256 [JH], and 3=Skein-256 [SKEIN]. The chosen hash function is
then applied to the Keccak state, and the resulting hash is the
output of CryptoNight.

The diagram below illustrates the result calculation:

+-------------------------------------------------------------+
| Final state |
+-------------+--------------+---------------+----------------+
| Bytes 0..31 | Bytes 32..63 | Bytes 64..191 | Bytes 192..199 |
+-------------+--------------+---------------+----------------+
| | | |
| +--------+ | |
| V | | |
|+-------------+ | | |
|| Round key 0 |-|---+---+ | |
|+-------------+ | | | | |
|| . | | | | | |
|| . | | | | | |
|| . | | | | | |
|+-------------+ | | | | |
+---+ || Round key 9 |-|-+-|-+ | V |
| | |+-------------+ | | | | | +-----+ |
| |-|----------------|-|-|-|-|->| XOR | |
| | | | | | | | +-----+ |
| S | | | | | | | | |
| | | | | | | | V |
| c | | | | | | +->+-----+ |
| | | | | | | | | |
| r | | | | | | | | |
| | | | | | | | AES | |
| a | | | | | | | | |
| | | | | | | | | |
| t | | | | | +--->+-----+ |
| | | | | | | |
| c | | | | | V |
| | | | | | +-----+ |
| h |-|----------------|-|-|----->| XOR | |
| | | | | | +-----+ |
| p | | | | | | |
| | | | | | . |
| a | | | | | . |
| | | | | | . |
| d | | | | | | |
| | | | | | V |
| | | | | | +-----+ |
| |-|----------------|-|-|----->| XOR | |
| | | | | | +-----+ |
+---+ | | | | | |
| | | | V |
| | | +----->+-----+ |
| | | | | |
| | | | | |
| | | | AES | |
| | | | | |
| | | | | |
| | +------->+-----+ |
| | | |
V V V V
+-------------+--------------+---------------+----------------+
| Bytes 0..31 | Bytes 32..63 | Bytes 64..191 | Bytes 192..199 |
+-------------+--------------+---------------+----------------+
| Modified state |
+-------------------------------------------------------------+
|
V
+----------+
| Keccak-f |
+----------+
| |
+-----------+ |
| |
V V
+-------------+ +-------------+
| Select hash |->| Chosen hash |
+-------------+ +-------------+
|
V
+--------------+
| Final result |
+--------------+

              Figure 5: Result calculation diagram

Hash examples:

  Empty string:
  eb14e8a833fac6fe9a43b57b336789c46ffe93f2868452240720607b14387e11.

  "This is a test":
  a084f01d1437a09c6985401b60d43554ae105802c5f5d8a9b3253649c0be6605.
  1. References

    [AES] "Announcing the ADVANCED ENCRYPTION STANDARD", FIPS 197, 2001.

    [BLAKE] Aumasson, J.-P., Henzen, L., Meier, W., and R. C.-W. Phan,
    "SHA-3 proposal BLAKE", 2010.

    [GROESTL] Gauravaram, P., Knudsen, L., Matusiewicz, K., Mendel, F.,
    Rechberger, C., Schlaffer, M., and S. Thomsen, "Groestl - a SHA-3
    candidate", 2011.

    [JH] Wu, H., "The Hash Function JH", 2011.

    [KECCAK] Bertoni, G., Daemen, J., Peeters, M., and G. Van Assche,
    "The Keccak reference", 2011.

    [SKEIN] Ferguson, N., Lucks, S., Schneier, B., Whiting, D., Bellare,
    M., Kohno, T., Callas, J., and J. Walker, "The Skein Hash Function
    Family", 2008.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容