diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-17 21:51:02 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-06-17 21:51:02 +0200 |
| commit | f2643a291339faa25d4d07d1006526b727fcc706 (patch) | |
| tree | aca9eee97d94b9798dab40dd941bd8697f01b479 /lib | |
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.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/dune | 4 | ||||
| -rw-r--r-- | lib/fp.ml | 25 | ||||
| -rw-r--r-- | lib/fp.mli | 58 | ||||
| -rw-r--r-- | lib/plonkers.ml | 1 |
4 files changed, 88 insertions, 0 deletions
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 |
