Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions fold_lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(name fold_lib)
(public_name fold_lib)
(library_flags -linkall)
(inline_tests)
(libraries core_kernel)
(preprocess
(pps ppx_jane ppx_deriving.eq bisect_ppx -- --conditional))
(pps bisect_ppx ppx_deriving.eq ppx_jane -- --conditional))
(synopsis "fold types"))
21 changes: 0 additions & 21 deletions fold_lib/fold.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ let of_list (xs : 'a list) : 'a t =
let of_array (xs : 'a array) : 'a t =
{ fold = (fun ~init ~f -> Array.fold xs ~init ~f) }

let%test_unit "fold-to-list" =
Quickcheck.test (Quickcheck.Generator.list Int.quickcheck_generator)
~f:(fun xs -> assert ([%equal: Int.t list] xs (to_list (of_list xs))))

let sexp_of_t f t = List.sexp_of_t f (to_list t)

let compose (t1 : 'a t) (t2 : 'a t) : 'a t =
Expand Down Expand Up @@ -85,23 +81,6 @@ let group3 ~default (t : 'a t) : ('a * 'a * 'a) t =
assert false )
}

let%test_unit "group3" =
Quickcheck.test (Quickcheck.Generator.list Int.quickcheck_generator)
~f:(fun xs ->
let default = 0 in
let n = List.length xs in
let tuples = to_list (group3 ~default (of_list xs)) in
let k = List.length tuples in
let r = n mod 3 in
(let padded =
xs @ if r = 0 then [] else List.init (3 - r) ~f:(fun _ -> default)
in
let concated =
List.concat_map ~f:(fun (b1, b2, b3) -> [ b1; b2; b3 ]) tuples
in
[%test_eq: int list] padded concated ) ;
assert ((n + 2) / 3 = k) )

let string_bits s =
let ith_bit_int n i = (n lsr i) land 1 = 1 in
{ fold =
Expand Down
5 changes: 5 additions & 0 deletions fold_lib/test/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(tests
(names test_fold)
(libraries alcotest core_kernel fold_lib)
(preprocess
(pps ppx_jane)))
38 changes: 38 additions & 0 deletions fold_lib/test/test_fold.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
open Core_kernel
open Fold_lib

(* Define Alcotest testable for int list *)
let int_list = Alcotest.(list int)

let test_fold_to_list () =
Quickcheck.test (Quickcheck.Generator.list Int.quickcheck_generator)
~f:(fun xs ->
Alcotest.(check int_list)
"to_list(of_list(xs)) = xs" xs
(Fold.to_list (Fold.of_list xs)) )

let test_group3 () =
Quickcheck.test (Quickcheck.Generator.list Int.quickcheck_generator)
~f:(fun xs ->
let default = 0 in
let n = List.length xs in
let tuples = Fold.to_list (Fold.group3 ~default (Fold.of_list xs)) in
let k = List.length tuples in
let r = n mod 3 in
let padded =
xs @ if r = 0 then [] else List.init (3 - r) ~f:(fun _ -> default)
in
let concated =
List.concat_map ~f:(fun (b1, b2, b3) -> [ b1; b2; b3 ]) tuples
in
Alcotest.(check int_list)
"padded list equals concatenated tuples" padded concated ;
Alcotest.(check bool) "tuple count is correct" true ((n + 2) / 3 = k) )

let () =
Alcotest.run "Fold_lib"
[ ( "Fold"
, [ Alcotest.test_case "fold_to_list" `Quick test_fold_to_list
; Alcotest.test_case "group3" `Quick test_group3
] )
]
74 changes: 11 additions & 63 deletions group_map/bw19.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(* Based on this paper. https://eprint.iacr.org/2019/403 *)
(** Based on the paper
{{:https://eprint.iacr.org/2019/403}Fast and simple constant-time hashing to the BLS12-381 elliptic curve}
*)

open Core_kernel

Expand Down Expand Up @@ -36,8 +38,8 @@ module Params = struct
; b = f b
}

(* A deterministic function for constructing a valid choice of parameters for a
given field. *)
(** A deterministic function for constructing a valid choice of parameters for
a given field. *)
let create (type t) (module F : Field_intf.S_unchecked with type t = t)
{ Spec.b } =
let open F in
Expand Down Expand Up @@ -97,6 +99,12 @@ struct
constant params.u - temp
in
(x1, x2, x3)

let field_to_conic _ = failwith "Not implemented"

let conic_to_s _ = failwith "Not implemented"

let _s_to_v _ = failwith "Not implemented"
end

let to_group (type t) (module F : Field_intf.S_unchecked with type t = t)
Expand All @@ -121,63 +129,3 @@ let to_group (type t) (module F : Field_intf.S_unchecked with type t = t)
in
let x1, x2, x3 = M.potential_xs t in
List.find_map [ x1; x2; x3 ] ~f:try_decode |> Option.value_exn

let%test_module "test" =
( module struct
module Fp = struct
include
Snarkette.Fields.Make_fp
(Snarkette.Nat)
(struct
let order =
Snarkette.Nat.of_string
"5543634365110765627805495722742127385843376434033820803590214255538854698464778703795540858859767700241957783601153"
end)

let b = of_int 7
end

module Make_tests (F : sig
include Field_intf.S_unchecked

val gen : t Quickcheck.Generator.t

val b : t
end) =
struct
module F = struct
include F

let constant = Fn.id
end

open F

let params = Params.create (module F) { b }

let curve_eqn u = (u * u * u) + params.b

(* Filter the two points which cause the group-map to blow up. This
is not an issue in practice because the points we feed into this function
will be the output of poseidon, and thus (modeling poseidon as a random oracle)
will not be either of those two points. *)
let gen =
Quickcheck.Generator.filter F.gen ~f:(fun t ->
let t2 = t * t in
let alpha_inv = (t2 + constant params.fu) * t2 in
not (equal alpha_inv zero) )

module M =
Make (F) (F)
(struct
let params = params
end)

let%test_unit "full map works" =
Quickcheck.test ~sexp_of:F.sexp_of_t gen ~f:(fun t ->
let x, y = to_group (module F) ~params t in
assert (equal (curve_eqn x) (y * y)) )
end

module T0 = Make_tests (Fp)
end )
3 changes: 1 addition & 2 deletions group_map/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
(name group_map)
(public_name group_map)
(preprocess
(pps ppx_jane ppx_deriving.eq))
(inline_tests)
(pps ppx_deriving.eq ppx_jane))
(libraries
snarkette ; This is actually just needed in tests
core_kernel))
Loading
Loading