Coder Social home page Coder Social logo

keep-starknet-strange / garaga Goto Github PK

View Code? Open in Web Editor NEW
169.0 4.0 37.0 13.22 MB

State-of-the-art Elliptic Curve operations and SNARKS verification for Cairo & Starknet 🐺.

License: MIT License

Python 8.10% Makefile 0.02% Cairo 86.44% Shell 0.03% Go 2.15% Assembly 2.32% Sage 0.38% Starlark 0.01% Rust 0.03% Jupyter Notebook 0.50%
elliptic-curve-cryptography elliptic-curves pairing snarks zero-knowledge zero-knowledge-proofs zk-snarks zkp cairo-lang starknet

garaga's People

Contributors

0x666c6f avatar abdelstark avatar bacharif avatar drspacemn avatar feltroidprime avatar petscheit avatar raugfer avatar swapnilraj avatar tekkac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

garaga's Issues

Implement Modular Polynomial Addition in Fp

Fp arithmetics using polynomial representation of field elements not only require modular polynomial multiplication, which should be completed (see #6) before starting this task, but also modular polynomial addition.

Based on the work on modular polynomial multiplication in mul_mod.py , implement a modular polynomial addition algorithm for BN curves.
Given a,b € Fp, A and B their respective polynomial representation, and p=F(t), t fixed for a BN prime, compute C=A+B and and find out how to reduce the coefficients so that C(t) == a+b mod p.

Implement pure python Hints for inverse functions to prepare for whitelisting

Currently, inverse functions in e2 and e12 are using hints for efficient computations.
The inverse is computed using the gnark library.

They should be computed in pure python to allow whitelisting of the hints on Starknet.
Some possible working code for pure python inversion is available in tests/cairo_programs/libs/

Update the inverse hints in src/bn254/towers/e2.cairo and e12.cairo
All protostar tests should pass.
Try to make the python hints as clean/succint as possible. It must be self-contained, ie: no imports. Define the function inside the hint.

bug: Fix precompiles test

Update tests/protostar_tests/bn254/test_precompiles.cairo and precompiles contract to reflect the changes in the pairing.cairo library for bn254. (Multi pairing now available)
the command protostar test-cairo0 tests/protostar_tests/bn254/test_precompiles.cairo --max-steps 5000000 should pass

feat: Implement scalar_to_base_neg3_le in Cairo1

Counting the positive and negative multiplicities of the base (-3) decomposition of a u128 is needed for efficient elliptic curve scalar multiplication.

Implement the Cairo1 version of

def scalar_to_base_neg3_le(u128: int) -> tuple[int, int, int, int]:
def sign(felt252: int) -> int:
# In cairo, the felt252 :
# - is considered positive if it is in [0, STARK//2[
# - is considered negative if it is in ]STARK//2, STARK[
# Where STARK = 2**251+ 17* 2**192 + 1
# Note : value being exactly STARK//2 will not happen in the construction here.
if felt252 > 0:
return 1
elif felt252 < 0:
return -1
else:
# Note : the case where the input is 0, the sign does not matter.
# Any value could be returned, so choose whatever is more performant in implementation.
return 0
digits = neg_3_base_le(u128)
# Even if input is u128, both sum_p and sum_n might be larger and negative. Output of positive_negative_multiplicities should be felt252.
sum_p, sum_n = positive_negative_multiplicities(digits)
# return type : felt252, felt252, felt252, felt252
return (abs(sum_p), abs(sum_n), sign(sum_p), sign(sum_n))

Annotations were added on the python function to help you on the expected types.

FYI, the CairoZero version of it is here

func scalar_to_base_neg3_le{range_check_ptr}(scalar: felt, neg_3_pow: felt*) -> (
where the decomposition in digits happens inside the hint and is verified.

Since it is not possible in Cairo1, you must also implement the following function

def neg_3_base_le(scalar: int) -> list[int]:
"""
Decomposes a scalar into base -3 representation.
:param scalar: The integer to be decomposed.
:return: A list of coefficients in base -3 representation. (Least significant bit first),
with digits [-1, 0, 1] such that scalar = sum((-3) ** i * d for (i, d) in enumerate(digits))
"""
if scalar == 0:
return [0]
digits = []
while scalar != 0:
remainder = scalar % 3
if (
remainder == 2
): # if the remainder is 2, we set it to -1 and add 1 to the next digit
remainder = -1
scalar += 1
# For remainder 1 and 0, no change is required
digits.append(remainder)
scalar = -(scalar // 3) # divide by -3 for the next digit
return digits
then count the positive and negative multiplicities.

However, the CairoZero version may still contains important information or clarifications.

  • Be sure to implement this function for u128 in input.
  • Use constant array of felt252 for powers of (-3) % STARK where STARK = 2^251+ 17*2^192 + 1. , similarly to the Cairo0 version.
  • Write the utils functions in the contracts branch of the repo, under src/cairo/src/utils.cairo. Generate some test vectors with the python functions and test it. Try to automatize the creation of a test function to simplify its generation.
  • Be as performant as possible. Benchmark your implementation.

Implement "Traditional" Towered Extension Fields for Fp12 arithmetics

Relevant references and explanations are present in section 7.3 of Document A.
Direct formulas can be found in appendix of Document C.
Use (already or naïvely) implemented Cairo functions for modular addition and multiplication in Fp to construct the tower of Fields for Fp12.

feat: Increase BN254 base to 2**96

For better security of linear combination in the polynomial commitment, BN254 base should be increased to 96 bits.
Change it in curve.cairo and update constants and relevant hints

Implement BLS12-381 arithmetics and Pairing

Following the working implementation of pairing and arithmetics for bn254, create a folder in src/bls12-381 and implement everything similarly to bn254.
Create a subfolder in tests/protostar_tests for bls12-381.
Update the parsing tool from gnark for this curve.

Add needed gnark tower operations to go cli

Feature Request

Describe the Feature Request

Update the go CLI implementation with the needed e12 and e6 operations

Describe Preferred Solution

  • Add an argument to the command to be able to pick the field to do the operations from (e2, e6 and e12).
    Currently, only e2 is available.
  • Modify main.go CLI to be able to call operation using a specific command and parse the specific input.

Describe Alternatives

Related Code
old:
./main op 1 2 3 4
needed
./main field op [x as a list of fp limbs] [y as a list of fp limbs]̀

Additional Context

If the feature request is approved, would you be willing to submit a PR?
(Help can be provided if you need assistance submitting a PR)

  • Yes
  • No

feat: Verify Noir circuits

Write a verifier in python for Honk proofs based on this repo : https://github.com/Maddiaa0/honk-verifier/tree/master

how to :
- use garaga's python classes : [PyFelt](https://github.com/keep-starknet-strange/garaga/blob/ 07ad865/src/algebra.py#L5) for modular arithmetic operations.
- use / add (if needed) Elliptic curves information and constants from definitions.py
- write the verifier in a single python file under src/precompiled_circuits/honk.py.
- Do not write a ModuloCircuit class (this will be done later), only the verifier inside a function.
- You can copy paste and re-use the code here for EC scalar multiplication https://github.com/feltroidprime/zk-ecip-py/blob/main/src/curve.py when the solidity is using the ECMul/EcADD precompile
- You can use the gnark CLI backend when the solidity is using the EcPairing precompile
- add some python functions to parse and map the honk proof into a proper python dataclass inside honk.py
- write a similar test than in the honk-verifier repo inside the if __name__ == "__main__": part of the honk.py file.
- write clear code with type hints

Done.

Next task : Write a cairo implementation.
Requires #112

Parse Gnark go library functions from python hints and cairo

We want to test Garaga against functions from https://github.com/ConsenSys/gnark-crypto/tree/master/ in our protostar tests. Gnark is an audited and trustworthy library which can be used for reference.

For example, this function that has 4 ints as input https://github.com/ConsenSys/gnark-crypto/blob/1a7a29904a7c29ba3e1c644d9752765b6ae9775e/ecc/bn254/internal/fptower/e2.go#L114

The scenario would be to use a hint to run a bash command which executes go code from inside python with the arguments parsed from Cairo, and parse stdout in python, write them to cairo, and then assert the correct in Cairo to test the functions.

Provide an example on the add function of two E2 elements in Gnark, that translates to a Fq2.add() function in cairo.
Write a protostar test with the correct hint to parse and run the go functions, and test the resulting value.

Make the tool as reusable and reproducible as possible for other functions from the Gnark library.

feat: CairoVM Rust integration

Work on https://github.com/feltroidprime/cairo-vm

  • Implement bn254 hints, with unit cairo programs tests for all of them
  • Integrate the CairoVM into the garaga repo, modify make-run scripts to run the program with CairoVM
  • Handle input.json with a specific hint that loops through all the variable in the .json and fill them to the corresponding variable names in the Cairo memory.

@bacharif

feat: LOVE a pairing delegation algorithm

Describe how https://eprint.iacr.org/2021/1029.pdf could be implemented assuming Starknet will be the weak client having access to a source of randomness, and locally-proven Cairo program being the strong server.
This should consist of a smart contract having two functions for pre-computing and verifying values on-chain and a local Cairo program. Local cairo might not even be needed in fact, the results may be passed as call data. It that's the case, it is a big win.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.