Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jtcoolen committed Oct 27, 2024
1 parent c70d9d9 commit 32c55c3
Show file tree
Hide file tree
Showing 5 changed files with 7,884 additions and 21,415 deletions.
33 changes: 15 additions & 18 deletions examples/kzg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module ToyCurve = struct
(`Quotient
(Polynomial.create
[|
Finite_field.(inj_ring (one fp));
Finite_field.(inj_ring (zero fp));
Finite_field.(inj_ring (one fp));
Finite_field.(one fp);
Finite_field.(zero fp);
Finite_field.(one fp);
|]))

let g1_x =
Expand Down Expand Up @@ -73,12 +73,12 @@ end
module KZG :
Polynomial_commitment
with type common_input = kzg_common_input
and type polynomial = (finite_field, ring) typ Polynomial.t
and type polynomial = Finite_field.t Polynomial.t
and type scalar = Finite_field.t
and type evaluation = Finite_field.t
and type commitment = Finite_field.t Elliptic_curve.elt = struct
type common_input = kzg_common_input
type polynomial = (finite_field, ring) typ Polynomial.t
type polynomial = Finite_field.t Polynomial.t
type scalar = Finite_field.t
type evaluation = Finite_field.t
type commitment = Finite_field.t Elliptic_curve.elt
Expand All @@ -87,21 +87,18 @@ module KZG :
let commit c p =
assert (Polynomial.degree p < Vector.length c.srs_g1);
let f x p cm =
let n = Option.get Finite_field.(inj_prime_field (inj_field x)) in
let n = Option.get Finite_field.(inj_prime_field x) in
Elliptic_curve.(add c.curve cm (mul c.curve ~n ~p))
in
let acc = Elliptic_curve.zero c.curve in
Polynomial.fold_left2_vec ~f ~acc p c.srs_g1

let prove c p x =
let y = Polynomial.(create [| eval p (Finite_field.inj_ring x) |]) in
let y = Polynomial.(create [| eval p x |]) in
let n = Polynomial.(sub p y) in
let d =
Polynomial.create
[|
Finite_field.(inj_ring (one c.finite_field_generator));
Finite_field.(inj_ring (neg x));
|]
[| Finite_field.(one c.finite_field_generator); Finite_field.(neg x) |]
in
let q = Polynomial.div n d in
commit c q
Expand Down Expand Up @@ -153,14 +150,14 @@ let c =
let p =
Polynomial.create
[|
Finite_field.(inj_ring (random c.finite_field_generator));
Finite_field.(inj_ring (zero c.finite_field_generator));
Finite_field.(inj_ring (random c.finite_field_generator));
Finite_field.(random c.finite_field_generator);
Finite_field.(zero c.finite_field_generator);
Finite_field.(random c.finite_field_generator);
|]

let cm = KZG.commit c p
let x = Finite_field.random c.finite_field_generator
let y = Finite_field.inj_field (Polynomial.eval p (Finite_field.inj_ring x))
let y = Polynomial.eval p x
let pi = KZG.prove c p x

let () =
Expand All @@ -170,8 +167,8 @@ let () =
let p' =
Polynomial.create
[|
Finite_field.(inj_ring (random c.finite_field_generator));
Finite_field.(inj_ring (random c.finite_field_generator));
Finite_field.(random c.finite_field_generator);
Finite_field.(random c.finite_field_generator);
|]

let cm' = KZG.commit c p'
Expand All @@ -181,7 +178,7 @@ let rhs = KZG.commit c (Polynomial.add p p')
(* KZG commitments are homomorphic *)
let () = assert (Elliptic_curve.(equal lhs rhs))
let x' = Finite_field.random c.finite_field_generator
let y' = Finite_field.inj_field (Polynomial.eval p' (Finite_field.inj_ring x'))
let y' = Polynomial.eval p' x'
let pi' = KZG.prove c p' x'
let () = assert (KZG.verify c cm' x' y' pi')

Expand Down
5 changes: 1 addition & 4 deletions examples/number_fields.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ let () = Printf.eprintf "%s\n" (Polynomial.to_string qq)
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 inj_rat = Polynomial.inj_base_ring ~inj:(fun x -> x |> Integer.inj_rat)

let () =
Printf.eprintf "%b\n"
Expand Down
13 changes: 4 additions & 9 deletions examples/pohlig_hellman.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ let pohlig_hellman_prime_power_order ~mul ~pow ~dlp_solve_prime ~base ~prime
let pohlig_hellman ~order ~mul ~pow ~dlp_solve_prime ~base ~group_order h =
let f base_order_factorization (prime, valuation) =
let ord = Integer.(pow prime (of_int valuation)) in
let n = Integer.(divexact (to_integer group_order) ord) in
let n = Integer.(divexact group_order ord) in
let*? intermediate_log =
pohlig_hellman_prime_power_order ~mul ~pow ~dlp_solve_prime
~base:(pow base n) ~prime (pow h n) ~base_order_factorization
Expand All @@ -54,7 +54,7 @@ let pohlig_hellman ~order ~mul ~pow ~dlp_solve_prime ~base ~group_order h =
else if dvdii (order base) (order h) = 0 then None
else
let factors = factor group_order in
let base_order = Integer.inj_unique_factorization_domain (order base) in
let base_order = order base in
let logs = Array.map (f (factor base_order)) factors in
if Array.for_all Option.is_some logs then
let logs = Vector.of_array (Array.map Option.get logs) in
Expand Down Expand Up @@ -148,9 +148,7 @@ let rho_pollard_with_retries ~one ~mul ~pow ~class_x ~group_order ~base h =
let zn_dlog ~base x =
with_stack_clean_opt (fun () ->
let modulo = Integer_mod.get_modulo base in
let group_order =
Integer.inj_unique_factorization_domain (eulerphi modulo)
in
let group_order = eulerphi modulo in
let class_x x =
Integer.(to_int (modulo (Integer_mod.lift x) (of_int 3)))
in
Expand All @@ -177,10 +175,7 @@ let ell_solve_dlog ~ell ~base x =
in
Integer.(to_int (modulo h (of_int 3)))
in
let group_order =
Integer.inj_unique_factorization_domain
(Elliptic_curve.order_elt ell base)
in
let group_order = Elliptic_curve.order_elt ell base in
let mul = Elliptic_curve.add ell in
let pow p n = Elliptic_curve.mul ell ~n ~p in
pohlig_hellman
Expand Down
24 changes: 9 additions & 15 deletions src/pari.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
include Pari_bindings

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

let t = gen

Expand All @@ -16,7 +16,7 @@ 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
type 'a elliptic_curve = Elliptic_curve of 'a

let register_gc v =
Gc.finalise_last (fun () -> pari_free Ctypes.(coerce gen (ptr void) v)) v
Expand Down Expand Up @@ -48,7 +48,7 @@ end
module Rational = struct
type t = gen

let[@inline] inj_ring x = Fun.id x
let of_int i = stoi (Signed.Long.of_int i)
let[@inline] inj_real x = Fun.id x
let[@inline] inj_complex x = Fun.id x
let shift x s = mpshift x (Signed.Long.of_int s)
Expand All @@ -60,8 +60,6 @@ module Integer = struct
let[@inline] inj_rat x = Fun.id x
let[@inline] inj_real x = Fun.id x
let[@inline] inj_complex x = Fun.id x
let[@inline] inj_unique_factorization_domain x = Fun.id x
let to_integer : gen -> t = Fun.id
let equal x y = equalii x y = 1
let of_int i = stoi (Signed.Long.of_int i)
let to_int i = Signed.Long.to_int (itos i)
Expand Down Expand Up @@ -341,7 +339,6 @@ module Number_field = struct
let discriminant nf = nf_get_disc nf
let z_basis nf = nf_get_zk nf
let elt a = Vector.(transpose_row (of_array a))
let inj_ring x = x
let add nf a b = nfadd nf a b
let mul nf a b = nfmul nf a b

Expand All @@ -362,12 +359,12 @@ module Number_field = struct
end

type 'a group_structure = {
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;
mul : 'a ty -> 'a ty -> 'a ty;
pow : 'a ty -> Integer.t -> 'a ty;
rand : unit -> 'a ty;
hash : 'a ty -> Unsigned.ULong.t;
equal : 'a ty -> 'a ty -> bool;
equal_identity : 'a ty -> bool;
bb_group : bb_group Ctypes.structure option;
}

Expand All @@ -381,9 +378,6 @@ end
module Finite_field = struct
type t = gen

let[@inline] inj_ring x = Fun.id x
let[@inline] inj_field x = Fun.id x

let generator ~order =
ff_primroot
(ffgen order Signed.Long.zero)
Expand Down
Loading

0 comments on commit 32c55c3

Please sign in to comment.