/* Copyright 2009 Colin Percival, 2011 ArtForz, 2011-2013 pooler,
 * 2013 David G. Andersen
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file was originally written by Colin Percival as part of the Tarsnap
 * online backup system.
 */

#include <inttypes.h>
#include <string.h>

/* 
 * Reference code from Colin Percival's original version.  (One tweak: for loop
 * changed to be ++ instead of += 2, as that was unnecessary).
 * Used for validation of cuda implementation.
 */

static inline void xor_salsa8_original(uint32_t B[16], const uint32_t Bx[16])
{
	uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
	int i;

	x00 = (B[ 0] ^= Bx[ 0]);
	x01 = (B[ 1] ^= Bx[ 1]);
	x02 = (B[ 2] ^= Bx[ 2]);
	x03 = (B[ 3] ^= Bx[ 3]);
	x04 = (B[ 4] ^= Bx[ 4]);
	x05 = (B[ 5] ^= Bx[ 5]);
	x06 = (B[ 6] ^= Bx[ 6]);
	x07 = (B[ 7] ^= Bx[ 7]);
	x08 = (B[ 8] ^= Bx[ 8]);
	x09 = (B[ 9] ^= Bx[ 9]);
	x10 = (B[10] ^= Bx[10]);
	x11 = (B[11] ^= Bx[11]);
	x12 = (B[12] ^= Bx[12]);
	x13 = (B[13] ^= Bx[13]);
	x14 = (B[14] ^= Bx[14]);
	x15 = (B[15] ^= Bx[15]);
	for (i = 0; i < 4; i++) {
#define R(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
		/* Operate on columns. */
		x04 ^= R(x00+x12, 7);	x09 ^= R(x05+x01, 7);
		x14 ^= R(x10+x06, 7);	x03 ^= R(x15+x11, 7);

		x08 ^= R(x04+x00, 9);	x13 ^= R(x09+x05, 9);
		x02 ^= R(x14+x10, 9);	x07 ^= R(x03+x15, 9);
		
		x12 ^= R(x08+x04,13);	x01 ^= R(x13+x09,13);
		x06 ^= R(x02+x14,13);	x11 ^= R(x07+x03,13);
		
		x00 ^= R(x12+x08,18);	x05 ^= R(x01+x13,18);
		x10 ^= R(x06+x02,18);	x15 ^= R(x11+x07,18);

		/* Operate on rows. */
		x01 ^= R(x00+x03, 7);	x06 ^= R(x05+x04, 7);
		x11 ^= R(x10+x09, 7);	x12 ^= R(x15+x14, 7);
		
		x02 ^= R(x01+x00, 9);	x07 ^= R(x06+x05, 9);
		x08 ^= R(x11+x10, 9);	x13 ^= R(x12+x15, 9);
		
		x03 ^= R(x02+x01,13);	x04 ^= R(x07+x06,13);
		x09 ^= R(x08+x11,13);	x14 ^= R(x13+x12,13);
		
		x00 ^= R(x03+x02,18);	x05 ^= R(x04+x07,18);
		x10 ^= R(x09+x08,18);	x15 ^= R(x14+x13,18);
#undef R
	}
	B[ 0] += x00;
	B[ 1] += x01;
	B[ 2] += x02;
	B[ 3] += x03;
	B[ 4] += x04;
	B[ 5] += x05;
	B[ 6] += x06;
	B[ 7] += x07;
	B[ 8] += x08;
	B[ 9] += x09;
	B[10] += x10;
	B[11] += x11;
	B[12] += x12;
	B[13] += x13;
	B[14] += x14;
	B[15] += x15;
}

/* Modifies X in place to produce scrypt(X) */

void scrypt_core_original(uint32_t *X) 
{
  uint32_t V[32*1024];

  for (int i = 0; i < 1024; i++) {
    memcpy(&V[i*32], X, 128);
    xor_salsa8_original(&X[0], &X[16]);
    xor_salsa8_original(&X[16], &X[0]);
  }
  for (int i = 0; i < 1024; i++) {
    int j = 32 * (X[16] & 1023);
    for (int k = 0; k < 32; k++) 
      X[k] ^= V[j + k];
    xor_salsa8_original(&X[0], &X[16]);
    xor_salsa8_original(&X[16], &X[0]);
  }
}

	.file	"tmpxft_002c0492_00000000-6_percival_scrypt.cudafe1.cpp"
	.text
#APP

#NO_APP
	.type	_ZL19xor_salsa8_originalPjPKj, @function
_ZL19xor_salsa8_originalPjPKj:
.LFB2008:
	.cfi_startproc
	pushq	%r15
	.cfi_def_cfa_offset 16
	.cfi_offset 15, -16
	movq	%rsi, %rdx
	movq	%rdi, %rax
	pushq	%r14
	.cfi_def_cfa_offset 24
	.cfi_offset 14, -24
	pushq	%r13
	.cfi_def_cfa_offset 32
	.cfi_offset 13, -32
	pushq	%r12
	.cfi_def_cfa_offset 40
	.cfi_offset 12, -40
	pushq	%rbp
	.cfi_def_cfa_offset 48
	.cfi_offset 6, -48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	.cfi_offset 3, -56
	movl	(%rdi), %ecx
	xorl	(%rsi), %ecx
	movl	%ecx, (%rdi)
	movl	%ecx, -72(%rsp)
	movl	4(%rdi), %ecx
	xorl	4(%rsi), %ecx
	movl	%ecx, 4(%rdi)
	movl	%ecx, -68(%rsp)
	movl	8(%rdi), %ecx
	xorl	8(%rsi), %ecx
	movl	%ecx, 8(%rdi)
	movl	%ecx, -64(%rsp)
	movl	12(%rdi), %ecx
	xorl	12(%rsi), %ecx
	movl	%ecx, 12(%rdi)
	movl	%ecx, -60(%rsp)
	movl	16(%rdi), %ecx
	xorl	16(%rsi), %ecx
	movl	%ecx, 16(%rdi)
	movl	%ecx, -56(%rsp)
	movl	20(%rdi), %ecx
	xorl	20(%rsi), %ecx
	movl	%ecx, 20(%rdi)
	movl	%ecx, -52(%rsp)
	movl	24(%rdi), %ecx
	xorl	24(%rsi), %ecx
	movl	%ecx, 24(%rdi)
	movl	%ecx, -48(%rsp)
	movl	28(%rdi), %ecx
	xorl	28(%rsi), %ecx
	movl	%ecx, 28(%rdi)
	movl	%ecx, -44(%rsp)
	movl	32(%rdi), %ecx
	xorl	32(%rsi), %ecx
	movl	%ecx, 32(%rdi)
	movl	%ecx, -40(%rsp)
	movl	36(%rdi), %ecx
	xorl	36(%rsi), %ecx
	movl	%ecx, 36(%rdi)
	movl	%ecx, -36(%rsp)
	movl	40(%rdi), %ecx
	xorl	40(%rsi), %ecx
	movl	%ecx, 40(%rdi)
	movl	%ecx, -32(%rsp)
	movl	44(%rdi), %ecx
	xorl	44(%rsi), %ecx
	movl	%ecx, 44(%rdi)
	movl	%ecx, -28(%rsp)
	movl	48(%rdi), %ecx
	xorl	48(%rsi), %ecx
	movl	%ecx, 48(%rdi)
	movl	%ecx, -24(%rsp)
	movl	52(%rdi), %ecx
	xorl	52(%rsi), %ecx
	movl	-56(%rsp), %ebp
	movl	$4, -4(%rsp)
	movl	%ecx, %ebx
	movl	-52(%rsp), %r8d
	movl	-60(%rsp), %r12d
	movl	%ecx, 52(%rdi)
	movl	-64(%rsp), %r9d
	movl	-72(%rsp), %r10d
	movl	%ecx, -20(%rsp)
	movl	56(%rdi), %ecx
	xorl	56(%rsi), %ecx
	movl	%ecx, 56(%rdi)
	movl	%ecx, %esi
	movl	%ecx, -16(%rsp)
	movl	60(%rdi), %ecx
	movl	%esi, %r11d
	movl	%ebx, %esi
	xorl	60(%rdx), %ecx
	movl	-28(%rsp), %ebx
	movl	-32(%rsp), %edi
	movl	-24(%rsp), %edx
	movl	%ecx, -8(%rsp)
	movl	%ecx, -88(%rsp)
	movl	-44(%rsp), %ecx
	movl	%ebx, -100(%rsp)
	movl	-36(%rsp), %ebx
	movl	%ecx, -96(%rsp)
	movl	-48(%rsp), %ecx
	movl	%edi, -92(%rsp)
	movl	-40(%rsp), %edi
	movl	%ecx, -104(%rsp)
	movl	-68(%rsp), %ecx
.L2:
	leal	(%r10,%rdx), %r13d
	movl	-92(%rsp), %r14d
	roll	$7, %r13d
	xorl	%ebp, %r13d
	leal	(%rcx,%r8), %ebp
	roll	$7, %ebp
	movl	%r13d, -84(%rsp)
	xorl	%ebx, %ebp
	movl	-104(%rsp), %ebx
	movl	%ebp, -80(%rsp)
	addl	%r14d, %ebx
	roll	$7, %ebx
	xorl	%r11d, %ebx
	movl	-88(%rsp), %r11d
	movl	%ebx, -76(%rsp)
	movl	-100(%rsp), %ebx
	addl	%ebx, %r11d
	movl	%r13d, %ebx
	addl	%r10d, %ebx
	roll	$7, %r11d
	roll	$9, %ebx
	xorl	%r12d, %r11d
	xorl	%edi, %ebx
	movl	%ebp, %edi
	addl	%r8d, %edi
	movl	%ebx, %r12d
	movl	-96(%rsp), %ebx
	roll	$9, %edi
	xorl	%esi, %edi
	movl	-76(%rsp), %esi
	movl	%edi, -12(%rsp)
	movl	%r14d, %edi
	addl	%esi, %edi
	movl	-88(%rsp), %esi
	roll	$9, %edi
	xorl	%r9d, %edi
	addl	%r11d, %esi
	leal	0(%r13,%r12), %r9d
	roll	$9, %esi
	roll	$13, %r9d
	xorl	%ebx, %esi
	xorl	%edx, %r9d
	movl	-76(%rsp), %ebx
	movl	-12(%rsp), %edx
	addl	%edi, %ebx
	leal	0(%rbp,%rdx), %ebp
	movl	-104(%rsp), %edx
	roll	$13, %ebp
	roll	$13, %ebx
	xorl	%ecx, %ebp
	xorl	%edx, %ebx
	movl	-100(%rsp), %ecx
	leal	(%r11,%rsi), %edx
	roll	$13, %edx
	leal	(%rdi,%rbx), %r14d
	xorl	%ecx, %edx
	leal	(%r12,%r9), %ecx
	rorl	$14, %ecx
	leal	(%rsi,%rdx), %r13d
	xorl	%ecx, %r10d
	movl	-12(%rsp), %ecx
	rorl	$14, %r14d
	rorl	$14, %r13d
	leal	(%rcx,%rbp), %r15d
	movl	-92(%rsp), %ecx
	rorl	$14, %r15d
	xorl	%r8d, %r15d
	movl	-84(%rsp), %r8d
	xorl	%ecx, %r14d
	movl	-88(%rsp), %ecx
	addl	%r15d, %r8d
	xorl	%ecx, %r13d
	leal	(%r11,%r10), %ecx
	roll	$7, %r8d
	roll	$7, %ecx
	xorl	%ebx, %r8d
	movl	-80(%rsp), %ebx
	xorl	%ebp, %ecx
	movl	%r8d, -104(%rsp)
	leal	(%rbx,%r14), %r8d
	movl	-12(%rsp), %ebx
	roll	$7, %r8d
	xorl	%edx, %r8d
	movl	-76(%rsp), %edx
	movl	%r8d, -100(%rsp)
	addl	%r13d, %edx
	roll	$7, %edx
	xorl	%r9d, %edx
	leal	(%r10,%rcx), %r9d
	roll	$9, %r9d
	xorl	%edi, %r9d
	movl	-104(%rsp), %edi
	addl	%r15d, %edi
	roll	$9, %edi
	xorl	%esi, %edi
	leal	0(%r13,%rdx), %esi
	movl	%edi, -96(%rsp)
	leal	(%r14,%r8), %edi
	roll	$9, %esi
	roll	$9, %edi
	xorl	%ebx, %esi
	movl	-104(%rsp), %ebx
	xorl	%r12d, %edi
	leal	(%rcx,%r9), %r12d
	roll	$13, %r12d
	xorl	%r11d, %r12d
	movl	-96(%rsp), %r11d
	leal	(%rbx,%r11), %ebp
	movl	-84(%rsp), %ebx
	movl	-80(%rsp), %r11d
	roll	$13, %ebp
	xorl	%ebx, %ebp
	movl	%r8d, %ebx
	movl	-76(%rsp), %r8d
	addl	%edi, %ebx
	roll	$13, %ebx
	xorl	%r11d, %ebx
	leal	(%rdx,%rsi), %r11d
	roll	$13, %r11d
	xorl	%r8d, %r11d
	leal	(%r9,%r12), %r8d
	rorl	$14, %r8d
	xorl	%r8d, %r10d
	movl	-96(%rsp), %r8d
	addl	%ebp, %r8d
	rorl	$14, %r8d
	xorl	%r15d, %r8d
	leal	(%rdi,%rbx), %r15d
	rorl	$14, %r15d
	xorl	%r14d, %r15d
	leal	(%rsi,%r11), %r14d
	rorl	$14, %r14d
	movl	%r15d, -92(%rsp)
	xorl	%r13d, %r14d
	decl	-4(%rsp)
	movl	%r14d, -88(%rsp)
	jne	.L2
	movl	-72(%rsp), %r15d
	addl	%r10d, %r15d
	movl	-68(%rsp), %r10d
	movl	%r15d, (%rax)
	addl	%ecx, %r10d
	movl	-64(%rsp), %ecx
	movl	%r10d, 4(%rax)
	movl	-104(%rsp), %r10d
	addl	%r9d, %ecx
	movl	-96(%rsp), %r9d
	movl	%ecx, 8(%rax)
	movl	-60(%rsp), %ecx
	addl	%r12d, %ecx
	movl	%ecx, 12(%rax)
	movl	-56(%rsp), %ecx
	addl	%ebp, %ecx
	movl	%ecx, 16(%rax)
	movl	-52(%rsp), %ecx
	addl	%r8d, %ecx
	movl	%ecx, 20(%rax)
	movl	-48(%rsp), %ecx
	addl	%r10d, %ecx
	movl	%ecx, 24(%rax)
	movl	-44(%rsp), %ecx
	addl	%r9d, %ecx
	movl	%ecx, 28(%rax)
	movl	-40(%rsp), %ecx
	addl	%edi, %ecx
	movl	-92(%rsp), %edi
	movl	%ecx, 32(%rax)
	movl	-36(%rsp), %ecx
	addl	%ebx, %ecx
	movl	-24(%rsp), %ebx
	movl	%ecx, 36(%rax)
	movl	-32(%rsp), %ecx
	addl	%edx, %ebx
	movl	-20(%rsp), %edx
	addl	%edi, %ecx
	movl	-100(%rsp), %edi
	movl	%ebx, 48(%rax)
	movl	%ecx, 40(%rax)
	movl	-28(%rsp), %ecx
	addl	%esi, %edx
	movl	%edx, 52(%rax)
	addl	%edi, %ecx
	movl	%ecx, 44(%rax)
	movl	-16(%rsp), %edx
	addl	%r11d, %edx
	movl	%edx, 56(%rax)
	movl	-8(%rsp), %edx
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%rbp
	.cfi_def_cfa_offset 40
	addl	%r14d, %edx
	popq	%r12
	.cfi_def_cfa_offset 32
	popq	%r13
	.cfi_def_cfa_offset 24
	movl	%edx, 60(%rax)
	popq	%r14
	.cfi_def_cfa_offset 16
	popq	%r15
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2008:
	.size	_ZL19xor_salsa8_originalPjPKj, .-_ZL19xor_salsa8_originalPjPKj
	.type	_ZL26__cudaUnregisterBinaryUtilv, @function
_ZL26__cudaUnregisterBinaryUtilv:
.LFB2012:
	.cfi_startproc
	endbr64
	movq	_ZL20__cudaFatCubinHandle(%rip), %rdi
	jmp	__cudaUnregisterFatBinary@PLT
	.cfi_endproc
.LFE2012:
	.size	_ZL26__cudaUnregisterBinaryUtilv, .-_ZL26__cudaUnregisterBinaryUtilv
	.globl	_Z20scrypt_core_originalPj
	.type	_Z20scrypt_core_originalPj, @function
_Z20scrypt_core_originalPj:
.LFB2009:
	.cfi_startproc
	endbr64
	pushq	%r14
	.cfi_def_cfa_offset 16
	.cfi_offset 14, -16
	pushq	%r13
	.cfi_def_cfa_offset 24
	.cfi_offset 13, -24
	pushq	%r12
	.cfi_def_cfa_offset 32
	.cfi_offset 12, -32
	pushq	%rbp
	.cfi_def_cfa_offset 40
	.cfi_offset 6, -40
	pushq	%rbx
	.cfi_def_cfa_offset 48
	.cfi_offset 3, -48
	leaq	-131072(%rsp), %r11
	.cfi_def_cfa 11, 131120
.LPSRL0:
	subq	$4096, %rsp
	orq	$0, (%rsp)
	cmpq	%r11, %rsp
	jne	.LPSRL0
	.cfi_def_cfa_register 7
	subq	$16, %rsp
	.cfi_def_cfa_offset 131136
	movq	%fs:40, %rax
	movq	%rax, 131080(%rsp)
	xorl	%eax, %eax
	leaq	8(%rsp), %r13
	movq	%rdi, %rbx
	leaq	64(%rdi), %rbp
	leaq	131080(%rsp), %r14
	movq	%r13, %r12
.L8:
	movq	%r13, %rdi
	movl	$32, %ecx
	movq	%rbx, %rsi
	subq	$-128, %r13
	rep movsl
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_ZL19xor_salsa8_originalPjPKj
	movq	%rbx, %rsi
	movq	%rbp, %rdi
	call	_ZL19xor_salsa8_originalPjPKj
	cmpq	%r13, %r14
	jne	.L8
	movl	$1024, %r13d
.L10:
	movl	64(%rbx), %eax
	xorl	%edx, %edx
	salq	$7, %rax
	andl	$130944, %eax
	addq	%r12, %rax
.L9:
	movl	(%rax,%rdx), %ecx
	xorl	%ecx, (%rbx,%rdx)
	addq	$4, %rdx
	cmpq	$128, %rdx
	jne	.L9
	movq	%rbp, %rsi
	movq	%rbx, %rdi
	call	_ZL19xor_salsa8_originalPjPKj
	movq	%rbx, %rsi
	movq	%rbp, %rdi
	call	_ZL19xor_salsa8_originalPjPKj
	decl	%r13d
	jne	.L10
	movq	131080(%rsp), %rax
	subq	%fs:40, %rax
	je	.L11
	call	__stack_chk_fail@PLT
.L11:
	addq	$131088, %rsp
	.cfi_def_cfa_offset 48
	popq	%rbx
	.cfi_def_cfa_offset 40
	popq	%rbp
	.cfi_def_cfa_offset 32
	popq	%r12
	.cfi_def_cfa_offset 24
	popq	%r13
	.cfi_def_cfa_offset 16
	popq	%r14
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE2009:
	.size	_Z20scrypt_core_originalPj, .-_Z20scrypt_core_originalPj
	.section	.text.startup,"ax",@progbits
	.type	_ZL24__sti____cudaRegisterAllv, @function
_ZL24__sti____cudaRegisterAllv:
.LFB2035:
	.cfi_startproc
	endbr64
	pushq	%rax
	.cfi_def_cfa_offset 16
	leaq	_ZL15__fatDeviceText(%rip), %rdi
	call	__cudaRegisterFatBinary@PLT
	movq	%rax, %rdi
	movq	%rax, _ZL20__cudaFatCubinHandle(%rip)
	call	__cudaRegisterFatBinaryEnd@PLT
	leaq	_ZL26__cudaUnregisterBinaryUtilv(%rip), %rdi
	popq	%rdx
	.cfi_def_cfa_offset 8
	jmp	atexit@PLT
	.cfi_endproc
.LFE2035:
	.size	_ZL24__sti____cudaRegisterAllv, .-_ZL24__sti____cudaRegisterAllv
	.section	.init_array,"aw"
	.align 8
	.quad	_ZL24__sti____cudaRegisterAllv
	.section	.nvFatBinSegment,"aw"
	.align 8
	.type	_ZL15__fatDeviceText, @object
	.size	_ZL15__fatDeviceText, 24
_ZL15__fatDeviceText:
	.long	1180844977
	.long	1
	.quad	fatbinData
	.quad	0
	.local	_ZL20__cudaFatCubinHandle
	.comm	_ZL20__cudaFatCubinHandle,8,8
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

/* Copyright 2009 Colin Percival, 2011 ArtForz, 2011-2013 pooler,
 * 2013 David G. Andersen
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file was originally written by Colin Percival as part of the Tarsnap
 * online backup system.
 */


#include <hip/hip_runtime.h>
#include <inttypes.h>
#include <string.h>

/* 
 * Reference code from Colin Percival's original version.  (One tweak: for loop
 * changed to be ++ instead of += 2, as that was unnecessary).
 * Used for validation of cuda implementation.
 */

static inline void xor_salsa8_original(uint32_t B[16], const uint32_t Bx[16])
{
	uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
	int i;

	x00 = (B[ 0] ^= Bx[ 0]);
	x01 = (B[ 1] ^= Bx[ 1]);
	x02 = (B[ 2] ^= Bx[ 2]);
	x03 = (B[ 3] ^= Bx[ 3]);
	x04 = (B[ 4] ^= Bx[ 4]);
	x05 = (B[ 5] ^= Bx[ 5]);
	x06 = (B[ 6] ^= Bx[ 6]);
	x07 = (B[ 7] ^= Bx[ 7]);
	x08 = (B[ 8] ^= Bx[ 8]);
	x09 = (B[ 9] ^= Bx[ 9]);
	x10 = (B[10] ^= Bx[10]);
	x11 = (B[11] ^= Bx[11]);
	x12 = (B[12] ^= Bx[12]);
	x13 = (B[13] ^= Bx[13]);
	x14 = (B[14] ^= Bx[14]);
	x15 = (B[15] ^= Bx[15]);
	for (i = 0; i < 4; i++) {
#define R(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
		/* Operate on columns. */
		x04 ^= R(x00+x12, 7);	x09 ^= R(x05+x01, 7);
		x14 ^= R(x10+x06, 7);	x03 ^= R(x15+x11, 7);

		x08 ^= R(x04+x00, 9);	x13 ^= R(x09+x05, 9);
		x02 ^= R(x14+x10, 9);	x07 ^= R(x03+x15, 9);
		
		x12 ^= R(x08+x04,13);	x01 ^= R(x13+x09,13);
		x06 ^= R(x02+x14,13);	x11 ^= R(x07+x03,13);
		
		x00 ^= R(x12+x08,18);	x05 ^= R(x01+x13,18);
		x10 ^= R(x06+x02,18);	x15 ^= R(x11+x07,18);

		/* Operate on rows. */
		x01 ^= R(x00+x03, 7);	x06 ^= R(x05+x04, 7);
		x11 ^= R(x10+x09, 7);	x12 ^= R(x15+x14, 7);
		
		x02 ^= R(x01+x00, 9);	x07 ^= R(x06+x05, 9);
		x08 ^= R(x11+x10, 9);	x13 ^= R(x12+x15, 9);
		
		x03 ^= R(x02+x01,13);	x04 ^= R(x07+x06,13);
		x09 ^= R(x08+x11,13);	x14 ^= R(x13+x12,13);
		
		x00 ^= R(x03+x02,18);	x05 ^= R(x04+x07,18);
		x10 ^= R(x09+x08,18);	x15 ^= R(x14+x13,18);
#undef R
	}
	B[ 0] += x00;
	B[ 1] += x01;
	B[ 2] += x02;
	B[ 3] += x03;
	B[ 4] += x04;
	B[ 5] += x05;
	B[ 6] += x06;
	B[ 7] += x07;
	B[ 8] += x08;
	B[ 9] += x09;
	B[10] += x10;
	B[11] += x11;
	B[12] += x12;
	B[13] += x13;
	B[14] += x14;
	B[15] += x15;
}

/* Modifies X in place to produce scrypt(X) */

void scrypt_core_original(uint32_t *X) 
{
  uint32_t V[32*1024];

  for (int i = 0; i < 1024; i++) {
    memcpy(&V[i*32], X, 128);
    xor_salsa8_original(&X[0], &X[16]);
    xor_salsa8_original(&X[16], &X[0]);
  }
  for (int i = 0; i < 1024; i++) {
    int j = 32 * (X[16] & 1023);
    for (int k = 0; k < 32; k++) 
      X[k] ^= V[j + k];
    xor_salsa8_original(&X[0], &X[16]);
    xor_salsa8_original(&X[16], &X[0]);
  }
}

	.text
	.file	"percival_scrypt.hip"
	.globl	_Z20scrypt_core_originalPj      # -- Begin function _Z20scrypt_core_originalPj
	.type	_Z20scrypt_core_originalPj,@function
_Z20scrypt_core_originalPj:             # @_Z20scrypt_core_originalPj
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	subq	$131080, %rsp                   # imm = 0x20008
	.cfi_def_cfa_offset 131120
	.cfi_offset %rbx, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movq	%rdi, %rbx
	leaq	64(%rdi), %r14
	xorl	%r15d, %r15d
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
	leaq	(%rsp,%r15), %rdi
	movl	$32, %ecx
	movq	%rbx, %rsi
	rep;movsl (%rsi), %es:(%rdi)
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_ZL19xor_salsa8_originalPjPKj
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	_ZL19xor_salsa8_originalPjPKj
	subq	$-128, %r15
	cmpq	$131072, %r15                   # imm = 0x20000
	jne	.LBB0_1
# %bb.2:                                # %.preheader.preheader
	xorl	%r15d, %r15d
	movl	$1023, %ebp                     # imm = 0x3FF
.LBB0_3:                                # %.preheader
                                        # =>This Loop Header: Depth=1
                                        #     Child Loop BB0_4 Depth 2
	movl	(%r14), %eax
	andl	%ebp, %eax
	shll	$7, %eax
	addq	%rsp, %rax
	xorl	%ecx, %ecx
.LBB0_4:                                #   Parent Loop BB0_3 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
	movl	(%rax,%rcx,4), %edx
	xorl	%edx, (%rbx,%rcx,4)
	incq	%rcx
	cmpq	$32, %rcx
	jne	.LBB0_4
# %bb.5:                                #   in Loop: Header=BB0_3 Depth=1
	movq	%rbx, %rdi
	movq	%r14, %rsi
	callq	_ZL19xor_salsa8_originalPjPKj
	movq	%r14, %rdi
	movq	%rbx, %rsi
	callq	_ZL19xor_salsa8_originalPjPKj
	incl	%r15d
	cmpl	$1024, %r15d                    # imm = 0x400
	jne	.LBB0_3
# %bb.6:
	addq	$131080, %rsp                   # imm = 0x20008
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end0:
	.size	_Z20scrypt_core_originalPj, .Lfunc_end0-_Z20scrypt_core_originalPj
	.cfi_endproc
                                        # -- End function
	.type	_ZL19xor_salsa8_originalPjPKj,@function # -- Begin function _ZL19xor_salsa8_originalPjPKj
_ZL19xor_salsa8_originalPjPKj:          # @_ZL19xor_salsa8_originalPjPKj
	.cfi_startproc
# %bb.0:
	pushq	%rbp
	.cfi_def_cfa_offset 16
	pushq	%r15
	.cfi_def_cfa_offset 24
	pushq	%r14
	.cfi_def_cfa_offset 32
	pushq	%r13
	.cfi_def_cfa_offset 40
	pushq	%r12
	.cfi_def_cfa_offset 48
	pushq	%rbx
	.cfi_def_cfa_offset 56
	subq	$24, %rsp
	.cfi_def_cfa_offset 80
	.cfi_offset %rbx, -56
	.cfi_offset %r12, -48
	.cfi_offset %r13, -40
	.cfi_offset %r14, -32
	.cfi_offset %r15, -24
	.cfi_offset %rbp, -16
	movl	(%rdi), %eax
	movl	4(%rdi), %ecx
	xorl	(%rsi), %eax
	movl	%eax, -112(%rsp)                # 4-byte Spill
	movl	%eax, (%rdi)
	xorl	4(%rsi), %ecx
	movl	%ecx, -108(%rsp)                # 4-byte Spill
	movl	%ecx, 4(%rdi)
	movl	8(%rdi), %eax
	xorl	8(%rsi), %eax
	movl	%eax, -116(%rsp)                # 4-byte Spill
	movl	%eax, 8(%rdi)
	movl	12(%rdi), %r15d
	xorl	12(%rsi), %r15d
	movl	%r15d, 12(%rdi)
	movl	16(%rdi), %r10d
	xorl	16(%rsi), %r10d
	movl	%r10d, 16(%rdi)
	movl	20(%rdi), %r12d
	xorl	20(%rsi), %r12d
	movl	%r12d, 20(%rdi)
	movl	24(%rdi), %r8d
	xorl	24(%rsi), %r8d
	movl	%r8d, 24(%rdi)
	movl	28(%rdi), %ebx
	xorl	28(%rsi), %ebx
	movl	%ebx, 28(%rdi)
	movl	32(%rdi), %edx
	xorl	32(%rsi), %edx
	movl	%edx, 32(%rdi)
	movl	36(%rdi), %r13d
	xorl	36(%rsi), %r13d
	movl	%r13d, 36(%rdi)
	movl	40(%rdi), %ebp
	xorl	40(%rsi), %ebp
	movl	%ebp, 40(%rdi)
	movl	44(%rdi), %ecx
	xorl	44(%rsi), %ecx
	movl	%ecx, 44(%rdi)
	movl	48(%rdi), %r9d
	xorl	48(%rsi), %r9d
	movl	%r9d, 48(%rdi)
	movl	52(%rdi), %r14d
	xorl	52(%rsi), %r14d
	movl	%r14d, 52(%rdi)
	movl	56(%rdi), %eax
	xorl	56(%rsi), %eax
	movl	%eax, 56(%rdi)
	movq	%rdi, -16(%rsp)                 # 8-byte Spill
	movl	60(%rdi), %edi
	xorl	60(%rsi), %edi
	movl	$4, -20(%rsp)                   # 4-byte Folded Spill
	movl	%edi, -72(%rsp)                 # 4-byte Spill
	movl	%edi, %r11d
	movl	%eax, -68(%rsp)                 # 4-byte Spill
                                        # kill: def $eax killed $eax def $rax
	movl	%r14d, -64(%rsp)                # 4-byte Spill
                                        # kill: def $r14d killed $r14d def $r14
	movl	%r9d, -60(%rsp)                 # 4-byte Spill
                                        # kill: def $r9d killed $r9d def $r9
	movl	%ecx, -56(%rsp)                 # 4-byte Spill
                                        # kill: def $ecx killed $ecx def $rcx
	movq	%rcx, -104(%rsp)                # 8-byte Spill
	movl	%ebp, -52(%rsp)                 # 4-byte Spill
                                        # kill: def $ebp killed $ebp def $rbp
	movl	%r13d, -48(%rsp)                # 4-byte Spill
                                        # kill: def $r13d killed $r13d def $r13
	movl	%edx, -44(%rsp)                 # 4-byte Spill
	movl	%edx, %esi
	movl	%ebx, -40(%rsp)                 # 4-byte Spill
                                        # kill: def $ebx killed $ebx def $rbx
	movl	%r8d, -36(%rsp)                 # 4-byte Spill
                                        # kill: def $r8d killed $r8d def $r8
	movl	%r12d, -32(%rsp)                # 4-byte Spill
                                        # kill: def $r12d killed $r12d def $r12
	movl	%r10d, -28(%rsp)                # 4-byte Spill
	movl	%r10d, %edx
	movl	%r15d, -24(%rsp)                # 4-byte Spill
	movl	%r15d, %ecx
	movl	-116(%rsp), %edi                # 4-byte Reload
	movl	%edi, %r15d
	movl	-108(%rsp), %edi                # 4-byte Reload
	movl	%edi, %r10d
	movl	-112(%rsp), %edi                # 4-byte Reload
                                        # kill: def $edi killed $edi def $rdi
.LBB1_1:                                # =>This Inner Loop Header: Depth=1
	movq	%r11, -96(%rsp)                 # 8-byte Spill
	movq	%r9, -88(%rsp)                  # 8-byte Spill
	movq	%r8, %r11
	movq	%r8, -80(%rsp)                  # 8-byte Spill
	addl	%edi, %r9d
	roll	$7, %r9d
	xorl	%edx, %r9d
	leal	(%r12,%r10), %edx
	roll	$7, %edx
	xorl	%r13d, %edx
	movq	%rdx, -128(%rsp)                # 8-byte Spill
	leal	(%r8,%rbp), %edx
	roll	$7, %edx
	xorl	%eax, %edx
	movq	-96(%rsp), %r11                 # 8-byte Reload
	movq	-104(%rsp), %rax                # 8-byte Reload
	addl	%r11d, %eax
	roll	$7, %eax
	xorl	%ecx, %eax
	movq	%rax, %r13
	leal	(%r9,%rdi), %eax
	movq	%r9, -8(%rsp)                   # 8-byte Spill
	roll	$9, %eax
	xorl	%esi, %eax
	movq	%rax, 8(%rsp)                   # 8-byte Spill
	movq	-128(%rsp), %r8                 # 8-byte Reload
	leal	(%r8,%r12), %eax
	roll	$9, %eax
	xorl	%r14d, %eax
	movq	%rax, %r14
	movq	%rdx, (%rsp)                    # 8-byte Spill
	leal	(%rdx,%rbp), %ecx
	roll	$9, %ecx
	xorl	%r15d, %ecx
	leal	(%r11,%r13), %esi
	movq	%r13, %r15
	roll	$9, %esi
	xorl	%ebx, %esi
	movq	8(%rsp), %rax                   # 8-byte Reload
	leal	(%rax,%r9), %ebx
	roll	$13, %ebx
	xorl	-88(%rsp), %ebx                 # 4-byte Folded Reload
	leal	(%r14,%r8), %r9d
	movq	%r14, %r13
	roll	$13, %r9d
	xorl	%r10d, %r9d
	leal	(%rcx,%rdx), %r14d
	roll	$13, %r14d
	xorl	-80(%rsp), %r14d                # 4-byte Folded Reload
	movq	%r15, %rdx
	addl	%esi, %r15d
	roll	$13, %r15d
	xorl	-104(%rsp), %r15d               # 4-byte Folded Reload
	leal	(%rbx,%rax), %r8d
	roll	$18, %r8d
	xorl	%edi, %r8d
	leal	(%r9,%r13), %edi
	roll	$18, %edi
	xorl	%r12d, %edi
	movq	%rdi, %r12
	leal	(%r14,%rcx), %edi
	roll	$18, %edi
	xorl	%ebp, %edi
	movq	%rdi, %rbp
	leal	(%r15,%rsi), %edi
	roll	$18, %edi
	xorl	%r11d, %edi
	movq	%rdi, %r11
	leal	(%r8,%rdx), %r10d
	movq	%r8, %rax
	movq	%r8, 16(%rsp)                   # 8-byte Spill
	roll	$7, %r10d
	xorl	%r9d, %r10d
	movq	%r12, -80(%rsp)                 # 8-byte Spill
	movq	-8(%rsp), %rdi                  # 8-byte Reload
	leal	(%r12,%rdi), %r8d
	roll	$7, %r8d
	xorl	%r14d, %r8d
	movq	%rbp, -88(%rsp)                 # 8-byte Spill
	movq	-128(%rsp), %rdi                # 8-byte Reload
	addl	%ebp, %edi
	roll	$7, %edi
	xorl	%r15d, %edi
	movq	%r11, -96(%rsp)                 # 8-byte Spill
	movq	(%rsp), %r9                     # 8-byte Reload
	addl	%r11d, %r9d
	roll	$7, %r9d
	xorl	%ebx, %r9d
	leal	(%r10,%rax), %r15d
	roll	$9, %r15d
	xorl	%ecx, %r15d
	leal	(%r8,%r12), %ebx
	roll	$9, %ebx
	xorl	%esi, %ebx
	leal	(%rdi,%rbp), %esi
	roll	$9, %esi
	xorl	8(%rsp), %esi                   # 4-byte Folded Reload
	leal	(%r9,%r11), %r14d
	roll	$9, %r14d
	xorl	%r13d, %r14d
	leal	(%r15,%r10), %ecx
	roll	$13, %ecx
	xorl	%edx, %ecx
	leal	(%rbx,%r8), %edx
	roll	$13, %edx
	xorl	-8(%rsp), %edx                  # 4-byte Folded Reload
	movq	%rdi, -104(%rsp)                # 8-byte Spill
	leal	(%rsi,%rdi), %r13d
	roll	$13, %r13d
	xorl	-128(%rsp), %r13d               # 4-byte Folded Reload
	leal	(%r14,%r9), %eax
	roll	$13, %eax
	xorl	(%rsp), %eax                    # 4-byte Folded Reload
	leal	(%rcx,%r15), %edi
	roll	$18, %edi
	xorl	16(%rsp), %edi                  # 4-byte Folded Reload
	leal	(%rdx,%rbx), %r12d
	roll	$18, %r12d
	xorl	-80(%rsp), %r12d                # 4-byte Folded Reload
	leal	(%rsi,%r13), %ebp
	roll	$18, %ebp
	xorl	-88(%rsp), %ebp                 # 4-byte Folded Reload
	leal	(%rax,%r14), %r11d
	roll	$18, %r11d
	xorl	-96(%rsp), %r11d                # 4-byte Folded Reload
	decl	-20(%rsp)                       # 4-byte Folded Spill
	jne	.LBB1_1
# %bb.2:
	addl	-112(%rsp), %edi                # 4-byte Folded Reload
	movq	%r10, -128(%rsp)                # 8-byte Spill
	movq	-16(%rsp), %r10                 # 8-byte Reload
	movl	%edi, (%r10)
	movq	-128(%rsp), %r10                # 8-byte Reload
	addl	-108(%rsp), %r10d               # 4-byte Folded Reload
	movq	-16(%rsp), %rdi                 # 8-byte Reload
	movl	%r10d, 4(%rdi)
	addl	-116(%rsp), %r15d               # 4-byte Folded Reload
	movl	%r15d, 8(%rdi)
	addl	-24(%rsp), %ecx                 # 4-byte Folded Reload
	movl	%ecx, 12(%rdi)
	addl	-28(%rsp), %edx                 # 4-byte Folded Reload
	movl	%edx, 16(%rdi)
	addl	-32(%rsp), %r12d                # 4-byte Folded Reload
	movl	%r12d, 20(%rdi)
	addl	-36(%rsp), %r8d                 # 4-byte Folded Reload
	movl	%r8d, 24(%rdi)
	addl	-40(%rsp), %ebx                 # 4-byte Folded Reload
	movl	%ebx, 28(%rdi)
	addl	-44(%rsp), %esi                 # 4-byte Folded Reload
	movl	%esi, 32(%rdi)
	addl	-48(%rsp), %r13d                # 4-byte Folded Reload
	movl	%r13d, 36(%rdi)
	addl	-52(%rsp), %ebp                 # 4-byte Folded Reload
	movl	%ebp, 40(%rdi)
	movq	-104(%rsp), %rcx                # 8-byte Reload
	addl	-56(%rsp), %ecx                 # 4-byte Folded Reload
	movl	%ecx, 44(%rdi)
	addl	-60(%rsp), %r9d                 # 4-byte Folded Reload
	movl	%r9d, 48(%rdi)
	addl	-64(%rsp), %r14d                # 4-byte Folded Reload
	movl	%r14d, 52(%rdi)
	addl	-68(%rsp), %eax                 # 4-byte Folded Reload
	movl	%eax, 56(%rdi)
	addl	-72(%rsp), %r11d                # 4-byte Folded Reload
	movl	%r11d, 60(%rdi)
	addq	$24, %rsp
	.cfi_def_cfa_offset 56
	popq	%rbx
	.cfi_def_cfa_offset 48
	popq	%r12
	.cfi_def_cfa_offset 40
	popq	%r13
	.cfi_def_cfa_offset 32
	popq	%r14
	.cfi_def_cfa_offset 24
	popq	%r15
	.cfi_def_cfa_offset 16
	popq	%rbp
	.cfi_def_cfa_offset 8
	retq
.Lfunc_end1:
	.size	_ZL19xor_salsa8_originalPjPKj, .Lfunc_end1-_ZL19xor_salsa8_originalPjPKj
	.cfi_endproc
                                        # -- End function
	.type	__hip_cuid_,@object             # @__hip_cuid_
	.bss
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               # 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_

	.text
	.p2alignl 7, 3214868480
	.fill 96, 4, 3214868480
	.type	__hip_cuid_,@object             ; @__hip_cuid_
	.section	.bss,"aw",@nobits
	.globl	__hip_cuid_
__hip_cuid_:
	.byte	0                               ; 0x0
	.size	__hip_cuid_, 1

	.ident	"AMD clang version 18.0.0git (https://github.com/RadeonOpenCompute/llvm-project roc-6.3.2 25012 e5bf7e55c91490b07c49d8960fa7983d864936c4)"
	.section	".note.GNU-stack","",@progbits
	.addrsig
	.addrsig_sym __hip_cuid_
	.amdgcn_target "amdgcn-amd-amdhsa--gfx1100"
	.amdgpu_metadata
---
amdhsa.kernels:  []
amdhsa.target:   amdgcn-amd-amdhsa--gfx1100
amdhsa.version:
  - 1
  - 2
...

	.end_amdgpu_metadata
