Skip to content

GitLab

  • Projects
  • Groups
  • Snippets
  • Help
    • Loading...
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in
zkevm-circuits
zkevm-circuits
  • Project overview
    • Project overview
    • Details
    • Activity
    • Releases
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 0
    • Issues 0
    • List
    • Boards
    • Labels
    • Service Desk
    • Milestones
  • Merge Requests 0
    • Merge Requests 0
  • CI / CD
    • CI / CD
    • Pipelines
    • Jobs
    • Schedules
  • Operations
    • Operations
    • Incidents
    • Environments
  • Packages & Registries
    • Packages & Registries
    • Package Registry
  • Analytics
    • Analytics
    • CI / CD
    • Repository
    • Value Stream
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Members
    • Members
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar

新注册的用户请输入邮箱并保存,随后登录邮箱激活账号。后续可直接使用邮箱登录!

  • zkp
  • zkevm-circuitszkevm-circuits
  • Wiki
    • Zkevm docs
  • 8 arithmetic

8 arithmetic · Changes

Page history
feat: update div_mod authored Nov 29, 2023 by liuxingxing1's avatar liuxingxing1
Hide whitespace changes
Inline Side-by-side
Showing with 37 additions and 16 deletions
+37 -16
  • zkevm-docs/8-arithmetic.markdown zkevm-docs/8-arithmetic.markdown +37 -16
  • No files found.
zkevm-docs/8-arithmetic.markdown
View page @ 7b69a216
...@@ -60,7 +60,8 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b ...@@ -60,7 +60,8 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b
不同 Tag 对应的约束不同 不同 Tag 对应的约束不同
- AddWith3rdConfined (含义:a+b=c+overflow\*2^256,且c的hi lo被约束为8个16bit之和) - AddWith3rdConfined (含义:a+b=c+overflow\*2^256,且 c 的 hi lo 被约束为 8 个 16bit 之和)
- 注:加法可以用这个 - 注:加法可以用这个
- 如果是 cnt=0 行,则 cnt_prev=1,cnt_prev_prev=0 - 如果是 cnt=0 行,则 cnt_prev=1,cnt_prev_prev=0
- c_lo = u16 sum(rotation cur) - c_lo = u16 sum(rotation cur)
...@@ -70,8 +71,9 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b ...@@ -70,8 +71,9 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b
- c lo + carry lo \* 2^128 = a lo + b lo - c lo + carry lo \* 2^128 = a lo + b lo
- c hi + carry hi \* 2^128 = a hi + b hi + carry lo - c hi + carry hi \* 2^128 = a hi + b hi + carry lo
- AddWith1stConfined (含义:a+b=c+overflow\*2^256,且a的hi lo被约束为8个16bit之和) (可以使用 select struct) - AddWith1stConfined (含义:a+b=c+overflow\*2^256,且 a 的 hi lo 被约束为 8 个 16bit 之和)
- 注:减法,LT,GT都可以用这个
- 注:减法,LT,GT 都可以用这个
- a_lo = u16 sum(rotation cur) - a_lo = u16 sum(rotation cur)
- a_hi = u16 sum(rotation prev) - a_hi = u16 sum(rotation prev)
- carry hi is bool - carry hi is bool
...@@ -81,9 +83,19 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b ...@@ -81,9 +83,19 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b
- 注意:carry_hi=1 等价于 b>c; carry_hi=0 等价于 b<=c - 注意:carry_hi=1 等价于 b>c; carry_hi=0 等价于 b<=c
- Div_Mod (a\*b+c=d 同时约束 c 小于 b) - Div_Mod (a\*b+c=d 同时约束 c 小于 b)
```
- define t_lo=operand3_0*operand1_0+(operand3_0*operand1_1+operand3_1*operand1_0)*2^64 if tag is div, (a,b,c,d) = (push, pop2, pop1 - push \* pop2, pop1)
- define t_hi=(operand3_0*operand1_2+operand3_1*operand1_1+operand3_2*operand1_0)+(operand3_0*operand1_3+operand3_1*operand1_2+operand3_2*operand1_1+operand3_3*operand1_0)*2^64
if tag is mod, (a,b,c,d) = (if pop2 is zero{0}else{pop1/pop2},pop2,if pop2 is zero{pop1}else{push},pop1)
- define t0 = a0 \* b0
- define t1 = a0 \* b1 + a1 \* b0
- define t2 = a0 \* b2 + a2 \* b0 + a1 \* b1
- define t3 = a0 \* b3 + a3 \* b0 + a2 \* b1 + a1 \* b2
- define t_lo=t0+(t1)\*2^64
- define t_hi=(t2)+(t3)\*2^64
- define carry_lo = (t0 + (t1 << 64) + c_lo).saturating_sub(d_lo) >> 128
- define carry_hi = (t2 + (t3 << 64) + c_hi + carry_lo).saturating_sub(d_hi) >> 128
- 如果是 0 行,约束 num_row is 8,并且约束 cnt 自增的有效性 - 如果是 0 行,约束 num_row is 8,并且约束 cnt 自增的有效性
- a_lo = u16 sum(rotation cur) - a_lo = u16 sum(rotation cur)
- a_hi = u16 sum(rotation -1) - a_hi = u16 sum(rotation -1)
...@@ -93,14 +105,23 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b ...@@ -93,14 +105,23 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b
- c_hi = u16 sum(rotation -5) - c_hi = u16 sum(rotation -5)
- d_lo = u16 sum(rotation -6) - d_lo = u16 sum(rotation -6)
- d_hi = u16 sum(rotation -7) - d_hi = u16 sum(rotation -7)
- (t_lo+operand2_3+operand2_2*2^64-car_lo*2^128) - (operand0_3+operand0_2\*2^64) - (t_lo+c_lo-car_lo\*2^128) - d_lo
- (t_hi+operand2_0+operand2_1*2^64+car_lo-car_hi*2^128) - (operand0_1+operand0_0\*2^64) - (t_hi+c_hi+car_lo-car_hi\*2^128) - d_hi
- 约束 c lt d - residue < divisor when divisor != 0
``` - overflow == 0 for opcode DIV/MOD overflow = carry*hi + a1 * b3 + a2 _ b2 + a3 _ b1 + a2 _ b3 + a3 _ b2 + a3 \_ b3
- if tag is div 约束 a - a \* (1.expr() - divisor_is_zero.expr()) a 是 core gadget push value
- is tag is mod 约束 c - c \* (1.expr() - divisor_is_zero.expr()) c 是 core gadget push value
- Mul(需要 6 行对 a,b,c lookup ) 其中 operand0 是 a,operand1 是 b - Mul(需要 6 行对 a,b,c lookup ) 其中 operand0 是 a,operand1 是 b
```
- define t_lo=operand0_0*operand1_0+(operand0_0*operand1_1+operand0_1*operand1_0)*2^64 - define t0 = a0 \* b0
- define t_hi=(operand0_0*operand1_2+operand0_1*operand1_1+operand0_2*operand1_0)+(operand0_0*operand1_3+operand0_1*operand1_2+operand0_2*operand1_1+operand0_3*operand1_0)*2^64 - define t1 = a0 \* b1 + a1 \* b0
- define t2 = a0 \* b2 + a2 \* b0 + a1 \* b1
- define t3 = a0 \* b3 + a3 \* b0 + a2 \* b1 + a1 \* b2
- define t_lo=t0+(t1)\*2^64
- define t_hi=(t2)+(t3)\*2^64
- define carry_lo = (t0 + (t1 << 64) + c_lo).saturating_sub(d_lo) >> 128
- define carry_hi = (t2 + (t3 << 64) + c_hi + carry_lo).saturating_sub(d_hi) >> 128
- 如果是 0 行,约束 num_row is 6,并且约束 cnt 自增的有效性 - 如果是 0 行,约束 num_row is 6,并且约束 cnt 自增的有效性
- a_lo = u16 sum(rotation cur) - a_lo = u16 sum(rotation cur)
- a_hi = u16 sum(rotation -1) - a_hi = u16 sum(rotation -1)
...@@ -108,9 +129,9 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b ...@@ -108,9 +129,9 @@ operand* 用来存放算术中的参数值,如 a+b=c+overflow 指令中的 a,b
- b_hi = u16 sum(rotation -3) - b_hi = u16 sum(rotation -3)
- c_lo = u16 sum(rotation -4) - c_lo = u16 sum(rotation -4)
- c_hi = u16 sum(rotation -5) - c_hi = u16 sum(rotation -5)
- (t_lo-car_lo*2^128) -(operand2_3+operand2_2*2^64) - (t_lo-car_lo\*2^128) -(c_lo)
- (t_hi+car_lo-car_hi*2^128)- (operand2_1+operand2_0*2^64) - (t_hi+car_lo-car_hi\*2^128)- (c_hi)
```
- Slt_Sgt (以下操作待写) - Slt_Sgt (以下操作待写)
- Sdiv_Smod - Sdiv_Smod
- Addmod - Addmod
......
Clone repository
  • basics
    • evm
    • halo2
  • code notes
    • binary_number_with_real_selector
    • how to use macro
    • simple_lt
    • simple_lt_word
  • Home
  • image
  • zkevm docs
    • 1 introduction
    • 10 public
    • 11 fixed
    • 12 exp
    • 13 keccak
    • 14 comparisons
    • 15 differences
View All Pages

Copyright © 2024 ChainWeaver Org. All Rights Reserved. 版权所有。

京ICP备2023035722号-3

京公网安备 11010802044225号