RESIDUE NUMBER SYSTEM

(introduction to hardware aspects)

Dr. Danila Gorodecky [email protected] Terminology

– Residue number system (RNS) (refers to Chinese remainder theorem)

– Residue numeral system (RNS)

– Modular (MA) (refers to moduli – X (mod P) )

– Complete residue system

– Clock arithmetic (refers to 12-hour arrow clock in which numbers "wrap around“ upon reaching the modulo)

2 Milestones

– Chinese mathematician Sunzi Suanjing proposed a theorem (Chinese remainder theorem) in the 3rd century AD;

– the theorem was generalized by Chinese mathematician Qin Jiushao in 1247;

– first real implementation of the theorem by German mathematician Carl Gauss in 1801 "to find the years that have a certain period number with respect to the solar and lunar cycle and the Roman indiction“;

– first implementation in computer science by Czechoslovakian engineer Miro Valach in 1955 “Origin of the code and number system of remainder classes”, Stroje Na ZpracovaniInformaci, vol. 3, Nakl. CSAV, Prague. Implementation of RNS

– Processing of results of the Unified State Exam (utilized to entrance to University in Russia;

– Digital filternig with finite impulse response (FIR-filtering);

– Crypto system of Federal Reserve System of USA;

– Air Defense System (USA, Russia);

– cryptography in Space (Russia);

– Space flight control (Russia)

4 Chinese remainder theorem

Let’s p 1 , p 2 ,..., p n are positive integers (are often called as moduli) such, that for

a couple  p i , p j  equals ‘1’.

yx1mod p1 yx2 mod p2  Then the system ...

yxn mod pn  has a simultaneous solution which is unique modulo

5 Example for the Theorem

P  p1  p2  p3  579  315

We can express an arbitrary number definitely in the scope from 0 to 314

A1000mod 5 Let’s A  100 , hence A1002mod 7 A1001mod 9

and A   0 , 2 , 1  in the RNS representation

6 NOT positional numeral system

RNS is not positional numeral system

100(mod 5), 100(mod 7), 100(mod 9)  (0, 2, 1) 100 100(mod 7), 100(mod 9), 100(mod 5)  (2, 1, 0) 100

Binary system

(1100100)2 10010

(0110100)2  5210

7 Example of the computation in RNS

P  p1  p2  p3  579  315 AB10013S

1) A0,2,1 B3,6,4

2) A  B   0  3 mod 5, 2  6 mod 7, 1 4 mod 9   3 mod 5, 1 mod 7, 5 mod 9   3,1,5

8 Example of the computation in RNS 3) S  S1 Y1  S2 Y2  S3 Y3  r  P

 P  Yi Yi   ki ; 1 (mod p ); r  P  S Y  S Y  S Y  r 1 P  p  i 1 1 2 2 3 3  i  pi

315 a)   63 k1 Y1    k1  63  k1 and  1 (mod 5 ) , then k  2 and Y 126  5  5 1 1  315  b) 45  k2 Y2    k2  45  k2 and  1 (mod 7 ) , then k  5 and Y2  225  7  7 2

 315  35  k3 с) Y3    k3  35  k3 and  1 (mod 9 ) , then k  8 andY  280  9  9 3 3 d) r 315  3126  8 225  5 280  r 1315, then r  6

S  3126  8 225  5 280  6315   2003 1890 113 Exercises

1) What is maximum bit range of A and B should be chosen for unambiguous representation A + B = S in RNS with moduli 11,13, and 15?

P = 11 * 13 * 15 = 2145

0 ≤ S < 2145 and S is 12-bit number.

Hence, in order to represent A + B = S, A and B should be limited 11-bit tuples, when A and B both equal 1077.

2) What is maximum bit range of A, B, and C can be used for unambiguous representation A * B * C = R in RNS with moduli 11,13, and 15?

P = 11 * 13 * 15 = 2145

0 ≤ S < 2145 and S is 12-bit number.

Hence, in order to represent A * B * C = R, A, B, and C should be limited 4-bit tuples, when A, B, and C equal 12. 10 Main advantage of RNS

Significantly smaller ranges of numbers in arithmetic calculations than initial numbers

11 Example of the computation in RNS

A  {A1, A2, A3}  A  SS1Y1S2Y2S3Y3rP Ai (mod pi )  A    pi  pi  Yi 0A314  1 (mod pi ) pi  P  B  {B1, B2 , B3} Y  k i  p  i  B   i  Bi (mod pi )  B    pi  pi  0B314

i  1,2,3 ki 1, pi r  0,1,2,... 12 X (mod P) hardware calculations in RNS

X mod P A 1 A2 summator/multiplier (mod p ) S1 1 A1 Converter of Ag Converter of

A1 A2 positional modular

A2 numbers S2 representation S summator/multiplier (mod p2)

to Ag to

modular positional Ag representation A number 1 (1) A2 (2) Sm summator/multiplier (mod pm)

Ag

13 Pipelining approach of X (mod P) calculations

1) Pipelining (iterative) approach. It is based on the formula:

  1 0 X  P Q  A  P  2  q  P  2  q 1 ... P  2  q0  A

X  x , x1,...,x1 P  p , p 1,..., p1

P  2 1  2 1 P  2

14 Pipelining approach of X (mod P) calculations 510 mod77QAmod7 6 5 4 3 2 1 0 72 q672 q572 q472 q372 q272 q172 q0Amod 7 Amod 7

510 mod 77QA726723Amod 76 mod 7

15 Recursive approaches of calculation X (mod P) in hardware

n 2) Recursive approach. It is suitable for special moduli, e.c. 2  1 and 2n3 2  1  x , x , ..., x ,...{0, 1} X  x1  2x2  2 x3  ... 2 x  2 x 1  ... 1 2 

… x1,x2,...,x X1 x 1,x 2,...,x2 X2  2 X  X1  2 X2  2 X3  ...     as 2  1  mod 2  1  , so X mod 2 1 X1  X1  X3  ... mod 2 1

Example. X (mod P), where X  437   110110101  and P  23 1  7

a) 110110101110110101mod710001mod7

b)  010  001 mod 7  011 mod 7  3 mod 7

16 Recursive approaches of calculation X (mod P) in hardware

3) It is suitable for an arbitrary modulo and is based on the next formula: 0 1 2 3 X2 x12 x22 x32 x4...modP 0 1 2 3 2 x1modP2 x2modP2 x3modP2 x4modP...

x1,x2,x3,x4,...{0,1}

Example. X (mod P), where X   x 1 , x 2 ,..., x 10  and P23 0 1 2 3 4 5 6 7 8 9 X 2 x12 x22 x32 x42 x52 x62 x72 x82 x92 x10 mod23 x1mod232x2mod234x3mod238x4mod2316x5mod23 9x6mod2318x7mod2313x8mod233x9mod236x10mod23 – If 0S23 X mod 23X – If 46S69 X mod 23X 2P – If 23S46 X mod 23X P – If 69S92 X mod 23X 3P

Let’s X  1023 10 (mod 23 )   1111111111  2  mod 23 

17 2^n (mod P) calculation in hardware

4) X (mod P), where P2n

n n X mod 2  x , x 1,..., xn, xn1,..., x1 mod 2  n  xn, xn1,..., x1 mod 2 

x1,x2,x3,x4,...{0,1}

Example. X (mod P), where X   0111110101 1100111101 101  and P16

X  0111110101 1100111101 101mod 16  1101 14

18 Exercises

Using one of the considered techniques, calculate:

1) 65536 (mod 23 ) 0?

2) 65536 (mod 23 1) 2?

3) (1010101010101010101) (mod 23 1)  1?

4) with technique 3) Arithmetic calculations in RNS

Arithmetic calculations on moduli

A1

A2 summator/multiplier (mod p ) S1 1 A1 Converter of Ag Converter of

A1 A2 positional modular

A2 numbers S2 representation S summator/multiplier (mod p2)

to Ag to

modular positional Ag representation A number 1 (1) A2 (2) Sm summator/multiplier (mod pm)

Ag

20 Arithmetic calculations in RNS Standard approach of arithmetic calculations in RNS includes

1) arithmetic calculations (A·B=R, A+B=S, and etc., where A and B vary from 0 to P-1); 2) modulus function calculation (R (mod P), S (mod P), and etc.)

Example. A·B=R (mod 7), hence A and B vary from 0 to 6. Lets A=5 and B=6.

1) 56  30

2) 3010(mod 7)  (11110)2 (mod 7)   ((011)  (110)) (mod 7)  (1001)(mod 7)   ((001)  (001)) (mod 7)  21  (010)2 (mod 7)  210 (mod 7) Arithmetic calculations in RNS

What is about P  2 300 ?

1) A B  R  2600

2) R (mod 2300) or Montgomery and “a-la Montgomery” multiplication:

Example. (56) (mod 7)  2 0 2 1  ((101)2 (110)2 )(mod 7)  ((2  2 )(2  2 ))(mod 7)   24 (mod 7)  23 (mod 7)  22 (mod 7)  21(mod 7)  (2 1 4  2)(mod 7)   9(mod 7)  (1001)(mod 7)  23 (mod 7)  20 (mod 7)  (11)(mod 7)

22 Exercises

1) How many rows and columns in the truth table of system of Boolean functions, which represents A + B = R (mod 15)?

2) How many rows and columns in the truth table of system of Boolean functions, which represents A * B = R (mod 17)? Backward conversion

S  S1 Y1  S2 Y2 ... Sm Ym  r  P A 1 A2 summator/multiplier (mod p ) S1 1 A1 Converter of Ag Converter of

A1 A2 positional modular

A2 numbers S2 representation S summator/multiplier (mod p2)

to Ag to

modular positional Ag representation A number 1 (1) A2 (2) Sm summator/multiplier (mod pm)

Ag

24 Backward conversion

S  S1 Y1  S2 Y2 ... Sm Ym  r  P Y  P  i  1 (mod p ) Y  k i i  p  i pi  i 

1) multiplication by a big number;

2) big numbers summation;

3) comparison

Example.

A B in RNS with moduli set P {p1, p2 , p3} {31,32,33}

R  s1 16864  s2 31713 s3 16896  r 32736 25 Exercises

Multiply 129 * 103 in RNS with moduli set {11, 13, 15, 16}? Electronic Design Automation (EDA) tools

• Synopsys – executes X mod P;

• Xilinx (ISE, Vivado) – implementation IP- blocks;

• LeonardoSpectrum (Mentor Graphics) – allows to use custom libraries;

• and etc. 27 Special sets of moduli applied in RNS

28 Five moduli set

739 In order to calculate A  B , where A , B  2 , the average bit-range of 5 moduli sets is 300 bits, i.e.

300 p1p2p3p4p52

29 Moduli set for A·B, where A and B are 739 bits

P = {1021 1019 1013 1009 997 991 983 977 971 967 961 953 947 941 937 929 919 911 907 887 883 881 877 863 859 857 853 841 839 829 827 823 821 811 809 797 787 773 769 761 757 751 743 739 733 729 727 719 709 701 691 683 677 673 661 659 653 647 643 641 631 625 619 617 613 607 601 599 593 587 577 571 569 563 557 547 541 529 523 521 512 509 503 499 491 487 479 467 463 461 457 449 443 439 433 431 421 419 409 401 397 389 383 379 373 367 361 359 353 349 347 343 337 331 317 313 311 307 293 289 283 281 277 271 269 263 257 251 241 239 233 229 227 223 211 199 197 193 191 181 179 173 169 167 163 157 151 149 139 137 131 127 121 113 109 107 103 101 97 89 83 79 73 71 67 61 59 53 47 43 41 37}

P 172 P21478

30 Features and problems It is assumed, that: – the main feature is the high speed processing (it is achieved with hundreds bits numbers); – independence of calculation under each modulo; – flexibility of layout; – small power consumption; – reliability

Problems: – unknown an efficient approach of hardware realization for an arbitrary modulo P

– no IP-blocks and no hardware libraries for RNS system realization;

– slow speed converters to/from RNS for non special sets of moduli

31 RNS literature (in English) Residue Number Systems: Digital arithmetic Algorithms and Architectures Morgan Kaufmann Publishers, 2004 Kluwer Academic Publishers, 2002

Residue Number Systems: Theory and Implementation Imperial College Press, 2007

Finite precision number Residue Number System systems and arithmetic Bookvika Publishing, 2012 Cambridge University Press, 2010

32