argon2_kernel.cl 23.1 KB
Newer Older
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* C compatibility For dumb IDEs: */
#ifndef __OPENCL_VERSION__
#ifndef __cplusplus
typedef int bool;
#endif
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef size_t uintptr_t;
typedef ptrdiff_t intptr_t;
#ifndef __kernel
#define __kernel
#endif
#ifndef __global
#define __global
#endif
#ifndef __private
#define __private
#endif
#ifndef __local
#define __local
#endif
#ifndef __constant
#define __constant const
#endif
#endif /* __OPENCL_VERSION__ */

31 32 33
#define ARGON2_D  0
#define ARGON2_I  1
#define ARGON2_ID 2
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#define ARGON2_VERSION_10 0x10
#define ARGON2_VERSION_13 0x13

#define ARGON2_BLOCK_SIZE 1024
#define ARGON2_QWORDS_IN_BLOCK (ARGON2_BLOCK_SIZE / 8)
#define ARGON2_SYNC_POINTS 4

#define THREADS_PER_LANE 32
#define QWORDS_PER_THREAD (ARGON2_QWORDS_IN_BLOCK / 32)

#ifndef ARGON2_VERSION
#define ARGON2_VERSION ARGON2_VERSION_13
#endif

#ifndef ARGON2_TYPE
#define ARGON2_TYPE ARGON2_I
#endif

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
ulong u64_build(uint hi, uint lo)
{
    return upsample(hi, lo);
}

uint u64_lo(ulong x)
{
    return (uint)x;
}

uint u64_hi(ulong x)
{
    return (uint)(x >> 32);
}

struct u64_shuffle_buf {
    uint lo[THREADS_PER_LANE];
    uint hi[THREADS_PER_LANE];
};
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
ulong u64_shuffle(ulong v, uint thread_src, uint thread,
                  __local struct u64_shuffle_buf *buf)
{
    uint lo = u64_lo(v);
    uint hi = u64_hi(v);

    buf->lo[thread] = lo;
    buf->hi[thread] = hi;

    barrier(CLK_LOCAL_MEM_FENCE);

    lo = buf->lo[thread_src];
    hi = buf->hi[thread_src];

    return u64_build(hi, lo);
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
89 90 91 92 93

struct block_g {
    ulong data[ARGON2_QWORDS_IN_BLOCK];
};

94 95
struct block_th {
    ulong a, b, c, d;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
96 97
};

98
ulong cmpeq_mask(uint test, uint ref)
99
{
100
    uint x = -(uint)(test == ref);
101 102 103
    return u64_build(x, x);
}

104
ulong block_th_get(const struct block_th *b, uint idx)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
105
{
106
    ulong res = 0;
107 108 109 110
    res ^= cmpeq_mask(idx, 0) & b->a;
    res ^= cmpeq_mask(idx, 1) & b->b;
    res ^= cmpeq_mask(idx, 2) & b->c;
    res ^= cmpeq_mask(idx, 3) & b->d;
111 112
    return res;
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
113

114 115
void block_th_set(struct block_th *b, uint idx, ulong v)
{
116 117 118 119
    b->a ^= cmpeq_mask(idx, 0) & (v ^ b->a);
    b->b ^= cmpeq_mask(idx, 1) & (v ^ b->b);
    b->c ^= cmpeq_mask(idx, 2) & (v ^ b->c);
    b->d ^= cmpeq_mask(idx, 3) & (v ^ b->d);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
}

void move_block(struct block_th *dst, const struct block_th *src)
{
    *dst = *src;
}

void xor_block(struct block_th *dst, const struct block_th *src)
{
    dst->a ^= src->a;
    dst->b ^= src->b;
    dst->c ^= src->c;
    dst->d ^= src->d;
}

void load_block(struct block_th *dst, __global const struct block_g *src,
                uint thread)
{
    dst->a = src->data[0 * THREADS_PER_LANE + thread];
    dst->b = src->data[1 * THREADS_PER_LANE + thread];
    dst->c = src->data[2 * THREADS_PER_LANE + thread];
    dst->d = src->data[3 * THREADS_PER_LANE + thread];
}

void load_block_xor(struct block_th *dst, __global const struct block_g *src,
                    uint thread)
{
    dst->a ^= src->data[0 * THREADS_PER_LANE + thread];
    dst->b ^= src->data[1 * THREADS_PER_LANE + thread];
    dst->c ^= src->data[2 * THREADS_PER_LANE + thread];
    dst->d ^= src->data[3 * THREADS_PER_LANE + thread];
}

void store_block(__global struct block_g *dst, const struct block_th *src,
                 uint thread)
{
    dst->data[0 * THREADS_PER_LANE + thread] = src->a;
    dst->data[1 * THREADS_PER_LANE + thread] = src->b;
    dst->data[2 * THREADS_PER_LANE + thread] = src->c;
    dst->data[3 * THREADS_PER_LANE + thread] = src->d;
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
#ifdef cl_amd_media_ops
#pragma OPENCL EXTENSION cl_amd_media_ops : enable

ulong rotr64(ulong x, ulong n)
{
    uint lo = u64_lo(x);
    uint hi = u64_hi(x);
    uint r_lo, r_hi;
    if (n < 32) {
        r_lo = amd_bitalign(hi, lo, (uint)n);
        r_hi = amd_bitalign(lo, hi, (uint)n);
    } else {
        r_lo = amd_bitalign(lo, hi, (uint)n - 32);
        r_hi = amd_bitalign(hi, lo, (uint)n - 32);
    }
    return u64_build(r_hi, r_lo);
}
#else
180 181 182 183
ulong rotr64(ulong x, ulong n)
{
    return rotate(x, 64 - n);
}
184
#endif
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
185

186 187 188 189 190 191 192 193 194
ulong f(ulong x, ulong y)
{
    uint xlo = u64_lo(x);
    uint ylo = u64_lo(y);
    return x + y + 2 * u64_build(mul_hi(xlo, ylo), xlo * ylo);
}

void g(struct block_th *block)
{
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
195
    ulong a, b, c, d;
196 197 198 199
    a = block->a;
    b = block->b;
    c = block->c;
    d = block->d;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
200

201
    a = f(a, b);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
202
    d = rotr64(d ^ a, 32);
203
    c = f(c, d);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
204
    b = rotr64(b ^ c, 24);
205
    a = f(a, b);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
206
    d = rotr64(d ^ a, 16);
207
    c = f(c, d);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
208 209
    b = rotr64(b ^ c, 63);

210 211 212 213
    block->a = a;
    block->b = b;
    block->c = c;
    block->d = d;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
214 215
}

216
uint apply_shuffle_shift1(uint thread, uint idx)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
217
{
218 219
    return (thread & 0x1c) | ((thread + idx) & 0x3);
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
220

221 222 223
uint apply_shuffle_unshift1(uint thread, uint idx)
{
    idx = (QWORDS_PER_THREAD - idx) % QWORDS_PER_THREAD;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
224

225 226
    return apply_shuffle_shift1(thread, idx);
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
227

228 229 230 231 232 233
uint apply_shuffle_shift2(uint thread, uint idx)
{
    uint lo = (thread & 0x1) | ((thread & 0x10) >> 3);
    lo = (lo + idx) & 0x3;
    return ((lo & 0x2) << 3) | (thread & 0xe) | (lo & 0x1);
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
234

235 236 237
uint apply_shuffle_unshift2(uint thread, uint idx)
{
    idx = (QWORDS_PER_THREAD - idx) % QWORDS_PER_THREAD;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
238

239 240
    return apply_shuffle_shift2(thread, idx);
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
241

242 243 244 245 246
void shuffle_shift1(struct block_th *block, uint thread,
                    __local struct u64_shuffle_buf *buf)
{
    for (uint i = 0; i < QWORDS_PER_THREAD; i++) {
        uint src_thr = apply_shuffle_shift1(thread, i);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
247

248 249 250 251
        ulong v = block_th_get(block, i);
        v = u64_shuffle(v, src_thr, thread, buf);
        block_th_set(block, i, v);
    }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
252 253
}

254 255
void shuffle_unshift1(struct block_th *block, uint thread,
                      __local struct u64_shuffle_buf *buf)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
256 257
{
    for (uint i = 0; i < QWORDS_PER_THREAD; i++) {
258
        uint src_thr = apply_shuffle_unshift1(thread, i);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
259

260 261 262 263 264
        ulong v = block_th_get(block, i);
        v = u64_shuffle(v, src_thr, thread, buf);
        block_th_set(block, i, v);
    }
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
265

266 267 268 269 270
void shuffle_shift2(struct block_th *block, uint thread,
                    __local struct u64_shuffle_buf *buf)
{
    for (uint i = 0; i < QWORDS_PER_THREAD; i++) {
        uint src_thr = apply_shuffle_shift2(thread, i);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
271

272 273 274 275 276
        ulong v = block_th_get(block, i);
        v = u64_shuffle(v, src_thr, thread, buf);
        block_th_set(block, i, v);
    }
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
277

278 279 280
void shuffle_unshift2(struct block_th *block, uint thread,
                      __local struct u64_shuffle_buf *buf)
{
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
281
    for (uint i = 0; i < QWORDS_PER_THREAD; i++) {
282 283 284 285 286
        uint src_thr = apply_shuffle_unshift2(thread, i);

        ulong v = block_th_get(block, i);
        v = u64_shuffle(v, src_thr, thread, buf);
        block_th_set(block, i, v);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
287 288 289
    }
}

290 291
void transpose(struct block_th *block, uint thread,
               __local struct u64_shuffle_buf *buf)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
292
{
293
    uint thread_group = (thread & 0x0C) >> 2;
294
    for (uint i = 1; i < QWORDS_PER_THREAD; i++) {
295 296
        uint thr = (i << 2) ^ thread;
        uint idx = thread_group ^ i;
297

298
        ulong v = block_th_get(block, idx);
299
        v = u64_shuffle(v, thr, thread, buf);
300
        block_th_set(block, idx, v);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
301
    }
302
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
303

304 305 306 307
void shuffle_block(struct block_th *block, uint thread,
                   __local struct u64_shuffle_buf *buf)
{
    transpose(block, thread, buf);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
308

309
    g(block);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
310

311
    shuffle_shift1(block, thread, buf);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
312

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    g(block);

    shuffle_unshift1(block, thread, buf);
    transpose(block, thread, buf);

    g(block);

    shuffle_shift2(block, thread, buf);

    g(block);

    shuffle_unshift2(block, thread, buf);
}

void compute_ref_pos(uint lanes, uint segment_blocks,
                     uint pass, uint lane, uint slice, uint offset,
                     uint *ref_lane, uint *ref_index)
{
    uint lane_blocks = ARGON2_SYNC_POINTS * segment_blocks;

    *ref_lane = *ref_lane % lanes;

    uint base;
    if (pass != 0) {
        base = lane_blocks - segment_blocks;
    } else {
        if (slice == 0) {
            *ref_lane = lane;
        }
        base = slice * segment_blocks;
    }

    uint ref_area_size = base + offset - 1;
    if (*ref_lane != lane) {
        ref_area_size = min(ref_area_size, base);
    }

    *ref_index = mul_hi(*ref_index, *ref_index);
    *ref_index = ref_area_size - 1 - mul_hi(ref_area_size, *ref_index);

    if (pass != 0 && slice != ARGON2_SYNC_POINTS - 1) {
        *ref_index += (slice + 1) * segment_blocks;
        if (*ref_index >= lane_blocks) {
            *ref_index -= lane_blocks;
        }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
358 359
    }
}
360 361 362 363

void argon2_core(
        __global struct block_g *memory, __global struct block_g *mem_curr,
        struct block_th *prev, struct block_th *tmp,
364
        __local struct u64_shuffle_buf *shuffle_buf, uint lanes,
365 366 367
        uint thread, uint pass, uint ref_index, uint ref_lane)
{
    __global struct block_g *mem_ref;
368
    mem_ref = memory + ref_index * lanes + ref_lane;
369 370 371 372 373 374 375 376 377 378 379 380 381

#if ARGON2_VERSION == ARGON2_VERSION_10
    load_block_xor(prev, mem_ref, thread);
    move_block(tmp, prev);
#else
    if (pass != 0) {
        load_block(tmp, mem_curr, thread);
        load_block_xor(prev, mem_ref, thread);
        xor_block(tmp, prev);
    } else {
        load_block_xor(prev, mem_ref, thread);
        move_block(tmp, prev);
    }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
382 383
#endif

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    shuffle_block(prev, thread, shuffle_buf);

    xor_block(prev, tmp);

    store_block(mem_curr, prev, thread);
}

void next_addresses(struct block_th *addr, struct block_th *tmp,
                    uint thread_input, uint thread,
                    __local struct u64_shuffle_buf *buf)
{
    addr->a = u64_build(0, thread_input);
    addr->b = 0;
    addr->c = 0;
    addr->d = 0;

    shuffle_block(addr, thread, buf);

    addr->a ^= u64_build(0, thread_input);
    move_block(tmp, addr);

    shuffle_block(addr, thread, buf);

    xor_block(addr, tmp);
}

410
#if ARGON2_TYPE == ARGON2_I || ARGON2_TYPE == ARGON2_ID
411 412 413 414 415 416 417 418 419 420 421 422
struct ref {
    uint ref_lane;
    uint ref_index;
};

/*
 * Refs hierarchy:
 * lanes -> passes -> slices -> blocks
 */
__kernel void argon2_precompute_kernel(
        __local struct u64_shuffle_buf *shuffle_bufs, __global struct ref *refs,
        uint passes, uint lanes, uint segment_blocks)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
423
{
424 425 426 427 428 429 430 431 432 433 434
    uint block_id = get_global_id(0) / THREADS_PER_LANE;
    uint warp = get_local_id(0) / THREADS_PER_LANE;
    uint thread = get_local_id(0) % THREADS_PER_LANE;

    __local struct u64_shuffle_buf *shuffle_buf = &shuffle_bufs[warp];

    uint segment_addr_blocks = (segment_blocks + ARGON2_QWORDS_IN_BLOCK - 1)
            / ARGON2_QWORDS_IN_BLOCK;
    uint block = block_id % segment_addr_blocks;
    uint segment = block_id / segment_addr_blocks;

435
    uint slice, pass, lane;
436
#if ARGON2_TYPE == ARGON2_ID
437 438 439
    slice = segment % (ARGON2_SYNC_POINTS / 2);
    lane = segment / (ARGON2_SYNC_POINTS / 2);
    pass = 0;
440
#else
441
    uint pass_id;
442

443 444 445 446 447
    slice = segment % ARGON2_SYNC_POINTS;
    pass_id = segment / ARGON2_SYNC_POINTS;

    pass = pass_id % passes;
    lane = pass_id / passes;
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
#endif

    struct block_th addr, tmp;

    uint thread_input;
    switch (thread) {
    case 0:
        thread_input = pass;
        break;
    case 1:
        thread_input = lane;
        break;
    case 2:
        thread_input = slice;
        break;
    case 3:
        thread_input = lanes * segment_blocks * ARGON2_SYNC_POINTS;
        break;
    case 4:
        thread_input = passes;
        break;
    case 5:
        thread_input = ARGON2_TYPE;
        break;
    case 6:
        thread_input = block + 1;
        break;
    default:
        thread_input = 0;
        break;
    }

    next_addresses(&addr, &tmp, thread_input, thread, shuffle_buf);

    refs += segment * segment_blocks;

    for (uint i = 0; i < QWORDS_PER_THREAD; i++) {
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
485
        uint pos = i * THREADS_PER_LANE + thread;
486 487 488 489 490 491 492 493 494 495 496 497
        uint offset = block * ARGON2_QWORDS_IN_BLOCK + pos;
        if (offset < segment_blocks) {
            ulong v = block_th_get(&addr, i);
            uint ref_index = u64_lo(v);
            uint ref_lane  = u64_hi(v);

            compute_ref_pos(lanes, segment_blocks, pass, lane, slice, offset,
                            &ref_lane, &ref_index);

            refs[offset].ref_index = ref_index;
            refs[offset].ref_lane  = ref_lane;
        }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
498
    }
499
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
500

501 502 503 504 505
void argon2_step_precompute(
        __global struct block_g *memory, __global struct block_g *mem_curr,
        struct block_th *prev, struct block_th *tmp,
        __local struct u64_shuffle_buf *shuffle_buf,
        __global const struct ref **refs,
506 507
        uint lanes, uint segment_blocks, uint thread,
        uint lane, uint pass, uint slice, uint offset)
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
{
    uint ref_index, ref_lane;
    bool data_independent;
#if ARGON2_TYPE == ARGON2_I
    data_independent = true;
#elif ARGON2_TYPE == ARGON2_ID
    data_independent = pass == 0 && slice < ARGON2_SYNC_POINTS / 2;
#else
    data_independent = false;
#endif
    if (data_independent) {
        ref_index = (*refs)->ref_index;
        ref_lane = (*refs)->ref_lane;
        (*refs)++;
    } else {
        ulong v = u64_shuffle(prev->a, 0, thread, shuffle_buf);
        ref_index = u64_lo(v);
        ref_lane  = u64_hi(v);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
526

527 528 529
        compute_ref_pos(lanes, segment_blocks, pass, lane, slice, offset,
                        &ref_lane, &ref_index);
    }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
530

531 532
    argon2_core(memory, mem_curr, prev, tmp, shuffle_buf, lanes, thread, pass,
                ref_index, ref_lane);
533
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
534

535 536 537 538 539 540 541 542
__kernel void argon2_kernel_segment_precompute(
        __local struct u64_shuffle_buf *shuffle_bufs,
        __global struct block_g *memory, __global const struct ref *refs,
        uint passes, uint lanes, uint segment_blocks,
        uint pass, uint slice)
{
    uint job_id = get_global_id(1);
    uint lane   = get_global_id(0) / THREADS_PER_LANE;
543 544
    uint warp   = (get_local_id(1) * get_local_size(0) + get_local_id(0))
            / THREADS_PER_LANE;
545 546 547 548 549 550 551
    uint thread = get_local_id(0) % THREADS_PER_LANE;

    __local struct u64_shuffle_buf *shuffle_buf = &shuffle_bufs[warp];

    uint lane_blocks = ARGON2_SYNC_POINTS * segment_blocks;

    /* select job's memory region: */
552
    memory += (size_t)job_id * lanes * lane_blocks;
553 554 555 556

    struct block_th prev, tmp;

    __global struct block_g *mem_segment =
557
            memory + slice * segment_blocks * lanes + lane;
558 559 560 561
    __global struct block_g *mem_prev, *mem_curr;
    uint start_offset = 0;
    if (pass == 0) {
        if (slice == 0) {
562 563
            mem_prev = mem_segment + 1 * lanes;
            mem_curr = mem_segment + 2 * lanes;
564 565
            start_offset = 2;
        } else {
566
            mem_prev = mem_segment - lanes;
567 568 569
            mem_curr = mem_segment;
        }
    } else {
570
        mem_prev = mem_segment + (slice == 0 ? lane_blocks * lanes : 0) - lanes;
571
        mem_curr = mem_segment;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
572 573
    }

574
    load_block(&prev, mem_prev, thread);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
575

576 577 578 579 580 581 582 583 584
#if ARGON2_TYPE == ARGON2_ID
        if (pass == 0 && slice < ARGON2_SYNC_POINTS / 2) {
            refs += lane * (lane_blocks / 2) + slice * segment_blocks;
            refs += start_offset;
        }
#else
        refs += (lane * passes + pass) * lane_blocks + slice * segment_blocks;
        refs += start_offset;
#endif
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
585

586 587 588
    for (uint offset = start_offset; offset < segment_blocks; ++offset) {
        argon2_step_precompute(
                    memory, mem_curr, &prev, &tmp, shuffle_buf, &refs, lanes,
589
                    segment_blocks, thread, lane, pass, slice, offset);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
590

591
        mem_curr += lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
592
    }
593
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
594

595 596 597 598 599 600 601
__kernel void argon2_kernel_oneshot_precompute(
        __local struct u64_shuffle_buf *shuffle_bufs,
        __global struct block_g *memory, __global const struct ref *refs,
        uint passes, uint lanes, uint segment_blocks)
{
    uint job_id = get_global_id(1);
    uint lane   = get_global_id(0) / THREADS_PER_LANE;
602
    uint warp   = get_local_id(1) * lanes + get_local_id(0) / THREADS_PER_LANE;
603 604 605 606 607 608 609
    uint thread = get_local_id(0) % THREADS_PER_LANE;

    __local struct u64_shuffle_buf *shuffle_buf = &shuffle_bufs[warp];

    uint lane_blocks = ARGON2_SYNC_POINTS * segment_blocks;

    /* select job's memory region: */
610
    memory += (size_t)job_id * lanes * lane_blocks;
611 612 613

    struct block_th prev, tmp;

614 615 616
    __global struct block_g *mem_lane = memory + lane;
    __global struct block_g *mem_prev = mem_lane + 1 * lanes;
    __global struct block_g *mem_curr = mem_lane + 2 * lanes;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

    load_block(&prev, mem_prev, thread);

#if ARGON2_TYPE == ARGON2_ID
    refs += lane * (lane_blocks / 2) + 2;
#else
    refs += lane * passes * lane_blocks + 2;
#endif

    uint skip = 2;
    for (uint pass = 0; pass < passes; ++pass) {
        for (uint slice = 0; slice < ARGON2_SYNC_POINTS; ++slice) {
            for (uint offset = 0; offset < segment_blocks; ++offset) {
                if (skip > 0) {
                    --skip;
                    continue;
                }

                argon2_step_precompute(
                            memory, mem_curr, &prev, &tmp, shuffle_buf, &refs,
637 638
                            lanes, segment_blocks, thread,
                            lane, pass, slice, offset);
639

640
                mem_curr += lanes;
641 642
            }

643
            barrier(CLK_GLOBAL_MEM_FENCE);
644 645 646 647
        }

        mem_curr = mem_lane;
    }
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
648
}
649 650 651 652 653 654
#endif /* ARGON2_TYPE == ARGON2_I || ARGON2_TYPE == ARGON2_ID */

void argon2_step(
        __global struct block_g *memory, __global struct block_g *mem_curr,
        struct block_th *prev, struct block_th *tmp, struct block_th *addr,
        __local struct u64_shuffle_buf *shuffle_buf,
655
        uint lanes, uint segment_blocks, uint thread, uint *thread_input,
656 657 658 659 660 661 662 663 664 665
        uint lane, uint pass, uint slice, uint offset)
{
    uint ref_index, ref_lane;
    bool data_independent;
#if ARGON2_TYPE == ARGON2_I
    data_independent = true;
#elif ARGON2_TYPE == ARGON2_ID
    data_independent = pass == 0 && slice < ARGON2_SYNC_POINTS / 2;
#else
    data_independent = false;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
666
#endif
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
    if (data_independent) {
        uint addr_index = offset % ARGON2_QWORDS_IN_BLOCK;
        if (addr_index == 0) {
            if (thread == 6) {
                ++*thread_input;
            }
            next_addresses(addr, tmp, *thread_input, thread, shuffle_buf);
        }

        uint thr = addr_index % THREADS_PER_LANE;
        uint idx = addr_index / THREADS_PER_LANE;

        ulong v = block_th_get(addr, idx);
        v = u64_shuffle(v, thr, thread, shuffle_buf);
        ref_index = u64_lo(v);
        ref_lane  = u64_hi(v);
    } else {
        ulong v = u64_shuffle(prev->a, 0, thread, shuffle_buf);
        ref_index = u64_lo(v);
        ref_lane  = u64_hi(v);
    }

    compute_ref_pos(lanes, segment_blocks, pass, lane, slice, offset,
                    &ref_lane, &ref_index);

692 693
    argon2_core(memory, mem_curr, prev, tmp, shuffle_buf, lanes, thread, pass,
                ref_index, ref_lane);
694
}
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
695 696

__kernel void argon2_kernel_segment(
697
        __local struct u64_shuffle_buf *shuffle_bufs,
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
698 699 700
        __global struct block_g *memory, uint passes, uint lanes,
        uint segment_blocks, uint pass, uint slice)
{
701 702
    uint job_id = get_global_id(1);
    uint lane   = get_global_id(0) / THREADS_PER_LANE;
703 704
    uint warp   = (get_local_id(1) * get_local_size(0) + get_local_id(0))
            / THREADS_PER_LANE;
705 706 707
    uint thread = get_local_id(0) % THREADS_PER_LANE;

    __local struct u64_shuffle_buf *shuffle_buf = &shuffle_bufs[warp];
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
708 709 710 711

    uint lane_blocks = ARGON2_SYNC_POINTS * segment_blocks;

    /* select job's memory region: */
712
    memory += (size_t)job_id * lanes * lane_blocks;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
713

714
    struct block_th prev, addr, tmp;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
715
    uint thread_input;
716

717
#if ARGON2_TYPE == ARGON2_I || ARGON2_TYPE == ARGON2_ID
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    switch (thread) {
    case 0:
        thread_input = pass;
        break;
    case 1:
        thread_input = lane;
        break;
    case 2:
        thread_input = slice;
        break;
    case 3:
        thread_input = lanes * lane_blocks;
        break;
    case 4:
        thread_input = passes;
        break;
    case 5:
735
        thread_input = ARGON2_TYPE;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
736 737 738 739 740 741 742 743 744 745
        break;
    default:
        thread_input = 0;
        break;
    }

    if (pass == 0 && slice == 0 && segment_blocks > 2) {
        if (thread == 6) {
            ++thread_input;
        }
746
        next_addresses(&addr, &tmp, thread_input, thread, shuffle_buf);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
747 748 749
    }
#endif

750
    __global struct block_g *mem_segment =
751
            memory + slice * segment_blocks * lanes + lane;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
752 753 754 755
    __global struct block_g *mem_prev, *mem_curr;
    uint start_offset = 0;
    if (pass == 0) {
        if (slice == 0) {
756 757
            mem_prev = mem_segment + 1 * lanes;
            mem_curr = mem_segment + 2 * lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
758 759
            start_offset = 2;
        } else {
760
            mem_prev = mem_segment - lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
761 762 763
            mem_curr = mem_segment;
        }
    } else {
764
        mem_prev = mem_segment + (slice == 0 ? lane_blocks * lanes : 0) - lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
765 766 767
        mem_curr = mem_segment;
    }

768
    load_block(&prev, mem_prev, thread);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
769 770

    for (uint offset = start_offset; offset < segment_blocks; ++offset) {
771
        argon2_step(memory, mem_curr, &prev, &tmp, &addr, shuffle_buf,
772
                    lanes, segment_blocks, thread, &thread_input,
773
                    lane, pass, slice, offset);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
774

775
        mem_curr += lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
776 777 778 779
    }
}

__kernel void argon2_kernel_oneshot(
780 781 782
        __local struct u64_shuffle_buf *shuffle_bufs,
        __global struct block_g *memory, uint passes, uint lanes,
        uint segment_blocks)
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
783
{
784 785
    uint job_id = get_global_id(1);
    uint lane   = get_global_id(0) / THREADS_PER_LANE;
786
    uint warp   = get_local_id(1) * lanes + get_local_id(0) / THREADS_PER_LANE;
787 788 789
    uint thread = get_local_id(0) % THREADS_PER_LANE;

    __local struct u64_shuffle_buf *shuffle_buf = &shuffle_bufs[warp];
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
790 791 792 793

    uint lane_blocks = ARGON2_SYNC_POINTS * segment_blocks;

    /* select job's memory region: */
794
    memory += (size_t)job_id * lanes * lane_blocks;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
795

796
    struct block_th prev, addr, tmp;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
797
    uint thread_input;
798 799

#if ARGON2_TYPE == ARGON2_I || ARGON2_TYPE == ARGON2_ID
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
800 801 802 803 804 805 806 807 808 809 810
    switch (thread) {
    case 1:
        thread_input = lane;
        break;
    case 3:
        thread_input = lanes * lane_blocks;
        break;
    case 4:
        thread_input = passes;
        break;
    case 5:
811
        thread_input = ARGON2_TYPE;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
812 813 814 815 816 817 818 819 820 821
        break;
    default:
        thread_input = 0;
        break;
    }

    if (segment_blocks > 2) {
        if (thread == 6) {
            ++thread_input;
        }
822
        next_addresses(&addr, &tmp, thread_input, thread, shuffle_buf);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
823 824 825
    }
#endif

826 827 828
    __global struct block_g *mem_lane = memory + lane;
    __global struct block_g *mem_prev = mem_lane + 1 * lanes;
    __global struct block_g *mem_curr = mem_lane + 2 * lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
829

830 831
    load_block(&prev, mem_prev, thread);

Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
832 833 834 835 836 837 838 839 840
    uint skip = 2;
    for (uint pass = 0; pass < passes; ++pass) {
        for (uint slice = 0; slice < ARGON2_SYNC_POINTS; ++slice) {
            for (uint offset = 0; offset < segment_blocks; ++offset) {
                if (skip > 0) {
                    --skip;
                    continue;
                }

841
                argon2_step(memory, mem_curr, &prev, &tmp, &addr, shuffle_buf,
842
                            lanes, segment_blocks, thread, &thread_input,
843
                            lane, pass, slice, offset);
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
844

845
                mem_curr += lanes;
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
846 847
            }

848
            barrier(CLK_GLOBAL_MEM_FENCE);
849

850
#if ARGON2_TYPE == ARGON2_I || ARGON2_TYPE == ARGON2_ID
Ondrej Mosnáček's avatar
Ondrej Mosnáček committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
            if (thread == 2) {
                ++thread_input;
            }
            if (thread == 6) {
                thread_input = 0;
            }
#endif
        }
#if ARGON2_TYPE == ARGON2_I
        if (thread == 0) {
            ++thread_input;
        }
        if (thread == 2) {
            thread_input = 0;
        }
#endif
        mem_curr = mem_lane;
    }
}