diff options
Diffstat (limited to 'lib/pairing.ml')
| -rw-r--r-- | lib/pairing.ml | 121 |
1 files changed, 73 insertions, 48 deletions
diff --git a/lib/pairing.ml b/lib/pairing.ml index ef67901..33c09c6 100644 --- a/lib/pairing.ml +++ b/lib/pairing.ml @@ -1,23 +1,86 @@ +(** Boneh-Lynn-Shacham (BLS) pairing curves implementation. *) + +(** Module signature BLS pairing curves. *) +module type S = sig + type g1 + (** The elliptic curve element of the first pairing group. *) + + type g2 + (** The elliptic curve element of the second pairing group. *) + + (** The target group of the pairing, usually denoted as [GT]. *) + module Gt : sig + type t + (** An element in the pairing target group. *) + + type f + (** The underlying field type of the pairing target group. *) + + val id : t + (** The group's identity element. *) + + val dot : t -> t -> t + (** [dot a b] is the group operation applied to [a] and [b]. *) + + val inv : t -> t + (** [inv a] is the inverse of [a]. Note that the invariant + [equal id (dot a (inv a))] always holds. *) + + val equal : t -> t -> bool + (** [equal a b] is true if [a] has the same value as [b] *) + + val to_f : t -> f + (** [to_fp12 a] is the underlying field value of the group element [a]. *) + + val to_string : t -> string + (** [to_string a] is the string representation of [a]. *) + + (** Unsafe pairing group construction functions. *) + module Unsafe : sig + val of_f_unchecked : f -> t + (** [of_fp12_unchecked a] is a pairing element of value [a]. This function + does not check whether the value actually belongs to the pairing group + [Gt]. *) + + val of_string_unchecked : string -> t + (** [of_string_unchecked s] returns the pairing element represented by + [s]. This function does not check whether the value belongs to the + group [Gt]. *) + end + end + + val e : g1 -> g2 -> Gt.t + (** [e p q] is the elliptic curve pairing of [p] and [q]. *) + + val check : (g1 * g2) list -> bool + (** [check pairs] checks whether the product of the pairings is equal to + [Gt.id]. *) +end + +(** BLS pairing curves construction. *) module Make (BLS : sig module G1 : Curve.S module G2 : Curve.S module F : Field.SX val x : Z.t - val g : F.t - val g1_to_coords : G1.t -> (F.t * F.t) option - val g2_to_coords : G2.t -> (F.t * F.t) option -end) = + val g1_extend : G1.t -> (F.t * F.t) option + val g2_twist : G2.t -> (F.t * F.t) option +end) : + S with type g1 = BLS.G1.t and type g2 = BLS.G2.t and type Gt.f = BLS.F.t = struct module G1 = BLS.G1 module G2 = BLS.G2 module F = BLS.F + type g1 = G1.t + type g2 = G2.t + module Gt = struct type t = F.t + type f = F.t let id = F.one - let g = BLS.g let dot = F.mul let inv = F.inv let to_f a = a @@ -47,17 +110,17 @@ struct | Some x -> x | None -> invalid_arg "line with point at infinity" in - let g1_to_coords p = BLS.g1_to_coords p |> unwrap in - let g2_to_coords p = BLS.g2_to_coords p |> unwrap in + let g1_extend p = BLS.g1_extend p |> unwrap in + let g2_twist p = BLS.g2_twist p |> unwrap in fun p q r -> - let xp, yp = g2_to_coords p in - let xr, yr = g1_to_coords r in + let xp, yp = g2_twist p in + let xr, yr = g1_extend r in if G2.equal p q then let m = F.(three * xp * xp / (two * yp)) in F.((m * (xr - xp)) - (yr - yp)) else if G2.(equal p (neg q)) then F.(xr - xp) else - let xq, yq = g2_to_coords q in + let xq, yq = g2_twist q in let m = F.((yq - yp) / (xq - xp)) in F.((m * (xr - xp)) - (yr - yp)) @@ -88,41 +151,3 @@ struct let e p q = pairing [ (p, q) ] let check pairs = pairing pairs |> Gt.(equal id) end - -module P = Make (struct - module G1 = Curves.E - module G2 = Curves.E2 - module F = Fields.Fp12 - - let x = Z.of_string "-0xd201000000010000" - - let g = - F.of_coeffs - @@ Array.map Fields.Fp.of_string - [| - "3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532"; - "1017299873256115394687936133146817339783508925356207904663165117501336940438300180831508525168135575931758510829235"; - "1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694"; - "233557331756520805040932826416017142246700002714928636467514104620558679338483625829873520553081791155104026612174"; - "2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068"; - "579878855790610229145575079003483905368886712027403088810647643659132674704592975818766405092658613973478871626949"; - "3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511"; - "2688711983546324406847903726099677850388169649284572370749691792743294691894680263880362424577638309433508620982332"; - "3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719"; - "1555267885602801620621747613743921378408444002839854792606782732724136830563310004098024452104661167182680744848589"; - "2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447"; - "873321072950766150592434764742436722571574260472569072177205897167854308426485977134306337249021436737462538398830"; - |] - - let g1_to_coords p = - G1.to_coords p |> Option.map (fun (x, y) -> (F.of_f x, F.of_f y)) - - let g2_to_coords p = Curves.E12.to_coords @@ Curves.twist p -end) - -module G1 = P.G1 -module G2 = P.G2 -module Gt = P.Gt - -let e = P.e -let check = P.check |
