a painless guide to crc error detection algorithms.docx
《a painless guide to crc error detection algorithms.docx》由会员分享,可在线阅读,更多相关《a painless guide to crc error detection algorithms.docx(33页珍藏版)》请在冰豆网上搜索。
apainlessguidetocrcerrordetectionalgorithms
目录
原文网址http:
//www.repairfaq.org/filipg/LINK/F_crc_v3.html2
2.Introduction:
ErrorDetection2
3.TheNeedForComplexity4
4.TheBasicIdeaBehindCRCAlgorithms5
5.Polynomial(多项式)Arithmetic7
Chapter6BinaryArithmeticwithNoCarries10
Chapter7AFullyWorkedExample13
Chapter8ChoosingAPoly16
Chapter9)AStraightforwardCRCImplementation17
Chapter10ATable-DrivenImplementation19
Chapter11ASlightlyMangled(损坏的)Table-DrivenImplementation23
Chapter12)"Reflected"Table-DrivenImplementations27
Chapter13)"Reversed"Polys29
Chapter14)InitialandFinalValues30
Chapter15)DefiningAlgorithmsAbsolutely30
Chapter16)AParameterizedModelForCRCAlgorithms31
Chapter17)ACatalogofParameterSetsforStandards33
Chapter18)AnImplementationoftheModelAlgorithm35
Chapter19)RollYourOwnTable-DrivenImplementation35
Chapter20)GeneratingALookupTable36
Chapter21)Summary37
Chapter22)Corrections37
Chapter23)Glossary37
Chapter24)References38
Chapter25)ReferencesIHaveDetectedButHaven'tYetSighted39
原文网址http:
//www.repairfaq.org/filipg/LINK/F_crc_v3.html
2.Introduction:
ErrorDetection
Theaimofanerrordetectiontechniqueistoenablethereceiverofamessagetransmittedthroughanoisy(error-introducing)channeltodeterminewhetherthemessagehasbeencorrupted.Todothis,thetransmitterconstructsavalue(calledachecksum)thatisafunctionofthemessage,andappendsittothemessage.Thereceivercanthenusethesamefunctiontocalculatethechecksumofthereceivedmessageandcompareitwiththeappendedchecksumtoseeifthemessagewascorrectlyreceived.Forexample,ifwechoseachecksumfunctionwhichwassimplythesumofthebytesinthemessagemod256(i.e.modulo256),thenitmightgosomethingasfollows.Allnumbersareindecimal.
Message:
6234
Messagewithchecksum:
623433
Messageaftertransmission:
627433
Intheabove,thesecondbyteofthemessagewascorrupted(变坏)from23to27bythecommunicationschannel.However,thereceivercandetectthisbycomparingthetransmittedchecksum(33)withthecomputerchecksumof37(6+27+4).Ifthechecksumitselfiscorrupted,acorrectlytransmittedmessagemightbeincorrectlyidentifiedasacorruptedone.However,thisisasafe-sidefailure.Adangerous-sidefailureoccurswherethemessageand/orchecksumiscorruptedinamannerthatresultsinatransmissionthatisinternallyconsistent.Unfortunately,thispossibilityiscompletelyunavoidableandthebestthatcanbedoneistominimizeitsprobabilitybyincreasingtheamountofinformationinthechecksum(e.g.wideningthechecksumfromonebytetotwobytes).
Othererrordetectiontechniquesexistthatinvolveperformingcomplextransformationsonthemessagetoinjectitwithredundant(冗长的)information.However,thisdocumentaddressesonlyCRCalgorithms,whichfallintotheclassoferrordetectionalgorithmsthatleavethedataintact(原封不动的)andappendachecksumontheend.i.e.:
3.TheNeedForComplexity
Inthechecksumexampleintheprevioussection,wesawhowacorruptedmessagewasdetectedusingachecksumalgorithmthatsimplysumsthebytesinthemessagemod256:
Message:
6234
Messagewithchecksum:
623433
Messageaftertransmission:
627433
Aproblemwiththisalgorithmisthatitistoosimple.Ifanumberofrandomcorruptionsoccur,thereisa1in256chancethattheywillnotbedetected.Forexample:
Message:
6234
Messagewithchecksum:
623433
Messageaftertransmission:
820533
Tostrengthenthechecksum,wecouldchangefroman8-bitregistertoa16-bitregister(i.e.sumthebytesmod65536insteadofmod256)soastoapparentlyreducetheprobabilityoffailurefrom1/256to1/65536(why?
因为结果有65536种可能性,不变只占其中一种).Whilebasicallyagoodidea,itfailsinthiscasebecausetheformulausedisnotsufficiently"random";withasimplesummingformula,eachincomingbyteaffectsroughlyonlyonebyteofthesummingregisternomatterhowwideitis.Forexample,inthesecondexampleabove,thesummingregistercouldbeaMegabyte(兆字节)wide,andtheerrorwouldstillgoundetected.Thisproblemcanonlybesolvedbyreplacingthesimplesummingformulawithamoresophisticatedformulathatcauseseachincomingbytetohaveaneffectontheentirechecksumregister.
Thus,weseethatatleasttwoaspectsarerequiredtoformastrongchecksumfunction:
WIDTH
Aregisterwidthwideenoughtoprovidealowa-prioriprobabilityoffailure(e.g.32-bitsgivesa1/2^32chanceoffailure).
CHAOS
Aformulathatgiveseachinputbytethepotentialtochangeanynumberofbitsintheregister.
Note:
Theterm"checksum"waspresumablyusedtodescribeearlysummingformulas,buthasnowtakenonamoregeneralmeaningencompassingmoresophisticatedalgorithmssuchastheCRCones.TheCRCalgorithmstobedescribedsatisfythesecondconditionverywell,andcanbeconfiguredtooperatewithavarietyofchecksumwidths.
4.TheBasicIdeaBehindCRCAlgorithms
Wheremightwegoinoursearchforamorecomplexfunctionthansumming?
Allsortsofschemesspringtomind.Wecouldconstruct(建造)tablesusingthedigitsofpi,orhash(散列)eachincomingbytewithallthebytesintheregister.Wecouldevenkeepalargetelephonebookon-line,anduseeachincomingbytecombinedwiththeregisterbytestoindexanewphonenumberwhichwouldbethenextregistervalue.Thepossibilitiesarelimitless.
However,wedonotneedtogosofar;thenextarithmeticstepsuffices(足够).Whileadditionisclearlynotstrongenoughtoformaneffectivechecksum,itturnsoutthatdivisionis,solongasthedivisorisaboutaswideasthechecksumregister.
ThebasicideaofCRCalgorithmsissimplytotreatthemessageasanenormousbinarynumber,todivideitbyanotherfixedbinarynumber,andtomaketheremainderfromthisdivisionthechecksum.Uponreceiptofthemessage,thereceivercanperformthesamedivisionandcomparetheremainderwiththe"checksum"(transmittedremainder).
Example:
Supposethethemessageconsistedofthetwobytes(6,23)asinthepreviousexample.Thesecanbeconsideredtobethehexadecimalnumber0617whichcanbeconsideredtobethebinarynumber0000-0110-0001-0111.Supposethatweuseachecksumregisterone-bytewideanduseaconstantdivisorof1001,thenthechecksumistheremainderafter0000-0110-0001-0111isdividedby1001.Whileinthiscase,thiscalculationcouldobviouslybeperformedusingcommongardenvariety32-bitregisters,inthegeneralcasethisismessy.Soinstead,we'lldothedivisionusinggood-'ollongdivisionwhichyoulearnedinschool(remember?
).Exceptthistime,it'sinbinary:
...0000010101101=00AD=173=QUOTIENT
____-___-___-___-
9=1001)0000011000010111=0617=1559=DIVIDEND
DIVISOR0000.,,....,.,,,
----.,,....,.,,,
0000,,....,.,,,
0000,,....,.,,,
----,,....,.,,,
0001,....,.,,,
0000,....,.,,,
----,....,.,,,
0011....,.,,,
0000....,.,,,
----....,.,,,
0110...,.,,,
0000...,.,,,
----...,.,,,
1100..,.,,,
1001..,.,,,
====..,.,,,
0110.,.,,,
0000.,.,,,
----.,.,,,
1100,.,,,
1001,.,,,
====,.,,,
0111.,,,
0000.,,,
----.,,,
1110,,,
1001,,,
====,,,
1011,,
1001,,
====,,
0101,
0000,
----
1011
1001
====
0010=02=2=REMAINDER
Indecimalthisis"1559dividedby9is173witharemainderof2".
Althoughtheeffectofeachbitoftheinputmessageonthequotientisnotallthatsignificant,the4-bitremaindergetskickedaboutquitealotduringthecalculation,andifmorebyteswereaddedtothemessage(dividend)it'svaluecouldchangeradically(完全的)againveryquickly.Thisiswhydivisionworkswhereadditiondoesn't.
Incaseyou'rewondering,usingthis4-bitchecksumthetransmittedmessagewouldlooklikethis(inhexadecimal):
06172(wherethe0617isthemessageandthe2isthechecksum).Thereceiverwoulddivide0617by9andseewhethertheremainderwas2.
5.Polynomial(多项式)Arithmetic
WhilethedivisionschemedescribedintheprevioussectionisveryverysimilartothechecksummingschemescalledCRCschemes,theCRCschemesareinfactabitweirder,andweneedtodelve(钻研)intosomestrangenumbersystemstounderstandthem.
ThewordyouwillhearallthetimewhendealingwithCRCalgorithmsistheword"polynomial".AgivenCRCalgorithmwillbesaidtobeusingaparticularpolynomial,andCRCalgorithmsingeneralaresaidtobeoperatingusingpolynomialarithmetic.Whatdoesthismean?
Insteadofthedivisor,dividend(message),quotient(商),andremainder(asdescribedintheprevioussection)beingviewedaspositiveintegers,theyareviewedaspolynomialswithbinarycoefficients.Thisisdonebytreatingeachnumberasabit-stringwhosebitsarethecoefficientsofapolynomial.Forexample,theordinarynumber23(decimal)is17(hex)and10111binaryandsoitcorrespondstothepolynomial:
1*x^4+0*x^3+1*x^2+1*x^1+1*x^0
or,moresimply:
x^4+x^2+x^1+x^0
Usingthistechnique,themessage,andthedivisorcanberepresentedaspolynomialsandwecandoallourarithmeticjustasbefore,exceptthatnowit'sallcluttered(弄乱)upwithXs.Forexample,supposewewantedtomultiply1101by1011.Wecandothissimplybymultiplyingthepolynomials:
(x^3+x^2+x^0)(x^3+x^1+x^0)
=(x^6+x^4+x^3
+x^5+x^3+x^2
+x^3+x^1+x^0)=x^6+x^5+x^4+3*x^3+x^2+x^1+x^0
Atthispoint,togettherightanswer,wehavetopretendthatxis2andpropagatebinarycarriesfromthe3*x^3yielding:
x^7+x^3+x^2+x^1+x^0
It'sjustlikeordinaryarithmeticexceptthatthebaseisabs