diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-18 10:09:32 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-18 10:09:40 +0200 |
| commit | 4b5936586e570b125b1c68d04b1479262af9c98b (patch) | |
| tree | 9646f29a15abe369f66afcbe053a959246ccebbe /lib | |
| parent | f2643a291339faa25d4d07d1006526b727fcc706 (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.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/field.ml | 55 | ||||
| -rw-r--r-- | lib/fields.ml | 42 | ||||
| -rw-r--r-- | lib/fields.mli | 34 | ||||
| -rw-r--r-- | lib/fp.ml | 25 | ||||
| -rw-r--r-- | lib/fp.mli | 58 | ||||
| -rw-r--r-- | lib/plonkers.ml | 2 |
6 files changed, 132 insertions, 84 deletions
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 |
