Applications of Mathematics in Computer Science (MACS)


Checksums and hash codes

Concepts:

  • Modular arithmetics;
  • Luhn's algorithm;
  • hash table.

David Tolpin, david.tolpin@gmail.com

Computer science problems

  • Redundancy in numbers (ids, card numbers, etc.)
  • Data integrity checking
  • Authorization
  • Hash tables

Redundancy in numbers

  • How can we quickly check that the number is correct?
  • תעודת זהות (כולל ספרת ביקורת)
    • 318892652*
    • 3188926525! Why 5?
  • "Card number is invalid" How do they know?

Data integrity checking

Authorization

Hash tables (dictionaries)

dictionaries in Python


>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
						

Modular arithmetics

$$m = a \% b$$ means that for some $n$ $$a = n*b + m$$

$$0 \le m < b$$

For example: $$n \% 1 = 0$$ $$10 \% 3 = 1$$ $$3 \% 2 = 1$$ $$5 \% 3 = 2$$

Modular equivalence

$$a \equiv b \pmod c$$ Means that $$a \% c = b \% c$$

For example: $$9 \equiv 5 \pmod 4$$ $$4 \equiv 2 \pmod 2$$ $$3 \equiv 5 \pmod 2$$

Properties

  • Reflexivity: $a \equiv a \pmod n$
  • Symmetry: $a \equiv b \pmod n \Leftrightarrow b\equiv a \pmod n$
  • Transitivity: $a = b \pmod n \land b \equiv c \pmod n \Rightarrow a \equiv c \pmod n$

More properties

If a1 ≡ b1 (mod n) and a2 ≡ b2 (mod n), or if a ≡ b (mod n), then:

  • a + k ≡ b + k (mod n)
  • k a ≡ k b (mod n) for any integer k
  • a1 + a2 ≡ b1 + b2 (mod n)
  • a1 – a2 ≡ b1 – b2 (mod n)
  • a1 · a2 ≡ b1 · b2 (mod n)

We can compute with remainders.

Example

  • As: 1000 integers sent
  • Ar: 1000 integers received
  • Is S=R?

						def checksum(A):
							cksum = 0
							for a in A: 
								cksum += a
							return cksum % 101
						

Will overflow if integers are big

Example

  • As: 1000 integers sent
  • Ar: 1000 integers received
  • Is S=R?

						def checksum(A):
							cksum = 0
							for a in A: 
								cksum += a % 101
							return cksum % 101
						

Will NOT overflow even if integers are big.

Example

  • As: 1000 integers sent
  • Ar: 1000 integers received
  • Is S=R?

						def checksum(A):
							cksum = 0
							for a in A: 
								cksum += a % 101
								cksum %= 101
							return cksum
						

Will NOT overflow even if integers are big.

Application examples

Open In Colab