<s>
Kochanski	B-Algorithm
multiplication	I-Algorithm
is	O
an	O
algorithm	O
that	O
allows	O
modular	O
arithmetic	O
(	O
multiplication	O
or	O
operations	O
based	O
on	O
it	O
,	O
such	O
as	O
exponentiation	O
)	O
to	O
be	O
performed	O
efficiently	O
when	O
the	O
modulus	O
is	O
large	O
(	O
typically	O
several	O
hundred	O
bits	O
)	O
.	O
</s>
<s>
This	O
has	O
particular	O
application	O
in	O
number	O
theory	O
and	O
in	O
cryptography	O
:	O
for	O
example	O
,	O
in	O
the	O
RSA	B-Architecture
cryptosystem	I-Architecture
and	O
Diffie	B-Protocol
–	I-Protocol
Hellman	I-Protocol
key	I-Protocol
exchange	I-Protocol
.	O
</s>
<s>
The	O
most	O
common	O
way	O
of	O
implementing	O
large-integer	O
multiplication	O
in	O
hardware	O
is	O
to	O
express	O
the	O
multiplier	O
in	O
binary	O
and	O
enumerate	O
its	O
bits	O
,	O
one	O
bit	O
at	O
a	O
time	O
,	O
starting	O
with	O
the	O
most	O
significant	O
bit	O
,	O
perform	O
the	O
following	O
operations	O
on	O
an	O
accumulator	B-General_Concept
:	O
</s>
<s>
Double	O
the	O
contents	O
of	O
the	O
accumulator	B-General_Concept
(	O
if	O
the	O
accumulator	B-General_Concept
stores	O
numbers	O
in	O
binary	O
,	O
as	O
is	O
usually	O
the	O
case	O
,	O
this	O
is	O
a	O
simple	O
"	O
shift	O
left	O
"	O
that	O
requires	O
no	O
actual	O
computation	O
)	O
.	O
</s>
<s>
If	O
the	O
current	O
bit	O
of	O
the	O
multiplier	O
is	O
1	O
,	O
add	O
the	O
multiplicand	O
into	O
the	O
accumulator	B-General_Concept
;	O
if	O
it	O
is	O
0	O
,	O
do	O
nothing	O
.	O
</s>
<s>
Double	O
the	O
contents	O
of	O
the	O
accumulator	B-General_Concept
.	O
</s>
<s>
If	O
the	O
result	O
is	O
greater	O
than	O
or	O
equal	O
to	O
r	O
,	O
subtract	O
r	O
.	O
(	O
Equivalently	O
,	O
subtract	O
r	O
from	O
the	O
accumulator	B-General_Concept
and	O
store	O
the	O
result	O
back	O
into	O
the	O
accumulator	B-General_Concept
if	O
and	O
only	O
if	O
it	O
is	O
non-negative	O
)	O
.	O
</s>
<s>
If	O
the	O
current	O
bit	O
of	O
the	O
multiplier	O
is	O
1	O
,	O
add	O
the	O
multiplicand	O
into	O
the	O
accumulator	B-General_Concept
;	O
if	O
it	O
is	O
0	O
,	O
do	O
nothing	O
.	O
</s>
<s>
Addition	O
of	O
long	O
integers	O
suffers	O
from	O
the	O
problem	O
that	O
carries	B-Algorithm
have	O
to	O
be	O
propagated	O
from	O
right	O
to	O
left	O
and	O
the	O
final	O
result	O
is	O
not	O
known	O
until	O
this	O
process	O
has	O
been	O
completed	O
.	O
</s>
<s>
Carry	O
propagation	O
can	O
be	O
speeded	O
up	O
with	O
carry	O
look-ahead	O
logic	O
,	O
but	O
this	O
still	O
makes	O
addition	O
very	O
much	O
slower	O
than	O
it	O
needs	O
to	O
be	O
(	O
for	O
512-bit	O
addition	O
,	O
addition	O
with	O
carry	O
look-ahead	O
is	O
32	O
times	O
slower	O
than	O
addition	O
without	O
carries	B-Algorithm
at	O
all	O
)	O
.	O
</s>
<s>
Non-modular	O
multiplication	O
can	O
make	O
use	O
of	O
carry-save	O
adders	O
,	O
which	O
save	O
time	O
by	O
storing	O
the	O
carries	B-Algorithm
from	O
each	O
digit	O
position	O
and	O
using	O
them	O
later	O
:	O
for	O
example	O
,	O
by	O
computing	O
111111111111+000000000010	O
as	O
111111111121	O
instead	O
of	O
waiting	O
for	O
the	O
carry	O
to	O
propagate	O
through	O
the	O
whole	O
number	O
to	O
yield	O
the	O
true	O
binary	O
value	O
1000000000001	O
.	O
</s>
<s>
Unfortunately	O
,	O
the	O
modular	O
multiplication	O
method	O
outlined	O
above	O
needs	O
to	O
know	O
the	O
magnitude	O
of	O
the	O
accumulated	O
value	O
at	O
every	O
step	O
,	O
in	O
order	O
to	O
decide	O
whether	O
to	O
subtract	O
r	O
:	O
for	O
example	O
,	O
if	O
it	O
needs	O
to	O
know	O
whether	O
the	O
value	O
in	O
the	O
accumulator	B-General_Concept
is	O
greater	O
than	O
1000000000000	O
,	O
the	O
carry-save	O
representation	O
111111111121	O
is	O
useless	O
and	O
needs	O
to	O
be	O
converted	O
to	O
its	O
true	O
binary	O
value	O
for	O
the	O
comparison	O
to	O
be	O
made	O
.	O
</s>
<s>
The	O
principle	O
of	O
the	O
Kochanski	O
algorithm	O
is	O
one	O
of	O
making	O
guesses	O
as	O
to	O
whether	O
or	O
not	O
r	O
should	O
be	O
subtracted	O
,	O
based	O
on	O
the	O
most	O
significant	O
few	O
bits	O
of	O
the	O
carry-save	O
value	O
in	O
the	O
accumulator	B-General_Concept
.	O
</s>
<s>
Such	O
a	O
guess	O
will	O
be	O
wrong	O
some	O
of	O
the	O
time	O
,	O
since	O
there	O
is	O
no	O
way	O
of	O
knowing	O
whether	O
latent	O
carries	B-Algorithm
in	O
the	O
less	O
significant	O
digits	O
(	O
which	O
have	O
not	O
been	O
examined	O
)	O
might	O
not	O
invalidate	O
the	O
result	O
of	O
the	O
comparison	O
.	O
</s>
<s>
In	O
that	O
case	O
the	O
result	O
in	O
the	O
accumulator	B-General_Concept
is	O
greater	O
than	O
r	O
(	O
although	O
the	O
algorithm	O
does	O
n't	O
know	O
it	O
yet	O
)	O
,	O
and	O
so	O
after	O
the	O
next	O
shift	O
left	O
,	O
2r	O
will	O
need	O
to	O
be	O
subtracted	O
from	O
the	O
accumulator	B-General_Concept
.	O
</s>
<s>
In	O
that	O
case	O
the	O
result	O
in	O
the	O
accumulator	B-General_Concept
is	O
less	O
than	O
0	O
(	O
although	O
the	O
algorithm	O
does	O
n't	O
know	O
it	O
yet	O
)	O
,	O
and	O
so	O
after	O
the	O
next	O
shift	O
left	O
,	O
r	O
or	O
even	O
2r	O
will	O
need	O
to	O
be	O
added	O
back	O
to	O
the	O
accumulator	B-General_Concept
to	O
make	O
it	O
positive	O
again	O
.	O
</s>
<s>
It	O
turns	O
out	O
that	O
examining	O
the	O
most	O
significant	O
4	O
bits	O
of	O
the	O
accumulator	B-General_Concept
is	O
sufficient	O
to	O
keep	O
the	O
errors	O
within	O
bounds	O
and	O
that	O
the	O
only	O
values	O
that	O
need	O
to	O
be	O
added	O
to	O
the	O
accumulator	B-General_Concept
are	O
−2r	O
,	O
−r	O
,	O
0	O
,	O
+r	O
,	O
and	O
+2r	O
,	O
all	O
of	O
which	O
can	O
be	O
generated	O
instantaneously	O
by	O
simple	O
shifts	O
and	O
negations	O
.	O
</s>
<s>
At	O
the	O
end	O
of	O
a	O
complete	O
modular	O
multiplication	O
,	O
the	O
true	O
binary	O
result	O
of	O
the	O
operation	O
has	O
to	O
be	O
evaluated	O
and	O
it	O
is	O
possible	O
that	O
an	O
additional	O
addition	O
or	O
subtraction	O
of	O
r	O
will	O
be	O
needed	O
as	O
a	O
result	O
of	O
the	O
carries	B-Algorithm
that	O
are	O
then	O
discovered	O
;	O
but	O
the	O
cost	O
of	O
that	O
extra	O
step	O
is	O
small	O
when	O
amortized	O
over	O
the	O
hundreds	O
of	O
shift-and-add	O
steps	O
that	O
dominate	O
the	O
overall	O
cost	O
of	O
the	O
multiplication	O
.	O
</s>
<s>
Brickell	O
has	O
published	O
a	O
similar	O
algorithm	O
that	O
requires	O
greater	O
complexity	O
in	O
the	O
electronics	O
for	O
each	O
digit	O
of	O
the	O
accumulator	B-General_Concept
.	O
</s>
<s>
Montgomery	B-Algorithm
multiplication	I-Algorithm
is	O
an	O
alternative	O
algorithm	O
which	O
processes	O
the	O
multiplier	O
"	O
backwards	O
"	O
(	O
least	O
significant	O
digit	O
first	O
)	O
and	O
uses	O
the	O
least	O
significant	O
digit	O
of	O
the	O
accumulator	B-General_Concept
to	O
control	O
whether	O
or	O
not	O
the	O
modulus	O
should	O
be	O
added	O
.	O
</s>
<s>
This	O
avoids	O
the	O
need	O
for	O
carries	B-Algorithm
to	O
propagate	O
.	O
</s>
