From f2643a291339faa25d4d07d1006526b727fcc706 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 17 Jun 2024 21:51:02 +0200 Subject: Implement Finite Prime Field Adds a finite prime field implementation that is used by the BN254 curve and pairings. Extended finite fields will be coming next. --- .gitignore | 1 + .ocamlformat | 0 README.md | 4 ++ dune-project | 24 ++++++++++++ lib/dune | 4 ++ lib/fp.ml | 25 ++++++++++++ lib/fp.mli | 58 +++++++++++++++++++++++++++ lib/plonkers.ml | 1 + plonkers.opam | 33 ++++++++++++++++ test/dune | 3 ++ test/test_fp.ml | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/test_plonkers.ml | 1 + test/testable.ml | 3 ++ 13 files changed, 263 insertions(+) create mode 100644 .gitignore create mode 100644 .ocamlformat create mode 100644 README.md create mode 100644 dune-project create mode 100644 lib/dune create mode 100644 lib/fp.ml create mode 100644 lib/fp.mli create mode 100644 lib/plonkers.ml create mode 100644 plonkers.opam create mode 100644 test/dune create mode 100644 test/test_fp.ml create mode 100644 test/test_plonkers.ml create mode 100644 test/testable.ml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69fa449 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_build/ diff --git a/.ocamlformat b/.ocamlformat new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md new file mode 100644 index 0000000..736f0dc --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# PLONKers! + +This repository contains a from-scratch implementation of a PLONK zero knowledge +proof system for education purposes over the BN254 (alt_bn128) curve. diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..d4c66cc --- /dev/null +++ b/dune-project @@ -0,0 +1,24 @@ +(lang dune 3.15) + +(name plonkers) + +(generate_opam_files true) + +(source + (sourcehut nlordell/plonkers)) + +(authors "Nicholas Rodrigues Lordello") + +(maintainers "Nicholas Rodrigues Lordello") + +(license GPL-3.0-only) + +(documentation https://git.sr.ht/~nlordell/plonkers) + +(package + (name plonkers) + (synopsis "PLONK From Scratch") + (description "A from-scratch implementation of a PLONK zero knowledge proof for educational purposes.") + (depends ocaml dune zarith) + (tags + (topics "zero knowledge" zkp plonk))) diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..582b094 --- /dev/null +++ b/lib/dune @@ -0,0 +1,4 @@ +(library + (name plonkers) + (public_name plonkers) + (libraries zarith)) diff --git a/lib/fp.ml b/lib/fp.ml new file mode 100644 index 0000000..2489f11 --- /dev/null +++ b/lib/fp.ml @@ -0,0 +1,25 @@ +type t = Z.t + +let p = + Z.of_string + "21888242871839275222246405745257275088696311157297823662689037894645226208583" + +let p_minus_2 = Z.(p - ~$2) +let mod_p x = Z.erem x p +let zero = Z.zero +let one = Z.one +let of_string s = Z.of_string s |> mod_p +let to_string = Z.to_string +let equal = Z.equal +let neg a = Z.neg a |> mod_p +let inv a = Z.powm a p_minus_2 p +let add a b = Z.add a b |> mod_p +let sub a b = add a (neg b) +let mul a b = Z.mul a b |> mod_p +let div a b = mul a (inv b) +let ( ~- ) = neg +let ( ~/ ) = inv +let ( + ) = add +let ( - ) = sub +let ( * ) = mul +let ( / ) = div diff --git a/lib/fp.mli b/lib/fp.mli new file mode 100644 index 0000000..f73fe7f --- /dev/null +++ b/lib/fp.mli @@ -0,0 +1,58 @@ +(** Finite field used by the BN254 curve *) + +type t +(** A type representing an element in the finite field *) + +val zero : t +(** The zero value. *) + +val one : t +(** The one value. *) + +val of_string : string -> t +(** [of_string s] returns the field element represented by [s]. *) + +val to_string : t -> string +(** [to_string a] returns the string representation of [a]. *) + +val equal : t -> t -> bool +(** [equal a b] returns [true] if [a] is equal to [b]. *) + +val neg : t -> t +(** [neg a] is the additive inverse of [a]. [add a (neg a) = 0] for all [a]. *) + +val inv : t -> t +(** [inv a] is the multiplicative inverse of [a]. [mul a (inv a) = 1] for all + non-zero [a]. Can raise [Divide_by_zero]. *) + +val add : t -> t -> t +(** [add a b] is the sum of [a] plus [b] *) + +val sub : t -> t -> t +(** [sub a b] is the difference of [a] minus [b]. It is equivalent to + [add a (neg b)]. *) + +val mul : t -> t -> t +(** [add a b] is the product of [a] times [b] *) + +val div : t -> t -> t +(** [div a b] is the field division of [a] by [b]. It is equivalent to + [mul a (inv b)]. Can raise [Divide_by_zero]. *) + +val ( ~- ) : t -> t +(** [neg]. *) + +val ( ~/ ) : t -> t +(** [inv]. *) + +val ( + ) : t -> t -> t +(** [add]. *) + +val ( - ) : t -> t -> t +(** [sub]. *) + +val ( * ) : t -> t -> t +(** [mul]. *) + +val ( / ) : t -> t -> t +(** [div]. *) diff --git a/lib/plonkers.ml b/lib/plonkers.ml new file mode 100644 index 0000000..6076a6b --- /dev/null +++ b/lib/plonkers.ml @@ -0,0 +1 @@ +module Fp = Fp diff --git a/plonkers.opam b/plonkers.opam new file mode 100644 index 0000000..dcef03d --- /dev/null +++ b/plonkers.opam @@ -0,0 +1,33 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "PLONK From Scratch" +description: + "A from-scratch implementation of a PLONK zero knowledge proof for educational purposes." +maintainer: ["Nicholas Rodrigues Lordello"] +authors: ["Nicholas Rodrigues Lordello"] +license: "GPL-3.0-only" +tags: ["topics" "zero knowledge" "zkp" "plonk"] +homepage: "https://sr.ht/~nlordell/plonkers" +doc: "https://git.sr.ht/~nlordell/plonkers" +bug-reports: "https://todo.sr.ht/~nlordell/plonkers" +depends: [ + "ocaml" + "dune" {>= "3.15"} + "zarith" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://git.sr.ht/~nlordell/plonkers" diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..73ee316 --- /dev/null +++ b/test/dune @@ -0,0 +1,3 @@ +(test + (name test_plonkers) + (libraries alcotest plonkers)) diff --git a/test/test_fp.ml b/test/test_fp.ml new file mode 100644 index 0000000..536b059 --- /dev/null +++ b/test/test_fp.ml @@ -0,0 +1,106 @@ +module Fp = Plonkers.Fp + +let fp = Testable.make Fp.to_string Fp.equal +let f = Fp.of_string + +let test_add () = + Alcotest.(check fp) + "equals" + (f + "5034212870056682811541563993093442956024207074167934414259669251040086104221") + (Fp.add + (f + "3019612232293951348432769707427639303474751231631017441050730061639747788964") + (f + "2014600637762731463108794285665803652549455842536916973208939189400338315257")); + Alcotest.(check fp) + "equals" Fp.zero + (Fp.add + (f + "21888242871839275222246405745257275088696311157297823662689037894645226208582") + Fp.one); + Alcotest.(check fp) + "equals" + (f + "7363893478117210948657131929839491162862302555393162961985689652909776739235") + (Fp.add + (f + "7363893478117210948657131929839491162862302555393162961985689652909776739235") + Fp.zero) + +let test_sub () = + Alcotest.(check fp) + "equals" + (f + "1647997538064335793847192302859723563372189463772006638527106196094592175035") + (Fp.sub + (f + "17522329235179667504196475280824254606128390327458085261948007699233694805172") + (f + "15874331697115331710349282977964531042756200863686078623420901503139102630137")); + Alcotest.(check fp) + "equals" + (f + "21888242871839275222246405745257275088696311157297823662689037894645226208582") + (Fp.sub Fp.zero Fp.one); + Alcotest.(check fp) + "equals" + (f + "1550460075629904631277637708383660261580971272511193857451434052003108082807") + (Fp.add + (f + "1550460075629904631277637708383660261580971272511193857451434052003108082807") + Fp.zero) + +let test_mul () = + Alcotest.(check fp) + "equals" + (f + "20539703399960180369319660518633290048071922000666914740466234074847854697757") + (Fp.mul + (f + "16652515618518207323315643285221741056245295412987863357779458647516886387587") + (f + "15388577223719168739739036375931130294575142146897880107473273404618844312776")); + Alcotest.(check fp) + "equals" Fp.zero + (Fp.mul + (f + "10935302729977221140784701859408855206669147633587285404353277660075789680922") + Fp.zero) + +let test_div () = + Alcotest.(check fp) + "equals" + (f + "7363953525538778156567393633860492517850968441297156550375719598579598367322") + (Fp.div + (f + "7363953525538778156567393633860492517850968441297156550375719598579598367322") + Fp.one) + +let test_inv () = + Alcotest.(check fp) + "equals" + (f + "14812345552713361047837749161990489217154524375614120373819285291721752372748") + (Fp.inv + (f + "1160181877204925047836599448201084379181489517022839288462621953652233427695")); + Alcotest.(check fp) + "equals" Fp.one + (let a = + f + "17876831255806984619368587498426968568242044248616647019388897055879503636463" + in + Fp.mul a (Fp.inv a)) + +let tests = + Alcotest. + [ + test_case "Addition" `Quick test_add; + test_case "Subtraction" `Quick test_sub; + test_case "Multiplication" `Quick test_mul; + test_case "Division" `Quick test_mul; + test_case "Multiplicative Inverse" `Quick test_inv; + ] diff --git a/test/test_plonkers.ml b/test/test_plonkers.ml new file mode 100644 index 0000000..16db4ca --- /dev/null +++ b/test/test_plonkers.ml @@ -0,0 +1 @@ +let () = Alcotest.run "Plonkers" [ ("Fp", Test_fp.tests) ] diff --git a/test/testable.ml b/test/testable.ml new file mode 100644 index 0000000..914b135 --- /dev/null +++ b/test/testable.ml @@ -0,0 +1,3 @@ +let make str eq = + let pp fmt a = str a |> Format.fprintf fmt "%s" in + Alcotest.testable pp eq -- cgit v1.2.3