summaryrefslogtreecommitdiffstats
path: root/perl/mul.pl
blob: d4dea4819253b664e56b233b6c86f87cc4ab54c3 (plain)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/local/bin/perl

use ExtUtils::testlib;

use SSLeay;


sub mul
	{
	my($ab,$cd,$num)=@_;

	if ($num <= 4096)
		{
		return($ab*$cd);
		}
	else
		{
		my($a,$b,$c,$d,$n,$ac,$bd,$m,$t1,$t2);

		$n=$num/2;

		$a=$ab->mask_bits($n);
		$b=$ab->rshift($n);
		$c=$cd->mask_bits($n);
		$d=$cd->rshift($n);

		$t1=($b-$a);
		$t2=($c-$d);
		$m= &mul($t1,$t2,$n);
		$ac=&mul($a,$c,$n);
		$bd=&mul($b,$d,$n);
		$m=$m+$ac+$bd;
		$m=$m->lshift($n);
		$bd=$bd->lshift($num);

		$r=$ac+$m+$bd;
		return($r);
		}
	}

$num=4096*32;
$a=SSLeay::BN::rand($num);
$b=SSLeay::BN::rand($num);

#for (1 .. 10)
	{
	$r=&mul($a,$b,$num);
	}

#for (1 .. 10)
	{
	$rr=$a*$b;
	}

$res=$rr-$r;
print $res->bn2hex()."\n";