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

feat: implement Bulletproofs #8

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
24 changes: 16 additions & 8 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.x"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
Expand All @@ -59,20 +59,28 @@ jobs:
pytest
- name: pytest
if: ${{ !startsWith(matrix.platform.target, 'x86') && matrix.platform.target != 'ppc64' }}
uses: uraimo/run-on-arch-action@v2.5.0
uses: uraimo/run-on-arch-action@v2
with:
arch: ${{ matrix.platform.target }}
distro: ubuntu22.04
githubToken: ${{ github.token }}
# Copied from https://github.com/codecov/codecov-rs/blob/main/.github/workflows/publish.yml
install: |
apt-get update
apt-get install -y --no-install-recommends python3 python3-pip
pip3 install -U pip pytest pylint
apt-get install -y gnupg ca-certificates
echo "deb https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy main" >> /etc/apt/sources.list.d/deadsnakes.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776
apt-get update
apt-get install -y --no-install-recommends python3.12 python3.12-venv python3-pip
python3.12 -m venv /venv
source /venv/bin/activate
pip3 install pytest pylint
run: |
set -e
source /venv/bin/activate
pip3 install zksnake --find-links dist --force-reinstall
pylint --disable=R,C,fixme,import-error python
pytest
python3 -m pylint --disable=R,C,fixme,import-error python
python3 -m pytest

windows:
runs-on: ${{ matrix.platform.runner }}
Expand All @@ -87,7 +95,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.x"
architecture: ${{ matrix.platform.target }}
- name: Build wheels
uses: PyO3/maturin-action@v1
Expand Down Expand Up @@ -123,7 +131,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.10"
python-version: "3.x"
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
Expand Down
122 changes: 120 additions & 2 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ rayon = "1.10.0"
serde = {version="1.0.200", features = ["derive"]}
ark-serialize = { version = "0.4", features = ["derive"] }
ark-bls12-381 = "0.4.0"
sha2 = "0.10.8"
bn254_hash2curve = "0.1.2"

[features]
parallel = ["ark-ff/parallel", "ark-poly/parallel", "ark-ec/parallel", "ark-std/parallel"]
parallel = ["ark-ff/parallel", "ark-poly/parallel", "ark-ec/parallel", "ark-std/parallel"]
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ zksnake currently only support **Groth16** proving scheme with `BN254` and `BLS1

## Usage

### Build constraints into QAP
### Build constraints

```python
from zksnake.symbolic import Symbol
Expand All @@ -30,7 +30,7 @@ cs.add_constraint(v1 == x*x)
cs.add_constraint(y - 5 - x == v1*x)
cs.set_public(y)

qap = cs.compile()
r1cs = cs.compile()
```

Alternatively, you can import the constraints from [Circom](https://github.com/iden3/circom):
Expand All @@ -39,7 +39,7 @@ Alternatively, you can import the constraints from [Circom](https://github.com/i
from zksnake.r1cs import ConstraintSystem

cs = ConstraintSystem.from_file("circuit.r1cs", "circuit.sym")
qap = cs.compile()
r1cs = cs.compile()
```

Note that some constraints that are complex or expensive (require off-circuit computation) cannot be imported directly and require you to add "hint" function to pre-define the variable value (see [Example](./examples/example_bitify_circom.py)).
Expand All @@ -50,7 +50,7 @@ Note that some constraints that are complex or expensive (require off-circuit co
from zksnake.groth16 import Setup

# one time setup
setup = Setup(qap)
setup = Setup(r1cs)
prover_key, verifier_key = setup.generate()
```

Expand All @@ -63,7 +63,7 @@ from zksnake.groth16 import Prover, Verifier
public_witness, private_witness = cs.solve({'x': 3}, {'y': 35})

# proving
prover = Prover(qap, prover_key)
prover = Prover(r1cs, prover_key)
proof = prover.prove(public_witness, private_witness)

# verification
Expand Down
18 changes: 18 additions & 0 deletions examples/example_range_proof.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""
Prove that v is in range of [0, 2^32-1] without revealing the value of v itself
using Inner Product Argument (Bulletproofs)
"""
from zksnake.bulletproofs.range_proof import Prover, Verifier

bitsize = 32
prover = Prover(bitsize, 'BN254')

# secret value v
value = 133337

proof, commitment = prover.prove(value)
print("Proof:", proof.to_bytes().hex())

verifier = Verifier(bitsize, 'BN254')
assert verifier.verify(proof, commitment)
print("Proof is valid!")
42 changes: 0 additions & 42 deletions examples/example_rsa.py

This file was deleted.

Empty file.
Loading