diff options
| -rw-r--r-- | lib/field.ml | 183 | ||||
| -rw-r--r-- | lib/fields.ml | 173 | ||||
| -rw-r--r-- | plonkers.opam | 13 | ||||
| -rw-r--r-- | test/test_fp.ml | 30 | ||||
| -rw-r--r-- | test/test_fp12.ml | 121 | ||||
| -rw-r--r-- | test/test_fp2.ml | 61 |
6 files changed, 406 insertions, 175 deletions
diff --git a/lib/field.ml b/lib/field.ml index cb5cbe4..3c4b2b6 100644 --- a/lib/field.ml +++ b/lib/field.ml @@ -1,14 +1,13 @@ (** Finite field. *) -exception Invalid_arrity -(** Raised when creating a polynomial extension field element with the wrong - number of coefficients. *) - (** Module signature for a finite field. *) module type S = sig type t (** A type representing an element in the finite field. *) + val p : Z.t + (** The prime modulus of the field. *) + val zero : t (** The zero value, i.e. the additive identity. *) @@ -46,6 +45,10 @@ module type S = sig (** [div a b] is the field division of [a] by [b]. It is equivalent to [mul a (inv b)]. Furthermore, we define [div a zero = zero]. *) + val pow : t -> Z.t -> t + (** [pow a exp] is [a] to the power of [exp]. [pow zero zero] is defined to be + [zero]. *) + val ( ~- ) : t -> t (** [neg]. *) @@ -81,3 +84,175 @@ module type Sx = sig val to_coeffs : t -> f array (** [to_coeffs a] are the polynomial coefficients of [a]. *) end + +module Make (P : sig + val p : Z.t +end) : S = struct + type t = Z.t + + let p = P.p + let mod_p x = Z.erem x p + let zero = Z.zero + let one = Z.one + let of_string s = Z.of_string s |> mod_p + let to_string = Z.to_string + let equal = Z.equal + let neg a = Z.neg a |> mod_p + let add a b = Z.add a b |> mod_p + let mul a b = Z.mul a b |> mod_p + let pow a exp = if equal a zero then zero else Z.powm a exp p + let inv a = pow a Z.minus_one + let sub a b = add a (neg b) + let div a b = mul a (inv b) + let ( ~- ) = neg + let ( ~/ ) = inv + let ( + ) = add + let ( - ) = sub + let ( * ) = mul + let ( / ) = div +end + +module MakeExtension (P : sig + module F : S + + val q : F.t array +end) : Sx with type f = P.F.t = struct + module F = P.F + + type f = F.t + + let degree = Array.length P.q + let () = assert (degree >= 2) + let of_f a = Array.init degree (function 0 -> a | _ -> F.zero) + + let of_coeffs a = + if Array.length a == degree then Array.copy a + else invalid_arg "wrong number of elements" + + let to_coeffs = Array.copy + + type t = f array + + let p = F.p + let zero = Array.make degree F.zero + let one = Array.init degree (function 0 -> F.one | _ -> F.zero) + + let of_string s = + let strip_parens s = + if String.starts_with ~prefix:"(" s && String.ends_with ~suffix:")" s then + String.sub s 1 (String.length s - 2) + else invalid_arg "field element missing parentheses" + in + let a = + strip_parens s |> String.split_on_char ',' |> List.map F.of_string + |> Array.of_list + in + if Array.length a = degree then a + else invalid_arg "wrong number of elements" + + let to_string a = + Array.map F.to_string a |> Array.to_list |> String.concat "," + |> Printf.sprintf "(%s)" + + let equal = Array.for_all2 F.equal + let neg a = Array.map F.neg a + let add a b = Array.map2 F.add a b + + let mul a b = + let r = Array.make ((degree * 2) - 1) F.zero in + for i = 0 to degree - 1 do + for j = 0 to degree - 1 do + r.(i + j) <- F.add r.(i + j) (F.mul a.(i) b.(j)) + done + done; + for i = Array.length r - 1 downto degree do + let q = r.(i) in + if not (F.equal q F.zero) then + for j = 0 to degree - 1 do + let j' = i - degree + j in + r.(j') <- F.sub r.(j') (F.mul q P.q.(j)) + done + else () + done; + Array.sub r 0 degree + + let inv a = + let deg a = + let rec deg' = function + | 0 -> 0 + | i -> if F.equal a.(i) F.zero then deg' (i - 1) else i + in + deg' (Array.length a - 1) + in + let ediv a b = + let da = deg a in + let db = deg b in + let q = Array.make (da + 1) F.zero in + let r = Array.sub a 0 (da + 1) in + let c = b.(db) in + for i = da - db downto 0 do + let qd = F.div r.(db + i) c in + q.(i) <- qd; + for j = 0 to db do + r.(i + j) <- F.sub r.(i + j) (F.mul qd b.(j)) + done + done; + q + in + let submulq a b q = + let r = Array.copy a in + for i = 0 to deg b do + for j = 0 to deg q do + r.(i + j) <- F.sub r.(i + j) (F.mul b.(i) q.(j)) + done + done; + r + in + let t = ref (Array.append zero [| F.zero |]) in + let newt = ref (Array.append one [| F.zero |]) in + let r = ref (Array.append P.q [| F.one |]) in + let newr = ref (Array.append a [| F.zero |]) in + while Array.for_all F.(equal zero) !newr = false do + let q = ediv !r !newr in + let newr' = submulq !r !newr q in + let newt' = submulq !t !newt q in + r := !newr; + newr := newr'; + t := !newt; + newt := newt' + done; + let a0 = F.inv !r.(0) in + Array.map (F.mul a0) (Array.sub !t 0 degree) + + let pow a exp = + if Array.to_seq a |> Seq.drop 1 |> Seq.for_all F.(equal zero) then + let f = a.(0) in + if F.(equal zero f) then zero + else if F.(equal one f) then one + else if Z.(equal exp zero) then one + else of_f @@ F.pow a.(0) exp + else if Z.(equal exp zero) then one + else if Z.(equal exp one) then a + else + let abs = Z.abs exp in + let nbits = Z.numbits abs in + let rec loop pow bit res = + let res' = if Z.testbit abs bit then mul res pow else res in + let bit' = bit + 1 in + if bit' = nbits then res' + else + let pow' = mul pow pow in + loop pow' bit' res' + in + let res = loop a 0 one in + if Z.sign exp >= 0 then res else inv res + + let sub a b = add a (neg b) + let div a b = mul a (inv b) + let ( ~- ) = neg + let ( ~/ ) = inv + let ( + ) = add + let ( - ) = sub + let ( * ) = mul + let ( / ) = div +end diff --git a/lib/fields.ml b/lib/fields.ml index 13fc07f..8bb4c5e 100644 --- a/lib/fields.ml +++ b/lib/fields.ml @@ -1,175 +1,18 @@ -module Fimpl (F : sig - type t - - val zero : t - val one : t - val of_string : string -> t - val to_string : t -> string - val equal : t -> t -> bool - val neg : t -> t - val inv : t -> t - val add : t -> t -> t - val mul : t -> t -> t -end) : Field.S with type t = F.t = struct - type t = F.t - - let zero = F.zero - let one = F.one - let of_string = F.of_string - let to_string = F.to_string - let equal = F.equal - let neg = F.neg - let inv = F.inv - let add = F.add - let sub a b = add a (neg b) - let mul = F.mul - let div a b = mul a (inv b) - let ( ~- ) = neg - let ( ~/ ) = inv - let ( + ) = add - let ( - ) = sub - let ( * ) = mul - let ( / ) = div -end - -module Fp = struct - let q = +module Fp = Field.Make (struct + let p = Z.of_string "4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559787" +end) - let q_minus_2 = Z.(q - ~$2) - let mod_q x = Z.erem x q - - include Fimpl (struct - type t = Z.t - - let zero = Z.zero - let one = Z.one - let of_string s = Z.of_string s |> mod_q - let to_string = Z.to_string - let equal = Z.equal - let neg a = Z.neg a |> mod_q - let inv a = Z.powm a q_minus_2 q - let add a b = Z.add a b |> mod_q - let mul a b = Z.mul a b |> mod_q - end) -end - -module FpN (R : sig - val q : Fp.t array -end) = -struct - type f = Fp.t - - let degree = Array.length R.q - let of_f a = Array.init degree (function 0 -> a | _ -> Fp.zero) - - let of_coeffs a = - if Array.length a != degree then raise Field.Invalid_arrity - else Array.copy a - - let to_coeffs = Array.copy - - include Fimpl (struct - type t = f array - - let zero = Array.make degree Fp.zero - let one = Array.init degree (function 0 -> Fp.one | _ -> Fp.zero) - - let of_string s = - let strip_parens s = - if String.starts_with ~prefix:"(" s && String.ends_with ~suffix:")" s - then String.sub s 1 (String.length s - 2) - else failwith "field element missing parentheses" - in - let a = - strip_parens s |> String.split_on_char ',' |> List.map Fp.of_string - |> Array.of_list - in - if Array.length a = degree then a - else invalid_arg "wrong number of elements" - - let to_string a = - Array.map Fp.to_string a |> Array.to_list |> String.concat "," - |> Printf.sprintf "(%s)" - - let equal = Array.for_all2 Fp.equal - let neg a = Array.map Fp.neg a - let add a b = Array.map2 Fp.add a b - - let mul a b = - let r = Array.make ((degree * 2) - 1) Fp.zero in - for i = 0 to degree - 1 do - for j = 0 to degree - 1 do - r.(i + j) <- Fp.add r.(i + j) (Fp.mul a.(i) b.(j)) - done - done; - for i = Array.length r - 1 downto degree do - let q = r.(i) in - if not (Fp.equal q Fp.zero) then - for j = 0 to degree - 1 do - let j' = i - degree + j in - r.(j') <- Fp.sub r.(j') (Fp.mul q R.q.(j)) - done - else () - done; - Array.sub r 0 degree - - let inv a = - let deg a = - let rec deg' = function - | 0 -> 0 - | i -> if Fp.equal a.(i) Fp.zero then deg' (i - 1) else i - in - deg' (Array.length a - 1) - in - let ediv a b = - let da = deg a in - let db = deg b in - let q = Array.make (da + 1) Fp.zero in - let r = Array.sub a 0 (da + 1) in - let c = b.(db) in - for i = da - db downto 0 do - let qd = Fp.div r.(db + i) c in - q.(i) <- qd; - for j = 0 to db do - r.(i + j) <- Fp.sub r.(i + j) (Fp.mul qd b.(j)) - done - done; - q - in - let submulq a b q = - let r = Array.copy a in - for i = 0 to deg b do - for j = 0 to deg q do - r.(i + j) <- Fp.sub r.(i + j) (Fp.mul b.(i) q.(j)) - done - done; - r - in - let t = ref (Array.append zero [| Fp.zero |]) in - let newt = ref (Array.append one [| Fp.zero |]) in - let r = ref (Array.append R.q [| Fp.one |]) in - let newr = ref (Array.append a [| Fp.zero |]) in - while Array.for_all Fp.(equal zero) !newr = false do - let q = ediv !r !newr in - let newr' = submulq !r !newr q in - let newt' = submulq !t !newt q in - r := !newr; - newr := newr'; - t := !newt; - newt := newt' - done; - let a0 = Fp.inv !r.(0) in - Array.map (Fp.mul a0) (Array.sub !t 0 degree) - end) -end +module Fp2 = Field.MakeExtension (struct + module F = Fp -module Fp2 = FpN (struct let q = Fp.[| one; zero |] end) -module Fp12 = FpN (struct +module Fp12 = Field.MakeExtension (struct + module F = Fp + let q = Fp. [| diff --git a/plonkers.opam b/plonkers.opam index e030cc0..f829730 100644 --- a/plonkers.opam +++ b/plonkers.opam @@ -1,18 +1,18 @@ # This file is generated by dune, edit dune-project instead opam-version: "2.0" -synopsis: "PLONK From Scratch" +synopsis: "PLONK from scratch" description: "A from-scratch implementation of a PLONK zero knowledge proof for educational purposes." maintainer: ["Nicholas Rodrigues Lordello"] authors: ["Nicholas Rodrigues Lordello"] license: "GPL-3.0-only" tags: ["topics" "plonk" "zero knowledge" "zksnark"] -homepage: "https://sr.ht/~nlordell/plonkers" -doc: "https://git.sr.ht/~nlordell/plonkers" -bug-reports: "https://todo.sr.ht/~nlordell/plonkers" +homepage: "https://codeberg.org/nlordell/plonkers" +doc: "https://codeberg.org/nlordell/plonkers" +bug-reports: "https://codeberg.org/nlordell/plonkers/issues" depends: [ "ocaml" - "dune" {>= "3.15"} + "dune" {>= "3.19"} "zarith" "alcotest" {with-test} "odoc" {with-doc} @@ -31,4 +31,5 @@ build: [ "@doc" {with-doc} ] ] -dev-repo: "git+https://git.sr.ht/~nlordell/plonkers" +dev-repo: "git+https://codeberg.org/nlordell/plonkers.git" +x-maintenance-intent: ["(latest)"] diff --git a/test/test_fp.ml b/test/test_fp.ml index 687170f..f3d24e6 100644 --- a/test/test_fp.ml +++ b/test/test_fp.ml @@ -152,6 +152,35 @@ let test_inv () = Alcotest.(check fp) "equals" Fp.one (Fp.mul a (Fp.inv a)); Alcotest.(check fp) "equals" Fp.zero (Fp.inv Fp.zero) +let test_pow () = + Alcotest.(check fp) "equals" Fp.zero Fp.(pow zero Z.zero); + let a = + f + "883009121996480731997844824055715487937601322843563036349689188317953865689440811145131419685576769413625710611759" + in + let exp = + Z.of_string + "223757086517925205380917559391900479644866286017013462881064438760593187397026055204642886097455808395507938587263" + in + Alcotest.(check fp) + "equals" + (f + "3268225642397183913064723644900368513801378259146181554748612053788987883399665250663628812236993697779970815748294") + (Fp.pow a exp); + let a = + f + "2283425044965116906757153222622463605433968256276699869444255593166230225314960979860307763136427211928023277465543" + in + let exp = + Z.of_string + "-1313828300586738506740341541676431011123481634174240343155076118345421135367256885266553742244726431317227000519371" + in + Alcotest.(check fp) + "equals" + (f + "3656881829387973665970026963644201607212675144469633255094847585836426137076083194800285021271892592999115642210326") + (Fp.pow a exp) + let tests = Alcotest. [ @@ -162,4 +191,5 @@ let tests = test_case "Division" `Quick test_div; test_case "Additive Inverse" `Quick test_neg; test_case "Multiplicative Inverse" `Quick test_inv; + test_case "Exponentiation" `Quick test_pow; ] diff --git a/test/test_fp12.ml b/test/test_fp12.ml index 6807d0f..040bea8 100644 --- a/test/test_fp12.ml +++ b/test/test_fp12.ml @@ -357,6 +357,126 @@ let test_inv () = Alcotest.(check fp12) "equals" Fp12.one (Fp12.mul a (Fp12.inv a)); Alcotest.(check fp12) "equals" Fp12.zero (Fp12.inv Fp12.zero) +let test_pow () = + Alcotest.(check fp12) "equals" Fp12.zero Fp12.(pow zero Z.zero); + let a = + f + [| + "3243946662156380360401533691271902622351216700088828801371694425169510018967527607195510653472613127628684457518895"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + |] + in + let exp = + Z.of_string + "4786395996711131997851369005645373402034333833710769512437307609838449510878590131404494026990569494826881867762725" + in + Alcotest.(check fp12) + "equals" + (f + [| + "93505687369509791585523581418311859879808007995075528779053674394162338631824811308905650303250543152947425931864"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + "0"; + |]) + (Fp12.pow a exp); + let a = + f + [| + "581184364395709998532427920065204131097627572243414467773984372410930594032604783915123507383421084980957463121074"; + "2632456401246680983721085320745914343873827696323764944744909114035458049120708554529974176475513328251247147836109"; + "24345862764080708067934017839092765694372561325369394824358778494499957277645028739526134943517661202607195681874"; + "2654060922890107462430619070159723322802664028016101999580163834909869729412457392616437464520060328985029635369899"; + "1234199423993097969388562903710956069098291646153620227683382690044719081978395203667156040012577821217408074874361"; + "2327254416315986733730126201873032747694702671280192172720971579574777885381686040178833996348099474109238030990438"; + "308456031957100469469026660074251580523576344563743597543846510047273452631171833183938660416210087947577740395957"; + "2070641978992199849713397118375564104488717989981967333699186001535709721572872457631373642046564356166890693724525"; + "1023115789687830757320296131233972372806693488520357178009183008096126250029658521153644503180508667254344969657933"; + "2771708931742116174029064994358808349206398185573306516963793281889674054650455265946393136685382711512086095486153"; + "2415935655833924444587585147004375260727208675807945593059746951931040642185593984108205203840145367007729879698502"; + "3747451142253730570511648553667071849534921523034778216681941333936844891879955748062523628659060692375379991747670"; + |] + in + let exp = + Z.of_string + "4635419594850854522995180126268396373397782627168612260872144180754217625899496757038300024715816053523747817924459" + in + Alcotest.(check fp12) + "equals" + (f + [| + "2438171351058836832369911502732339870005598901564531105907103021028264831134869120777132856961562084121531742783535"; + "1097012181782714231544213745005695044345330467519225986859898554381542803267478493509808468367873481243049888913481"; + "3441010539322298489597317353107916838786869966838301203783777602998428100162154678688942721030940877368891071276372"; + "3593500705793409307232272185743009596418063943678130894945284701622786996176800414429475246325197262589426620173858"; + "964224237371176259088842629766232638500742818372760021780790193279027515591323535144113122546903105322182676907862"; + "3684244022634105112704496490992911506549020986026906093605209267003449476801356226047451076092998396677303085121809"; + "1965489668709265814072193089447911761432074428191996806738429523212667383283896153392549640989745803011537725980936"; + "2551726489979038828948495463625657418800786828538215581426649722088527266035405451818233303351446481412104533533364"; + "3964495084560562221340255167677547850832406051894824346109563370270525276347653640848719119649764365606267783038033"; + "2866503721379455577991619765590795362830616055864444930975126436054827719887566874168103260196645293003945616411536"; + "3692743837621692913449496726591269549647593441723033661300120151666779123446873010864470091860812844172699271693312"; + "982267349706018011588333944011806032269384205274857614588482845900836245714620157539009784576658152730964002634141"; + |]) + (Fp12.pow a exp); + let a = + f + [| + "517554183474565900946818039009633520572576806453115590677457726872451536258897896625585575140288687629345028556687"; + "804192970074616669533467647394379953654107179940186436178127026151992832864814401261296545974230007591965462103056"; + "2503639195058255885119003112929617238334162143220485778531810479310221150572133796548682240164964042178173586634141"; + "3431424336172958736295465509239701653584585432691078671221927189971363708827182094133397404147985425759490280378706"; + "953285779753237668442931840498081005646288619461422772012651004147820333482663712112141889782072093341181928817686"; + "3711169283099264542922435637442151317386528554971948266366687273289063043069918796381040804380529583766240342013513"; + "1791471994755958914271980585077660655286607863946512699149289608031009310612204307552330931844413095653965765327758"; + "3008466831712656130005727986998419703746889516538057258400795824261402981674823740163694825164287433960375111029620"; + "2387950836082387027973169497589631641161156457719183784873528937718893927814612246905389531036355149097312143657775"; + "2571808137740754351942389542927111675955441592454531894705216542351374812484180762764896669806426501173261426585268"; + "549253400379930422329621510413460874866344551229142442812532136611231511345405457803439222143097301551334881103094"; + "597296218403409837200855257971214266530681970030808478517056660057461542523203616410446873972385495777887414127138"; + |] + in + let exp = + Z.of_string + "-3667081312888142516070958782736288102210645458674689131546095190584164448587870524463541023829639780830349310002188" + in + Alcotest.(check fp12) + "equals" + (f + [| + "3778225024695192726450053538672271960465063334975822169885989605299905198600554611286690862277006898262692393173204"; + "1100678303917569754073715448443872121669768946382076991745600699949236869847922040927866727318805460066075347408056"; + "2575434022406640282647396997235375073255786637198979034483306201439250156054828532722759380529451673142400582558319"; + "141030785938329416380297660581416487681561455268895551048692156869124237201619702441378197235235154631299159110590"; + "3818386169472484912594997885361299732069360949602244204012368075962936301921746721896556883215834634668221667632031"; + "431068183083318414141019877550130820538186918053706113005381257264282126636474327359650258197178732294565622826902"; + "1444821826621931088190793564436822532840471337479364966075772563973043866193644452117965281678067289101527064145382"; + "326705016751523330711231810902065778158048061222978975631028088465968483398111642382574581611053002594065419984370"; + "2590558016277599639325970669172815909591831863058757858275263285669958704344475473881605742238108942152775575819921"; + "860801923520796484127388756389887657380482233286294407214932626052789638279849320104552687581993207164608583794675"; + "3397659117957673478090316761897634222297760936461824966172431604521997466498354729809670376239331734393424886923691"; + "1809397363561603126960710152825757691419619983666237192735364118138290701919129233449204965301855024045028933401664"; + |]) + (Fp12.pow a exp) + let tests = Alcotest. [ @@ -367,4 +487,5 @@ let tests = test_case "Division" `Quick test_div; test_case "Additive Inverse" `Quick test_neg; test_case "Multiplicative Inverse" `Quick test_inv; + test_case "Exponentiation" `Quick test_pow; ] diff --git a/test/test_fp2.ml b/test/test_fp2.ml index 83e7581..5059d97 100644 --- a/test/test_fp2.ml +++ b/test/test_fp2.ml @@ -157,6 +157,66 @@ let test_inv () = Alcotest.(check fp2) "equals" Fp2.one (Fp2.mul a (Fp2.inv a)); Alcotest.(check fp2) "equals" Fp2.zero (Fp2.inv Fp2.zero) +let test_pow () = + Alcotest.(check fp2) "equals" Fp2.zero Fp2.(pow zero Z.zero); + let a = + f + [| + "474115497072541322895389858489480609172578950628809946348866025065821669435363079989255346498739571226982616295818"; + "0"; + |] + in + let exp = + Z.of_string + "3105336457051725105829261703611869046484138143354012906741039250427604409359163819090456024500436833258860072165950" + in + Alcotest.(check fp2) + "equals" + (f + [| + "2571743831872147698027367396132516348773097418476706526959368167138968446116795191425142285541764038435924260708363"; + "0"; + |]) + (Fp2.pow a exp); + let a = + f + [| + "932397165113779971182112385466314994992365193304655286543885749901082941249407819721182501953932208210362359376767"; + "1514649361613438802105844164899346094055523615340349556627790306031335196151918107909670840541546143609966982148422"; + |] + in + let exp = + Z.of_string + "2896092933603266792420232188353085769364905557488064704211975662711617490282151438049471651752826349052481481665648" + in + Alcotest.(check fp2) + "equals" + (f + [| + "3047777769814684423038394620344802935229914459733231366149544800615464476011898542082941067129242206146300893290320"; + "410251538016797964359198251269349029304976484640221760394127819569082974482553326526117632906720735620521291693830"; + |]) + (Fp2.pow a exp); + let a = + f + [| + "205710166965963603981012255296970477922437454037238920251967980959566298218968922835594194618961919044498577759348"; + "963023020504828556541291942945998766804668055855414942365370050456823498198439603491492748408347910041342372760234"; + |] + in + let exp = + Z.of_string + "-3717874360341563294447445068927906050304754585312972454442053991648892388201310980640957589010533879202484676900264" + in + Alcotest.(check fp2) + "equals" + (f + [| + "2845520999043806566523917811328055505734856357107180420543700246274556445757865214330868774046996016398221391782248"; + "3538340034387881562854789767007685445208855109972362565047060928502157681913108620351811899896455421105521733042888"; + |]) + (Fp2.pow a exp) + let tests = Alcotest. [ @@ -167,4 +227,5 @@ let tests = test_case "Division" `Quick test_div; test_case "Additive Inverse" `Quick test_neg; test_case "Multiplicative Inverse" `Quick test_inv; + test_case "Exponentiation" `Quick test_pow; ] |
