Skip to content

Commit

Permalink
Merge branch 'main' into petscheit/main
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Dec 10, 2024
2 parents ad3933c + 245c09f commit d609d5f
Show file tree
Hide file tree
Showing 117 changed files with 44,074 additions and 118,884 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cairo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- uses: actions/checkout@v3
- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.4"
scarb-version: "2.9.1"
- run: scarb fmt --check
working-directory: src/
- run: cd src/ && scarb test
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Setup Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.4"
scarb-version: "2.9.1"
- name: Install dependencies
run: make setup

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/hydra.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- name: Set up Scarb
uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.4"
scarb-version: "2.9.1"
- name: Run make rewrite and check for unstaged changes
run: |
source venv/bin/activate
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/maturin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ jobs:
strategy:
matrix:
platform:
- runner: macos-12
- runner: macos-14
target: x86_64
- runner: macos-14
target: aarch64
Expand Down Expand Up @@ -164,7 +164,7 @@ jobs:

- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.4"
scarb-version: "2.9.1"
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:

- uses: software-mansion/setup-scarb@v1
with:
scarb-version: "2.8.4"
scarb-version: "2.9.1"
- name: Set up Python 3.10.14
uses: actions/setup-python@v5
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To get started with Garaga, you'll need to have some tools and dependencies inst

Ensure you have the following installed:
- [Python 3.10](https://www.python.org/downloads/) - /!\ Make sure `python3.10` is a valid command in your terminal. The core language used for development. Make sure you have the correct dependencies installed (in particular, GMP) for the `fastecdsa` python package. See [here](https://pypi.org/project/fastecdsa/#installing) for linux and [here](https://github.com/AntonKueltz/fastecdsa/issues/74) for macos.
- [Scarb 2.8.4](https://docs.swmansion.com/scarb/download.html) - The Cairo package manager. Comes with Cairo inside. Requires [Rust](https://www.rust-lang.org/tools/install).
- [Scarb 2.9.1](https://docs.swmansion.com/scarb/download.html) - The Cairo package manager. Comes with Cairo inside. Requires [Rust](https://www.rust-lang.org/tools/install).

##### Optionally :

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ garaga
Once your verifying key is ready, you can use the following command : 

```
garaga gen --system groth16 --vk vk.json
garaga gen --system groth16 --vk vk.json
```

You should see an output like this :

```bash
(venv) :~/garaga-flow garaga gen --system groth16 --vk vk_bls.json
Please enter the name of your project. Press enter for default name. [my_project]:
(venv) :~/garaga-flow garaga gen --system groth16 --vk vk_bls.json
Please enter the name of your project. Press enter for default name. [my_project]:
Detected curve: CurveID.BLS12_381
⠧ Generating Smart Contract project for ProofSystem.Groth16 using vk_bls.json...
Done!
Expand Down Expand Up @@ -301,41 +301,41 @@ MAINNET_ACCOUNT_ADDRESS=0x4
```
{% endcode %}

Then, you can run the command `garaga declare-project`, which will build the contract and declare it to Starknet. If the class hash is already deployed, it will return it as well. Declaring the contract involves sending all its bytecode and it is quite an expensive operation. Make sure you dapp is properly tested before!
Then, you can run the command `garaga declare`, which will build the contract and declare it to Starknet. If the class hash is already deployed, it will return it as well. Declaring the contract involves sending all its bytecode and it is quite an expensive operation. Make sure you dapp is properly tested before!

```bash
Usage: garaga declare-project [OPTIONS]
Declare your smart contract to Starknet. Obtain its class hash and a explorer link.
Usage: garaga declare [OPTIONS]

Declare your smart contract to Starknet. Obtain its class hash and a explorer link.

╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --project-path DIRECTORY Path to the Cairo project holding the Scarb.toml file to declare
│ [default: /home/felt/PycharmProjects/garaga-flow/my_project/] │
│ [default: /home/felt/garaga-flow/my_project/] │
│ --env-file FILE Path to the environment file │
│ [default: /home/felt/PycharmProjects/garaga-flow/my_project/.secrets] │
│ [default: /home/felt/garaga-flow/my_project/.secrets] │
│ --network [sepolia|mainnet] Starknet network [default: sepolia] │
│ --fee TEXT Fee token type [eth, strk] [default: eth] │
│ --help -h Show this message and exit. │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
```

```bash
garaga declare-project --project-path my-project/ --env-file .secrets --network sepolia --fee strk
garaga declare --project-path my_project/ --env-file .secrets --network sepolia --fee strk
```

This command will return the class hash, used in the next step.\
\
Finally, to deploy the contract, a use `garaga deploy-project` :
Finally, to deploy the contract, a use `garaga deploy` :

```bash
Usage: garaga deploy-project [OPTIONS]
Deploy an instance of a smart contract class hash to Starknet. Obtain its address, the available endpoints and a explorer link.
Usage: garaga deploy [OPTIONS]

Deploy an instance of a smart contract class hash to Starknet. Obtain its address, the available endpoints and a explorer link.

╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
* --class-hash TEXT Contract class hash to deploy. Can be decimal or hex string [default: None] [required] │
│ --env-file FILE Path to the environment file containing rpc, address, private_key │
│ [default: /home/felt/PycharmProjects/garaga-flow/my_project/.secrets] │
│ [default: /home/felt/garaga-flow/my_project/.secrets] │
│ --network [sepolia|mainnet] Starknet network [default: sepolia] │
│ --fee TEXT Fee token type [eth, strk] [default: strk] │
│ --help -h Show this message and exit. │
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ The Garaga CLI takes care of converting your proof to the correct calldata and c
To do this, use the garaga `verify-onchain` command. 

```bash
Usage: garaga verify-onchain [OPTIONS]
Invoke a SNARK verifier on Starknet given a contract address, a proof and a verification key.
Usage: garaga verify-onchain [OPTIONS]

Invoke a SNARK verifier on Starknet given a contract address, a proof and a verification key.

╭─ Options ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
* --system [groth16] Proof system [default: None] [required] │
* --contract-address TEXT Starknet contract address [default: None] [required] │
Expand Down Expand Up @@ -73,7 +73,7 @@ The command should look like this:

{% code overflow="wrap" %}
```
garaga verify-onchain --system groth16 --address 0x1234... --vk vk.json --proof proof.json --public-inputs public.json --env-file .secrets --network sepolia
garaga verify-onchain --system groth16 --contract-address 0x1234... --vk vk.json --proof proof.json --public-inputs public.json --env-file .secrets --network sepolia
```
{% endcode %}

Expand Down
2 changes: 1 addition & 1 deletion docs/gitbook/installation/developer-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ icon: wrench
To work with Garaga, you need the following dependencies : 

* Python 3.10. The command `python3.10` should be available and working in your terminal. 
* [Scarb](https://docs.swmansion.com/scarb/download.html) v2.8.4. 
* [Scarb](https://docs.swmansion.com/scarb/download.html) v2.9.1. 
* [Rust](https://www.rust-lang.org/tools/install)

Simply clone the [repository](https://github.com/keep-starknet-strange/garaga) :
Expand Down
18 changes: 13 additions & 5 deletions hydra/garaga/algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def __rtruediv__(self, left: PyFelt | int) -> PyFelt:
return self.__inv__().__mul__(left)

def is_quad_residue(self) -> bool:
if self.value == 0:
return True
return legendre_symbol(self.value, self.p) == 1

def sqrt(self, min_root: bool = True) -> PyFelt:
Expand Down Expand Up @@ -390,6 +392,8 @@ class ModuloCircuitElement:
emulated_felt: PyFelt
offset: int

__repr__ = lambda self: f"ModuloCircuitElement({hex(self.value)}, {self.offset})"

@property
def value(self) -> int:
return self.emulated_felt.value
Expand Down Expand Up @@ -1043,12 +1047,16 @@ def degrees_infos(self) -> dict[str, dict[str, int]]:
"b": self.b.degrees_infos(),
}

def validate_degrees(self, msm_size: int) -> bool:
def validate_degrees(self, msm_size: int, batched: bool = True) -> bool:
degrees = self.degrees_infos()
assert degrees["a"]["numerator"] <= msm_size + 1
assert degrees["a"]["denominator"] <= msm_size + 2
assert degrees["b"]["numerator"] <= msm_size + 2
assert degrees["b"]["denominator"] <= msm_size + 5
if batched:
extra = 2
else:
extra = 0
assert degrees["a"]["numerator"] <= msm_size + 1 + extra
assert degrees["a"]["denominator"] <= msm_size + 2 + extra
assert degrees["b"]["numerator"] <= msm_size + 2 + extra
assert degrees["b"]["denominator"] <= msm_size + 5 + extra
return True

def print_as_sage_poly(self, var: str = "x", as_hex: bool = False) -> str:
Expand Down
56 changes: 55 additions & 1 deletion hydra/garaga/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
SECP256K1_ID = 2
SECP256R1_ID = 3
ED25519_ID = 4
GRUMPKIN_ID = 5


class ProofSystem(Enum):
Groth16 = "groth16"
UltraKeccakHonk = "ultra_keccak_honk"

@property
def supported_curves(self) -> set[int]:
if self == ProofSystem.Groth16:
return {BN254_ID, BLS12_381_ID}
if self == ProofSystem.UltraKeccakHonk:
return {BN254_ID}
return set()


Expand All @@ -43,6 +47,7 @@ class CurveID(Enum):
SECP256K1 = 2
SECP256R1 = 3
ED25519 = 4
GRUMPKIN = 5

@staticmethod
def from_str(s: str) -> "CurveID":
Expand Down Expand Up @@ -428,6 +433,19 @@ def bit(value, index):
Gy=0x6666666666666666666666666666666666666666666666666666666666666658,
swu_params=None,
),
GRUMPKIN_ID: WeierstrassCurve(
cairo_zero_namespace_name="grumpkin",
id=GRUMPKIN_ID,
p=0x30644E72E131A029B85045B68181585D2833E84879B9709143E1F593F0000001,
n=0x30644E72E131A029B85045B68181585D97816A916871CA8D3C208C16D87CFD47,
h=1,
a=0,
b=-17 % 0x30644E72E131A029B85045B68181585D2833E84879B9709143E1F593F0000001,
fp_generator=5,
Gx=0x1,
Gy=0x2CF135E7506A45D632D270D45F1181294833FC48D823F272C,
swu_params=None,
),
}

ISOGENY_MAP_G2 = {
Expand Down Expand Up @@ -617,6 +635,9 @@ class G1Point:
curve_id: CurveID
iso_point: bool = False

def __repr__(self) -> str:
return f"G1Point({hex(self.x)}, {hex(self.y)}) on {self.curve_id.value}"

def __str__(self) -> str:
return f"G1Point({self.x}, {self.y}) on curve {self.curve_id}"

Expand Down Expand Up @@ -920,6 +941,9 @@ class G2Point:
y: tuple[int, int]
curve_id: CurveID

def __repr__(self):
return f"G2Point({hex(self.x[0])}, {hex(self.x[1])}, {hex(self.y[0])}, {hex(self.y[1])}, {self.curve_id})"

def __post_init__(self):
assert isinstance(CURVES[self.curve_id.value], PairingCurve)
if self.is_infinity():
Expand Down Expand Up @@ -1294,5 +1318,35 @@ def replace_consecutive_zeros(lst):
return result


def recode_naf_bits(lst):
result = []
i = 0
while i < len(lst):
if i < len(lst) - 1 and lst[i] == 0 and (lst[i + 1] == 1 or lst[i + 1] == -1):
# "01" or "0-1"
if lst[i + 1] == 1:
result.append(3) # Replace "01" with 3
else:
result.append(4) # Replace "0-1" with 4
i += 2
elif i < len(lst) - 1 and (lst[i] == 1 or lst[i] == -1) and lst[i + 1] == 0:
# "10" or "-10"
if lst[i] == 1:
result.append(1) # Replace 10 with 6
else:
result.append(2) # Replace -10 with 7
i += 2
elif i < len(lst) - 1 and lst[i] == 0 and lst[i + 1] == 0:
result.append(0) # Replace consecutive zeros with 0
i += 2
else:
raise ValueError(f"Unexpected bit sequence at index {i}")
return result


if __name__ == "__main__":
pass
r = recode_naf_bits(jy00(6 * 0x44E992B44A6909F1 + 2)[2:])
print(r, len(r))

# bls = [int(x) for x in bin(0xD201000000010000)[2:]][2:]
# recode_naf_bits(bls)
26 changes: 26 additions & 0 deletions hydra/garaga/hints/ecip.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,32 @@ def print_ff(ff: FF):
return string


def n_points_from_n_coeffs(n_coeffs: int, batched: bool) -> int:
if batched:
extra = 4 * 2
else:
extra = 0

# n_coeffs = 10 + 4n_points => 4n_points = n_coeffs - 10
assert n_coeffs >= 10 + extra
assert (n_coeffs - 10 - extra) % 4 == 0
return (n_coeffs - 10 - extra) // 4


def n_coeffs_from_n_points(n_points: int, batched: bool) -> tuple[int, int, int, int]:
if batched:
extra = 2
else:
extra = 0

return (
1 + n_points + extra,
1 + n_points + 1 + extra,
1 + n_points + 1 + extra,
1 + n_points + 4 + extra,
)


if __name__ == "__main__":
import random

Expand Down
Loading

0 comments on commit d609d5f

Please sign in to comment.