3 # ====================================================================
4 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
12 # The module implements bn_GF2m_mul_2x2 polynomial multiplication
13 # used in bn_gf2m.c. It's kind of low-hanging mechanical port from
14 # C for the time being... The subroutine runs in 37 cycles, which is
15 # 4.5x faster than compiler-generated code. Though comparison is
16 # totally unfair, because this module utilizes Galois Field Multiply
19 while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
20 open STDOUT,">$output";
22 ($rp,$a1,$a0,$b1,$b0)=("A4","B4","A6","B6","A8"); # argument vector
24 ($Alo,$Alox0,$Alox1,$Alox2,$Alox3)=map("A$_",(16..20));
25 ($Ahi,$Ahix0,$Ahix1,$Ahix2,$Ahix3)=map("B$_",(16..20));
26 ($B_0,$B_1,$B_2,$B_3)=("B5","A5","A7","B7");
33 EXTU $B,8,24,$B_2 ; smash $B to 4 bytes
36 SHRU $A,16, $Ahi ; smash $A to two halfwords
39 XORMPY $Alo,$B_2,$Alox2 ; 16x8 bits muliplication
40 || XORMPY $Ahi,$B_2,$Ahix2
42 XORMPY $Alo,$B_0,$Alox0
43 || XORMPY $Ahi,$B_0,$Ahix0
44 XORMPY $Alo,$B_3,$Alox3
45 || XORMPY $Ahi,$B_3,$Ahix3
46 XORMPY $Alo,$B_1,$Alox1
47 || XORMPY $Ahi,$B_1,$Ahix1
51 my ($OUTlo,$OUThi,$A,$B)=@_;
53 EXTU $B,8,24,$B_2 ; smash $B to 4 bytes
56 SHRU $A,16, $Ahi ; smash $A to two halfwords
59 XOR $Ahix0,$Alox2,$Ahix0
61 || XORMPY $Alo,$B_2,$Alox2
62 XORMPY $Ahi,$B_2,$Ahix2
64 || XORMPY $Alo,$B_0,A1 ; $Alox0
65 XOR $Ahix1,$Alox3,$Ahix1
66 || SHL $Ahix0,16,$OUTlo
67 || SHRU $Ahix0,16,$Ahix0
68 XOR $Alox0,$OUTlo,$OUTlo
69 || XOR $Ahix0,$OUThi,$OUThi
70 || XORMPY $Ahi,$B_0,$Ahix0
71 || XORMPY $Alo,$B_3,$Alox3
72 || SHL $Alox1,8,$Alox1
73 || SHL $Ahix3,8,$Ahix3
74 XOR $Alox1,$OUTlo,$OUTlo
75 || XOR $Ahix3,$OUThi,$OUThi
76 || XORMPY $Ahi,$B_3,$Ahix3
77 || SHL $Ahix1,24,$Alox1
78 || SHRU $Ahix1,8, $Ahix1
79 XOR $Alox1,$OUTlo,$OUTlo
80 || XOR $Ahix1,$OUThi,$OUThi
81 || XORMPY $Alo,$B_1,$Alox1
82 || XORMPY $Ahi,$B_1,$Ahix1
87 my ($OUTlo,$OUThi)=@_;
90 XOR $Ahix0,$Alox2,$Ahix0
93 XOR $Ahix1,$Alox3,$Ahix1
94 || SHL $Ahix0,16,$OUTlo
95 || SHRU $Ahix0,16,$Ahix0
96 XOR $Alox0,$OUTlo,$OUTlo
97 || XOR $Ahix0,$OUThi,$OUThi
98 || SHL $Alox1,8,$Alox1
99 || SHL $Ahix3,8,$Ahix3
100 XOR $Alox1,$OUTlo,$OUTlo
101 || XOR $Ahix3,$OUThi,$OUThi
102 || SHL $Ahix1,24,$Alox1
103 || SHRU $Ahix1,8, $Ahix1
104 XOR $Alox1,$OUTlo,$OUTlo
105 || XOR $Ahix1,$OUThi,$OUThi
111 .if .ASSEMBLER_VERSION<7000000
115 .asg bn_GF2m_mul_2x2,_bn_GF2m_mul_2x2
118 .global _bn_GF2m_mul_2x2
123 &mul_1x1_upper($a0,$b0); # a0·b0
128 &mul_1x1_merged("A28","B28",$A,$B); # a0·b0/a1·b1
133 &mul_1x1_merged("A31","B31",$A,$B); # a1·b1/(a0+a1)·(b0+b1)
136 || XOR B28,B31,B29 ; a0·b0+a1·b1
138 &mul_1x1_lower("A30","B30"); # (a0+a1)·(b0+b1)
142 || XOR B29,B30,B30 ; (a0+a1)·(b0+b1)-a0·b0-a1·b1