Skip to content

Commit

Permalink
fix typing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcoolen committed Mar 12, 2024
1 parent 7b68531 commit 562a076
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
18 changes: 5 additions & 13 deletions examples/kzg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ module KZG :
let commit c p =
assert (Polynomial.degree p < Vector.length c.srs_g1);
let f x p cm =
let x = Finite_field. (inj_field x) in
let x: (finite_field, ring) typ Polynomial.t = Finite_field.(residue_class x) in
let n = Polynomial.(x.%[0]) in
let n = Option.get Finite_field.(inj_prime_field (inj_field x)) in
Elliptic_curve.(add c.curve cm (mul c.curve ~n ~p))
in
let acc = Elliptic_curve.zero c.curve in
Expand All @@ -113,16 +111,12 @@ module KZG :
Elliptic_curve.(
sub c.curve
Vector.(c.srs_g2.%[2])
(mul c.curve
~n:Polynomial.((Finite_field.residue_class x).%[0])
~p:c.g2))
(mul c.curve ~n:(Option.get Finite_field.(inj_prime_field x)) ~p:c.g2))
in
let numerator =
Elliptic_curve.(
sub c.curve cm
(mul c.curve
~n:Polynomial.((Finite_field.residue_class y).%[0])
~p:c.g1))
(mul c.curve ~n:(Option.get Finite_field.(inj_prime_field y)) ~p:c.g1))
in
let lhs =
Elliptic_curve.weil_pairing c.curve ~l:c.curve_subgroup_order pi
Expand All @@ -139,10 +133,9 @@ let c =
let g = Finite_field.generator ~order:ToyCurve.r in
let secret = Finite_field.random g in
let coeff p i =
let n = Finite_field.pow secret (Integer.of_int i) in
Elliptic_curve.mul ToyCurve.curve
~n:
Polynomial.(
Finite_field.(residue_class (pow secret (Integer.of_int i))).%[0])
~n:(Option.get Finite_field.(inj_prime_field n))
~p
in
let srs_g1 = Vector.init 100 ~f:(coeff ToyCurve.g1) in
Expand Down Expand Up @@ -207,4 +200,3 @@ let rhs =
r

let () = assert (Finite_field.(equal lhs rhs))

10 changes: 8 additions & 2 deletions examples/number_fields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ let () = Printf.eprintf "%b\n" (Polynomial.is_irreducible q)
let qmin : Integer.t Polynomial.t = Polynomial.minimal q
let () = Printf.eprintf "%s\n" (Polynomial.to_string qmin)

let inj_rat =
Polynomial.inj_base_ring ~inj:(fun x ->
x |> Integer.inj_rat |> Rational.inj_ring)

let () =
Printf.eprintf "%b\n" Number_field.(are_isomorphic (create q) (create qmin))
Printf.eprintf "%b\n"
Number_field.(are_isomorphic (create (inj_rat q)) (create (inj_rat qmin)))

(* Gaussian integers: the ring Z[i] (here we work in the field Q(i)) *)
let gaussian_integers =
(* Q(i) = Q[X]/(X^2+1) *)
Number_field.create
(Polynomial.create
[| Integer.of_int 1; Integer.of_int 0; Integer.of_int 1 |])
[| Integer.of_int 1; Integer.of_int 0; Integer.of_int 1 |]
|> inj_rat)

(* Euclidean division of 6 + 8i by 1 + 5i. *)
let a =
Expand Down
39 changes: 25 additions & 14 deletions src/pari.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ type ('kind, 'structure) typ = gen

let t = gen

type group = Group
type ring = Ring
type field= Field
type unique_factorization_domain= Unique_factorization_domain
type complex= Complex
type real= Real
type rational= Rational
type integer= Integer
type polynomial= Polynomial
type integer_mod= Integer_mod
type finite_field= Finite_field
type number_field= Number_field
type elliptic_curve= Elliptic_curve

type group = Group
type ring = Ring
type field = Field
type unique_factorization_domain = Unique_factorization_domain
type complex = Complex
type real = Real
type rational = Rational
type integer = Integer
type 'a polynomial = Polynomial of 'a
type integer_mod = Integer_mod
type finite_field = Finite_field
type number_field = Number_field
type elliptic_curve = Elliptic_curve

let register_gc v =
Gc.finalise_last (fun () -> pari_free Ctypes.(coerce gen (ptr void) v)) v
Expand Down Expand Up @@ -274,6 +273,8 @@ module Polynomial = struct
acc := f p.%[i] Vector.(v.%[i + 1]) !acc
done;
!acc

let inj_base_ring ~inj:_ p = p
end

module Integer_mod = struct
Expand Down Expand Up @@ -384,6 +385,16 @@ module Finite_field = struct

let prime_field_element x ~p = ff_z_mul (ff_1 (generator ~order:p)) x

let inj_prime_field x =
let p = ff_to_fpxq_i x in

if
glength p = Signed.Long.one
|| Polynomial.degree p = 0
|| Polynomial.degree p = 1
then Some Polynomial.(p.%[0])
else None

let finite_field_element coeffs a =
let len = Array.length coeffs in
fst
Expand Down
27 changes: 15 additions & 12 deletions src/pari.mli
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,17 @@ type bb_field
type bb_algebra
type bb_ring
type ring = private Ring
type field= private Field
type unique_factorization_domain= private Unique_factorization_domain
type complex= private Complex
type real= private Real
type rational= private Rational
type integer= private Integer
type polynomial= private Polynomial
type integer_mod= private Integer_mod
type finite_field= private Finite_field
type number_field= private Number_field
type elliptic_curve= private Elliptic_curve
type field = private Field
type unique_factorization_domain = private Unique_factorization_domain
type complex = private Complex
type real = private Real
type rational = private Rational
type integer = private Integer
type 'a polynomial = private Polynomial of 'a
type integer_mod = private Integer_mod
type finite_field = private Finite_field
type number_field = private Number_field
type elliptic_curve = private Elliptic_curve

val factor :
('kind, unique_factorization_domain) typ ->
Expand Down Expand Up @@ -267,7 +267,7 @@ module Matrix : sig
end

module rec Polynomial : sig
type 'a t = (polynomial, ring) typ constraint 'a = ('b, ring) typ
type 'a t = ('a polynomial, ring) typ constraint 'a = ('b, ring) typ

val to_string : 'a t -> string
val mul : 'a t -> 'a t -> 'a t
Expand Down Expand Up @@ -351,6 +351,8 @@ module rec Polynomial : sig
'a t ->
('b, _) Vector.t ->
('c, 'd) typ

val inj_base_ring : inj:('a -> 'b) -> 'a t -> 'b t
end

and Fp : sig
Expand All @@ -367,6 +369,7 @@ and Finite_field : sig
val inj_field : (finite_field, ring) typ -> t
val generator : order:Integer.t -> t
val prime_field_element : Integer.t -> p:Integer.t -> t
val inj_prime_field : t -> Fp.t option
val finite_field_element : Integer.t array -> t -> t

val create : p:int -> degree:int -> (finite_field, ring) typ Polynomial.t
Expand Down

0 comments on commit 562a076

Please sign in to comment.