aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-06-18 10:09:32 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-06-18 10:09:40 +0200
commit4b5936586e570b125b1c68d04b1479262af9c98b (patch)
tree9646f29a15abe369f66afcbe053a959246ccebbe
parentf2643a291339faa25d4d07d1006526b727fcc706 (diff)
Journal and next steps
In this commit, we refactor the module to contain all the fields that we need: namely the prime field for the BN254 curve, but also the extension fields (polynomial rings) needed for the elliptic curve pairings used by PLONK. In addition, I added a "journal" for documenting findings as I go as well as useful resources I used when implementing this from scratch.
-rw-r--r--README.md3
-rw-r--r--docs/journal/README.md8
-rw-r--r--docs/journal/pages/field-extension.md11
-rw-r--r--docs/journal/pages/finite-fields.md25
-rw-r--r--docs/journal/pages/template.md11
-rw-r--r--lib/field.ml55
-rw-r--r--lib/fields.ml42
-rw-r--r--lib/fields.mli34
-rw-r--r--lib/fp.ml25
-rw-r--r--lib/fp.mli58
-rw-r--r--lib/plonkers.ml2
-rw-r--r--test/test_fp.ml66
12 files changed, 221 insertions, 119 deletions
diff --git a/README.md b/README.md
index 736f0dc..8115352 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,3 @@
# PLONKers!
-This repository contains a from-scratch implementation of a PLONK zero knowledge
-proof system for education purposes over the BN254 (alt_bn128) curve.
+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/docs/journal/README.md b/docs/journal/README.md
new file mode 100644
index 0000000..7407bff
--- /dev/null
+++ b/docs/journal/README.md
@@ -0,0 +1,8 @@
+# PLONKers! Development Journal
+
+This directory contains the development journal for PLONKers!. It is a collection of markdown files that document the development process as well as concepts that I learned along the way.
+
+## Table of Contents
+
+1. [Finite Fields](./pages/finite-fields.md)
+2. [Field Extension](./pages/field-extension.md)
diff --git a/docs/journal/pages/field-extension.md b/docs/journal/pages/field-extension.md
new file mode 100644
index 0000000..6494df5
--- /dev/null
+++ b/docs/journal/pages/field-extension.md
@@ -0,0 +1,11 @@
+> [Index](../README.md)
+
+# Title
+
+Body.
+
+# Resources
+
+- [Polynomial Ring](https://en.wikipedia.org/wiki/Polynomial_ring)
+
+> [Index](../README.md)
diff --git a/docs/journal/pages/finite-fields.md b/docs/journal/pages/finite-fields.md
new file mode 100644
index 0000000..43518f7
--- /dev/null
+++ b/docs/journal/pages/finite-fields.md
@@ -0,0 +1,25 @@
+> [Index](../README.md)
+
+# Finite Fields
+
+One of the mathematical principles underpinning the PLONK proof systems are finite fields. These are used in both the PLONK circuits themselves, but also in the eliptic curve pairings necessary for the proof system.
+
+In PLONKers!, we are building a very specific PLONK construction over the BN254 curve (sometimes referred to as "alt-bn128" or "bn128" - why? They are **B**arreto-**N**aehrig curves with a **254**-bit prime and an initially estimated security of **128** bits). This curve is defined over a finite field with a prime modulus. This sounds complicated, but it really means that:
+
+- `p = 21888242871839275222246405745257275088696311157297823662689037894645226208583`, a very large prime
+- It is the set of whole numbers from `0` to `p - 1` (inclusive)
+- Addition and multiplication are done modulo `p`
+- Substraction is the addition of the additive inverse (`-a = p - a`, so `a + (-a) = 0` for all `a`)
+- Division is the multiplication by the multiplicative inverse (`1/a = a ^ (p - 2) mod p`, so `a * (1/a) = 1` for all `a` except `0`, the inverse of `0` is undefined)
+
+And with this we build the "numbers" that we use for PLONK and eliptic curve operations.
+
+The BLS12-381 curve as it has some nicer properties over BN254, and may show up in a future episode :).
+
+# Resources
+
+- [BN256 For The Rest Of Us](https://hackmd.io/@jpw/bn254)
+- [BLS12-381 For The Rest Of Us](https://hackmd.io/@benjaminion/bls12-381)
+- [`ethereum/py_ecc`](https://github.com/ethereum/py_ecc)
+
+> [Index](../README.md)
diff --git a/docs/journal/pages/template.md b/docs/journal/pages/template.md
new file mode 100644
index 0000000..4178ac7
--- /dev/null
+++ b/docs/journal/pages/template.md
@@ -0,0 +1,11 @@
+> [Index](../README.md)
+
+# Title
+
+Body.
+
+# Resources
+
+- [Resource](https://example.com)
+
+> [Index](../README.md)
diff --git a/lib/field.ml b/lib/field.ml
new file mode 100644
index 0000000..4b6408f
--- /dev/null
+++ b/lib/field.ml
@@ -0,0 +1,55 @@
+(** Finite field. *)
+
+module type S = sig
+ type t
+ (** A type representing an element in the finite field *)
+
+ val zero : t
+ (** The zero value, i.e. the additive identity. *)
+
+ val one : t
+ (** The one value, i.e. the multiplicative identity. *)
+
+ 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) = zero] 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]. *)
+end
diff --git a/lib/fields.ml b/lib/fields.ml
new file mode 100644
index 0000000..2cdbbda
--- /dev/null
+++ b/lib/fields.ml
@@ -0,0 +1,42 @@
+let p =
+ Z.of_string
+ "21888242871839275222246405745257275088696311157297823662689037894645226208583"
+
+let p_minus_2 = Z.(p - ~$2)
+let mod_p x = Z.erem x p
+
+module Fq = struct
+ type t = Z.t
+
+ let zero = Z.zero
+ let one = Z.one
+ 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
+ let of_string s = Z.of_string s |> mod_p
+ let to_string = Z.to_string
+end
+
+module ExtensionField =
+functor
+ (Ext : sig
+ type t
+ end)
+ ->
+ struct
+ type t = Ext.t
+ end
+
+module Fq2 = ExtensionField (struct
+ type t = Fq.t * Fq.t
+end)
diff --git a/lib/fields.mli b/lib/fields.mli
new file mode 100644
index 0000000..3fc15dc
--- /dev/null
+++ b/lib/fields.mli
@@ -0,0 +1,34 @@
+(** Finite fields used by the BN254 curve and pairings *)
+
+module Fq : sig
+ include Field.S
+
+ 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]. *)
+end
+(* The prime field over which the BN254 curve is defined. *)
+
+module Fq2 : Field.S with type t = Fq.t * Fq.t
+(* The extension field of degree 2 over [Fq]. *)
+
+(*
+ module Fq12 :
+ S
+ with type t =
+ Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ * Fq.t
+ (* The extension field of degree 12 over [Fq]. *)
+*)
diff --git a/lib/fp.ml b/lib/fp.ml
deleted file mode 100644
index 2489f11..0000000
--- a/lib/fp.ml
+++ /dev/null
@@ -1,25 +0,0 @@
-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
deleted file mode 100644
index f73fe7f..0000000
--- a/lib/fp.mli
+++ /dev/null
@@ -1,58 +0,0 @@
-(** 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
index 6076a6b..8ce4014 100644
--- a/lib/plonkers.ml
+++ b/lib/plonkers.ml
@@ -1 +1 @@
-module Fp = Fp
+module Fields = Fields
diff --git a/test/test_fp.ml b/test/test_fp.ml
index 536b059..2189c7d 100644
--- a/test/test_fp.ml
+++ b/test/test_fp.ml
@@ -1,99 +1,99 @@
-module Fp = Plonkers.Fp
+module Fq = Plonkers.Fields.Fq
-let fp = Testable.make Fp.to_string Fp.equal
-let f = Fp.of_string
+let fq = Testable.make Fq.to_string Fq.equal
+let f = Fq.of_string
let test_add () =
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"5034212870056682811541563993093442956024207074167934414259669251040086104221")
- (Fp.add
+ (Fq.add
(f
"3019612232293951348432769707427639303474751231631017441050730061639747788964")
(f
"2014600637762731463108794285665803652549455842536916973208939189400338315257"));
- Alcotest.(check fp)
- "equals" Fp.zero
- (Fp.add
+ Alcotest.(check fq)
+ "equals" Fq.zero
+ (Fq.add
(f
"21888242871839275222246405745257275088696311157297823662689037894645226208582")
- Fp.one);
- Alcotest.(check fp)
+ Fq.one);
+ Alcotest.(check fq)
"equals"
(f
"7363893478117210948657131929839491162862302555393162961985689652909776739235")
- (Fp.add
+ (Fq.add
(f
"7363893478117210948657131929839491162862302555393162961985689652909776739235")
- Fp.zero)
+ Fq.zero)
let test_sub () =
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"1647997538064335793847192302859723563372189463772006638527106196094592175035")
- (Fp.sub
+ (Fq.sub
(f
"17522329235179667504196475280824254606128390327458085261948007699233694805172")
(f
"15874331697115331710349282977964531042756200863686078623420901503139102630137"));
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"21888242871839275222246405745257275088696311157297823662689037894645226208582")
- (Fp.sub Fp.zero Fp.one);
- Alcotest.(check fp)
+ (Fq.sub Fq.zero Fq.one);
+ Alcotest.(check fq)
"equals"
(f
"1550460075629904631277637708383660261580971272511193857451434052003108082807")
- (Fp.add
+ (Fq.add
(f
"1550460075629904631277637708383660261580971272511193857451434052003108082807")
- Fp.zero)
+ Fq.zero)
let test_mul () =
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"20539703399960180369319660518633290048071922000666914740466234074847854697757")
- (Fp.mul
+ (Fq.mul
(f
"16652515618518207323315643285221741056245295412987863357779458647516886387587")
(f
"15388577223719168739739036375931130294575142146897880107473273404618844312776"));
- Alcotest.(check fp)
- "equals" Fp.zero
- (Fp.mul
+ Alcotest.(check fq)
+ "equals" Fq.zero
+ (Fq.mul
(f
"10935302729977221140784701859408855206669147633587285404353277660075789680922")
- Fp.zero)
+ Fq.zero)
let test_div () =
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"7363953525538778156567393633860492517850968441297156550375719598579598367322")
- (Fp.div
+ (Fq.div
(f
"7363953525538778156567393633860492517850968441297156550375719598579598367322")
- Fp.one)
+ Fq.one)
let test_inv () =
- Alcotest.(check fp)
+ Alcotest.(check fq)
"equals"
(f
"14812345552713361047837749161990489217154524375614120373819285291721752372748")
- (Fp.inv
+ (Fq.inv
(f
"1160181877204925047836599448201084379181489517022839288462621953652233427695"));
- Alcotest.(check fp)
- "equals" Fp.one
+ Alcotest.(check fq)
+ "equals" Fq.one
(let a =
f
"17876831255806984619368587498426968568242044248616647019388897055879503636463"
in
- Fp.mul a (Fp.inv a))
+ Fq.mul a (Fq.inv a))
let tests =
Alcotest.