Math Concepts
Main Source:
- Factorization — Wikipedia
- The Mathematics of Cryptography — Zach Star
- Some Khan Academy's CS articles
- Various Google search
Factorization
Factorization is the process of decomposing a number into several numbers that, when multiplied together, result in the original number. In other words, it is a way to represent a number as a product of several numbers.
Integer Factorization
Integer factorization is the factorization of a number into their integer product. For example, we can represent the number 24 as the multiply of:
When we ask, what is the factors of 24? They are going to be 1, 2, 3, 4, 6, 12, 24. When we can represent a number with the multiply of two or more number smaller than the number itself, they are called composite number.
Prime Factorization
To review, prime number is a natural number greater than 1 that is divisible only by 1 and itself, with no other positive divisors. Prime number is the opposite of composite number, we can only represent them in two number, which is 1 and itself.
Source: https://en.wikipedia.org/wiki/Prime_number
Prime factorization is the factorization using prime numbers, in other word, we represent a number in a product of prime numbers. We can get a prime factorization of a number by continuing their integer factorization. For example, number 24, we can continue with one of the factor: . is already a prime, we can still break down further into , further again into . This result in , therefore, the prime factorization of is .
One of the way to find the prime factorization is to decompose a number into some number multiplied by a prime number. The prime number will start from , increasing to , , and so on. While the technique is simple, it can be quite inefficient for large number like . First, we can factor it by , resulting in , then we will try dividing by , which is not possible. Divide it by , also not possible, until , which result in .
In the worst case, the technique will be more inefficient when we encounter a prime number, this mean we will need to brute force every single prime number smaller than the number itself.
Source: https://thirdspacelearning.com/gcse-maths/number/factor-tree/
A better way would to decompose the number into a multiply of larger number first, just like the image above.
Greatest Common Divisor (GCD)
Greatest Common Divisor (GCD), also known as Greatest Common Factor (GCF), is the largest positive integer that divides two or more integers, exactly without remainder, where the two or more integers are not zero.
When we ask the GCD of and , we are asking what is the largest number that divides both number. Number can indeed divide both of them, but there is an integer larger than . The GCD of and is , divided by is and divided by is . Further division cannot be performed on both and , indicating that is the final answer.
There are several methods to find the GCD:
-
Listing Factors: To use this method, we will find the factor of each number. For example, the factor of is [, , , , , ] and the factor of is [, , , , , ]. To actually find the GCD, we will take the largest common factors shared by both numbers, which is .
-
Prime Factorization: Find the prime factorization of each number. The prime factorization of is , prime factorization of is . To find the GCD, we will multiply the common factors shared by both numbers. When there are duplicate (e.g., there are and , also and ), we will take the lowest one. The common factors are and , we are ignoring the and . Multiplying and will result in , which is the GCD.
-
Euclidean Algorithm: The Euclidean algorithm is a recursive formula to find the GCD. The formula is , where "mod" represents the modulus operation, which gives the remainder when is divided by . divided by will have remainder of , therefore, .
This algorithm is recursive because it will repeatedly apply the formula until it reaches the base case, which is when any of the number becomes zero. At that point, the algorithm terminates, and the GCD is found to be the non-zero value.
Relative Prime
Also known as coprime, when two numbers are relatively prime, it means they have no common positive integer divisors other than 1. In other words, the greatest common divisor (GCD) of the two numbers is 1.
For example, consider the numbers 15 and 28. The GCD of 15 and 28 is 1, which means they are relatively prime. There is no positive integer greater than 1 that can divide both 15 and 28 without leaving a remainder.
Least Common Multiply (LCM)
Least Common Multiply (LCM) is the smallest positive integer that is divisible by two or more given numbers without leaving a remainder. When we ask for LCM of and , we are asking what is the smallest number that can be divided by both and exactly, the answer is .
Finding LCM:
- Prime Factorization: This method is very similar to the prime factorization method to find GCD. We will take the common factor shared by both numbers, the difference is, instead of taking the lowest number, we will instead take the highest number. To actually find the LCM, we will multiply that number.
- With GCD: This method uses a formula: , it needs the GCD of two numbers.
Modular Arithmetic
Modular arithmetic is a branch of mathematics that deals with the arithmetic operations performed on remainders. It involves working with numbers in a specific modulus or modulo. Modulo, as we know before, is a remainder of a division. Modular arithmetic focuses on the operations that can be performed within a specific modulus or modulo.
Congruence
The key concept is the congruence, two numbers are said to be congruent modulo of a given modulus if they have the same remainder when divided by that modulus. This is denoted by the symbol . For example, means that and have the same remainder when divided by .
In other word, when we say two numbers are congruent within some modulo, it means they will have the same remainder when divided by the modulo number. divided by left out , which is the same remainder as divided by (we can't divide by , therefore the remainder is 3 itself).
One way to think about modular arithmetic is to visualize it in terms of clock. A clock starts from 1 to 12, then in 13:00 PM, it will repeat to 1 again up to 12 in 00:00 PM. The repeating behavior of clock can be thought as the modulo operator. For example, in a clock with a 12-hour format, if it is currently 8 o'clock, and we add 5 hours, we can use the modulo operator () to determine the resulting hour: .
In the context of a clock analogy, being congruent of modulo 12 would mean that two numbers would occupy the same position on the clock face (e.g., ).
Source: https://mathsbyagirl.wordpress.com/2016/06/20/modular-arithmetic-whats-the-point/
In modular arithmetic, it can be thought that the numbers "wrap around" a fixed modulus. The modulus defines the range of values that can be represented within the arithmetic system.
Quotient Remainder Theorem
The theorem states that, given two integers and , where is not zero, there exist unique integers (the quotient) and (the remainder) such that:
When you divide one integer (a) by another integer (b), you can express the dividend (a) as the product of the divisor (b) and the quotient (q), plus a remainder (r). This theorem is closely related to the modulo operator.
The above theorem can be expressed in terms of modulo and division:
Where is integer division or the quotient obtained when dividing with . Integer division is an operation that returns the largest whole number that is less than or equal to the quotient obtained when dividing one integer by another. For example, would be just instead of .
Modular Addition & Subtraction
These are the addition and subtraction performed under modular arithmetic system.
Modular addition involves adding two numbers and then taking the remainder when divided by the modulus. Mathematically, given two numbers and , the modular sum is computed by performing the addition of and , and then taking the remainder when divided by .
For example, let's consider modular addition with a modulus of . If we want to calculate , we simply add and , which gives us . Then, when we divide by , the remainder is . Therefore, is equal to .
It is similar for subtraction, basically we subtract instead of adding the number. When we are taking the modulo of negative number, we can simply ignore the minus sign. For example, and .
Here are two properties of modular addition and subtraction:
Modular Multiplication
Similar to addition and subtraction, doing modulo operator that involve multiplication, we are going to do the multiplication first.
For example, consider modular multiplication with a modulus of . If we want to calculate , we multiply and , which gives us . Then, when we divide by , the remainder is . Therefore, is equal to .
The properties of modular multiplication:
Modular Exponentiation
Modular exponentiation is another operation similar to addition, subtraction, and multiplication. We will complete the number raised to the power first before taking the modulo. However, often times the power become very large, therefore, making it hard to calculate. One way to tackle this is to use exponent properties and use the modular multiplication.
We break down the large power into smaller power and then calculate it independently, at the end, we will multiply them together. This technique is called divide and conquer, that is, breaking down a big problem into smaller problem, solving them independently, and then combining the result to solve the overall problem.
There are also more efficient algorithm to calculate the modulo of exponentiation.
Properties of modular exponentiation:
Modular Inverse
Inverse of a number is divided by that number. For example, the inverse of number is , or in exponent, it is the negative power of the number itself (e.g., , inverse is ). The properties of inverse number is the inverse of a number multiplied by the number itself will always equal to .
Modular arithmetic doesn't have division operator, but we can make it possible by using the exponent properties. Instead of direct division, we can multiply number together with the other number being negative exponent. For example, dividing by can be represented as: , which will result in .
In modular arithmetic, the modular inverse of a number is another number that, when multiplied with the original number, yields a remainder of when divided by a given modulus.
Mathematically written as:
Finding the modular inverse involves solving a congruence equation. For example, to find the modular inverse of modulo , you would need to find a number such that:
Finding it using a naive method, trying out every single number:
Source: https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-inverses
The modular inverse of is the value that makes .
Extended Euclidean Algorithm
Using the Euclidean algorithm used to find GCD, we can also use the algorithm to find modular inverse more efficiently than the brute force way.
The properties of Euclidean Algorithm:
- If and then where is an integer, is an integer between and .
For review, the first and second properties is the base case of Euclidean algorithm, the third property is the GCD algorithm formula: combined with the quotient remainder theorem.
Bézout's Identity
Bézout's Identity states that for any two integers and such that their GCD is equal to 1 (i.e., they are relatively prime), there exist integers and such that:
In the context of modular arithmetic, this equation can be written as:
Using Extended Euclidean Algorithm
Using the Bézout's identity, the extended Euclidean algorithm follows a recursive process to calculate the GCD and the Bézout's identity coefficients and . It starts with the base case where is , in which case the GCD is and the coefficients are (1, 0). If is not , it recursively calculates the GCD and the coefficients based on the equation: .
The formula for extended Euclidean algorithm in code like:
extended_gcd(a, m):
if m = 0:
return a, 1, 0
else:
gcd, x, y = extended_gcd(m, a % m)
return gcd, y, x - (a // m) * y
Euler's Totient Function
The Euler's Totient function, denoted as , is a mathematical function that counts the number of positive integers less than or equal to that are relatively prime to . In other words, it calculates the count of numbers between and (inclusive) that do not share any common factors with except for .
For example, consider the number . The positive integers less than or equal to are . Among these numbers, the coprime numbers to are , as they do not share any common factors with except for , therefore, .
Source: https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/EulerPhi.svg/800px-EulerPhi.svg.png
Primality Test
Primality test is a method or algorithm used to determine whether a given number is prime or composite (not prime). The naive way to determine is to try to divide the number with every single number smaller than the number itself, this method is called trial division. However, the method will become inefficient for large number.
Fermat's Primality Test
One of the more efficient method is the Fermat's Primality Test, it is based on Fermat's Little Theorem. It states that if is a prime number and is any positive integer less than , then .
The test randomly selects values of and checks if the equality holds. If it fails for any value, the number is composite; otherwise, it is likely prime. This method is a probabilistic primality test, it offers a good balance between efficiency and accuracy when determining the primality of large numbers.