Residue Number System
Total Page:16
File Type:pdf, Size:1020Kb
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 arithmetic (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 greatest common divisor for a couple p i , p j equals ‘1’. yx1mod p1 yx2 mod p2 Then the system ... yxn mod pn has a simultaneous solution which is unique modulo 5 Example for the Theorem P p1 p2 p3 579 315 We can express an arbitrary number definitely in the scope from 0 to 314 A1000mod 5 Let’s A 100 , hence A1002mod 7 A1001mod 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 579 315 AB10013S 1) A0,2,1 B3,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 3126 8 225 5 280 r 1315, then r 6 S 3126 8 225 5 280 6315 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 SS1Y1S2Y2S3Y3rP Ai (mod pi ) A pi pi Yi 0A314 1 (mod pi ) pi P B {B1, B2 , B3} Y k i p i B i Bi (mod pi ) B pi pi 0B314 i 1,2,3 ki 1, pi r 0,1,2,... 12 X (mod P) hardware calculations in RNS X mod P A1 A2 S1 summator/multiplier (mod p1) 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 , x1,...,x1 P p , p 1,..., p1 P 2 1 2 1 P 2 14 Pipelining approach of X (mod P) calculations 510 mod77QAmod7 6 5 4 3 2 1 0 72 q672 q572 q472 q372 q272 q172 q0Amod 7 Amod 7 510 mod 77QA726723Amod 76 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 2n3 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) 110110101110110101mod710001mod7 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 X2 x12 x22 x32 x4...modP 0 1 2 3 2 x1modP2 x2modP2 x3modP2 x4modP... x1,x2,x3,x4,...{0,1} Example. X (mod P), where X x 1 , x 2 ,..., x 10 and P23 0 1 2 3 4 5 6 7 8 9 X 2 x12 x22 x32 x42 x52 x62 x72 x82 x92 x10 mod23 x1mod232x2mod234x3mod238x4mod2316x5mod23 9x6mod2318x7mod2313x8mod233x9mod236x10mod23 – If 0S23 X mod 23X – If 46S69 X mod 23X 2P – If 23S46 X mod 23X P – If 69S92 X mod 23X 3P Let’s X 1023 10 (mod 23 ) 1111111111 2 mod 23 17 2^n (mod P) calculation in hardware 4) X (mod P), where P2n n n X mod 2 x , x 1,..., xn, xn1,..., x1 mod 2 n x ,x ,x ,x ,...{0,1} xn, xn1,..., x1 mod 2 1 2 3 4 Example. X (mod P), where X 0111110101 1100111101 101 and P16 X 0111110101 1100111101 101mod 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 S1 summator/multiplier (mod p1) 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) 56 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. (56) (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) (11)(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 A1 A2 S1 summator/multiplier (mod p1) 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 1) multiplication by a big number; 2) big numbers summation; 3) comparison Yi 1 (mod pi ) Example. pi P Y k i p i A B in RNS with moduli set P {p1, p2 , p3} {31,32 i ,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.