ecc-curve25519-modp.asm 1.99 KB
Newer Older
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 31 32 33 34 35
C x86_64/ecc-25519-modp.asm

ifelse(<
   Copyright (C) 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
>)

	.file "ecc-25519-modp.asm"

define(<RP>, <%rsi>)
Niels Möller's avatar
Niels Möller committed
36
define(<U0>, <%rdi>)	C Overlaps unused modulo input
37 38
define(<U1>, <%rcx>)
define(<U2>, <%r8>)
39 40 41 42
define(<U3>, <%r9>)
define(<T0>, <%r10>)
define(<T1>, <%r11>)
define(<M>, <%rbx>)
43

44
PROLOGUE(_nettle_ecc_25519_modp)
45
	W64_ENTRY(2, 0)
46 47 48 49
	push	%rbx

	C First fold the limbs affecting bit 255
	mov	56(RP), %rax
50 51
	mov	$38, M
	mul	M
52 53 54 55
	mov	24(RP), U3
	xor	T0, T0
	add	%rax, U3
	adc	%rdx, T0
56

57
	mov	40(RP), %rax	C Do this early as possible
58 59
	mul	M
	
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
	add	U3, U3
	adc	T0, T0
	shr	U3		C Undo shift, clear high bit

	C Fold the high limb again, together with RP[5]
	imul	$19, T0

	mov	(RP), U0
	mov	8(RP), U1
	mov	16(RP), U2
	add	T0, U0
	adc	%rax, U1
	mov	32(RP), %rax
	adc	%rdx, U2
	adc	$0, U3

	C Fold final two limbs, RP[4] and RP[6]
77
	mul	M
78 79 80
	mov	%rax, T0
	mov	48(RP), %rax
	mov	%rdx, T1
81
	mul	M
82
	add	T0, U0
83
	mov	U0, (RP)
84
	adc	T1, U1
85
	mov	U1, 8(RP)
86
	adc	%rax, U2
87
	mov	U2, 16(RP)
88 89
	adc	%rdx, U3
	mov	U3, 24(RP)
90

91
	pop	%rbx
92 93
	W64_EXIT(2, 0)
	ret
94
EPILOGUE(_nettle_ecc_25519_modp)