Computer Architecture 10. Residue Number Systems

Similar documents
Residue Number Systems Ivor Page 1

Chinese Remainder Algorithms. Çetin Kaya Koç Spring / 22

Chinese Remainder Theorem

GENERALIZED ARYABHATA REMAINDER THEOREM

International Association of Scientific Innovation and Research (IASIR) (An Association Unifying the Sciences, Engineering, and Applied Research)

Discrete Structures Lecture Solving Congruences. mathematician of the eighteenth century). Also, the equation gggggg(aa, bb) =

COMP239: Mathematics for Computer Science II. Prof. Chadi Assi EV7.635

Notes on Systems of Linear Congruences

Chapter 9 Mathematics of Cryptography Part III: Primes and Related Congruence Equations

Number Theory. CSS322: Security and Cryptography. Sirindhorn International Institute of Technology Thammasat University CSS322. Number Theory.

Complement Arithmetic

MATH 2112/CSCI 2112, Discrete Structures I Winter 2007 Toby Kenney Homework Sheet 5 Hints & Model Solutions

2.3 In modular arithmetic, all arithmetic operations are performed modulo some integer.

3 The fundamentals: Algorithms, the integers, and matrices

Part I, Number Systems. CS131 Mathematics for Computer Scientists II Note 1 INTEGERS

Chapter 1: Solutions to Exercises

MATH 433 Applied Algebra Lecture 4: Modular arithmetic (continued). Linear congruences.

CHAPTER 3. Congruences. Congruence: definitions and properties

4.4 Solving Congruences using Inverses

a the relation arb is defined if and only if = 2 k, k

4 Number Theory and Cryptography

LECTURE 4: CHINESE REMAINDER THEOREM AND MULTIPLICATIVE FUNCTIONS

KEYWORDS: Multiple Valued Logic (MVL), Residue Number System (RNS), Quinary Logic (Q uin), Quinary Full Adder, QFA, Quinary Half Adder, QHA.

Residue Number Systems. Alternative number representations. TSTE 8 Digital Arithmetic Seminar 2. Residue Number Systems.

International Journal of Advanced Research in Computer Science and Software Engineering

Relations. Binary Relation. Let A and B be sets. A (binary) relation from A to B is a subset of A B. Notation. Let R A B be a relation from A to B.

cse 311: foundations of computing Fall 2015 Lecture 11: Modular arithmetic and applications

Conversions between Decimal and Binary

Number Theory. Modular Arithmetic

Optimization of new Chinese Remainder theorems using special moduli sets

Frequency Domain Finite Field Arithmetic for Elliptic Curve Cryptography

MATH Dr. Halimah Alshehri Dr. Halimah Alshehri

ax b mod m. has a solution if and only if d b. In this case, there is one solution, call it x 0, to the equation and there are d solutions x m d

MTH 346: The Chinese Remainder Theorem

The Chinese Remainder Theorem

Numbers. Çetin Kaya Koç Winter / 18

M381 Number Theory 2004 Page 1

cse 311: foundations of computing Fall 2015 Lecture 12: Primes, GCD, applications

MATH 3240Q Introduction to Number Theory Homework 4

Finite Fields. SOLUTIONS Network Coding - Prof. Frank H.P. Fitzek

CHAPTER 6. Prime Numbers. Definition and Fundamental Results

CSE 20 DISCRETE MATH. Winter

Introduction to Information Security

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

SEVENTH EDITION and EXPANDED SEVENTH EDITION

Lecture notes: Algorithms for integers, polynomials (Thorsten Theobald)

CMPUT 403: Number Theory

Algebra. Modular arithmetic can be handled mathematically by introducing a congruence relation on the integers described in the above example.

Number Theory Proof Portfolio

A field F is a set of numbers that includes the two numbers 0 and 1 and satisfies the properties:

Basic elements of number theory

Basic elements of number theory

Math From Scratch Lesson 20: The Chinese Remainder Theorem

CSE 20: Discrete Mathematics

Math 109 HW 9 Solutions

A Guide to Arithmetic

2x 1 7. A linear congruence in modular arithmetic is an equation of the form. Why is the solution a set of integers rather than a unique integer?

Lecture 2: Number Representations (2)

Integers and Division

ICS141: Discrete Mathematics for Computer Science I

An integer p is prime if p > 1 and p has exactly two positive divisors, 1 and p.

Exercise Sheet Cryptography 1, 2011

1 Overview and revision

The Chinese Remainder Theorem

NORTHWESTERN UNIVERSITY Tuesday, Oct 6th, 2015 ANSWERS FALL 2015 NU PUTNAM SELECTION TEST

PUTNAM TRAINING NUMBER THEORY. Exercises 1. Show that the sum of two consecutive primes is never twice a prime.

Outline. Some Review: Divisors. Common Divisors. Primes and Factors. b divides a (or b is a divisor of a) if a = mb for some m

Outline. AIT 682: Network and Systems Security. GCD and Euclid s Algorithm Modulo Arithmetic Modular Exponentiation Discrete Logarithms

8 Primes and Modular Arithmetic

CHAPTER 2 NUMBER SYSTEMS

Wilson s Theorem and Fermat s Little Theorem

ENGIN 112 Intro to Electrical and Computer Engineering

LECTURE NOTES IN CRYPTOGRAPHY

3.2 Solving linear congruences. v3

Lecture 2. The Euclidean Algorithm and Numbers in Other Bases

COMPUTER ARITHMETIC. 13/05/2010 cryptography - math background pp. 1 / 162

Algorithms CMSC Basic algorithms in Number Theory: Euclid s algorithm and multiplicative inverse

CIS 6930/4930 Computer and Network Security. Topic 5.1 Basic Number Theory -- Foundation of Public Key Cryptography

Elementary Number Theory. Franz Luef

Numbering Systems. Contents: Binary & Decimal. Converting From: B D, D B. Arithmetic operation on Binary.

Know the Well-ordering principle: Any set of positive integers which has at least one element contains a smallest element.

Solutions to Problem Set 3 - Fall 2008 Due Tuesday, Sep. 30 at 1:00

Algorithms (II) Yu Yu. Shanghai Jiaotong University

CPSC 467b: Cryptography and Computer Security

The security of RSA (part 1) The security of RSA (part 1)

Course 2BA1: Trinity 2006 Section 9: Introduction to Number Theory and Cryptography

This is a recursive algorithm. The procedure is guaranteed to terminate, since the second argument decreases each time.

The Chinese Remainder Theorem

Lecture 7 Number Theory Euiseong Seo

CPSC 467: Cryptography and Computer Security

Homework #2 solutions Due: June 15, 2012

Answers (1) A) 36 = - - = Now, we can divide the numbers as shown below. For example : 4 = 2, 2 4 = -2, -2-4 = -2, 2-4 = 2.

MATH 4400 SOLUTIONS TO SOME EXERCISES. 1. Chapter 1

Volume 3, No. 1, January 2012 Journal of Global Research in Computer Science RESEARCH PAPER Available Online at

10 Problem 1. The following assertions may be true or false, depending on the choice of the integers a, b 0. a "

Course MA2C02, Hilary Term 2013 Section 9: Introduction to Number Theory and Cryptography

Mathematics for Cryptography

Lecture Notes. Advanced Discrete Structures COT S

Number Theory Alex X. Liu & Haipeng Dai

A High-Speed Realization of Chinese Remainder Theorem

Transcription:

Computer Architecture 10 Residue Number Systems Ma d e wi t h Op e n Of f i c e. o r g 1

A Puzzle What number has the reminders 2, 3 and 2 when divided by the numbers 7, 5 and 3? x mod 7 = 2 x mod 5 = 3 x mod 3 = 2 x =??? Chinese mathematician Sun Tzu - third-century AD (not the famous military strategist Sun Tzu) Ma d e wi t h Op e n Of f i c e. o r g 2

Chinese Remainder Theorem There exists an integer x solving the system of equations: x mod m 1 = x 1... x mod m n = x n where numbers m 1...m n are relatively prime. Ma d e wi t h Op e n Of f i c e. o r g 3

Residue Representation Convert X-value into RNS representation: find a set of numbers (i.e. moduli), which are all relatively prime to each other (greatest common divisor is 1): m k-1, m k-2,... m 1, m 0 m k-1 > m k-2 >... > m 1 > m 0 e.g. 7, 5, 3 compute a set of residues with respect to moduli: x k-1, x k-2,... x 1, x 0 where x i = X mod m i = <x> mi e.g. (X=12) 5, 2, 0 residue list is treated as a k-digit RNS number: X = (x k-1 x k-2... x 1 x 0 ) RNS(mk-1 m k-2... m 1 m 0 ) e.g. (5 2 0) RNS(7 5 3) Ma d e wi t h Op e n Of f i c e. o r g 4

RNS Examples Assume RNS(8 7 5 3) 123 = (3 4 3 0) RNS 5 = (5 5 0 2) RNS 0 = (0 0 0 0) RNS 123 moduli residues 5 moduli residues 0 moduli residues 8 7 5 3 3 4 3 0 8 7 5 3 5 5 0 2 8 7 5 3 0 0 0 0 Ma d e wi t h Op e n Of f i c e. o r g 5

Congruency Relation Two integers a and b are said to be congruent modulo n, if their difference (a b) is an integer multiple of n. a b (mod n) X x (mod m) x = X km e.g. 13 3 (mod 5) 3 = 13 2*5 13 8 (mod 5) 8 = 13 1*5 13 13 (mod 5) 13 = 13 0*5 Ma d e wi t h Op e n Of f i c e. o r g 6

Operations on Congruent Numbers For congruencies A i a i (with modulus n): A i a i modulus = 5 A i A j a i a j A1 a1 A2 a2 13 3 9 4 A i a i A s a s i i v A i v a i sum 22 7 (2) diff. 4-1 (4) mult. 117 12 (2) (except division) Ma d e wi t h Op e n Of f i c e. o r g 7

Operations on RNS Numbers X = (x k-1 x k-2... x 1 x 0 ) RNS(m RNS(mk-1 m k-2... m 1 m 0 ) X (x k-1 x k-2... x 1 x 0 ) X oper Y (x k-1 oper y k-1... x 1 oper y 1 x 0 oper y 0 ) Ma d e wi t h Op e n Of f i c e. o r g 8

Operations on RNS Numbers Operation (+,,*,^),*,^) on RNS numbers is performed on all corresponding residues, totally in parallel Operation on residues at i-positions is performed modulo m i The resulting RNS number uniquely identifies the result of operation (within the dynamic range) Ma d e wi t h Op e n Of f i c e. o r g 9

Example of RNS Operations All operations are performed in parallel on corresponding residues (modulo m i ) RNS (8 7 5 3) A 21 (5 0 1 0) B 8 (0 1 3 2) A+B A B 29 (5 1 4 2) 13 (5 6 3 1) A*B 168 (0 0 3 0) A^2 441 (1 0 1 0) B^3 512 (0 1 2 2) 3*A 63 (7 0 3 0) Ma d e wi t h Op e n Of f i c e. o r g 10

Representation Range RNS numbers may uniquely identify M numbers, where M = (m k-1 *...* m 1 * m 0 ) = m i e.g. RNS(8 7 5 3) 8*7*5*3 = 840 unique combinations M is called a dynamic range for a given RNS The range can cover any interval of M-consecutive numbers e.g. for M=840 0..839, 420..419, etc. (0 0 0 0) RNS = 0 or 840 or 1680... (7 6 4 2) RNS = 839 or -1 Ma d e wi t h Op e n Of f i c e. o r g 11

Negative RNS Numbers Negative numbers can be represented using a complement system (with M-complement) ½x 0 M-1 x = M x x ½M 0 ½M 1 Residues of x are equal to residues of M x ( x) mod m i = (M x) mod m i Residues of x x are m i -complements of x x = (x k-1... x 0 ) x = (m k-1 x k-1... m 0 x 0 ) Ma d e wi t h Op e n Of f i c e. o r g 12

Negative RNS - Examples Take RNS representation of positive number x = 21 = (5 0 1 0) RNS ( RNS(8 7 5 3) ) Calculate m i -complements of all residues (8 5 7 0 5 1 3 0) RNS = (3 0 4 0) RNS = 21 0 = (0 0 0 0) RNS 0 = (0 0 0 0) RNS 1 = (1 1 1 1) RNS 1 = (7 6 4 2) RNS 419 = (3 6 4 2) RNS 419 = (5 1 1 1) RNS Ma d e wi t h Op e n Of f i c e. o r g 13

Difficult RNS Operations Speed of addition and multiplication in RNS is counterbalanced by difficulty of other important arithmetical operations: division sign test magnitude comparison overflow detection Applications of RNS are limited to fields with predominant use of addition and multiplication within the known range (FFT, DSP) Ma d e wi t h Op e n Of f i c e. o r g 14

Efficiency of RNS Representation Residues are kept internally as separate binary numbers (bit-fields) e.g. 5 = (5 5 0 2) RNS(8 7 5 3) 10110100010 (11-bits) How many bits are needed for M-different representations (in NBC)? log 2 M log 2 840 = 9.714 bits Repr. efficiency is the ratio possible RNS representations related to NBC with the same bit-field length n Eff = M(n) / 2 n 840/2048 = 41% Ma d e wi t h Op e n Of f i c e. o r g 15

RNS Arithmetical Unit Operand A Operand B RNS(8 7 5 3) mod 8 mod 7 mod 5 mod 3 Result: A oper B Ma d e wi t h Op e n Of f i c e. o r g 16

Fast Arithmetics with LUT Small size of operands permits the lookup- table implementation of residue arithm.units oper n-bits n-bits n-bits A B Results of addition 2 n x 2 n Results of subtract. 2 n x 2 n Result Results of mult. 2 n x 2 n... e.g. 4-operations on 4-bits operands 4 * (2 4 * 2 4 ) = 4*256 = 1024 4bit words (512B) Ma d e wi t h Op e n Of f i c e. o r g 17

Choosing the RNS Moduli Moduli set (m k-1...m 0 ) affects both representation efficiency complexity of arithmetical units For the chosen range M: find the moduli: primes (or semi-primes) with product M roughly equal bit-size of moduli should be favored (not an easy task) moduli 2 n an 2 n -1 simplify arithmetic units e.g. M=65535 RNS(13 11 9 7 5 2), m i =90090, 20 bits :-( Ma d e wi t h Op e n Of f i c e. o r g 18

Choosing the Moduli - Example M = 100 000 (17 bits in NBC) Select consecutive primes RNS(17 13 11 7 5 3 2) =510510, 23 bits Remove primes to scale the result RNS(17 13 11 7 3 2) =102102, 20 bits Combine primes equalize moduli length RNS(26 21 17 11) =102102, 19 bits Use powers of small primes RNS(15 13 11 2 3 7) =102102, 18 bits, max 4-bit field Favor 2 n an 2 n -1moduli RNS(2 5 2 5-1 2 4-1 2 3-1) =104160, 17 bits, eff 100% Ma d e wi t h Op e n Of f i c e. o r g 19

Conversion to RNS Calculate the residues m k-1...m 0 : From decimal requires division From binary requires addition of precomputed values from lookup table and simple division b k-1...b 0 mod m i = (2 k-1 b k-1 mod m i +... + 2 0 b 0 mod m 0 ) mod m i NBC number Precomputed for given RNS Precomputed for given RNS i-th residue of RNS representation requires division of relatively small number - can be implemented with LUT Ma d e wi t h Op e n Of f i c e. o r g 20

Binary to RNS - Example 164: 10100100 NBC RNS(8 7 5 3)? 10100100 NBC mod 8 = 100 NBC = 4 (trivial) 10100100 NBC mod 7 = (2+4+4) mod 7 = 3 10100100 NBC mod 5 = (3+2+4) mod 5 = 4 10100100 NBC mod 3 = (2+2+1) mod 3 = 2 164: 10100100 NBC (4 3 4 2) Lookup table with precomputed partial residues for RNS with moduli (8 7 5 3) Final division uses small numbers and can also be implemented with LUT 2 (4 3 4 2) k mod... k 2 k 8 7 5 3 0 1 1 1 1 1 1 2 2 2 2 2 2 4 4 4 4 1 3 8 0 1 3 2 4 16 0 2 1 1 5 32 0 4 2 2 6 64 0 1 4 1 7 128 0 2 3 2 Ma d e wi t h Op e n Of f i c e. o r g 21

Conversion from RNS Any RNS has associated mixed-radix system (MRS) RNS(m k-1... m 0 ) = MRS(w k-1... w 0 ) Conversion from RNS to any positional system is possible if the weights w k-1...w 0 are known Any RNS number (x k-1... x 0 ) can be expressed as x k-1 (1 0... 0) + x k-2 (0 1... 0) +...+ x 1 (0... 1 0) +x 0 (0... 0 1) The weights w k-1...w 0 are equal to RNS numbers (1 0... 0), (0 1... 0)... (0... 1 0), (0... 0 1) Ma d e wi t h Op e n Of f i c e. o r g 22

Conversion from RNS Values of weights for the given RNS can be precomputed and used as constants w k-1 = (1 0... 0) RNS,... w 0 = (0... 0 1) RNS All conversions are done modulo M. e.g. for RNS(8 7 5 3) (1 0 0 0) RNS = 105 (0 1 0 0) RNS = 120 (0 0 1 0) RNS = 336 (0 0 0 1) RNS = 280 (3 2 4 2) RNS = (3*105+2*120+4*336+2*280) mod 840 = (2459) mod 840 = 779 Ma d e wi t h Op e n Of f i c e. o r g 23

Conversion from RNS How to calculate (1 0... 0) RNS...(0... 0 1) RNS e.g. (1 0... 0) RNS means the number that is dividable by moduli m k-2...m 0 RNS? (1 0... 0) RNS = m k-2 *...*m 0 * n (n=1,2...,m k-1-1) e.g (1 0 0 0) RNS(8 7 5 3) = 7*5*3*n (m k-2 *...*m 0 (m k-2 * n) mod m k-1 = 1 n=? (7*5*3*n) mod 8 = 1 n=1 (1 0... 0) RNS = 7*5*3 = 105 Selection of n is easy due to very limited range [1,...,m-1] Ma d e wi t h Op e n Of f i c e. o r g 24