|
1 | 1 | ---
|
2 |
| -title: 01. Integer Arithmetic Basics |
| 2 | +title: 01. Basic Integer Operations |
3 | 3 | tags:
|
4 | 4 | - zk
|
5 | 5 | - basic
|
6 | 6 | - integer
|
7 | 7 | ---
|
8 | 8 |
|
9 |
| -# WTF zk Tutorial Lesson 1: Integer Arithmetic Basics |
| 9 | +# Zero-Knowledge Proof Tutorial Lesson 1: Basic Integer Operations |
10 | 10 |
|
11 | 11 | 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.
|
12 | 12 |
|
13 |
| -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. |
| 13 | + |
| 14 | +If you are new to Python, we recommend installing [Anaconda](https://www.anaconda.com/download) to easily set up and manage your Python environment. |
14 | 15 |
|
15 | 16 | ## 1. Basic Definitions
|
16 | 17 |
|
17 |
| -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. |
| 18 | +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. |
18 | 19 |
|
19 | 20 | $$
|
20 | 21 | \mathbb{Z} = \lbrace \ldots, -3, -2, -1, 0, 1, 2, 3, \ldots \rbrace
|
21 | 22 | $$
|
22 | 23 |
|
23 |
| -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. |
| 24 | +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. |
24 | 25 |
|
25 | 26 | $$
|
26 | 27 | \lvert 69 \rvert = 69
|
|
30 | 31 | \lvert -69 \rvert = 69
|
31 | 32 | $$
|
32 | 33 |
|
33 |
| -We often use the natural numbers $\mathbb{N}$, which are a subset of integers and include all positive integers. |
| 34 | +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. |
34 | 35 |
|
35 | 36 | $$
|
36 | 37 | \mathbb{N} = \lbrace 1, 2, 3, \ldots \rbrace
|
37 | 38 | $$
|
38 | 39 |
|
39 |
| -In addition, we sometimes use non-negative integers, which we denote as $\mathbb{N_0}$: |
| 40 | +Additionally, we sometimes refer to non-negative integers as $\mathbb{N_0}$: |
40 | 41 |
|
41 | 42 | $$
|
42 | 43 | \mathbb{N_0} = \lbrace 0, 1, 2, 3, \ldots \rbrace
|
43 | 44 | $$
|
44 | 45 |
|
45 |
| -> 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. |
46 |
| -
|
47 |
| -## 2. Integer Arithmetic |
| 46 | +> 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)) |
48 | 47 |
|
49 |
| -Integer arithmetic includes addition, subtraction, multiplication, and division. Let's review the rules for these basic operations: |
| 48 | +## 2. Integer Operations |
50 | 49 |
|
51 |
| -- **Addition ( $+$ ):** For integers $a$ and $b$, their sum $a + b$ is the result of adding them together. |
| 50 | +Integer operations include addition, subtraction, multiplication, and division. Let's review the rules for these basic operations: |
52 | 51 |
|
| 52 | +- **Addition ($+$):** To add two integers $a$ and $b$, simply sum them up. |
| 53 | + |
53 | 54 | ```python
|
54 | 55 | a, b = 7, 5
|
55 | 56 | sum_result = a + b
|
56 |
| - print(f'Addition example: {sum_result}') |
57 |
| - # Addition example: 12 |
| 57 | + print(f'Example: {sum_result}') |
| 58 | + # Example: 12 |
58 | 59 | ```
|
59 | 60 |
|
60 |
| -- **Subtraction ( $-$ ):** For integers $a$ and $b$, their difference $a - b$ is the result of subtracting $b$ from $a$. |
61 |
| - |
| 61 | +- **Subtraction ($-$):** To subtract an integer $b$ from another integer $a$, subtract $b$ from $a$. |
| 62 | + |
62 | 63 | ```python
|
63 | 64 | diff_result = a - b
|
64 |
| - print(f'Subtraction example: {diff_result}') |
65 |
| - # Subtraction example: 2 |
| 65 | + print(f'Example: {diff_result}') |
| 66 | + # Example: 2 |
66 | 67 | ```
|
67 | 68 |
|
68 |
| -- **Multiplication ( $\times$ ):** For integers $a$ and $b$, their product $a \times b$ is the result of multiplying them together. |
69 |
| - |
| 69 | +- **Multiplication ($\times$):** To multiply two integers $a$ and $b$, multiply them together. |
| 70 | + |
70 | 71 | ```python
|
71 | 72 | product_result = a * b
|
72 |
| - print(f'Multiplication example: {product_result}') |
73 |
| - # Multiplication example: 35 |
| 73 | + print(f'Example: {product_result}') |
| 74 | + # Example: 35 |
74 | 75 | ```
|
75 | 76 |
|
76 | 77 |
|
77 | 78 | ## 3. Properties of Integers
|
78 | 79 |
|
79 | 80 | Integers have some important properties:
|
80 | 81 |
|
81 |
| -- **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. |
| 82 | +- **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. |
82 | 83 |
|
83 |
| -- **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$. |
| 84 | +- **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$. |
84 | 85 |
|
85 |
| -- **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$. |
| 86 | +- **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)$. |
86 | 87 |
|
87 | 88 | ## 4. Euclidean Division
|
88 | 89 |
|
89 | 90 | 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:
|
90 | 91 |
|
91 |
| -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|$. |
| 92 | +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|$. |
92 | 93 |
|
93 |
| -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$. |
| 94 | +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$. |
94 | 95 |
|
95 |
| -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). |
| 96 | +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). |
96 | 97 |
|
97 | 98 | ```python
|
98 | 99 | def euclidean_division(a, b):
|
99 | 100 | quotient, remainder = divmod(a, b)
|
100 | 101 | if remainder < 0:
|
101 | 102 | # Adjust the remainder to ensure it is non-negative
|
102 | 103 | remainder += abs(b)
|
103 |
| - # Adjust the quotient to maintain the equation |
| 104 | + # Adjust the quotient to maintain the equation's validity |
104 | 105 | quotient += 1
|
105 | 106 | return quotient, remainder
|
106 | 107 |
|
107 | 108 | quotient, remainder = euclidean_division(a, b)
|
108 |
| -print(f'Division example: quotient is {quotient}, remainder is {remainder}') |
109 |
| -# Division example: quotient is 1, remainder is 2 |
| 109 | +print(f'Example: Quotient is {quotient}, Remainder is {remainder}') |
| 110 | +# Example: Quotient is 1, Remainder is 2 |
110 | 111 | ```
|
111 | 112 |
|
112 | 113 | ## 5. Summary
|
113 | 114 |
|
114 |
| -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! |
| 115 | +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)! |
0 commit comments