Modular Arithmetic

模運算基礎(chǔ)知識

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/what-is-modular-arithmetic

An Introduction to Modular Math

When we divide two integers we will have an equation that looks like the following:

A is the dividend B is the divisor Q is the quotient R is the remainder

Sometimes, we are only interested in what the remainder is when we divide AAAby BBB. For these cases there is an operator called the modulo operator (abbreviated as mod).

Using the same A, B, Q, and R as above, we would have: A mod B=R

We would say this as A modulo B is equal to R. Where B is referred to as the modulus.

Congruence Modulo

You may see an expression like:

A≡B(mod C)

This says that A is congruent to B modulo C.

Equivalent Statements

Before proceeding it’s important to remember the following statements are equivalent

  • A≡B (mod C)

  • A mod C=B mod C

  • C ∣ (A?B)

    (The | symbol means divides, or is a factor of)

  • A=B+K?C (where K is some integer)

This lets us move back and forth between different forms of expressing the same idea.

Modular operation

addition and subtraction

(A + B) mod C = (A mod C + B mod C) mod C

(A - B) mod C = (A mod C - B mod C) mod C

multiplication

(A * B) mod C = (A mod C * B mod C) mod C

exponentiation

A^B mod C = ( (A mod C)^B ) mod C

Modular inverses

What is a modular inverse?

In modular arithmetic we do not have a division operation. However, we do have modular inverses.

  • The modular inverse of A (mod C) is A^-1

  • (A * A^-1) ≡ 1 (mod C) or equivalently (A * A^-1) mod C = 1

  • Only the numbers coprime to C (numbers that share no prime factors with C) have a modular inverse (mod C)

How to find a modular inverse

A naive method of finding a modular inverse for A (mod C) is:

step 1. Calculate A * B mod C for B values 0 through C-1

step 2. The modular inverse of A mod C is the B value that makes A * B mod C = 1

Note that the term B mod C can only have an integer value 0 through C-1, so testing larger values for B is redundant.

The Euclidean Algorithm

Recall that the Greatest Common Divisor (GCD) of two integers A and B is the largest integer that divides both A and B.

The Euclidean Algorithm is a technique for quickly finding the GCD of two integers.

The Algorithm

The Euclidean Algorithm for finding GCD(A,B) is as follows:

  • If A = 0 then GCD(A,B)=B, since the GCD(0,B)=B, and we can stop.

  • If B = 0 then GCD(A,B)=A, since the GCD(A,0)=A, and we can stop.

  • Write A in quotient remainder form (A = B?Q + R)

  • Find GCD(B,R) using the Euclidean Algorithm since GCD(A,B) = GCD(B,R)

求線性同余數(shù)(Using Euclid’s Algorithm)

http://www.maths.manchester.ac.uk/~mdc/MATH10101/2010-11/Notes2010-11/Ch3%20II%20Congruences.pdf

求指數(shù)同余數(shù)(Using Fast modular exponentiation)

https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末沸伏,一起剝皮案震驚了整個濱河市识腿,隨后出現(xiàn)的幾起案子挚赊,更是在濱河造成了極大的恐慌笤妙,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件勺爱,死亡現(xiàn)場離奇詭異披诗,居然都是意外死亡,警方通過查閱死者的電腦和手機端壳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來枪蘑,“玉大人损谦,你說我怎么就攤上這事≡榔模” “怎么了成翩?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長赦役。 經(jīng)常有香客問我,道長栅炒,這世上最難降的妖魔是什么掂摔? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮赢赊,結(jié)果婚禮上乙漓,老公的妹妹穿的比我還像新娘。我一直安慰自己释移,他們只是感情好叭披,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著玩讳,像睡著了一般涩蜘。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上熏纯,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天同诫,我揣著相機與錄音,去河邊找鬼樟澜。 笑死误窖,一個胖子當著我的面吹牛叮盘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播霹俺,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼柔吼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了丙唧?” 一聲冷哼從身側(cè)響起愈魏,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎艇棕,沒想到半個月后蝌戒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡沼琉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年北苟,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片打瘪。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡友鼻,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闺骚,到底是詐尸還是另有隱情彩扔,我是刑警寧澤,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布僻爽,位于F島的核電站虫碉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏胸梆。R本人自食惡果不足惜敦捧,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望碰镜。 院中可真熱鬧兢卵,春花似錦、人聲如沸绪颖。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽柠横。三九已至窃款,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間滓鸠,已是汗流浹背雁乡。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留糜俗,地道東北人踱稍。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓曲饱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親珠月。 傳聞我的和親對象是個殘疾皇子扩淀,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容