diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2025-08-01 11:33:20 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2025-08-01 11:36:01 +0200 |
| commit | b7f0c124574603619d10892f509558679aa59ddb (patch) | |
| tree | 0989034b9fc109748c36927a9319830d6e8fc13e /lib | |
| parent | 395fa4a06d5c4ac90f65838137728ed569bc4f90 (diff) | |
Pairings With `py_ecc` Implementation
We now have pairings working with the `py_ecc` implementation, but it
looks like different pairing implementations (such as `@noble/curves`)
map to different `Gt` groups.
I don't think it matters much (as long as we map to _some_ valid
group `GT`, and have bilinear properties), but I want to understand
_why_ this is the case.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/field.ml | 10 | ||||
| -rw-r--r-- | lib/pairing.ml | 149 | ||||
| -rw-r--r-- | lib/pairing.mli | 20 |
3 files changed, 156 insertions, 23 deletions
diff --git a/lib/field.ml b/lib/field.ml index 5b11983..f53f1a6 100644 --- a/lib/field.ml +++ b/lib/field.ml @@ -83,6 +83,10 @@ module type SX = sig val to_coeffs : t -> f array (** [to_coeffs a] are the polynomial coefficients of [a]. *) + + val conjugate : t -> t + (** [conjugate a] is the conjugate of the polynomial in the extension field. + *) end module Make (PRIME : sig @@ -248,6 +252,12 @@ end) : SX with type f = POLY.F.t = struct let res = loop a 0 one in if Z.sign exp >= 0 then res else inv res + let conjugate a = + let half = degree / 2 in + let same = Array.to_seq a |> Seq.drop half |> Seq.for_all F.(equal zero) in + if same then a + else Array.mapi (fun i c -> if i < half then c else F.neg c) a + let sub a b = add a (neg b) let div a b = mul a (inv b) let ( ~- ) = neg diff --git a/lib/pairing.ml b/lib/pairing.ml index dc8e59c..68430dd 100644 --- a/lib/pairing.ml +++ b/lib/pairing.ml @@ -1,24 +1,139 @@ -module G1 = Curves.E -module G2 = Curves.E2 +module Make (BLS : sig + module G1 : Curve.S + module G2 : Curve.S + module F : Field.SX -module Gt = struct - module Fp12 = Fields.Fp12 + 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) = +struct + module G1 = BLS.G1 + module G2 = BLS.G2 + module F = BLS.F - type t = Fp12.t + module Gt = struct + type t = F.t - let identity = Fp12.one - let dot = Fp12.mul - let inverse = Fp12.inv - let to_fp12 a = a - let equal = Fp12.equal - let to_string = Fp12.to_string + let id = F.one + let g = BLS.g + let dot = F.mul + let inv = F.inv + let to_f a = a + let equal = F.equal + let to_string = F.to_string - module Unsafe = struct - let of_fp12_unchecked a = a - let of_string_unchecked = Fp12.of_string + module Unsafe = struct + let of_f_unchecked a = a + let of_string_unchecked = F.of_string + end end + + let ate_loop = + lazy + (let x = Z.abs BLS.x in + let len = Z.numbits x - 1 in + Array.init len (fun i -> + let pos = len - i - 1 in + Z.testbit x pos)) + + let negative_x = Z.sign BLS.x < 0 + let final_exponent = lazy Z.((pow F.p 12 - one) / G1.n) + + let line = + let two = F.(one + one) in + let three = F.(one + one + one) in + let unwrap = function + | 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 + fun p q r -> + let xp, yp = g2_to_coords p in + let xr, yr = g1_to_coords 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 m = F.((yq - yp) / (xq - xp)) in + F.((m * (xr - xp)) - (yr - yp)) + + let e' p q = + if G1.(equal p inf) || G2.(equal q inf) then F.one + else + let ate_loop = Lazy.force ate_loop in + let _, f = + Array.fold_left + (fun (r, f) bit -> + let t = line r r p in + let f' = F.(f * f * t) in + let r' = G2.add r r in + if bit then + let t = line r' q p in + let f'' = F.(f' * t) in + let r'' = G2.add r' q in + (r'', f'') + else (r', f')) + (q, F.one) ate_loop + in + if negative_x then f else f + + let e p q = + let t = e' p q in + let exp = Lazy.force final_exponent in + F.pow t exp end -let e p q = - let _ = (p, q) in - failwith "todo" +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 + Fields.Fp. + [| + of_string + "2819105605953691245277803056322684086884703000473961065716485506033588504203831029066448642358042597501014294104502"; + of_string + "1323968232986996742571315206151405965104242542339680722164220900812303524334628370163366153839984196298685227734799"; + of_string + "2987335049721312504428602988447616328830341722376962214011674875969052835043875658579425548512925634040144704192135"; + of_string + "3879723582452552452538684314479081967502111497413076598816163759028842927668327542875108457755966417881797966271311"; + of_string + "261508182517997003171385743374653339186059518494239543139839025878870012614975302676296704930880982238308326681253"; + of_string + "231488992246460459663813598342448669854473942105054381511346786719005883340876032043606739070883099647773793170614"; + of_string + "3993582095516422658773669068931361134188738159766715576187490305611759126554796569868053818105850661142222948198557"; + of_string + "1074773511698422344502264006159859710502164045911412750831641680783012525555872467108249271286757399121183508900634"; + of_string + "2727588299083545686739024317998512740561167011046940249988557419323068809019137624943703910267790601287073339193943"; + of_string + "493643299814437640914745677854369670041080344349607504656543355799077485536288866009245028091988146107059514546594"; + of_string + "734401332196641441839439105942623141234148957972407782257355060229193854324927417865401895596108124443575283868655"; + of_string + "2348330098288556420918672502923664952620152483128593484301759394583320358354186482723629999370241674973832318248497"; + |] + + 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 e = P.e diff --git a/lib/pairing.mli b/lib/pairing.mli index 8d27b02..a76343d 100644 --- a/lib/pairing.mli +++ b/lib/pairing.mli @@ -4,20 +4,23 @@ module G2 : Curve.S with type t = Curves.E2.t and type f = Fields.Fp2.t module Gt : sig type t - val identity : t + val id : t (** The group's identity element. *) + val g : t + (** The group's generator. *) + val dot : t -> t -> t (** [dot a b] is the group operation applied to [a] and [b]. *) - val inverse : t -> t - (** [inverse a] is the inverse of [a]. Note that the invariant - [equal identity (dot a (inverse a))] always holds. *) + 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_fp12 : t -> Fields.Fp12.t + val to_f : t -> Fields.Fp12.t (** [to_fp12 a] is the underlying field value of the group element [a]. *) val to_string : t -> string @@ -25,7 +28,7 @@ module Gt : sig (** Unsafe pairing group construction functions. *) module Unsafe : sig - val of_fp12_unchecked : Fields.Fp12.t -> t + val of_f_unchecked : Fields.Fp12.t -> 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]. *) @@ -37,5 +40,10 @@ module Gt : sig end end +val e' : G1.t -> G2.t -> Fields.Fp12.t +(** [e p q] is the elliptic curve pairing of [p] and [q] without final + exponentiation. This is useful when you only want to compare pairings, + without actually mapping them to [Gt]. *) + val e : G1.t -> G2.t -> Gt.t (** [e p q] is the elliptic curve pairing of [p] and [q]. *) |
