2 # Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
19 # The module implements bn_GF2m_mul_2x2 polynomial multiplication
20 # used in bn_gf2m.c. It's kind of low-hanging mechanical port from
21 # C for the time being... The subroutine runs in 37 cycles, which is
22 # 4.5x faster than compiler-generated code. Though comparison is
23 # totally unfair, because this module utilizes Galois Field Multiply
26 $output = pop and open STDOUT,">$output";
28 ($rp,$a1,$a0,$b1,$b0)=("A4","B4","A6","B6","A8"); # argument vector
30 ($Alo,$Alox0,$Alox1,$Alox2,$Alox3)=map("A$_",(16..20));
31 ($Ahi,$Ahix0,$Ahix1,$Ahix2,$Ahix3)=map("B$_",(16..20));
32 ($B_0,$B_1,$B_2,$B_3)=("B5","A5","A7","B7");
39 EXTU $B,8,24,$B_2 ; smash $B to 4 bytes
42 SHRU $A,16, $Ahi ; smash $A to two halfwords
45 XORMPY $Alo,$B_2,$Alox2 ; 16x8 bits multiplication
46 || XORMPY $Ahi,$B_2,$Ahix2
48 XORMPY $Alo,$B_0,$Alox0
49 || XORMPY $Ahi,$B_0,$Ahix0
50 XORMPY $Alo,$B_3,$Alox3
51 || XORMPY $Ahi,$B_3,$Ahix3
52 XORMPY $Alo,$B_1,$Alox1
53 || XORMPY $Ahi,$B_1,$Ahix1
57 my ($OUTlo,$OUThi,$A,$B)=@_;
59 EXTU $B,8,24,$B_2 ; smash $B to 4 bytes
62 SHRU $A,16, $Ahi ; smash $A to two halfwords
65 XOR $Ahix0,$Alox2,$Ahix0
67 || XORMPY $Alo,$B_2,$Alox2
68 XORMPY $Ahi,$B_2,$Ahix2
70 || XORMPY $Alo,$B_0,A1 ; $Alox0
71 XOR $Ahix1,$Alox3,$Ahix1
72 || SHL $Ahix0,16,$OUTlo
73 || SHRU $Ahix0,16,$Ahix0
74 XOR $Alox0,$OUTlo,$OUTlo
75 || XOR $Ahix0,$OUThi,$OUThi
76 || XORMPY $Ahi,$B_0,$Ahix0
77 || XORMPY $Alo,$B_3,$Alox3
78 || SHL $Alox1,8,$Alox1
79 || SHL $Ahix3,8,$Ahix3
80 XOR $Alox1,$OUTlo,$OUTlo
81 || XOR $Ahix3,$OUThi,$OUThi
82 || XORMPY $Ahi,$B_3,$Ahix3
83 || SHL $Ahix1,24,$Alox1
84 || SHRU $Ahix1,8, $Ahix1
85 XOR $Alox1,$OUTlo,$OUTlo
86 || XOR $Ahix1,$OUThi,$OUThi
87 || XORMPY $Alo,$B_1,$Alox1
88 || XORMPY $Ahi,$B_1,$Ahix1
93 my ($OUTlo,$OUThi)=@_;
96 XOR $Ahix0,$Alox2,$Ahix0
99 XOR $Ahix1,$Alox3,$Ahix1
100 || SHL $Ahix0,16,$OUTlo
101 || SHRU $Ahix0,16,$Ahix0
102 XOR $Alox0,$OUTlo,$OUTlo
103 || XOR $Ahix0,$OUThi,$OUThi
104 || SHL $Alox1,8,$Alox1
105 || SHL $Ahix3,8,$Ahix3
106 XOR $Alox1,$OUTlo,$OUTlo
107 || XOR $Ahix3,$OUThi,$OUThi
108 || SHL $Ahix1,24,$Alox1
109 || SHRU $Ahix1,8, $Ahix1
110 XOR $Alox1,$OUTlo,$OUTlo
111 || XOR $Ahix1,$OUThi,$OUThi
117 .if .ASSEMBLER_VERSION<7000000
121 .asg bn_GF2m_mul_2x2,_bn_GF2m_mul_2x2
124 .global _bn_GF2m_mul_2x2
129 &mul_1x1_upper($a0,$b0); # a0·b0
134 &mul_1x1_merged("A28","B28",$A,$B); # a0·b0/a1·b1
139 &mul_1x1_merged("A31","B31",$A,$B); # a1·b1/(a0+a1)·(b0+b1)
142 || XOR B28,B31,B29 ; a0·b0+a1·b1
144 &mul_1x1_lower("A30","B30"); # (a0+a1)·(b0+b1)
148 || XOR B29,B30,B30 ; (a0+a1)·(b0+b1)-a0·b0-a1·b1