From 61b4dc407ee61d402c4cf72dfa3408af51f86a0e Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Mon, 11 Aug 2025 12:32:14 +0200 Subject: Remove the Test Vector from IETF Draft I could not get the result to match it, or find an implementation that matched it either... I plan on modifying the pairing implementation to match what I consider is the "canonical" one, that is implemented over a Fp12-tower and used in most implementations used in the wild (including Ethereum's and ZCash's). --- docs/journal/pages/pairings.md | 21 +++-- test/test_pairing.ml | 210 ----------------------------------------- 2 files changed, 13 insertions(+), 218 deletions(-) diff --git a/docs/journal/pages/pairings.md b/docs/journal/pages/pairings.md index 816e81a..793e5be 100644 --- a/docs/journal/pages/pairings.md +++ b/docs/journal/pages/pairings.md @@ -4,7 +4,7 @@ Pairings are the dark magic that make PLONK ZK systems work. They provide an **efficient** bilinear mapping from two groups to a third, all three with the same order `N`. -To have a feel for what you get with pairing, imagine a _fictional_ pairing function defined as `e(P, Q) = R` where, given `s` and `t` such that `P = sG` and `Q = tG` for some generator point `G`, then `R = (s * t)G`. This gives us the wonderful bilinear properties of pairings: `e(P, Q) = e(sG, tG) = e(G, stG) = e(tG, sG) = (s * t) * e(G, G)`. In many ways, this acts like "multiplication" in contract to the addition of points (where `P + Q = sG + tG = (s + t)G`). There is an issue with our _fictional_ pairing definition: it is not efficient. Specifically, given two points `P` and `Q` without knowing `s` and `t`, it is not possible to compute `s * t` (otherwise, you could efficiently solve the discrete logarithm problem, and you would have broken the cryptography that underpins Ethereum signatures). +To have a feel for what you get with pairing, imagine a _fictional_ pairing function defined as `e(P, Q) = R` where, given `s` and `t` such that `P = sG` and `Q = tG` for some generator point `G`, then `R = (s * t)G`. This gives us the wonderful bilinear properties of pairings: `e(P, Q) = e(sG, tG) = e(G, stG) = e(tG, sG) = (s * t) * e(G, G)`. In many ways, this acts like "multiplication" in contrast to the addition of points (where `P + Q = sG + tG = (s + t)G`). There is an issue with our _fictional_ pairing definition: it is not efficient. Specifically, given two points `P` and `Q` without knowing `s` and `t`, it is not possible to compute `s * t` (otherwise, you could efficiently solve the discrete logarithm problem, and you would have broken the cryptography that underpins Ethereum signatures). By some black magic, mathematicians found a function `e` that behaves like the multiplication above with some caveats: instead of all `P`, `Q` and `R` being points on a curve, they are part of three different groups (with the same order -- i.e. number of elements): @@ -12,23 +12,28 @@ By some black magic, mathematicians found a function `e` that behaves like the m - `Q` is a point on an elliptic curve over a polynomial extension field over the same finite field as `G1`, lets call that `G2` - `R` is **not** a point, but an element in the polynomial extension field over which `G2` is defined. -For BLS12-381: +There are a few such pairings (such as Weil and Tate pairings), but the most commonly used in practice are optimal Ate pairings. The optimal Ate pairing for BLS12-381 uses: - `G1` is just the elliptic curve BLS12-381 -- `G2` is... well complicated... _technically_, it is the BLS12-381 curve over a specific 12th degree polynomial extension field `Fp12`, with a chosen generator point. In practice, there exists another related curve over a 2nd degree polynomial extension field `Fp2` with a twisting operation that efficiently maps `E'[Fp2] -> E[Fp12]`. This allows you to do elliptic curve arithmetic much more efficiently in the `E'[Fp2]` curve, and then "convert" it to a point in `E[Fp12]` for the pairing computation itself. -- `GT` is a group of elements in `Fp12`, where the: - - The identity element is `1` - - The group operation is field multiplication +- `G2` is... well complicated... _technically_, it is the BLS12-381 curve over a specific 12th degree polynomial extension field `Fp12`. In practice, there exists another related curve over a 2nd degree polynomial extension field `Fp2` with a twisting operation that efficiently maps `E'[Fp2] -> E[Fp12]`. This allows you to do elliptic curve arithmetic much more efficiently in the `E'[Fp2]` curve, and then "convert" it to a point in `E[Fp12]` for the pairing computation itself. +- `GT` is a multiplicative subgroup of `Fp12` -The exact magic that we use isn't super important (and I don't pretend to fully understand it). +The exact magic that we use isn't super important (and I don't pretend to fully understand it). Additionally, I have found competing implementations compute the actual pairing value differently: +- The Pairing-Friendly Curves IETF draft document includes an algorithm for computing the optimal Ate pairing, however, I could not get an implementation to match the provided test vector. In fact, I could not find a single implementation that did pass the test vector (including implementations referenced in the IETF draft itself) +- Ethereum `py_ecc` Python implementation of ECC pairing implementation is the _closest_ to the algorithm in the Pairing-Friendly Curves IETF draft, but does not return the same value (it also ignore the sign of the curve's `x` parameter, which drives the Miller loop in the optimal Ate pairing implementation) +- Every other implementation I found (including `zkcrypto/bls12-381` used in ZCash, `kilic/bls12-381` used in Geth, Paul Miller's Noble Curves, etc.) returns yet another value, but implements the optimal Ate loop using more complicated yet more computationally optimal primitives -> Small note on differences in BN254 and BLS12-381 curves: the Miller Loop is implemented slightly differently in that there is a post-computation in the case of the BN254 curve at the end of the Miller Loop algorithm. As far as I can tell (information on this was relatively hard to find), but it is to untwist the point. +In the end, I don't believe the pairing value itself is _that_ important, as you typically build cryptography around "pairing checks", where `e(P1, Q1) * e(P2, Q2) * ... * e(Pn, Qn) = 1`. For example, for signing you provide a signature `S = k * H(m)`, where `k` is your secret key and `H(m)` is the hash of the message `m` to the `G2` curve; given a public key `P`, `e(G1, -S) * e(P, H(m)) = 1`, since that is equal to `e(G1, -k * H(m)) * e(k * G1, H(m)) = e(G1, H(m))^(-k) * e(G1, H(m))^k = 1`. This means that the what matters is that `e` is efficient, bilinear, and non-degenerate (which the implementations above all satisfy). + +> Small note on differences in BN254 and BLS12-381 curves: optimal Ate pairings are implemented differently for Barreto-Naehrig curves compared to Barreto-Lynn-Scott curves. # Resources - [BLS12-381 For The Rest Of Us](https://hackmd.io/@benjaminion/bls12-381) - [Pairings for Beginners](https://static1.squarespace.com/static/5fdbb09f31d71c1227082339/t/5ff394720493bd28278889c6/1609798774687/PairingsForBeginners.pdf) - [Exploring Elliptic Curve Pairings](https://vitalik.eth.limo/general/2017/01/14/exploring_ecp.html) +- [Ethereum Python ECC Pairing](https://github.com/ethereum/py_ecc) - [Noble Curves](https://github.com/paulmillr/noble-curves/blob/289db6f25979d226b3be2057c16d94e64dca3280/src/abstract/bls.ts#L97) +- [Pairing-Friendly Curves](https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11) > [Index](../README.md) diff --git a/test/test_pairing.ml b/test/test_pairing.ml index 2d698ba..d3cdb35 100644 --- a/test/test_pairing.ml +++ b/test/test_pairing.ml @@ -61,185 +61,6 @@ let test_pairing () = "77870636695957247485070729371505785036022507830356763789646790529980989972767478012625350608049848760401081873614"; |] ))) -module G1 = E -module G2 = E2 -module Fp = Plonkers.Bls12_381.Fields.Fp -module Fp2 = Plonkers.Bls12_381.Fields.Fp2 -module Fp12 = Plonkers.Bls12_381.Fields.Fp12 - -let reference_pairing (pt : G1.t) (q : G2.t) : Fp12.t = - let extend p = - match G1.to_coords p with - | None -> failwith "point at infinity" - | Some (x, y) -> Fp12.(of_f x, of_f y) - in - let twist p = - let o, z = Fp.(one, zero) in - let w = Fp12.of_coeffs [| z; o; z; z; z; z; z; z; z; z; z; z |] in - let w2 = Fp12.(w * w) in - let w3 = Fp12.(w2 * w) in - let w2inv = Fp12.inv w2 in - let w3inv = Fp12.inv w3 in - let iso a = - let c = Fp2.to_coeffs a in - let r0 = Fp.(c.(0) - c.(1)) in - let r6 = c.(1) in - Fp12.of_coeffs [| r0; z; z; z; z; z; r6; z; z; z; z; z |] - in - match G2.to_coords p with - | None -> failwith "point at infinity" - | Some (x, y) -> - let nx, ny = (iso x, iso y) in - Fp12.(nx * w2inv, ny * w3inv) - in - let line_function q1 q2 p = - let x1, y1 = twist q1 in - let x2, y2 = twist q2 in - let x, y = extend p in - let two = Fp12.(one + one) in - let three = Fp12.(two + one) in - let extra l = Fp12.((l * (x - x1)) + y1 - y) in - if G2.equal q1 q2 then - let l = Fp12.(three * x1 * x1 / (two * y1)) in - extra l - else if G2.(equal q1 ~-q2) then Fp12.(x - x1) - else - let l = Fp12.((y2 - y1) / (x2 - x1)) in - extra l - in - let ci = - [| - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - -1; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - -1; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - 0; - -1; - 0; - 0; - -1; - 0; - -1; - -1; - |] - in - let l = 63 in - let c = - Array.to_seqi ci - |> Seq.fold_left - (fun c (i, bit) -> - let ci = Z.(~$bit * (one lsl i)) in - Z.(c + ci)) - Z.zero - in - Alcotest.(check string) "c computation" "-d201000000010000" (Z.format "%x" c); - let k = 12 in - let r = G1.n in - let miller_loop () = - let f = ref Fp12.one in - let t = ref q in - (if ci.(l) = -1 then t := G2.(~- (!t))); - for i = l - 1 downto 0 do - (f := Fp12.(!f * !f * line_function !t !t pt)); - (t := G2.(!t + !t)); - if ci.(i) = 1 then ( - (f := Fp12.(!f * line_function !t q pt)); - t := G2.(!t + q)) - else if ci.(i) = -1 then ( - (f := Fp12.(!f * line_function !t G2.(~-q) pt)); - t := G2.(!t - q)) - done; - f := Fp12.(pow !f Z.(((pow p k) - one) / r)); - !f - in - miller_loop () - -(* https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-pairing-friendly-curves-11#name-test-vectors-of-optimal-ate *) -let test_vector () = - Alcotest.(check Primitives.Testables.fp12) - "equals" - (Primitives.Literals.fp12 - [| - "0x11619b45f61edfe3b47a15fac19442526ff489dcda25e59121d9931438907dfd448299a87dde3a649bdba96e84d54558"; - "0x153ce14a76a53e205ba8f275ef1137c56a566f638b52d34ba3bf3bf22f277d70f76316218c0dfd583a394b8448d2be7f"; - "0x095668fb4a02fe930ed44767834c915b283b1c6ca98c047bd4c272e9ac3f3ba6ff0b05a93e59c71fba77bce995f04692"; - "0x16deedaa683124fe7260085184d88f7d036b86f53bb5b7f1fc5e248814782065413e7d958d17960109ea006b2afdeb5f"; - "0x09c92cf02f3cd3d2f9d34bc44eee0dd50314ed44ca5d30ce6a9ec0539be7a86b121edc61839ccc908c4bdde256cd6048"; - "0x111061f398efc2a97ff825b04d21089e24fd8b93a47e41e60eae7e9b2a38d54fa4dedced0811c34ce528781ab9e929c7"; - "0x01ecfcf31c86257ab00b4709c33f1c9c4e007659dd5ffc4a735192167ce197058cfb4c94225e7f1b6c26ad9ba68f63bc"; - "0x08890726743a1f94a8193a166800b7787744a8ad8e2f9365db76863e894b7a11d83f90d873567e9d645ccf725b32d26f"; - "0x0e61c752414ca5dfd258e9606bac08daec29b3e2c57062669556954fb227d3f1260eedf25446a086b0844bcd43646c10"; - "0x0fe63f185f56dd29150fc498bbeea78969e7e783043620db33f75a05a0a2ce5c442beaff9da195ff15164c00ab66bdde"; - "0x10900338a92ed0b47af211636f7cfdec717b7ee43900eee9b5fc24f0000c5874d4801372db478987691c566a8c474978"; - "0x1454814f3085f0e6602247671bc408bbce2007201536818c901dbd4d2095dd86c1ec8b888e59611f60a301af7776be3d"; - |]) - (reference_pairing - (f1 - ( "0x17f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb", - "0x08b3f481e3aaa0f1a09e30ed741d8ae4fcf5e095d5d00af600db18cb2c04b3edd03cc744a2888ae40caa232946c5e7e1" - )) - (f2 - ( [| - "0x024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8"; - "0x13e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e"; - |], - [| - "0x0ce5d527727d6e118cc9cdc6da2e351aadfd9baa8cbdd3a76d429a695160d12c923ac9cc3baca289e193548608b82801"; - "0x0606c4a02ea734cc32acd2b02bc28b99cb3e287e85a763af267492ab572e99ab3f370d275cec1da1aaa9075ff05f79be"; - |] ))) - let test_bilinearity () = let a = Z.of_string @@ -274,37 +95,6 @@ let tests = [ test_case "Generator" `Slow test_generator; test_case "Pairing" `Slow test_pairing; - test_case "Vector" `Slow test_vector; test_case "Bilinearity" `Slow test_bilinearity; test_case "Inverse" `Slow test_inverse; ] - -(* - - 2675223149320665921521646202693247640790817874364255298732398972560239127864099887300921329285059103920214926247256 - 3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511 - 1437178847597838002205538891928304392505606959215308674013885606166925907773842499174371404735625830937323828692626 - 3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719 - 1506178926806320088529997600140680210913802140271006315226533143967895395199677036726919792764738011159067846598728 - 2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447 - 296397698419227591881949966489408966385204245298227609919201225879400018157857419730816679383241778672627140748220 - 1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455 - 2213584337862344967755109385575965635901738814384224456257761298779336140589044234514789656471272705700109501099024 - 2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198 - 2549209626480291013679779981989983528616421847439035724344204595297044667359758911489614886787335613326952862534008 - 3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957 - - 3408834164464458755751340723502736743445402614640994055433339214424916442641449510670327077303074893591740399366532 - 2985109681965551998729853692589086816773373894582799980668893018622694710052537683611179103960880088106135761730552 - 1919447955347661378578718469488414822573562393353397544098339472673901193098010131520945815709482515519580400649694 - 3768852223465146588376856999319887014310182817224079248864544031503472971152354238612814108575933872882790245947613 - 2882199334237819327910933128503231887000342950792587449681715803095618652567681747054312179958427092970985872417068 - 3422530699431057164272214746732420251187996107911604796521410492464898975786244888623921224036357050064415400932838 - 3268798540077874559188095304926415053902298079662269128631117894259354335713488241073281881110999874366368799440511 -X 1313697571675342986569886099636226306168713170654435514582366343380736958596157600562325204551377354604385651577455 - 3520140447471844017044610248175793726488927385800919015247604269617056365166670232096113218155158979455637700602719 -X 2447141669618865772796042211991982778148438817099153092725275403399894819927527860344663177024354496855213527711198 - 2626389147790168154036854297373352480470342009417426750876875476996308393122833154115295241935326582225976246741447 -X 3129088482270901242825355060993467433985308559466438813154852238956177342064351887308381291879994227300431734160957 - - *) -- cgit v1.2.3