Skip to content

Commit

Permalink
Tran:WTFAcademy#60 merge
Browse files Browse the repository at this point in the history
  • Loading branch information
buttonwild committed May 5, 2024
2 parents 877eeb4 + f01c57d commit b42f78c
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 997 deletions.
78 changes: 39 additions & 39 deletions Languages/en/00_Set/readme.md

Large diffs are not rendered by default.

65 changes: 33 additions & 32 deletions Languages/en/01_Integer/readme.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
---
title: 01. Integer Arithmetic Basics
title: 01. Basic Integer Operations
tags:
- zk
- basic
- integer
---

# WTF zk Tutorial Lesson 1: Integer Arithmetic Basics
# Zero-Knowledge Proof Tutorial Lesson 1: Basic Integer Operations

As a beginner-friendly tutorial on zero-knowledge proofs, we will start by learning the basics of integer arithmetic. Most of you have probably learned this in secondary school, so it should be quite easy. We will also implement integer arithmetic using Python, making it easy for beginners to get started.

For those who haven't used Python before, it is recommended to install [Anaconda](https://www.anaconda.com/download) to install and manage the Python environment.

If you are new to Python, we recommend installing [Anaconda](https://www.anaconda.com/download) to easily set up and manage your Python environment.

## 1. Basic Definitions

An integer is a number without a decimal part and can be either positive, negative, or zero. We use $\mathbb{Z}$ to represent the set of all integers.
An integer is a whole number without a decimal part. It can be positive, negative, or zero. We use the symbol $\mathbb{Z}$ to represent the set of all integers.

$$
\mathbb{Z} = \lbrace \ldots, -3, -2, -1, 0, 1, 2, 3, \ldots \rbrace
$$

For an integer $a \in \mathbb{Z}$, we use $\lvert a \rvert$ to represent the absolute value of $a$, which is the non-negative value of $a$ regardless of its sign.
For an integer $a \in \mathbb{Z}$, the absolute value of $a$, denoted as $\lvert a \rvert$, represents the non-negative value of $a$ without considering its sign.

$$
\lvert 69 \rvert = 69
Expand All @@ -30,85 +31,85 @@ $$
\lvert -69 \rvert = 69
$$

We often use the natural numbers $\mathbb{N}$, which are a subset of integers and include all positive integers.
We often use the term "natural numbers" to refer to positive integers. It is a subset of the set of integers and includes all positive integers.

$$
\mathbb{N} = \lbrace 1, 2, 3, \ldots \rbrace
$$

In addition, we sometimes use non-negative integers, which we denote as $\mathbb{N_0}$:
Additionally, we sometimes refer to non-negative integers as $\mathbb{N_0}$:

$$
\mathbb{N_0} = \lbrace 0, 1, 2, 3, \ldots \rbrace
$$

> Note: Some textbooks include 0 in the set of natural numbers, and there is currently [debate](https://zh.wikipedia.org/wiki/%E8%87%AA%E7%84%B6%E6%95%B0) on whether 0 should be included.
## 2. Integer Arithmetic
> Note: The inclusion of 0 in the set of natural numbers is a topic of debate. Some textbooks include 0, while others do not. (Source: [Wikipedia](https://en.wikipedia.org/wiki/Natural_number))
Integer arithmetic includes addition, subtraction, multiplication, and division. Let's review the rules for these basic operations:
## 2. Integer Operations

- **Addition ( $+$ ):** For integers $a$ and $b$, their sum $a + b$ is the result of adding them together.
Integer operations include addition, subtraction, multiplication, and division. Let's review the rules for these basic operations:

- **Addition ($+$):** To add two integers $a$ and $b$, simply sum them up.

```python
a, b = 7, 5
sum_result = a + b
print(f'Addition example: {sum_result}')
# Addition example: 12
print(f'Example: {sum_result}')
# Example: 12
```

- **Subtraction ( $-$ ):** For integers $a$ and $b$, their difference $a - b$ is the result of subtracting $b$ from $a$.

- **Subtraction ($-$):** To subtract an integer $b$ from another integer $a$, subtract $b$ from $a$.
```python
diff_result = a - b
print(f'Subtraction example: {diff_result}')
# Subtraction example: 2
print(f'Example: {diff_result}')
# Example: 2
```

- **Multiplication ( $\times$ ):** For integers $a$ and $b$, their product $a \times b$ is the result of multiplying them together.

- **Multiplication ($\times$):** To multiply two integers $a$ and $b$, multiply them together.
```python
product_result = a * b
print(f'Multiplication example: {product_result}')
# Multiplication example: 35
print(f'Example: {product_result}')
# Example: 35
```


## 3. Properties of Integers

Integers have some important properties:

- **Closure:** Integer addition and multiplication are closed operations within the set of integers, meaning that the sum or product of any two integers is still an integer.
- **Closure Property:** Integer addition and multiplication are closed within the set of integers. This means that the sum or product of any two integers will still be an integer.

- **Commutativity:** Integer addition and multiplication are commutative, meaning that $a + b = b + a$ and $a \times b = b \times a$ hold true for any integers $a$ and $b$.
- **Commutative Property:** Integer addition and multiplication are commutative. This means that the order of the integers does not affect the result. For any integers $a$ and $b$, $a + b = b + a$ and $a \times b = b \times a$.

- **Associativity:** Integer addition and multiplication are associative, meaning that $(a + b) + c = a + (b + c)$ and $(a \times b) \times c = a \times (b \times c)$ hold true for any integers $a$, $b$, and $c$.
- **Associative Property:** Integer addition and multiplication are associative. This means that the grouping of integers does not affect the result. For any integers $a$, $b$, and $c$, $(a + b) + c = a + (b + c)$ and $(a \times b) \times c = a \times (b \times c)$.

## 4. Euclidean Division

The division we commonly use is real number division, where the result of dividing two integers may not be an integer, e.g., $7 \div 5 = 1.4$ is not an integer. Therefore, we introduce integer division, also known as Euclidean Division. Its result consists of two parts: the quotient and the remainder. The definition of Euclidean Division is as follows:

For integers $a$ and $b$ (where $b \neq 0$), there exists a unique pair of integers $(q, r)$ such that $a = bq + r$, where $q$ is the quotient, $r$ is the remainder, and $0 \leq r \lt |b|$.
For integers $a$ and $b$ (where $b \neq 0$), there exists a unique pair of integers $(q, r)$ such that $a = bq + r$. Here, $q$ represents the quotient, $r$ represents the remainder, and $0 \leq r \lt |b|$.

If the remainder when dividing $a$ by $b$ is zero, we say that $a$ is divisible by $b$ ($b$ divides $a$) and write $b \mid a$. We can also call $b$ a factor of $a$. If the remainder is not zero, we write $b \nmid a$.
If the remainder of $a$ divided by $b$ is zero, we say that $a$ is divisible by $b$ ($b$ divides $a$), denoted as $b \mid a$. We can also say that $b$ is a factor of $a$. If the remainder is not zero, we denote it as $b \nmid a$.

We can implement Euclidean Division in Python, taking care to handle the case when $a$ or $b$ is negative: the `divmod` function in Python allows for negative remainders, whereas Euclidean Division requires $0 \leq r \lt |b|$. On the contrary, the modulo operation (`%`) in Python allows for negative numbers. Here, it is necessary to understand the internal implementation formula for the modulo operation in programming languages: $a\%b=a-(a//b) * b$. (The `//` here is Python's internal implementation of Euclidean Division, ensuring a positive remainder less than the divisor).
We can implement Euclidean Division in Python. When implementing it, we need to handle the cases where $a$ or $b$ is negative. The `divmod` function in Python allows for negative remainders, but Euclidean Division requires the remainder to be non-negative and less than the divisor. On the other hand, the modulo operation (`%`) in Python allows for negative remainders. It is necessary to understand the internal implementation formula of the modulo operation in programming languages: $a\%b=a-(a//b) * b$ (where `//` represents the internal implementation of Euclidean Division in Python, ensuring that the remainder is positive and less than the divisor).

```python
def euclidean_division(a, b):
quotient, remainder = divmod(a, b)
if remainder < 0:
# Adjust the remainder to ensure it is non-negative
remainder += abs(b)
# Adjust the quotient to maintain the equation
# Adjust the quotient to maintain the equation's validity
quotient += 1
return quotient, remainder

quotient, remainder = euclidean_division(a, b)
print(f'Division example: quotient is {quotient}, remainder is {remainder}')
# Division example: quotient is 1, remainder is 2
print(f'Example: Quotient is {quotient}, Remainder is {remainder}')
# Example: Quotient is 1, Remainder is 2
```

## 5. Summary

In this lesson, we introduced the basics of integers, including their definitions and basic operations (addition, subtraction, multiplication, and Euclidean Division), and implemented them using Python. We believe that most of you have learned these concepts in secondary school and find them straightforward. Let's continue the WTF zk journey!
In this lesson, we introduced the basics of integers, including their definitions and basic operations (addition, subtraction, multiplication, and Euclidean Division), and implemented them using Python. These concepts are usually covered in middle school and are relatively simple. Now, let's continue our journey into the world of Zero-Knowledge Proofs (ZKPs)!
51 changes: 22 additions & 29 deletions Languages/en/02_Prime/readme.md
Original file line number Diff line number Diff line change
@@ -1,63 +1,56 @@
---
title: 02. Prime Number Basics
tags:
- zk
- basic
- integer
---
# WTF zk Series: Tutorial 02 - Basics of Prime Numbers

# WTF zk Tutorial Lesson 2: Prime Number Basics
Welcome to the second lesson of the WTF zk tutorial series! In this tutorial, we will explore the basics of prime numbers. Prime numbers play a crucial role in cryptography, making it essential to understand them for learning zero-knowledge proofs.


## 1. Definition of Prime Numbers

Prime numbers are also known as prime numbers and are defined as follows: For a natural number greater than 1, if it cannot be divided evenly by any natural number other than 1 and itself, then it is a prime number.
Prime numbers are natural numbers greater than 1 that cannot be evenly divided by any other natural number except 1 and itself. In other words, a prime number has no factors other than 1 and itself.

2, 3, 5, and 7 are all prime numbers because they can only be divided evenly by 1 and themselves. In addition, all even numbers except 2 are not prime numbers because they can be divided evenly by 2 in addition to 1 and themselves.
For example, 2, 3, 5, and 7 are prime numbers because they can only be divided evenly by 1 and themselves. On the other hand, all even numbers (except 2) are not prime numbers because they can be divided evenly by 2.

## 2. Properties of Prime Numbers

Prime numbers are fundamental units of all natural numbers, and the fundamental theorem of arithmetic tells us:

> Any natural number greater than 1 can be expressed as a unique product of prime numbers, regardless of the order of the prime numbers.
Prime numbers are the basic building blocks of all natural numbers. The fundamental theorem of arithmetic states that any natural number greater than 1 can be expressed as a unique product of prime numbers, regardless of the order of the prime numbers.

For example:

$$
84 = 2^2 \times 3 \times 7
$$

Here, 2, 3, and 7 are prime numbers, and this factorization is unique.
In this example, 2, 3, and 7 are prime numbers, and this factorization is unique.

Prime Number Theorem: The number of prime numbers less than or equal to N is approximately $N/\ln{N}$, and there are infinitely many prime numbers.

Proof:

Euclidean proof
Euclidean Proof

1. **Assume a Finite Number of Prime Numbers:** First, assume that there are a finite number of prime numbers, which we denote as $p_1, p_2, \ldots, p_n$.
1. **Assume a Finite Number of Prime Numbers:** Let's assume that there are only a finite number of prime numbers and denote them as $p_1, p_2, \ldots, p_n$.

2. **Construct a New Number:** Consider the new number $N = p_1 \times p_2 \times \ldots \times p_n + 1$, which is obtained by multiplying all known prime numbers and adding 1.
2. **Construct a New Number:** Consider a new number $N = p_1 \times p_2 \times \ldots \times p_n + 1$. This number is obtained by multiplying all known prime numbers and adding 1.

3. **Properties of the New Number:** The number N is obviously a prime number because it is not a multiple of any known prime number, as dividing it by any known prime number leaves a remainder of 1.
3. **Properties of the New Number:** The number N is obviously a prime number because it is not divisible by any known prime number. Dividing N by any known prime number leaves a remainder of 1.

4. **Contradiction:** Hence, this leads to a contradiction, because if N is not a prime number, it must have a prime factor, which either is a known prime number or a new prime number different from the known prime numbers.
4. **Contradiction:** This leads to a contradiction because if N is not a prime number, then it must have a prime factor. This prime factor either belongs to the known prime numbers or is a new prime number different from the known ones.

5. **Conclusion:** In any case, this results in a contradiction with the initial assumption of a finite number of prime numbers. Therefore, the initial assumption is incorrect, and there must be infinitely many prime numbers.
5. **Conclusion:** In either case, this contradicts the initial assumption of a finite number of prime numbers. Therefore, the initial assumption is incorrect, and the number of prime numbers must be infinite.

## 3. Prime Numbers and Composite Numbers

We can classify natural numbers into prime numbers and composite numbers. Composite numbers are the complements of prime numbers: For a natural number greater than 1, if it has factors other than 1 and itself, it is a composite number. For example, 4, 6, 8, and 9 are composite numbers.
Natural numbers can be divided into two categories: prime numbers and composite numbers. Composite numbers are the opposite of prime numbers. A composite number is a natural number greater than 1 that has factors other than 1 and itself. For example, 4, 6, 8, and 9 are all composite numbers.

## 4. Finding Prime Numbers

Finding prime numbers is an important task in number theory, which has attracted attention since medieval times. People tried to find prime number formulas (formulas that can generate prime numbers only) during that time. By the time of Gauss, it was basically confirmed that simple prime number formulas do not exist. Therefore, Gauss believed that primality testing is a fairly difficult problem. Since then, this problem has attracted a large number of mathematicians. Primality testing algorithms can be divided into two main categories: deterministic algorithms and probabilistic algorithms. The former provides a definite result but is usually slower, while the latter provides a result that may vary.
Finding prime numbers is an important task in number theory. This problem gained attention in the Middle Ages when people tried to find formulas that could generate prime numbers exclusively. In the era of Gauss, it was basically confirmed that simple prime formulas do not exist. Gauss believed that determining primality is a very difficult problem. Since then, many mathematicians have been fascinated by this problem. Primality testing algorithms can be divided into two categories: deterministic algorithms and random algorithms. Deterministic algorithms provide a definite result but are usually slower, while random algorithms are the opposite.

### Deterministic Algorithms

- [Sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)

The most commonly used method is the Sieve of Eratosthenes. Its logic is very simple: first, determine a range to search, then eliminate all multiples of prime numbers between 0 and $\sqrt n$, leaving behind all prime numbers within the range.
The most commonly used method is the Sieve of Eratosthenes. Its logic is very simple: first, determine a range to search, then eliminate all multiples of prime numbers between 0 and $\sqrt{n}$. The remaining numbers within the range are all prime numbers.

We can implement this method in Python:

Expand All @@ -78,19 +71,19 @@ print(f'Prime numbers less than or equal to {limit}: {prime_numbers}')
# Prime numbers less than or equal to 20: [2, 3, 5, 7, 11, 13, 17, 19]
```

- [Lucas-Lehmer Primality Test](https://en.wikipedia.org/wiki/Lucas-Lehmer_test)
- [Lucas-Lehmer Primality Test](https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test)
- [AKS Primality Test](https://en.wikipedia.org/wiki/AKS_primality_test)

### Probabilistic Algorithms
### Random Algorithms

- Fermat Primality Test
- Uses [Fermat's Little Theorem](../07_Exp/readme.md) for testing.
- Test using [Fermat's Little Theorem](../07_Exp/readme.md).
- [Miller-Rabin Primality Test](https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test)

## 5. Applications of Prime Numbers in Cryptography
## 5. Application of Prime Numbers in Cryptography

Prime numbers play a significant role in the field of cryptography, particularly in public-key cryptography. For example, RSA (Rivest-Shamir-Adleman) is an asymmetric encryption algorithm that uses the product of large prime numbers as part of the public and private keys: Calculating the product of prime numbers is simple, but factoring a large composite number into prime factors is extremely difficult, ensuring the security of the RSA encryption algorithm.
Prime numbers play a crucial role in cryptography, especially in public-key cryptography. For example, RSA (Rivest-Shamir-Adleman) is an asymmetric encryption algorithm that uses the product of large prime numbers as part of the public and private keys. Calculating the product of prime numbers is simple, but factoring large composite numbers into prime factors is very difficult. This difficulty ensures the security of the RSA encryption algorithm.

## 6. Conclusion
## 6. Summary

In this tutorial, we have learned the basics of prime numbers, including their definition, properties, and methods for finding prime numbers. Prime numbers have important applications in both mathematics and cryptography, laying the foundation for understanding zero-knowledge proofs.
In this tutorial, we have learned the basics of prime numbers, including their definition, properties, and methods for finding prime numbers. Prime numbers have important applications in mathematics and cryptography, laying the foundation for our understanding of zero-knowledge proofs.
Loading

0 comments on commit b42f78c

Please sign in to comment.