Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circuit changes draft #24

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
35c815d
Add variable-base sign-scalar multiplication
ConstanceBeguier Mar 21, 2023
731bc10
Add CommitDomain creation from two personalizations
ConstanceBeguier Apr 19, 2023
b1e397f
Add commit_from_hash_point
ConstanceBeguier Apr 19, 2023
8cfe0ae
updated naming
PaulLaux Apr 20, 2023
54697b2
Add hash and blinding_factor functions
ConstanceBeguier Apr 17, 2023
4ce262d
Add some functionalities for MuxChip
ConstanceBeguier Apr 18, 2023
475f54d
Add Point::new_from_constant method (#17)
ConstanceBeguier Jun 23, 2023
4c3c00b
Optimized short range check on 4 and 5 bits (#21)
ConstanceBeguier Oct 16, 2023
f51eebe
Add multiplexer chip (#23)
ConstanceBeguier Oct 17, 2023
cba30b1
Add functions to evaluate a Sinsemilla hash from an initial private p…
ConstanceBeguier Oct 18, 2023
87464d4
Update comments
ConstanceBeguier Nov 23, 2023
d76d231
Reactivate i686 CI test
ConstanceBeguier Nov 23, 2023
ac7a90d
Some minor changes
ConstanceBeguier Nov 28, 2023
622875e
Remove q_sinsemilla4_private
ConstanceBeguier Nov 30, 2023
5f436dc
Move mux functionality into CondSwap chip
ConstanceBeguier Nov 30, 2023
75ea29c
add Variable-base sign-scalar multiplication, update Sinsemilla
Mar 4, 2024
bab9cfa
update decomposition
Mar 6, 2024
c2e2447
add multiplexer.md
Mar 7, 2024
11778cf
edit summary
Mar 7, 2024
21c2cac
fix code running problem
Mar 7, 2024
3b66777
update mux
Mar 7, 2024
a9dda24
fix the katex renderer fail problem
Mar 8, 2024
3214449
fix some math formation
Mar 8, 2024
e5d931f
change mux to cond_swap
Mar 11, 2024
39584d2
remove mux.md
Mar 11, 2024
9127199
restructuring decomposition
Mar 13, 2024
b9949d0
update book
Mar 27, 2024
7009dc4
add a section to compare two lookup arguments
Mar 28, 2024
d45de46
fix typos
Mar 28, 2024
b7df02d
typo
Apr 2, 2024
b645b24
change index for lookup table
May 29, 2024
a4c00a3
fix cargo.lock
May 29, 2024
5b6568e
modify book.toml
May 29, 2024
2e3dfe7
modify book.toml
May 29, 2024
b5286b2
Fix MD book generation as zcash/halo2 #814
May 29, 2024
2d486c5
Fix MD book generation as zcash/halo2 #814
May 29, 2024
2325e8a
update sinsemilla gadget
May 31, 2024
8c67aab
update decomposition
May 31, 2024
93c7425
remove space
May 31, 2024
4b23acf
Try to switch to a regular ubuntu repo instead of using azure mirror …
dmidem Jun 4, 2024
645a6a8
Revert "Try to switch to a regular ubuntu repo instead of using azure…
dmidem Jun 4, 2024
e7cf640
Try to do apt update before apt install in ci.yml
dmidem Jun 4, 2024
b2580ff
Fix error in ci.yml
dmidem Jun 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
toolchain: '1.76.0'
override: true

# - name: Setup mdBook
Expand All @@ -26,7 +26,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: install
args: mdbook --git https://github.com/HollowMan6/mdBook.git --rev 62e01b34c23b957579c04ee1b24b57814ed8a4d5
args: mdbook --git https://github.com/HollowMan6/mdBook.git --rev 5830c9555a4dc051675d17f1fcb04dd0920543e8

- name: Install mdbook-katex and mdbook-pdf
uses: actions-rs/cargo@v1
Expand All @@ -40,6 +40,11 @@ jobs:
- name: Build halo2 book
run: mdbook build book/

- uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2023-10-05
override: true

- name: Build latest rustdocs
uses: actions-rs/cargo@v1
with:
Expand Down
58 changes: 30 additions & 28 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ jobs:
with:
beta-features: ${{ matrix.stage == 'beta' }}
nightly-features: ${{ matrix.stage == 'nightly' }}
- name: Update apt
run: sudo apt-get update
- name: Install cross-platform support dependencies
run: sudo apt install gcc-multilib
run: sudo apt-get install gcc-multilib
- run: rustup target add i686-unknown-linux-gnu
- name: Run tests
run: >
Expand Down Expand Up @@ -125,33 +127,33 @@ jobs:
- name: Test halo2 book
run: mdbook test -L target/debug/deps book/

codecov:
name: Code coverage
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
# Use stable for this to ensure that cargo-tarpaulin can be built.
- id: prepare
uses: ./.github/actions/prepare
with:
toolchain: stable
nightly-features: true
- name: Install cargo-tarpaulin
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-tarpaulin
- name: Generate coverage report
uses: actions-rs/cargo@v1
with:
command: tarpaulin
args: >
${{ steps.prepare.outputs.feature-flags }}
--timeout 600
--out Xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3.1.4
# codecov:
# name: Code coverage
# runs-on: ubuntu-latest
#
# steps:
# - uses: actions/checkout@v3
# # Use stable for this to ensure that cargo-tarpaulin can be built.
# - id: prepare
# uses: ./.github/actions/prepare
# with:
# toolchain: stable
# nightly-features: true
# - name: Install cargo-tarpaulin
# uses: actions-rs/cargo@v1
# with:
# command: install
# args: cargo-tarpaulin
# - name: Generate coverage report
# uses: actions-rs/cargo@v1
# with:
# command: tarpaulin
# args: >
# ${{ steps.prepare.outputs.feature-flags }}
# --timeout 600
# --out Xml
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v3.1.4

doc-links:
name: Intra-doc links
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions book/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ title = "The halo2 Book"
macros = "macros.txt"
renderers = ["html"]

[output.katex]

[output.html]

[output.html.print]
Expand Down
5 changes: 4 additions & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
- [Incomplete and complete addition](design/gadgets/ecc/addition.md)
- [Fixed-base scalar multiplication](design/gadgets/ecc/fixed-base-scalar-mul.md)
- [Variable-base scalar multiplication](design/gadgets/ecc/var-base-scalar-mul.md)
- [Variable-base sign-scalar multiplication](design/gadgets/ecc/var-base-sign-scalar-mul.md)
- [Decomposition](design/gadgets/decomposition.md)
- [Sinsemilla](design/gadgets/sinsemilla.md)
- [Combined lookup table](design/gadgets/sinsemilla/combined_lookups.md)
- [MerkleCRH](design/gadgets/sinsemilla/merkle-crh.md)
- [Decomposition](design/gadgets/decomposition.md)
- [SHA-256](design/gadgets/sha256.md)
- [16-bit table chip](design/gadgets/sha256/table16.md)
- [Conditional swap](design/gadgets/cond_swap.md)
- [Background Material](background.md)
- [Fields](background/fields.md)
- [Polynomials](background/polynomials.md)
Expand Down
137 changes: 137 additions & 0 deletions book/src/design/gadgets/cond_swap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Conditional swap

The conditional swap gadgets include two instructions: swap and multiplexer (mux).
We use a multiplexer to compute differently for ZEC or for non-ZEC assets in Orchard protocol.
The graph shows where each new gadget (hash from private initial point and MUX on non-identity points) will be used.

![](https://imgur.com/kDQLoQQ.png)

## Multiplexer

Given a boolean flag $\textsf{choice}$, mux is used for selecting one of two values ($\textsf{left}$ and $\textsf{right}$) based on the boolean flag.
If $\textsf{choice}$ is true, it returns $\textsf{right}$; otherwise, it returns $\textsf{left}$. This functionality is crucial for circuits that require conditional logic.


## Chip instructions

```rust,ignore,no_run
pub trait CondSwapInstructions<F: Field>: UtilitiesInstructions<F> {
/// Given an input `(choice, left, right)` where `choice` is a boolean flag,
/// returns `left` if `choice` is not set and `right` if `choice` is set.
fn mux(
&self,
layouter: &mut impl Layouter<F>,
choice: Self::Var,
left: Self::Var,
right: Self::Var,
) -> Result<Self::Var, Error>;
}
```

## Implement chip traits

```rust,ignore,no_run
impl<F: PrimeField> CondSwapInstructions<F> for CondSwapChip<F> {
fn mux(
&self,
layouter: &mut impl Layouter<F>,
choice: Self::Var,
left: Self::Var,
right: Self::Var,
) -> Result<Self::Var, Error> {
let config = self.config();

layouter.assign_region(
|| "mux",
|mut region| {
// Enable `q_swap` selector
config.q_swap.enable(&mut region, 0)?;

// Copy in `a` value
let left = left.copy_advice(|| "copy left", &mut region, config.a, 0)?;

// Copy in `b` value
let right = right.copy_advice(|| "copy right", &mut region, config.b, 0)?;

// Copy `choice` value
let choice = choice.copy_advice(|| "copy choice", &mut region, config.swap, 0)?;

let a_swapped = left
.value()
.zip(right.value())
.zip(choice.value())
.map(|((left, right), choice)| {
if *choice == F::from(0_u64) {
left
} else {
right
}
})
.cloned();
let b_swapped = left
.value()
.zip(right.value())
.zip(choice.value())
.map(|((left, right), choice)| {
if *choice == F::from(0_u64) {
right
} else {
left
}
})
.cloned();

region.assign_advice(|| "out b_swap", self.config.b_swapped, 0, || b_swapped)?;
region.assign_advice(|| "out a_swap", self.config.a_swapped, 0, || a_swapped)
},
)
}
}
```

## Multiplexer logic on ECC Points

Mux can also be extended to work with elliptic curve points, facilitating operations like conditional selections between points.
Based on a boolean flag $\textsf{choice}$, it selects between two given points $\textsf{left}$ and $\textsf{right}$.
If $\textsf{choice}$ is true, it returns the point $\textsf{right}$; otherwise, it returns the point $\textsf{left}$.

```rust,ignore,no_run
impl CondSwapChip<pallas::Base> {
/// Given an input `(choice, left, right)` where `choice` is a boolean flag and `left` and `right` are `EccPoint`,
/// returns `left` if `choice` is not set and `right` if `choice` is set.
pub fn mux_on_points(
&self,
mut layouter: impl Layouter<pallas::Base>,
choice: &AssignedCell<pallas::Base, pallas::Base>,
left: &EccPoint,
right: &EccPoint,
) -> Result<EccPoint, plonk::Error> {
let x_cell = self.mux(&mut layouter, choice.clone(), left.x(), right.x())?;
let y_cell = self.mux(&mut layouter, choice.clone(), left.y(), right.y())?;
Ok(EccPoint::from_coordinates_unchecked(
x_cell.into(),
y_cell.into(),
))
}

/// Given an input `(choice, left, right)` where `choice` is a boolean flag and `left` and `right` are
/// `NonIdentityEccPoint`, returns `left` if `choice` is not set and `right` if `choice` is set.
pub fn mux_on_non_identity_points(
&self,
mut layouter: impl Layouter<pallas::Base>,
choice: &AssignedCell<pallas::Base, pallas::Base>,
left: &NonIdentityEccPoint,
right: &NonIdentityEccPoint,
) -> Result<NonIdentityEccPoint, plonk::Error> {
let x_cell = self.mux(&mut layouter, choice.clone(), left.x(), right.x())?;
let y_cell = self.mux(&mut layouter, choice.clone(), left.y(), right.y())?;
Ok(NonIdentityEccPoint::from_coordinates_unchecked(
x_cell.into(),
y_cell.into(),
))
}
}
```



Loading
Loading