Fletcher's checksum

The Fletcher checksum is an algorithm for computing a position-dependent checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs in the late 1970s.[1] The objective of the Fletcher checksum was to provide error-detection properties approaching those of a cyclic redundancy check but with the lower computational effort associated with summation techniques.

The algorithm

Review of simple checksums

As with simpler checksum algorithms, the Fletcher checksum involves dividing the binary data word to be protected from errors into short "blocks" of bits and computing the modular sum of those blocks. (Note that the terminology used in this domain can be confusing. The data to be protected, in its entirety, is referred to as a "word", and the pieces into which it is divided are referred to as "blocks".)

As an example, the data may be a message to be transmitted consisting of 136 characters, each stored as an 8-bit byte, making a data word of 1088 bits in total. A convenient block size would be 8 bits, although this is not required. Similarly, a convenient modulus would be 255, although, again, others could be chosen. So, the simple checksum is computed by adding together all the 8-bit bytes of the message, dividing by 255 and keeping only the remainder. (In practice, the modulo operation is performed during the summation to control the size of the result.) The checksum value is transmitted with the message, increasing its length to 137 bytes, or 1096 bits. The receiver of the message can re-compute the checksum and compare it to the value received to determine whether the message has been altered by the transmission process.

Weaknesses of simple checksums

The first weakness of the simple checksum is that it is insensitive to the order of the blocks (bytes) in the data word (message). If the order is changed, the checksum value will be the same and the change will not be detected. The second weakness is that the universe of checksum values is small, being equal to the chosen modulus. In our example, there are only 255 possible checksum values, so it is easy to see that even random data has about a 0.4% probability of having the same checksum as our message.

The Fletcher checksum

Fletcher addresses both of these weaknesses by computing a second value along with the simple checksum. This is the modular sum of the values taken by the simple checksum as each block of the data word is added to it. The modulus used is the same. So, for each block of the data word, taken in sequence, the block's value is added to the first sum and the new value of the first sum is then added to the second sum. Both sums start with the value zero (or some other known value). At the end of the data word, the modulus operator is applied and the two values are combined to form the Fletcher checksum value.

Sensitivity to the order of blocks is introduced because once a block is added to the first sum, it is then repeatedly added to the second sum along with every block after it. If, for example, two adjacent blocks become exchanged, the one that was originally first will be added to the second sum one fewer times and the one that was originally second will be added to the second sum one more time. The final value of the first sum will be the same, but the second sum will be different, detecting the change to the message.

The universe of possible checksum values is now the square of the value for the simple checksum. In our example, the two sums, each with 255 possible values, result in 65025 possible values for the combined checksum.

Overview of the different algorithm parameters

While there is an infinity of parameters, the original paper only studies the case K=8 (word length) with modulus 255 and 256.

The 16 and 32 bits versions (Fletcher-32 and -64) have been derived from the original case and studied in subsequent specifications or papers.

Fletcher-16

When the data word is divided into 8-bit blocks, as in the example above, two 8-bit sums result and are combined into a 16-bit Fletcher checksum. Usually, the second sum will be multiplied by 256 and added to the simple checksum, effectively stacking the sums side-by-side in a 16-bit word with the simple checksum at the least significant end. This algorithm is then called the Fletcher-16 checksum. The use of the modulus 28  1 = 255 is also generally implied.

Fletcher-32

When the data word is divided into 16-bit blocks, two 16-bit sums result and are combined into a 32-bit Fletcher checksum. Usually, the second sum will be multiplied by 216 and added to the simple checksum, effectively stacking the sums side-by-side in a 32-bit word with the simple checksum at the least significant end. This algorithm is then called the Fletcher-32 checksum. The use of the modulus 216  1 = 65,535 is also generally implied. The rationale for this choice is the same as for Fletcher-16.

Fletcher-64

When the data word is divided into 32-bit blocks, two 32-bit sums result and are combined into a 64-bit Fletcher checksum. Usually, the second sum will be multiplied by 232 and added to the simple checksum, effectively stacking the sums side-by-side in a 64-bit word with the simple checksum at the least significant end. This algorithm is then called the Fletcher-64 checksum. The use of the modulus 232  1 = 4,294,967,295 is also generally implied. The rationale for this choice is the same as for Fletcher-16 and Fletcher-32.

Comparison with the Adler checksum

The Adler-32 checksum is a specialization of the Fletcher-32 checksum devised by Mark Adler. The modulus selected (for both sums) is the prime number 65,521 (65,535 is divisible by 3, 5, 17 and 257). The first sum also begins with the value 1. The selection of a prime modulus results in improved "mixing" (error patterns are detected with more uniform probability, improving the probability that the least detectable patterns will be detected, which tends to dominate overall performance). However, the reduction in size of the universe of possible checksum values acts against this and reduces performance slightly. One study showed that Fletcher-32 outperforms Adler-32 in both performance and in its ability to detect errors. As modulo-65,535 addition is considerably simpler and faster to implement than modulo-65,521 addition, the Fletcher-32 checksum is generally a faster algorithm.[2]

Caution on modulus

A modulus of 255 is used above and in examples below for Fletcher-16, however some real-world implementations use 256. The TCP protocol's alternate checksum has Fletcher-16 with a 256 modulus,[3] as do the checksums of UBX-* messages from a U-blox GPS.[4] Which modulus is used is dependent on the implementation.

Example calculation of the Fletcher-16 checksum

As an example, a Fletcher-16 checksum shall be calculated and verified for a byte stream of 0x01 0x02.

  • C0_initial = 0
  • C1_initial = 0
Byte (B) C0 = (C0prev + B) mod 255 C1 = (C1prev + C0) mod 255 Description
0x01 0x01 0x01 First byte fed in
0x02 0x03 0x04 Second byte fed in

The checksum is therefore 0x0403. It could be transmitted with the byte stream and be verified as such on the receiving end. Another option is to compute in a second step a pair of check bytes, which can be appended to the byte stream so that the resulting stream has a global Fletcher-16 checksum value of 0.

The values of the checkbytes are computed as follows:

  • CB0 = 255 − ((C0 + C1) mod 255),
  • CB1 = 255 − ((C0 + CB0) mod 255),

where C0 and C1 are the result of the last step in the Fletcher-16 computation.

In our case the checksum bytes are CB0 = 0xF8 and CB1 = 0x04. The transmitted byte stream is 0x01 0x02 0xF8 0x04. The receiver runs the checksum on all four bytes and calculates a passing checksum of 0x00 0x00, as illustrated below:

Byte (B) C0 = (C0prev + B) mod 255 C1 = (C1prev + C0) mod 255 Description
0x01 0x01 0x01 First byte fed in
0x02 0x03 0x04 Second byte fed in
CB0 = 0xF8 (0x03 + 0xF8) % 0xFF = 0xFB (0x04 + 0xFB) % 0xFF = 0x00 Checksum calculation – byte 1
CB1 = 0x04 (0xFB + 0x04) % 0xFF = 0x00 (0x00 + 0x00) % 0xFF = 0x00 Checksum calculation – byte 2

Weaknesses

The Fletcher checksum cannot distinguish between blocks of all 0 bits and blocks of all 1 bits. For example, if a 16-bit block in the data word changes from 0x0000 to 0xFFFF, the Fletcher-32 checksum remains the same. This also means a sequence of all 00 bytes has the same checksum as a sequence (of the same size) of all FF bytes.

Implementation

These examples assume two's complement arithmetic, as Fletcher's algorithm will be incorrect on ones' complement machines.

Straightforward

The below is a treatment on how to calculate the checksum including the check bytes; i.e., the final result should equal 0, given properly-calculated check bytes. The code by itself, however, will not calculate the check bytes.

An inefficient but straightforward implementation of a C language function to compute the Fletcher-16 checksum of an array of 8-bit data elements follows:

uint16_t Fletcher16( uint8_t *data, int count )
{
   uint16_t sum1 = 0;
   uint16_t sum2 = 0;
   int index;

   for ( index = 0; index < count; ++index )
   {
      sum1 = (sum1 + data[index]) % 255;
      sum2 = (sum2 + sum1) % 255;
   }

   return (sum2 << 8) | sum1;
}

On lines 3 and 4, the sums are 16-bit variables so that the additions on lines 9 and 10 will not overflow. The modulo operation is applied to the first sum on line 9 and to the second sum on line 10. Here, this is done after each addition, so that at the end of the for loop the sums are always reduced to 8 bits. At the end of the input data, the two sums are combined into the 16-bit Fletcher checksum value and returned by the function on line 13.

Each sum is computed modulo 255 and thus remains less than 0xFF at all times. This implementation will thus never produce the checksum results 0x??FF, 0xFF?? or 0xFFFF (i.e., 511 out of the total 65536 possible 16-bit values are never used). It can produce the checksum result 0x0000, which may not be desirable in some circumstances (e.g. when this value has been reserved to mean "no checksum has been computed").

Check bytes

Example source code for calculating the check bytes, using the above function, is as follows. The check bytes may be appended to the end of the data stream, with the c0 coming before the c1.

uint16_t csum;
uint16_t c0,c1,f0,f1;

csum = Fletcher16(data, length);
f0 = csum & 0xff;
f1 = (csum >> 8) & 0xff;
c0 = 0xff - ((f0 + f1) % 0xff);
c1 = 0xff - ((f0 + c0) % 0xff);

Optimizations

In a 1988 paper, Anastase Nakassis discussed and compared different ways to optimize the algorithm. The most important optimization consists in using larger accumulators and delaying the relatively costly modulo operation for as long as it can be proven that no overflow will occur. Further benefit can be derived from replacing the modulo operator with an equivalent function tailored to this specific case—for instance, a simple compare-and-subtract, since the quotient never exceeds 1.[5]

Here is a C implementation that applies the first but not the second optimization:

#include <stdlib.h>		/* for size_t */
#include <stdint.h>		/* for uint8_t, uint16_t & uint32_t */

uint16_t fletcher16(const uint8_t *data, size_t len) {
	uint32_t c0, c1;

	/*  Found by solving for c1 overflow: */
	/* n > 0 and n * (n+1) / 2 * (2^8-1) < (2^32-1). */
	for (c0 = c1 = 0; len > 0; ) {
		size_t blocklen = len;
		if (blocklen > 5802) {
			blocklen = 5802;
		}
		len -= blocklen;
		do {
			c0 = c0 + *data++;
			c1 = c1 + c0;
		} while (--blocklen);
		c0 = c0 % 255;
		c1 = c1 % 255;
   }
   return (c1 << 8 | c0);
}

uint32_t fletcher32(const uint16_t *data, size_t len) {
	uint32_t c0, c1;
	len = (len + 1) & ~1;      /* Round up len to words */

	/* We similarly solve for n > 0 and n * (n+1) / 2 * (2^16-1) < (2^32-1) here. */
	/* On modern computers, using a 64-bit c0/c1 could allow a group size of 23726746. */
	for (c0 = c1 = 0; len > 0; ) {
		size_t blocklen = len;
		if (blocklen > 360*2) {
			blocklen = 360*2;
		}
		len -= blocklen;
		do {
			c0 = c0 + *data++;
			c1 = c1 + c0;
		} while ((blocklen -= 2));
		c0 = c0 % 65535;
		c1 = c1 % 65535;
	}
	return (c1 << 16 | c0);
}

// A similar routine could be written for fletcher64. The group size would be 92681.

The second optimization is not used because the "never exceeds 1" assumption only applies when the modulo is calculated naively; applying the first optimization would break it. On the other hand, modulo Mersenne numbers like 255 and 65535 is a quick operation on computers anyway, as tricks are available to do them without the costly division operation.[6]

Test vectors

8-bit implementation (16-bit checksum)

"abcde" -> 51440 (0xC8F0)
"abcdef" -> 8279 (0x2057)
"abcdefgh" -> 1575 (0x0627)

16-bit implementation (32-bit checksum), with 8-bit ASCII values of the input word assembled into 16-bit blocks in little-endian order, the word padded with zeros as necessary to the next whole block, using modulus 65535 and with the result presented as the sum-of-sums shifted left by 16 bits (multiplied by 65536) plus the simple sum

"abcde" -> 4031760169 (0xF04FC729)
"abcdef" -> 1448095018 (0x56502D2A)
"abcdefgh" -> 3957429649 (0xEBE19591)

32-bit implementation (64-bit checksum)

"abcde" -> 14467467625952928454 (0xC8C6C527646362C6)
"abcdef" -> 14467579776138987718 (0xC8C72B276463C8C6)
"abcdefgh" -> 3543817411021686982 (0x312E2B28CCCAC8C6)

Bit and byte ordering (endianness / network order)

As with any calculation that divides a binary data word into short blocks and treats the blocks as numbers, any two systems expecting to get the same result should preserve the ordering of bits in the data word. In this respect, the Fletcher checksum is not different from other checksum and CRC algorithms and needs no special explanation.

An ordering problem that is easy to envision occurs when the data word is transferred byte-by-byte between a big-endian system and a little-endian system and the Fletcher-32 checksum is computed. If blocks are extracted from the data word in memory by a simple read of a 16-bit unsigned integer, then the values of the blocks will be different in the two systems, due to the reversal of the byte order of 16-bit data elements in memory, and the checksum result will be different as a consequence. The implementation examples, above, do not address ordering issues so as not to obscure the checksum algorithm. Because the Fletcher-16 checksum uses 8-bit blocks, it is not affected by byte endianness.

References

  1. ^ Fletcher, J. G. (January 1982). "An Arithmetic Checksum for Serial Transmissions". IEEE Transactions on Communications. COM-30 (1): 247–252. doi:10.1109/tcom.1982.1095369.
  2. ^ Theresa C. Maxino, Philip J. Koopman (January 2009). "The Effectiveness of Checksums for Embedded Control Networks" (PDF). IEEE Transactions on Dependable and Secure Computing.
  3. ^ J. Zweig, UIUC, C. Partridge, BBN (February 1990). "TCP Alternate Checksum Options". Network Working Group.{{cite web}}: CS1 maint: multiple names: authors list (link)
  4. ^ Sartek, Various (June 2018). "Calculating UBX checksum correctly in python". uBlox Support Portal.
  5. ^ Anastase Nakassis (October 1988). "Fletcher's error detection algorithm: how to implement it efficiently and how toavoid the most common pitfalls". Newsletter ACM SIGCOMM Computer Communication Review Homepage Archive. 18 (5): 63–88. doi:10.1145/53644.53648. S2CID 17356816.
  6. ^ Jones, Douglas W. "Modulus without Division, a tutorial". THE UNIVERSITY OF IOWA Department of Computer Science. Retrieved 9 September 2019.

Read other articles:

陆军第十四集团军炮兵旅陆军旗存在時期1950年 - 2017年國家或地區 中国效忠於 中国 中国共产党部門 中国人民解放军陆军種類炮兵功能火力支援規模约90门火炮直屬南部战区陆军參與戰役1979年中越战争 中越边境冲突 老山战役 成都军区对越轮战 紀念日10月25日 陆军第十四集团军炮兵旅(英語:Artillery Brigade, 14th Army),是曾经中国人民解放军陆军第十四集团军下属�...

 

 

Pour les articles homonymes, voir Louvre. Musée du LouvreLogo du musée du LouvreLa cour Napoléon avec la pyramide.Informations généralesNom local Musée du LouvreType Musée d'artOuverture 10 août 1793[1], il y a 230 ans, 231 joursGestionnaire Service des Musées de FranceSurface 360 000 m2 dont 72 735 m2 de galeriesVisiteurs par an 7 100 000 (2016)[3]8 100 000 (2017)[4]10 200 000 (2018)[5]9 600 000 (2019)[6]2 7...

 

 

Katedral BathurstKatedral Hati KudusInggris: Sacred Heart CathedralKatedral Bathurst47°37′04″N 65°39′19″W / 47.6178°N 65.6554°W / 47.6178; -65.6554Koordinat: 47°37′04″N 65°39′19″W / 47.6178°N 65.6554°W / 47.6178; -65.6554LokasiBathurst, New BrunswickNegaraKanadaDenominasiGereja Katolik RomaArsitekturStatusKatedralStatus fungsionalAktifTipe arsitekturNorman-GothicAdministrasiKeuskupanKeuskupan Bathurst di Kanada Katedr...

Komando Distrik Militer 0713/BrebesLambang Korem 071/WijayakusumaDibentuk17 Agustus 1948Negara IndonesiaCabangTNI Angkatan DaratTipe unitKomando Distrik MiliterPeranSatuan TeritorialBagian dariKodam IV/DiponegoroMakodimBrebes, Jawa TengahJulukanKodim 0713/BrebesPelindungTentara Nasional IndonesiaMotoWijaya KusumaBaret H I J A U MaskotBunga WijayakusumaUlang tahun17 AgustusSitus web[kodim0713brebes.com]TokohDandimLetkol Inf. Tentrem BasukiKasdimMayor Inf. Drs. Abd Asis Lallo Kod...

 

 

Swedish politician (born 1981) Ida GabrielssonGabrielsson in 2010Member of the RiksdagIncumbentAssumed office 24 September 2018Constituency Stockholm Municipality (2022–present) Stockholm County (2018–2022) Personal detailsBorn1981 (age 42–43)Political partyLeft Party Ida Gabrielsson (born 1981)[1] is a Swedish politician. Since September 2018, she serves as Member of the Riksdag.[2] She was again elected as Member of the Riksdag in September 2022.[3]...

 

 

This article does not cite any sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Eizaburo Nishibori – news · newspapers · books · scholar · JSTOR (December 2009) (Learn how and when to remove this template message) Eizaburō NishiboriBorn(1903-01-28)January 28, 1903KyotoDiedApril 13, 1989(1989-04-13) (aged 86)TokyoResting place JapanOther name...

Запрос «Пугачёва» перенаправляется сюда; см. также другие значения. Алла Пугачёва На фестивале «Славянский базар в Витебске», 2016 год Основная информация Полное имя Алла Борисовна Пугачёва Дата рождения 15 апреля 1949(1949-04-15) (75 лет) Место рождения Москва, СССР[1]...

 

 

American animated television series Spirit Riding FreeGenreAdventureBased onSpirit: Stallion of the Cimarronby John FuscoDeveloped byAury WallingtonVoices of Amber Frank Sydney Park Bailey Gambertoglio Darcy Rose Byrnes Nolan North Kari Wahlgren Theme music composerKari Kimmel[1]Opening themeRiding Free, performed by Maisy StellaEnding themeRiding Free (Instrumental)ComposersJoachim Horsley (S1)James Allen Roberson (S2-8)Sunna Wehrmeijer (Pony Tales and Riding Academy)Peter Bateman (P...

 

 

Supreme Court of the United States38°53′26″N 77°00′16″W / 38.89056°N 77.00444°W / 38.89056; -77.00444EstablishedMarch 4, 1789; 235 years ago (1789-03-04)LocationWashington, D.C.Coordinates38°53′26″N 77°00′16″W / 38.89056°N 77.00444°W / 38.89056; -77.00444Composition methodPresidential nomination with Senate confirmationAuthorized byConstitution of the United States, Art. III, § 1Judge term lengthl...

2020年夏季奥林匹克运动会波兰代表團波兰国旗IOC編碼POLNOC波蘭奧林匹克委員會網站olimpijski.pl(英文)(波兰文)2020年夏季奥林匹克运动会(東京)2021年7月23日至8月8日(受2019冠状病毒病疫情影响推迟,但仍保留原定名称)運動員206參賽項目24个大项旗手开幕式:帕维尔·科热尼奥夫斯基(游泳)和马娅·沃什乔夫斯卡(自行车)[1]闭幕式:卡罗利娜·纳亚(皮划艇)&#...

 

 

Medical school of Florida Atlantic University Charles E. Schmidt College of MedicineTypePublicEstablished2011DeanJulie G. Pilitsis[1]Academic staff202StudentsMedicine: 256LocationBoca Raton, Florida, USAWebsitewww.fau.edu/medicine/ The Charles E. Schmidt College of Medicine is the medical school of Florida Atlantic University located in Boca Raton, Florida. The college offers the degrees of Doctor of Medicine (M.D.), Doctor of Philosophy (Ph.D.), and a master's in biomedical science.&...

 

 

EleyshahPermukimanEleyshahLocation in the Kingdom of Saudi ArabiaKoordinat: 24°38′N 46°43′E / 24.633°N 46.717°E / 24.633; 46.717Koordinat: 24°38′N 46°43′E / 24.633°N 46.717°E / 24.633; 46.717Negara Arab SaudiPemerintahan • Gubernur Pangeran RiyadhFaisal bin Bandar Al Saud • Wali kotaIbraheem Mohammed Al-SultanKetinggian612 m (2,008 ft)Zona waktuUTC+3 (AST) • Musim panas (DST)UTC+...

Team representing England in international rugby league This article is about the men's team. For the women's team, see England women's national rugby league team. Not to be confused with England national rugby union team or Great Britain national rugby league team. England Team informationNicknameWall of White, Three LionsGoverning bodyRugby Football LeagueRegionEuropeHead coachShaun WaneCaptainGeorge WilliamsMost capsJames Graham Ryan Hall (45)Top try-scorerRyan Hall (39)Top point-scorerKev...

 

 

Kanika with dalma (served in bowl) Kanika (Odia: କାନିକା) is an aromatic sweet rice dish. It is an Odia dish traditionally prepared during festivals and pujas. It is one of the cooked items of the 56 dishes prepared as part of Mahaprasada or Chappan Bhoga in the Jagannath Temple.[1] It is offered to Lord Jagannath as part of the morning meal known as sakala dhupa.[2][better source needed] It was traditionally served in Odia weddings or picnics but ...

 

 

Postgraduate scholarship AwardMarshall ScholarshipAwarded forAmericans to study for a graduate degree in the United KingdomSponsored byMarshall Aid Commemoration CommissionEstablished1953Websitewww.marshallscholarship.org The Marshall Scholarship is a postgraduate scholarship for intellectually distinguished young Americans [and] their country's future leaders to study at any university in the United Kingdom.[1] It is widely considered one of the most prestigious scholarships for U.S....

American author and Tibetan Buddhism expert Bruce Alan WallaceBorn1950Pasadena, CaliforniaOther namesB. Alan Wallace, Allan WallaceEducationAmherst College (BS)Stanford University (MA), (PhD)Occupation(s)Author, Tibetan Buddhist Expert Bruce[1] Alan Wallace (born 1950) is an American author and expert on Tibetan Buddhism. His books discuss Eastern and Western scientific, philosophical, and contemplative modes of inquiry, often focusing on the relationships between science and Bud...

 

 

Anime ExpoAnime Expo标识狀態活跃類型日本动画、 日本漫画國家/地区美国地点加利福尼亚州洛杉矶會場洛杉矶会展中心出席者100,420+ (2016)承办方日本动画振兴协会举办时间1992年7月3日,​32年前​(1992-07-03)報稅身份非营利網站www.anime-expo.org Anime Expo,簡稱AX,是美國的日本動畫展覽會之一,大約在每年美国独立日的周末於南加州舉行,為期四天,主辦者是非營利組織�...

 

 

Joel EdgertonEdgerton tahun 2017Lahir23 Juni 1974 (umur 50)Blacktown, New South Wales, AustraliaAlmamaterUniversitas Western SydneyPekerjaan Aktor penulis skenario produser sutradara Tahun aktif1995–sekarangPasanganChristine Centenera(2018–sekarang)Anak2 Joel Edgerton (lahir 23 Juni 1974) adalah seorang aktor dan pembuat film asal Australia. Ia dikenal karena perannya sebagai Will McGill pada dua musim pertama serial drama Australia, The Secret Life of Us (2001–02), dan karen...

Educating students with special needs For other uses, see Special education (disambiguation). This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages) The examples and perspective in this article may not represent a worldwide view of the subject. You may improve this article, discuss the issue on the talk page, or create a new article, as appropriate. (June 2022) (Learn how and when to remove this message)...

 

 

اضغط هنا للاطلاع على كيفية قراءة التصنيف ديكر أسود حالة الحفظ أنواع غير مهددة أو خطر انقراض ضعيف جدا  (IUCN 3.1)[1] المرتبة التصنيفية نوع[2]  التصنيف العلمي المملكة: حيوان الشعبة: حبليات الطائفة: ثدييات الرتبة: شفعيات الأصابع الفصيلة: بقريات الأسرة: ديكر الاسم العلم�...