Download Single Exponentiation Algorithm: Unsigned and Signed, Various Methods and more Study notes Advanced Computer Architecture in PDF only on Docsity!
Implementing Arithmetic in G
I Imagine you have a group G (i.e., you can represent elements and have
implemented the group operation).
I Beware! Since we are looking at G as an generic group, there are two
potential notations ...
Additive
I (^) The identity element is 0. I (^) The inverse of x is −x. I (^) x − y = x + (−y). I (^) If y < 0, x · y = (−x) · (−y). I (^) If y = 0, x · y = 0. I (^) If y = 1, x · y = x. I (^) If y > 1, x · y = x + x + · · · + x | {z } y terms
Multiplicative
I (^) The identity element is 1 I (^) The inverse of x is x−^1. I (^) x/y = x · y−^1. I (^) If y < 0, xy^ = (x−^1 )−y^. I (^) If y = 0, xy^ = 1. I (^) If y = 1, xy^ = x. I (^) If y > 1, xy^ = x · x · · · · · x | {z } y terms
I ... additive examples include ECC where G = E(K ), multiplicative examples
include RSA where G = Z
N.
Dan Page
Implementing Arithmetic in G
I The main challenge is efficient
1. scalar multiplication, i.e., y · x, or
2. exponentiation, i.e., xy
depending on whether we are working in an additive or multiplicative group.
I Algorithms for these operations basically only differ in their notation; from here
on we’ll use additive notation only, so some translation may be required.
I There are a number of cases of interest ...
1. a random or fixed exponent, i.e., y is dynamic or static,
2. a random or fixed base, i.e., x is dynamic or static.
3. a single or multiple operations, i.e., y · x or
Pm− 1
i= 0 y[i]^ ·^ x[i].
I ... all of which are derived from how higher-level schemes use the group.
Dan Page
Addition Chains (2)
I The point is, with the addition chain for y = 14, i.e.,
we can compute x · y = x · 14 using the sequence
〈x, 2 · x, 4 · x, 6 · x, 8 · x, 14 · x〉
or x
y
= y
using the sequence
D
x, x
, x
, x
, x
, x
E
I So with an addition chain of length n for y, scalar multiplication (resp.
exponentiation) of x ∈ G by y costs us n group operations.
Dan Page
Addition Chains (3)
I There are at least two useful generalisations:
1. An addition-subtraction chain is where we again have some sequence A where
A 0 = 1 and An = y, but now
Ak = Ai ± Aj
for 0 ≤ i, j < k, e.g.,
is an addition-subtraction chain of length n = 5 for y = 14.
2. An addition sequence for some set
{y 0 , y 1 ,... , ym− 1 }
is an addition chain which contains each yi ; this means we can compute yi · x for
all 0 ≤ i < m in an efficient way.
Dan Page
Single Exponentiation (1) – Unsigned, Binary
I Scalar multiplication is simply repeated addition, i.e.,
x · y = x + x + · · · + x + x
| {z }
y terms
I So if we select y = 6, then we obviously have
x · 6 = x + x + x + x + x + x.
I Another way of look it, is inclusion of another “weight” to the digits that
describe y; if we write y out in binary then
x · y = x ·
nX− 1
i= 0
yi · 2
i
nX− 1
i= 0
x · yi · 2
i
and, as such, if y = 6 = 110 ( 2 ) then
x · y = x · y 2 · 22 + x · y 1 · 21 + x · y 0 · 20
= x · 1 · 22 + x · 1 · 21 + x · 0 · 20
= x · 4 + x · 2 + x · 0
= x · 6
Dan Page
Single Exponentiation (2) – Unsigned, Binary
I If we bracket this in a nicer way, we can accumulate the result rather than
express it as separate terms ...
I ... this technique is due to Horner; if y = 6 = 110 ( 2 ) then
x · y = ( ( x · y 2 ) · 2 + x · y 1 ) · 2 + x · y 0
= ( ( x · 1 ) · 2 + x · 1 ) · 2 + x · 0
= ( x · 2 + x · 1 ) · 2 + x · 0
= x · 4 + x · 2 + x · 0
= x · 6
Dan Page
Single Exponentiation (4) – Unsigned, Binary
Algorithm (BINARY-L2R-1EXP)
Input : A group element x ∈ G of order n, an integer 0 ≤ y < n represented in base-2. Output : The group element r = y · x ∈ G.
1 t ← (^0) G ; 2 for i = |y| − 1 downto 0 step − 1 do 3 t ← 2 · t; 4 if yi = 1 then 5 t ← t + x; 6 end 7 end 8 return t;
Example
Imagine G = Z∗ N where N = 503 · 509 = 256027. Setting x = 1234 and y = 3094 = (^110000010110) ( 2 ), the algorithm performs
i t t 1 11 1 1234 t ← t^2 , t ← t · x 10 1234 98751 t ← t^2 , t ← t · x 9 98751 203625 t ← t^2 8 203625 80029 t ← t^2 7 80029 125436 t ← t^2 6 125436 50811 t ← t^2 5 50811 237480 t ← t^2 4 237480 66716 t ← t^2 , t ← t · x 3 66716 251288 t ← t^2 2 251288 190753 t ← t^2 , t ← t · x 1 190753 219479 t ← t^2 , t ← t · x 0 219479 63445 t ← t^2
corresponding to the addition chain fi 1 , 2 , 3 , 6 , 12 , 24 , 48 , 96 , 192 , 193 , 386 , 772 , 773 , 1546 , 1547 , 3094
fl .
Dan Page
An Aside: Representation
I Imagine G is Z∗ N (or Fp ):
I At the start, convert t and x into Montgomery representation.
I During the algorithm, hold t in Montgomery representation (i.e., don’t convert back
and forth).
I At the end, convert t back into standard integer representation before it is returned
as the result.
I Imagine G is E(K ) for some field K :
I At the start, convert t into projective representation.
I During the algorithm, hold t in projective coordinates (i.e., don’t convert back and
forth); notice that t + x is a mixed addition (t projective, x affine).
I At the end, convert t back into affine representation before it is returned as the
result.
Dan Page
Single Exponentiation (6) – Unsigned, Regular
Algorithm (BINARY-DUMMY-1EXP)
Input : A group element x ∈ G of order n, an integer 0 ≤ y < n represented in base-2. Output : The group element r = y · x ∈ G. 1 t ← (^0) G ; 2 for i = |y| − 1 downto 0 step − 1 do 3 t ← 2 · t; 4 if yi = 1 then 5 t ← t + x; 6 end 7 else 8 ⊥← t + x; 9 end 10 end 11 return t;
Dan Page
Single Exponentiation (7) – Unsigned, Regular
I Define
Lj =
P|y|− 1
i=j yi^ ·^2
i−j
Rj = Lj + 1
I Intuitively, if we are step j then
1. Lj+ 1 represents yj+ 1 ,...,|y|− 1 , i.e., the portion of y we’ve processed so far,
2. we are examining the j-th bit of y with the goal of having processed yj,...,|y|− 1 after
we’ve completed the step.
I Notice L 0 = y and that
Lj = 2 · Lj+ 1 + yj = Lj+ 1 + Rj+ 1 + yj − 1 = 2 · Rj+ 1 + yj − 2
so
(Lj , Rj ) =
( 2 · Lj+ 1 , Lj+ 1 + Rj+ 1 ) if yj = 0 ,
(Lj+ 1 + Rj+ 1 , 2 · Rj+ 1 ) if yj = 1.
I The Montgomery “ladder” keeps track of Lj and Rj in t 0 and t 1 , we start with
t 0 = 0 G and t 1 = x; at step j we have
(x · Lj , x · Rj ) =
(x · 2 · Lj+ 1 , x · (Lj+ 1 + Rj+ 1 )) if yj = 0 ,
(x · (Lj+ 1 + Rj+ 1 ), x · 2 · Rj+ 1 ) if yj = 1.
Dan Page
Single Exponentiation (9) – Unsigned, Windowed
I A problem:
I So far, we’ve only using the binary digit set { 0 , + 1 }.
I This isn’t a limit: if we use { 0 , + 1 , + 2 , + 3 } for example, and a base-4 expansion of
y, we simply need to
1. recode y into y′^ so that the digits are taken from { 0 , + 1 , + 2 , + 3 } rather than just
{ 0 , + 1 }, and
2. pre-compute some multiples of x so when we see the digit 2 in y′, we can add 2 · x in
one operation rather than two.
I Using digits with a larger magnitude means we pre-compute more multiples of x
(i.e., more space), but also reduces length of y′^ (i.e., less time) ...
I ... if we are really clever, we can reduce the “weight” of y′^ so there are less
non-zero digits.
Dan Page
Single Exponentiation (10) – Unsigned, Windowed
Algorithm (m-ARY-FIXED-RECODE)
Input : An integer 0 ≤ y < n represented in base-2. Output : An sequence y′, that represents y in m-ary form (i.e., base-m).
1 return Pn− 1 i= 0 y
′ i ·^ m i (^) where y′ i =^ by/m i (^) c mod m;
Algorithm (m-ARY-FIXED-1EXP)
Input : A group element x ∈ G of order n, an integer 0 ≤ y < n represented in base-2, an integer m. Output : The group element r = y · x ∈ G. 1 y′^ ← m-ARY-FIXED-RECODE(y, m); 2 Pre-compute T = [i · x | i ∈ { 1 , 2 , 3 ,... , m − 1 }]; 3 t ← (^0) G ; 4 for i = |y′| − 1 downto 0 step − 1 do 5 t ← m · t; 6 if y i′ 6 = 0 then 7 t ← t + T [y′ i − 1 ]; 8 end 9 end 10 return t;
I Note that this is perhaps most useful when m = 2 w^ for some w; this means
1. each y i′ ∈ { 0 , 1 ,... 2 w^ − 1 } which is basically a w-bit sized “window” of y, and
2. we have
m · t = 2 w^ · t = t + t + · · · + t
| {z }
2 w^ terms
which can be efficiently computed via w doublings (resp. squarings).
Dan Page
Single Exponentiation (12) – Unsigned, Windowed
I A problem:
I Imagine y = 24 = 00011000 ( 2 ) and we use the 2w^ -ary method with w = 4; we’d
recode y as *
| {z }
y′ 0
| {z }
y 1 ′
and then run the main exponentiation which would perform the two addition steps:
1. add 8 · x when i = 0 since y 0 ′ = 8 = 1000 ( 2 ),
2. add 1 · x when i = 1 since y 1 ′ = 1 = 0001 ( 2 ).
I But, given we using a 4-bit window, we could fit the window over both the non-zero
digits ...
I ... if we could do this, we’d only perform one addition step.
Dan Page
Single Exponentiation (13) – Unsigned, Windowed
Algorithm (2w^ - ARY-SLIDE-1EXP)
Input : A group element x ∈ G of order n, an integer 0 ≤ y < n represented in base-2, a window size w. Output : The group element r = y · x ∈ G. 1 Pre-compute T =
i · x | i ∈
1 , 3 , 5 ,... , 2 w^ − 1
2 t ← (^0) G ; 3 i ← |y| − 1; 4 while i ≥ 0 do 5 if yi = 0 then l ← i, u ← 0; 6 else 7 l ← max(i − w + 1 , 0 ); 8 while yi...l ≡ 0 (mod 2) do l ← l + 1; 9 u ← yi...l ; 10 end 11 t ← 2 i−l+^1 · t; 12 if u 6 = 0 then 13 t ← t + T [b(u − 1 )/ 2 c]; 14 end 15 i ← l − 1; 16 end 17 return t;
Dan Page