Skip to content

Commit

Permalink
rename pari types
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcoolen committed Dec 5, 2023
1 parent 58adddc commit b9189b8
Show file tree
Hide file tree
Showing 5 changed files with 12,357 additions and 11,266 deletions.
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ let pkgs = import (fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/1f0e8ac1f9a783c4cfa0515483094eeff4315fe2.tar.gz";
sha256 = "1mdnn0fj81pgvhzmzxh0g54g6yqxfqd2fim4h4c7cf7yskcp8g48";
}) {inherit system; }; in
let ocamlPackages = pkgs.ocaml-ng.ocamlPackages_4_14.overrideScope' (self: super: {
let ocamlPackages = pkgs.ocaml-ng.ocamlPackages_5_0.overrideScope' (self: super: {
ocaml = super.ocaml.override { flambdaSupport = true; };
});
in pkgs.fastStdenv.mkDerivation {
Expand Down
22 changes: 11 additions & 11 deletions examples/kzg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module type Polynomial_commitment = sig
common_input -> commitment -> scalar -> evaluation -> proof -> bool
end

let images_from_abscissa (ell : 'a Elliptic_curve.structure) (x : 'a) =
let images_from_abscissa (ell : 'a Elliptic_curve.t) (x : 'a) =
let open Elliptic_curve in
let a1 = get_a1 ell in
let a2 = get_a2 ell in
Expand Down Expand Up @@ -63,13 +63,13 @@ let _l_torsion_subgroup ell ~l =
Array.init !c (fun i -> sg.(i))

type kzg_common_input = {
srs_g1 : (Finite_field.t Elliptic_curve.t, [ `ROW ]) Vector.t;
srs_g2 : (Finite_field.t Elliptic_curve.t, [ `ROW ]) Vector.t;
srs_g1 : (Finite_field.t Elliptic_curve.elt, [ `ROW ]) Vector.t;
srs_g2 : (Finite_field.t Elliptic_curve.elt, [ `ROW ]) Vector.t;
finite_field_generator : Finite_field.t;
curve : Finite_field.t Elliptic_curve.structure;
curve : Finite_field.t Elliptic_curve.t;
curve_subgroup_order : Integer.t;
g1 : Finite_field.t Elliptic_curve.t;
g2 : Finite_field.t Elliptic_curve.t;
g1 : Finite_field.t Elliptic_curve.elt;
g2 : Finite_field.t Elliptic_curve.elt;
}

module ToyCurve = struct
Expand Down Expand Up @@ -116,16 +116,16 @@ end
module KZG :
Polynomial_commitment
with type common_input = kzg_common_input
and type polynomial = (finite_field, ring) t Polynomial.t
and type polynomial = (finite_field, ring) typ Polynomial.t
and type scalar = Finite_field.t
and type evaluation = Finite_field.t
and type commitment = Finite_field.t Elliptic_curve.t = struct
and type commitment = Finite_field.t Elliptic_curve.elt = struct
type common_input = kzg_common_input
type polynomial = (finite_field, ring) t Polynomial.t
type polynomial = (finite_field, ring) typ Polynomial.t
type scalar = Finite_field.t
type evaluation = Finite_field.t
type commitment = Finite_field.t Elliptic_curve.t
type proof = Finite_field.t Elliptic_curve.t
type commitment = Finite_field.t Elliptic_curve.elt
type proof = Finite_field.t Elliptic_curve.elt

let commit c p =
assert (Polynomial.degree p < Vector.length c.srs_g1);
Expand Down
8 changes: 4 additions & 4 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

(include_subdirs unqualified)

;(mdx
; (files :standard - *.mli)
; (libraries pari-bindings core integers ctypes ctypes.foreign mdx)
; (preludes prelude))
(mdx
(files :standard - *.mli)
(libraries pari pari-bindings core integers ctypes ctypes.foreign mdx)
(preludes prelude))

(env
(release
Expand Down
60 changes: 25 additions & 35 deletions src/pari.ml
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
include Pari_bindings

type ('kind, 'structure) t = gen
type ('kind, 'structure) typ = gen

let t = gen

type group
type ring
type field
type unique_factorization_domain
type complex
type real
type rational
type integer
type polynomial
type integer_mod
type finite_field
type number_field
type elliptic_curve

let register_gc v =
Gc.finalise_last (fun () -> pari_free Ctypes.(coerce gen (ptr void) v)) v
Expand All @@ -16,7 +25,6 @@ let gentostr = gentostr_raw
let gentobytes x = gentostr x |> String.to_bytes

module Complex = struct
type complex = private Complex
type t = gen

let create ~re ~im = mkcomplex re im
Expand All @@ -26,7 +34,6 @@ module Complex = struct
end

module Real = struct
type real = private Real
type t = gen

let[@inline] inj_complex x = Fun.id x
Expand All @@ -39,9 +46,7 @@ module Real = struct
end

module Rational = struct
type rational = private Rational
type t = gen
type ring = gen

let[@inline] inj_ring x = Fun.id x
let[@inline] inj_real x = Fun.id x
Expand All @@ -50,7 +55,6 @@ module Rational = struct
end

module Integer = struct
type integer = private Integer
type t = gen

let[@inline] inj_rat x = Fun.id x
Expand Down Expand Up @@ -142,7 +146,6 @@ module Set = struct
end

module Vector = struct
type ('a, 'b) p = ('a, 'b) t
type ('a, 'b) t = gen constraint 'a = gen constraint 'b = [< `COL | `ROW ]

let length x = glength x |> Signed.Long.to_int
Expand Down Expand Up @@ -204,7 +207,6 @@ end
let () = pari_init 50_000_000 (Unsigned.ULong.of_int 500_000)

module Polynomial = struct
type polynomial
type 'a t = gen constraint 'a = gen

let equal x y = gequal x y = 1
Expand Down Expand Up @@ -277,23 +279,17 @@ module Polynomial = struct
end

module Integer_mod = struct
type integer_mod = private Integer_mod
type ('a, 'b) p = ('a, 'b) t
type nonrec t = (integer_mod, ring) t
type t = gen

let[@inline] inj_group x = Fun.id x

let create : Integer.t -> modulo:Integer.t -> t =
fun x ~modulo -> mkintmod x modulo

let create_assume_prime_modulus :
Integer.t -> modulo:Integer.t -> (integer_mod, field) p =
fun x ~modulo -> mkintmod x modulo

let create_assume_prime_modulus x ~modulo = mkintmod x modulo
let lift = lift

let inverse : (integer_mod, 'a) p -> (integer_mod, 'a) p option =
fun x ->
let inverse x =
let x = Ctypes.(coerce gen (ptr gen) x) in
let modulo = Ctypes.(!@(x +@ 1)) in
let res = Ctypes.allocate t (Integer.zero ()) in
Expand Down Expand Up @@ -322,9 +318,8 @@ module Integer_mod = struct
end

module Number_field = struct
type number_field = private Number_field
type nonrec t = (number_field, field) t
type structure = gen
type t = gen
type elt = gen

let create p =
let nf = nfinit p Signed.Long.(of_int 4) in
Expand Down Expand Up @@ -362,12 +357,12 @@ module Number_field = struct
end

type 'a group_structure = {
mul : ('a, group) t -> ('a, group) t -> ('a, group) t;
pow : ('a, group) t -> Integer.t -> ('a, group) t;
rand : unit -> ('a, group) t;
hash : ('a, group) t -> Unsigned.ULong.t;
equal : ('a, group) t -> ('a, group) t -> bool;
equal_identity : ('a, group) t -> bool;
mul : ('a, group) typ -> ('a, group) typ -> ('a, group) typ;
pow : ('a, group) typ -> Integer.t -> ('a, group) typ;
rand : unit -> ('a, group) typ;
hash : ('a, group) typ -> Unsigned.ULong.t;
equal : ('a, group) typ -> ('a, group) typ -> bool;
equal_identity : ('a, group) typ -> bool;
bb_group : bb_group Ctypes.structure option;
}

Expand All @@ -378,11 +373,8 @@ module Fp = struct
let pow x ~exponent ~modulo = fp_pow x exponent modulo
end

type finite_field = private Finite_field

module Finite_field = struct
type ('a, 'b) p = ('a, 'b) t
type t = (finite_field, field) p
type t = gen

let[@inline] inj_ring x = Fun.id x
let[@inline] inj_field x = Fun.id x
Expand Down Expand Up @@ -428,8 +420,7 @@ module Finite_field = struct
| `Quotient modulo ->
Vector.((ffextend base_field_elt modulo Signed.Long.zero).%[1])

let fpxq_star ~(p : pari_ulong) ~(quotient : Fp.t Polynomial.t) :
finite_field group_structure =
let fpxq_star ~p ~quotient =
let open Ctypes in
let q = powuu p (Unsigned.ULong.of_int (Polynomial.degree quotient)) in
let ret = allocate (ptr void) (from_voidp void null) in
Expand Down Expand Up @@ -461,9 +452,8 @@ module Finite_field = struct
end

module Elliptic_curve = struct
type elliptic_curve
type nonrec 'a t = (elliptic_curve, group) t constraint 'a = gen
type 'a structure = gen constraint 'a = gen
type 'a t = gen constraint 'a = gen
type 'a elt = gen constraint 'a = gen

let create ?a1 ?a2 ?a3 ?a4 ?a6 ?(dom = Ctypes.(coerce (ptr void) gen null)) ()
=
Expand Down
Loading

0 comments on commit b9189b8

Please sign in to comment.