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
    • Code notes
  • binary_number_with_real_selector

binary_number_with_real_selector · Changes

Page history
restyle docs authored Aug 06, 2024 by geruiwang's avatar geruiwang
Hide whitespace changes
Inline Side-by-side
Showing with 73 additions and 73 deletions
+73 -73
  • code-notes/binary_number_with_real_selector.markdown code-notes/binary_number_with_real_selector.markdown +73 -73
  • No files found.
code-notes/binary_number_with_real_selector.markdown 0 → 100644
View page @ a8eeca49
# binary_number_with_real_selector.rs 介绍
主要功能:用多个列(列的值取0和1)来表示某一类型变量的值(在`State Circuit`、`Copy Circuit`等子电路中主要表示`Tag`类型)
## 内容
#### 列的定义
```Rust
#[derive(Clone, Copy, Debug)]
pub struct BinaryNumberConfig<T, const N: usize> {
/// Must be constrained to be binary for correctness.
pub bits: [Column<Advice>; N],
_marker: PhantomData<T>,
}
pub struct BinaryNumberChip<F, T, const N: usize> {
config: BinaryNumberConfig<T, N>,
_marker: PhantomData<F>,
}
```
定义了`N`个`Advice`列,其中`N`是表示`T.into()`(`T`是所表示的类型)所需的比特数
外界使用的是`BinaryNumberChip`,成员变量和`BinaryNumberConfig`没有什么不同,只是为了层次关系更好。
#### 所建立的约束
`BinaryNumberChip`含有以下几个约束(写于其`configure()`方法):
* "bit column is 0 or 1":每一个格子的值只能为0或1
* "binary number value”:`configure()`函数包含一个参数` value: Option<Column<Advice>>`(可以理解为一个用于参考的列),如果`value`非空,这个约束使得每一行表示的值与` value`列的对应行相等
* "binary number value in range":所表示的值没有超出`T.into()`的范围
> 以上约束在`selector`为`1`时生效,其中`selector`也是`configure()`方法的参数
#### 赋值过程
写于`BinaryNumberChip()`的`assign()`方法:
对于输入的`T`类型变量`value`,调用其`as_bits()`方法生成其二进制表示(由0、1构成的列表),依次把列表的各个元素填入相应的列
#### 其他
`BinaryNumberConfig`还实现了一些有用的方法:
* `value()`:返回指定的`rotation`处,`BinaryNumberConfig`表示的类型的`Expression`
* `value_equals()`:在指定的`rotation`处,比较某个`T`类型的值是否与`BinaryNumberConfig`所表示的值相等;若相等,则返回`1.expr()`,否则返回`0.expr()`
* `value_equals_expr()`:比较某个`T`类型的值是否与一个长度为`N`的`Expression`列表(每个元素只能为`0.expr()`或`1.expr()`)表示的值相等;若相等,则返回`1.expr()`,否则返回`0.expr()`
## 用法
以Scroll的`copy_circuit.rs`为例:
`tag`定义为`BinaryNumberConfig<CopyDataType, 3>`;
![image-20231109143852776](../image/binary_number-1.png)
使用`value_equals()`方法判断`tag`是否是某一类型,生成建立约束所需的`condition`表达式
![image-20231109144057941](../image/binary_number-2.png)
使用`value()`方法得到某个`rotation`处的`tag`对应的表达式
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号