Skip to content
zkFOLPublic

About

No description, website, or topics provided.

Resources

Stars

9 stars

Watchers

1 watching

Forks

Repository files navigation

zkFOL

This is a concrete implementation of the arithmetisation of FOL validity over finite models on integers proposed by Gabbay and Mendelsohn in their paper on “Integer SNARKs for First-Order Logic”, as per the draft included in the papers directory. This is based on Gabbay’s paper on “Arithmetisation of computation via polynomial semantics for first-order logic”, fulltext available on the Cryptology ePrint Archive.

The arithmetisation of FOL validity over finite models allows the validity (= the truth) of arbitrarily complex logical assertions over finite models on integers to be succinctly proved through cryptographic techniques. Concretely, this means that a prover can make some assertion about some arrays of whole numbers (just like sheets in a spreadsheet, but more powerful), and prove validity of that assertion to a verifier, just by sending a short cryptographic certificate.

As the reader may know, everything from Turing-complete computation to business compliance logic can be encoded into arrays (spreadsheets) of integers — so this is a powerful and expressive paradigm.

Benefits of the FOL approach are efficiency, expressivity, and susceptibility to powerful compiler optimisation techniques. Thanks to these advantages, the prototype in this repository is thousands of times faster than current state-of-the-art zkVM based techniques, as per our benchmarks. Note that the scales in the benchmarks are logarithmic: every unit represents a 10x speed advantage.

Benchmarks

The prototype is in its early stages and is improving all the time. You can check the current speed of the zkFOL prototype in comparison to existing systems here.

You can also run the benchmarks yourself: just install as per the instructions below, and then run the following in IEX (it might take a minute)

Examples.EBench.report()

zkFOL is an expressive and user-friendly high-level general-purpose coding language, with performance which exceeds zkVM approaches by orders of magnitude and with structure susceptible to efficient compiler optimisation. The benchmarks suggest that we can indeed have the best of all possible worlds: high-level programming, efficient compilation, and fast execution, all in one logic-based package.

Examples

zkFOL is EXPRESSIVE and FAST

Goal Can we write circuits in a way that is both expressive and fast?

Yes. zkFOL is expressive, and it is fast.

# Found in Examples.ESudoku!

defmodule Sudoku do
  defrel sudoku(xs, blocks) do
    length(xs, n)
    n = blocks ** 2
    blocks > 0
    each(xs, permutation(n))
    column(xs, cols)
    each(cols, permutation(n))
    boxes(blocks, xs, bs)
    each(bs, permutation(n))
    each(xs, all_labeled)
  end

  defrel boxes(n, rows, bs) do
    map(rows, chunk(n), runs)
    chunk(n, runs, bands)
    map(bands, column, stacks)
    map(stacks, mapcon, grouped)
    concat(grouped, bs)
  end

  defrel mapcon(xs, ys) do
    map(xs, concat, ys)
  end

  defrel all_labeled(xs) do
    each(xs, label)
  end
end

We just laid out the rules of sudoku, almost directly, with permutation(n) saying the values are distinct.

Can your expressive language run a 9x9 sudoku this fast? zkFOL was made with circuits in mind!

docs/images/sudoku-proof.png

zkFOL can SOLVE

Goal Give an easy way to solve and populate witnesses.

If you are used to imperative programming languages then you might assume that you would need a separate sudoku generator to live alongside its sudoku checker. Not with zkFOL. The logical paradigm is high-level enough that generating and checking are just two applications of the same specification; or to put it another way, the specification is the code. Watch the same codedebase seamlessly solve a sudoku puzzle.

defrel puzzle(
         1,
         [
           [_, _, _, _, _, _, _, _, _],
           [_, _, _, _, _, 3, _, 8, 5],
           [_, _, 1, _, 2, _, _, _, _],
           [_, _, _, 5, _, 7, _, _, _],
           [_, _, 4, _, _, _, 1, _, _],
           [_, 9, _, _, _, _, _, _, _],
           [5, _, _, _, _, _, _, 7, 3],
           [_, _, 2, _, 1, _, _, _, _],
           [_, _, _, _, 4, _, _, _, 9]
         ]
       )

defrel solved(x) do
  puzzle(1, x)
  sudoku(x, 3)
end

Zkfol.eval!(Examples.ESudoku.solved(), [:_], [])
|> Zkfol.Query.statement()
|> Zkfol.compile()

This particular puzzle has a unique solution, however zkFOL can find all solutions.

The found solution here is included almost directly into the witness. We can see the 3 _ 8 5 in column 9.

docs/images/sudoku-solution.png

Here is the actual full witness the system generates for Zinc+:

docs/images/sudoku-witness.png

zkFOL is TUNED for CIRCUITS

Goal Unleash the true potential of circuits.

Witnesses are matrices. Thus, in a ZK system the inputs and outputs have equal status as entries in the witnessing matrix. In true “logic programming” style, we are not restricted to forward computation; backward computation works just as well. Indeed, zkFOL does not need to distinguish between the two.

For example:

def fib(1) do
  1
end

def fib(2) do
  1
end

def fib(n) do
  fib(n - 1) + fib(n - 2)
end

With most systems you can only get the circuit with a known n. But what if you only know the result of fib and not the input? zkFOL lets you generate the witness in whatever way you feel most comfortable.

defmodule Fib do
  use Zkfol.Lang

  defrel fib(1, 1)
  defrel fib(2, 1)

  defrel fib(x, v) do
    x > 2
    fib(x - 1, v1)
    fib(x - 2, v2)
    v = v1 + v2
  end
end

# Proving it backwards
Zkfol.eval!(Fib.fib(), [:_, 55], [])
|> Zkfol.Query.statement()      # Finds fib(10, 55) for you!
|> Zkfol.compile()

# Proving it forwards, same exact witness!

Zkfol.eval!(Fib.fib(), [10, :_], [])
|> Zkfol.Query.statement()
|> Zkfol.compile()


# Want to have the system find many answers for you!?

Zkfol.eval!(Examples.EUser.fib(), [:_, :_], [])

docs/images/fib-answers.png

Our system lets you find and prove the specific one you want. zkFOL is a natural fit for solvers!

Dependencies

Elixir 1.18 on OTP 27+
Consult the installation instructions: here.
Rust
Consult the installation instructions here.
Glamorous Toolkit
This is an optional dependency that lets you visualize your code and cost performances. Installation instructions here.

Step By Step Setup

  1. Install Elixir
    • via asdf. Follow the installation guide here, then grab the toolchain
      asdf plugin add erlang
      asdf plugin add elixir
      asdf install erlang 27.3.4
      asdf install elixir 1.18.4
      asdf global erlang 27.3.4
      asdf global elixir 1.18.4
              
    • via mise. Follow the installation guide here, or in short
      curl https://mise.run | sh
      echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc
      source ~/.bashrc
      mise use -g erlang@27 elixir@1.18
              
  2. Install Rust via rustup
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
        

    If you already have an older Rust, update it instead

    rustup update stable
    rustup default stable
        
  3. Build and test
    mix deps.get
    mix test
        

Known Issues

  • If IEX and test refuse to work, it’s likely the Mnesia store needs to be reset rm -rf ./.mnesiastore-<node>/ (.mnesiastore-test/ for the tests).

Getting Up and Running

This codebase is an interactive one. Meaning that the best way to experience your programs is interactively!

Using IEX

IEX is probably the easiest way to get started.

iex --sname fol -S mix

This will spawn an interactive shell, where we can do fun things like

Zkfol.eval!(Examples.ESudoku.solved(), [:_], []) |> Zkfol.Query.statement() |> Zkfol.compile() |> Zkfol.Log.report()

Here we just solved, proved, and verified a sudoku puzzle. solved gives the sudoku predicate a puzzle with 17 clues, for it to figure out the rest.

For problems that have multiple solutions you can run something like this

ran = Zkfol.compile(Examples.EUser.fib(), args: [8])
query = Zkfol.eval!(Examples.EUser.fib(), [:_, :_])
Zkfol.Query.taken(query)           # [[1, 1]]
Zkfol.Query.next(query)            # {:ok, [2, 1]}

# no number on statement takes the latest answer
Zkfol.Query.statement(query) |> Zkfol.compile() |> Zkfol.Log.report()
# Take the 5th answer
Zkfol.Query.statement(query, 5) |> Zkfol.compile() |> Zkfol.Log.report()
Zkfol.Query.next(query) # {:ok, [6, 8]}

# To close the query
Zkfol.Query.close(query)

Functions in the Zkfol module are user facing, and the Example modules are a good way to get some bearing on the codebase.

A user guide should be coming soon that better explains how it works.

Using Glamorous Toolkit (GT)

GT is the main way the codebase is developed and offers a lot of visual guis to everything. However it takes a bit more setup, and requires you have GT installed.

This section is a WIP, the instructions should be enough for those willing to try, but a better user guide will come in the future.

To grab the package into GT.

Metacello new
      repository: 'github://zkFOL/zkfol:base/src';
      baseline: #Zkfol;
      load.
  • If you are doing active development on the Bridge or AL I suggest running the following instead
    Metacello new
      repository: 'github://zkFOL/zkfol:base/src';
      baseline: #Zkfol;
      load: #dev.
        

From the shell

~/ZKFOL % iex --sname fol -S mix
iex(fol@YU-NO)7> GtBridge.start_listener(1212)

14:55:21.253 [debug] starting tcp listener with [host: {0, 0, 0, 0}, port: 1212]
{:ok, #PID<0.520.0>}

14:55:21.253 [debug] tcp server on port 1212

Now from GT side evaluate

BeamLinkApplication stop.
BeamLinkApplication connectwithServerPort: 1212

Now you are connected.

The above example in IEX can be done more simply now and interactively

Zkfol.eval!(Examples.ESudoku.solved(), Examples.ESudoku.act(), [])
|> Zkfol.Query.statement()
|> Zkfol.compile()

For example evaluating any of these leads you to a gui that lets you find more answers

# Solve for the second argument
Zkfol.eval!(Examples.EUser.fib(), [8, :_], [])
# Solve for the first argument
Zkfol.eval!(Examples.EUser.fib(), [:_, 21], [])
# Solve for both arguments!
Zkfol.eval!(Examples.EUser.fib(), [:_, :_], [])

and then evaluate based on these answers, there are many benefits to using the GT interface.

Getting zkFOL in your project

It’s quite simple to include zkFOL in your Elixir project. Just add

{:zkfol, git: "https://github.com/zkFOL/zkfol.git"},

to the Elixir projects’ dependency.

Git

This codebase follows a git style similar to git or Linux.

New code should be based on base, and no attempt to keep it up to sync with master should be had. When one’s topic is ready, just submit a PR on github and a maintainer will handle any merge conflicts.

Releases happen every so often, however patches may be slated for a specific release, so do not be afraid if a maintainer says the PR is merged but it’s still open, this just means that it’s merged into next or master and will be included in the next scheduled release.

Happy hacking, and don’t be afraid to submit patches.

About

No description, website, or topics provided.

Resources

Stars

9 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages