package_name
stringlengths 2
45
| version
stringclasses 297
values | license
stringclasses 52
values | homepage
stringclasses 352
values | dev_repo
stringclasses 350
values | file_type
stringclasses 6
values | file_path
stringlengths 6
151
| file_content
stringlengths 0
9.28M
|
|---|---|---|---|---|---|---|---|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
dune
|
mirage-crypto-2.0.2/bench/dune
|
(executables
(names speed)
(modules speed)
(libraries mirage-crypto mirage-crypto-rng mirage-crypto-rng.unix
mirage-crypto-pk mirage-crypto-ec))
; marking as "(optional)" leads to OCaml-CI failures
; marking with "(package mirage-crypto-rng-miou-unix)" only has an effect with a "public_name"
;(executables
; (names miou)
; (modules miou)
; (libraries mirage-crypto-rng-miou-unix))
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
ml
|
mirage-crypto-2.0.2/bench/miou.ml
|
open Mirage_crypto
module Time = struct
let time ~n f a =
let t1 = Sys.time () in
for _ = 1 to n do ignore (f a) done ;
let t2 = Sys.time () in
(t2 -. t1)
let warmup () =
let x = ref 0 in
let rec go start =
if Sys.time () -. start < 1. then begin
for i = 0 to 10000 do x := !x + i done ;
go start
end in
go (Sys.time ())
end
let burn_period = 2.0
let sizes = [16; 64; 256; 1024; 8192]
(* let sizes = [16] *)
let burn f n =
let buf = Mirage_crypto_rng.generate n in
let (t1, i1) =
let rec loop it =
let t = Time.time ~n:it f buf in
if t > 0.2 then (t, it) else loop (it * 10) in
loop 10 in
let iters = int_of_float (float i1 *. burn_period /. t1) in
let time = Time.time ~n:iters f buf in
(iters, time, float (n * iters) /. time)
let mb = 1024. *. 1024.
let throughput title f =
Printf.printf "\n* [%s]\n%!" title ;
sizes |> List.iter @@ fun size ->
Gc.full_major () ;
let (iters, time, bw) = burn f size in
Printf.printf " % 5d: %04f MB/s (%d iters in %.03f s)\n%!"
size (bw /. mb) iters time
let bm name f = (name, fun () -> f name)
let benchmarks = [
bm "pfortuna" (fun name ->
let open Mirage_crypto_rng_miou_unix.Pfortuna in
Miou_unix.run ~domains:2 @@ fun () ->
let rng = Mirage_crypto_rng_miou_unix.(initialize (module Pfortuna)) in
let g = create () in
reseed ~g "abcd" ;
throughput name (fun buf ->
let buf = Bytes.unsafe_of_string buf in
generate_into ~g buf ~off:0 (Bytes.length buf));
Mirage_crypto_rng_miou_unix.kill rng) ;
]
let help () =
Printf.printf "available benchmarks:\n ";
List.iter (fun (n, _) -> Printf.printf "%s " n) benchmarks ;
Printf.printf "\n%!"
let runv fs =
Format.printf "accel: %a\n%!"
(fun ppf -> List.iter @@ fun x ->
Format.fprintf ppf "%s " @@
match x with `XOR -> "XOR" | `AES -> "AES" | `GHASH -> "GHASH")
accelerated;
Time.warmup () ;
List.iter (fun f -> f ()) fs
let () =
let seed = "abcd" in
let g = Mirage_crypto_rng.(create ~seed (module Fortuna)) in
Mirage_crypto_rng.set_default_generator g;
match Array.to_list Sys.argv with
| _::(_::_ as args) -> begin
try
let fs =
args |> List.map @@ fun n ->
snd (benchmarks |> List.find @@ fun (n1, _) -> n = n1) in
runv fs
with Not_found -> help ()
end
| _ -> help ()
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
ml
|
mirage-crypto-2.0.2/bench/speed.ml
|
open Mirage_crypto
module Time = struct
let time ~n f a =
let t1 = Sys.time () in
for _ = 1 to n do ignore (f a) done ;
let t2 = Sys.time () in
(t2 -. t1)
let warmup () =
let x = ref 0 in
let rec go start =
if Sys.time () -. start < 1. then begin
for i = 0 to 10000 do x := !x + i done ;
go start
end in
go (Sys.time ())
end
let burn_period = 2.0
let sizes = [16; 64; 256; 1024; 8192]
(* let sizes = [16] *)
let burn f n =
let buf = Mirage_crypto_rng.generate n in
let (t1, i1) =
let rec loop it =
let t = Time.time ~n:it f buf in
if t > 0.2 then (t, it) else loop (it * 10) in
loop 10 in
let iters = int_of_float (float i1 *. burn_period /. t1) in
let time = Time.time ~n:iters f buf in
(iters, time, float (n * iters) /. time)
let mb = 1024. *. 1024.
let throughput title f =
Printf.printf "\n* [%s]\n%!" title ;
sizes |> List.iter @@ fun size ->
Gc.full_major () ;
let (iters, time, bw) = burn f size in
Printf.printf " % 5d: %04f MB/s (%d iters in %.03f s)\n%!"
size (bw /. mb) iters time
let throughput_into ?(add = 0) title f =
Printf.printf "\n* [%s]\n%!" title ;
sizes |> List.iter @@ fun size ->
Gc.full_major () ;
let dst = Bytes.create (size + add) in
let (iters, time, bw) = burn (f dst) size in
Printf.printf " % 5d: %04f MB/s (%d iters in %.03f s)\n%!"
size (bw /. mb) iters time
let count_period = 10.
let count f n =
ignore (f n);
let i1 = 5 in
let t1 = Time.time ~n:i1 f n in
let iters = int_of_float (float i1 *. count_period /. t1) in
let time = Time.time ~n:iters f n in
(iters, time)
let count title f to_str args =
Printf.printf "\n* [%s]\n%!" title ;
args |> List.iter @@ fun arg ->
Gc.full_major () ;
let iters, time = count f arg in
Printf.printf " %s: %.03f ops per second (%d iters in %.03f)\n%!"
(to_str arg) (float iters /. time) iters time
let msg_str = String.make 100 '\xAA'
let msg_str_32 = String.sub msg_str 0 32
let msg_str_48 = String.sub msg_str 0 48
let msg_str_65 = String.sub msg_str 0 65
module PSS = Mirage_crypto_pk.Rsa.PSS(Digestif.SHA256)
let rsa_1024 =
let p = Z.of_string "10798561676627454710140432432014696449593673631094049392368450463276546091610832740190717321579865870896133380991892468262437092547408603618427685009427773"
and q = Z.of_string "10400664760062606994038747844895625872750212488858602663971334563613232045185857177383833781411830934303117994128623611996670112272953487791473086751129863"
and e = Z.of_string "65537"
in
match Mirage_crypto_pk.Rsa.priv_of_primes ~e ~p ~q with Ok r -> r | _ -> assert false
let enc_1024 = Mirage_crypto_pk.Rsa.(encrypt ~key:(pub_of_priv rsa_1024) msg_str)
let pkcs1_sig_1024 () =
Mirage_crypto_pk.Rsa.PKCS1.sign ~hash:`SHA256 ~key:rsa_1024 (`Message msg_str)
let pkcs1_enc_1024 () =
Mirage_crypto_pk.Rsa.(PKCS1.encrypt ~key:(pub_of_priv rsa_1024) msg_str)
let pss_sig_1024 () = PSS.sign ~key:rsa_1024 (`Message msg_str)
let rsa_2048 =
let p = Z.of_string "146881832325800831419400417618624202055588545997890787121932184528831630537012732415698782899346395306540669232648045731896347007978622067056705527305566180903122107927148832001099595387953189273726394573803912262323600581299712943797238366745329534148223987933536186022708693674753193534229263584177098260169"
and q = Z.of_string "146461957885839900502732892013745315236120923895767594427579857452138451155393985820080680192640369593315439290134409437965406213465262989382655388410330601076036910359057156988645246773259111682038096388585157599977808854893528900530706460128823381760931962351810679571404043148961709991714582814015259432029"
and e = Z.of_string "65537"
in
match Mirage_crypto_pk.Rsa.priv_of_primes ~e ~p ~q with Ok r -> r | _ -> assert false
let enc_2048 = Mirage_crypto_pk.Rsa.(encrypt ~key:(pub_of_priv rsa_2048) msg_str)
let pkcs1_sig_2048 () =
Mirage_crypto_pk.Rsa.PKCS1.sign ~hash:`SHA256 ~key:rsa_2048 (`Message msg_str)
let pkcs1_enc_2048 () =
Mirage_crypto_pk.Rsa.(PKCS1.encrypt ~key:(pub_of_priv rsa_2048) msg_str)
let pss_sig_2048 () = PSS.sign ~key:rsa_2048 (`Message msg_str)
let rsa_4096 =
let p = Z.of_string "30773596934476715066776070065844902670036493980016387964275170019397018472432997910667589359581914549510631424565206701540136804180560112829236103459317928059975099687383138310206374921731816027058152009810073337617754052401932141110921176212810704858018214605862299356217860547747262170495777126218319842708093667844701139914958775637423731967187071886349669479192453619522943080948061657926138418380417577129184420732857906610804965319661598089231703183044642635889126023201809407430354992888247464125783088294095728916671050049684448794153783082653555256735912037270303014887722063417225893745458164718800442738569"
and q = Z.of_string "25905916162566396401205858643227945415345838368190515936191926936462899261314859092468495558719305083654714669527919862817831941531613073577910643681172802147392797914485263753968375221243705167910636382434485717071007054833155618588980128488599406663210791261135710551020276087851551652652612955553056226986980360600996201307035494196112173475664509878923172924086102889718336621725968169373963280305056864698229857913526607314633711315503607289470716733189135747457446349029256257187264165837635026903463931381823712962360342258652047970731347111703873301687821992467888598546386551115261010493902143134851640738743"
and e = Z.of_string "65537"
in
match Mirage_crypto_pk.Rsa.priv_of_primes ~e ~p ~q with Ok r -> r | _ -> assert false
let enc_4096 = Mirage_crypto_pk.Rsa.(encrypt ~key:(pub_of_priv rsa_4096) msg_str)
let pkcs1_sig_4096 () =
Mirage_crypto_pk.Rsa.PKCS1.sign ~hash:`SHA256 ~key:rsa_4096 (`Message msg_str)
let pkcs1_enc_4096 () =
Mirage_crypto_pk.Rsa.(PKCS1.encrypt ~key:(pub_of_priv rsa_4096) msg_str)
let pss_sig_4096 () = PSS.sign ~key:rsa_4096 (`Message msg_str)
let dsa_1024 =
let p = Z.of_string "115320471016337933377056549329182706825658339080795846324118938187917903660539570102468495091957028599543345588517799627361082806070282899880721557018345825086927289316756283826093243695405203187016738458545513419551779925532261196890562077023934735570005318513791942265699098088390517334916527653326493928799"
and q = Z.of_string "823267969559752761552104454322087597915195665001"
and gg = Z.of_string "107937769619514611906619060647411205822947624664377868769814121409943849987480570028955037310439082345400300825969182219850876363034452830224901430080806055218560008392720388910894912162956691999057850754409178667408425482805161601110189024138168750376391340697351250728689008407981372513900837280131855895453"
and x = Z.of_string "33269272469299670210735451373406214067383586377"
and y = Z.of_string "43917635707590891841908570055721669604556135044554274942460553515946670787931699807386932177837523342760860376770220825997328312057886461226985675983491441562087492365801663397409369541614646669226917344513472367438132106373179011858311945451923744651780314133078253880297369792145807736223662521868826642853"
in
match Mirage_crypto_pk.Dsa.priv ~fips:true ~p ~q ~gg ~x ~y () with Ok p -> p | _ -> assert false
let dsa_sig_1024 () = Mirage_crypto_pk.Dsa.sign ~key:dsa_1024 msg_str
let dsa_2048 =
let p = Z.of_string "27787495469795504213817302334103600594688179071059183073859876165757248559489321478170600304273914000462158587756787453177210321379060448141559798652196363556897576291878245650614903612762833777567911000834171168229784178643222849655095281437320492725855855778320111645629834980350492228611813830302209080760811887894272862901026864911346096471199762409562102789142939773632891860019140618313962854554152891445175391927591825205548689170996430765723064763763481336517107917261869303217480777161449935319930795628114622197586510378927239068257979584784079128534248603619156372913573809491691986354447396965646770535701"
and q = Z.of_string "69694877308167037149745913456421442195328554169759046914164177549875778020469"
and gg = Z.of_string "16749627588066214399529603991445197534749244283120164288067836662918885787186948571007751498897778360267876697044209030527058098259975959998878027668545429739993477399366554325353523024222400972678469229055566504457717513550553993631550406867067991877458269091177591866978827953084168571646688881904998325355571633065354963580984543158204292013550690051754036914978697535194466008409541982818422484127204033337933785318568157008881227465007133605041651516579370726984057624602011504585990465767875831799514375203088558577008613314886723862237337922917306616319550969129882699756566436846243489458865623046875627558535"
and x = Z.of_string "52860861934156228997899838985740859941028688506510055821449681896336427977580"
and y = Z.of_string "16697822339875604612001674517725789858937385326266245308648897963274039128000291572870559603618903431422492771498840266582664620626826186158140214481920146230768719356298465470274085841064126012204317638870338958089054809559680905413861272320951150917299685355736888023985398767858005440235842845908452598291689850063919053521400020402671375982259094019780813889586357332424647365679629398571364161673401249910198031061651183589601097975488702613291524957230624044246866866974886847601585171352897595544769649242723658395277456627024303041369757410970512818793143517329469213213147704583953410882515213978051041273924"
in
match Mirage_crypto_pk.Dsa.priv ~fips:true ~p ~q ~gg ~x ~y () with Ok p -> p | _ -> assert false
let dsa_sig_2048 () = Mirage_crypto_pk.Dsa.sign ~key:dsa_2048 msg_str
let dsa_3072 =
let p = Z.of_string "4944862491052787177238323499959371418651354629231656321315236369672827559263545931134286049323485061071828187289578269594065783019111035804017538871324004047710342711620233110167493989997579634523303899794913823240058891327833786211541568251787338957336540247816021098378292806006955851897646808403078979142749428669072523191276645021175423303816467433407072660616741824124536840773744646488191896772232795413707995397140064396495425700133866462410490239713815308709711960470201906326732033816522202617817869465691798938486540955726912350768931476362143768721380759395525951947017232778140349423557015356082357043807910825817719748257213281893007933859227824276579765323175836008193865064772817200047353825332039369252224256435661514851653526942065285711420907389170574343434449883875510985495078384130667046036846831401643151166834922210257258578675547742596423035828159461629721005113634334227074529533688136165903014911127"
and q = Z.of_string "72036757532428134359049138716615314032674441223147930984416116642785279309001"
and gg = Z.of_string "988301665281495772390013694627509692333967846948672137624515090935924385717634154201978961497509784579332702743535206413508559565302483922898459454403718843912379531742192312937734625047119678718271184170003455506604118936761508195594240052138536667234693864514877750501896049675764191029147963148241546820518065141123555298022010467792468407477159110344370654433269478015817957411602389410658876373667769353995724289566719120654426746425129842353040271693696527020500630012804936844492302532860691617810440827122662134370347136275931360845416833023047973072799739252681873195380321841873819721774703093238289342578739869306714624065593724407718101053836638039267362740083113357679437895609399028133545708736803196232072972950098992845234240283344492163375862712470338417546036591824286944195749933069780384676421299008472374982388004050973085425949637720603596481254386896408204626665775305048865550117840561595366712598318"
and x = Z.of_string "57079433780483458942713357293831115449694380253611914431677328021806898761674"
and y = Z.of_string "2872172233173100601346399502391482510148300641600207189246652621232656647081281141886455256814187251102030580186748870087277263359506423691023579912476503628426574690699263443704236625550832436226673947182294798279064154375422081421444400893924415563728656687773617245084962617886701420982288621397340217078717788290878177343138178149109067141997920049624616209300715994802074480057682676445987819355778630849636079364539062466859717465892222793824712941403252645431023939220711618233660062829520555618350855085688062241702521140104357981881833598269736147371800258828202964600523335598361833482597511071900975835195171061421945579132277176597724513608746614358311884564512362186106480065540201711903122228315570389409204443391052987813355120223856839756971514334338065850104261467285471383663470187296456006848469765768400814476981451863643010333596484259098557995874127140419649424228480234701334240448168704764430867915060"
in
match Mirage_crypto_pk.Dsa.priv ~fips:true ~p ~q ~gg ~x ~y () with Ok p -> p | _ -> assert false
let dsa_sig_3072 () = Mirage_crypto_pk.Dsa.sign ~key:dsa_3072 msg_str
let dh_groups =
["oakley5 (1536)",Mirage_crypto_pk.Dh.Group.oakley_5;
"oakley14 (2048)",Mirage_crypto_pk.Dh.Group.oakley_14;
"ffdhe2048",Mirage_crypto_pk.Dh.Group.ffdhe2048;
"ffdhe3072",Mirage_crypto_pk.Dh.Group.ffdhe3072;
"ffdhe4096",Mirage_crypto_pk.Dh.Group.ffdhe4096;
"ffdhe6144",Mirage_crypto_pk.Dh.Group.ffdhe6144]
let dh_secrets =
List.map2 (fun (n, group) s ->
(n, group), Mirage_crypto_pk.Dh.key_of_secret group ~s)
dh_groups
(List.map (fun s -> Z.of_string s |> Mirage_crypto_pk.Z_extra.to_octets_be)
[
"31271182055444024732867835946284871743952969208281694762833912267184" ;
"27594341083884344999714422172371027333192426063917478556668524561591" ;
"49745209598738800585479479877345156590922715411063492309021724116430" ;
"54263413298355776701974737228250185414758929445654879795198916482466337662578919821" ;
"38599161911587526396222063388324161227700603198435442693976375015855884010520671067171149524070089" ;
"60057457975706301816395663645420233759377744187465730049174048360108513636349450241008234412972340882517684187851" ;
])
let ecdsa_p256 =
Result.get_ok
(Mirage_crypto_ec.P256.Dsa.priv_of_octets
"\x08\x9f\x4f\xfc\xcc\xf9\xba\x13\xfe\xdd\x09\x42\xef\x08\xcf\x2d\x90\x9f\x32\xe2\x93\x4a\xb5\xc9\x3b\x6c\x99\xbe\x5a\x9f\xf5\x27")
let ecdsa_p256_sig () = Mirage_crypto_ec.P256.Dsa.sign ~key:ecdsa_p256 msg_str_32
let ecdsa_p384 =
Result.get_ok
(Mirage_crypto_ec.P384.Dsa.priv_of_octets
"\xf5\xc0\xc9\xfb\x95\x17\x86\x41\xaf\x76\xf3\x83\x1f\x41\xe2\xd3\x7c\xfa\xaf\xff\xc7\xe6\x01\x72\xcf\xb0\x89\xfe\x60\x4b\x56\xa6\x1c\x7c\x31\xa6\x90\x4b\x3b\x5d\x08\x20\x7a\x4b\x81\xe2\x5e\xa5")
let ecdsa_p384_sig () = Mirage_crypto_ec.P384.Dsa.sign ~key:ecdsa_p384 msg_str_48
let ecdsa_p521 =
Result.get_ok
(Mirage_crypto_ec.P521.Dsa.priv_of_octets
"\x00\xb1\x8f\x60\xc0\x35\x2a\xd8\xe3\xef\x98\x2f\x1d\xdf\xcf\x6e\xec\x7f\xa6\xca\xf0\xe6\xf3\x68\x35\x4a\x8b\x02\xb2\xd8\xac\x1e\x05\x9e\x30\x98\x91\xe2\xbf\xa8\x57\x91\xa5\xe7\x1b\x40\xbd\xec\xbf\x90\x2b\xf2\x43\xdc\x3b\x00\x80\x49\x5c\xf4\xd9\x1c\x78\x72\x8b\xd5")
let ecdsa_p521_sig () = Mirage_crypto_ec.P521.Dsa.sign ~key:ecdsa_p521 msg_str_65
let ed25519 =
Result.get_ok (Mirage_crypto_ec.Ed25519.priv_of_octets
"\x3e\x0a\xb6\x82\x17\x12\x75\xc5\x69\xfc\xe9\xca\x8b\xcc\xd2\xd2\x77\x14\x54\xa2\x30\x0c\x35\x29\xf7\xa4\xd8\x0b\x84\x38\x83\xbc")
let ed25519_sig () = Mirage_crypto_ec.Ed25519.sign ~key:ed25519 msg_str
let ecdsas = [
("P256", `P256 (ecdsa_p256, ecdsa_p256_sig ()));
("P384", `P384 (ecdsa_p384, ecdsa_p384_sig ()));
("P521", `P521 (ecdsa_p521, ecdsa_p521_sig ()));
("Ed25519", `Ed25519 (ed25519, ed25519_sig ()));
]
let ecdh_shares =
[
("P256", `P256 (Mirage_crypto_ec.P256.Dh.secret_of_octets "\x47\x0d\x57\x70\x6c\x77\x06\xb6\x8a\x3f\x42\x3a\xea\xf4\xff\x7f\xdd\x02\x49\x4a\x10\xd3\xe3\x81\xc3\xc1\x1f\x72\x76\x80\x2c\xdc" |> Result.get_ok |> fst,
"\x04\x11\xb3\xfc\x82\x72\x1c\x26\x9a\x19\x90\x9a\x3b\x2f\xc2\x6d\x98\x95\x82\x6d\x0c\xfc\xbc\x1f\x76\x26\xe4\x88\xf0\x1f\x4c\xa6\xb5\xc5\xed\x76\xad\xee\x7a\xf8\x1b\xb2\x0b\x17\xcf\x23\x1c\xbf\x0c\x67\xdb\x02\x95\xd6\x8d\x1d\x92\xc2\xd2\xa5\xa8\x06\x38\xd7\x8d"));
("P384", `P384 (Mirage_crypto_ec.P384.Dh.secret_of_octets "\xee\x55\xe2\x9b\x61\x75\x2d\x5a\x3e\x52\x56\x56\xdb\x8b\xd8\xfe\x6f\x94\xfa\xb8\xaa\xcc\x9e\x92\xac\xff\x4c\x48\x12\xbf\x7a\x61\x87\xab\xa4\x6c\xc6\x0a\xb8\xf0\x8e\xfc\xf2\xd5\x74\x58\x4b\x74" |> Result.get_ok |> fst,
"\x04\x04\x89\xcf\x24\xbc\x80\xbf\x89\xfd\xfe\x9c\x05\xec\xc3\x9f\x69\x16\xad\x45\x09\xd9\x39\x85\x97\x95\x0d\x3d\x24\xe8\x28\xf6\xbf\x56\xba\x4a\xd6\xd2\x1e\xd7\x86\x3b\xed\x68\xe4\x13\x36\x4b\xd4\xc7\xb1\xe9\x04\x7d\x36\x12\x4c\x69\x53\xbe\x7c\x61\x20\x9c\xb3\xfc\x56\x45\x2f\x73\x05\x29\x37\x83\xc7\xc0\xed\x92\x9d\x6c\x98\xc7\xbc\x97\xf6\x0a\x72\xed\x22\x69\xa8\xeb\x19\xbb\x7e\xe1\x31"));
("P521", `P521 (Mirage_crypto_ec.P521.Dh.secret_of_octets "\x00\xaa\x47\x0b\xa1\xcc\x84\x3b\xa3\x14\x82\x1e\x72\xde\x4c\xd2\x99\xae\xc1\xf2\x6e\x9d\x64\xa0\xd8\x7d\xb1\x8a\x3d\xa9\xf6\x5c\x45\xec\xfc\xc5\x61\x7f\xf0\xd7\x3b\x2e\x0e\x1c\xdf\xf8\x04\x8e\x01\xbe\x5e\x20\x14\x94\x12\xe7\xdb\xfa\xb7\xfe\xae\x24\x9b\x1b\xfa\x4d" |> Result.get_ok |> fst,
"\x04\x00\x1d\x16\x29\xee\xb1\xc4\x25\xf9\x04\xd7\x55\x33\x00\x79\xd1\x3c\x77\xda\x92\x1e\x01\xcf\x50\xd7\x17\xe0\xd6\x85\x0a\x81\xa3\x90\x2b\xb9\x2a\x03\xfa\xea\xcb\xd6\x28\x9c\x15\x90\x68\x5a\x60\x44\xb5\xe9\x4d\xcf\xc4\x1d\xeb\x6a\x88\xdb\x62\xa8\x91\xb0\xb8\x93\xbb\x00\xe4\x2a\x66\xb2\xf0\x13\xbd\xd0\xd2\x7d\x8e\x07\xcb\x35\xfc\x3e\x2c\x2b\x22\xf9\x3e\xcf\xd5\xea\xb7\x88\x61\x97\xca\x07\x3c\x2c\x5e\x68\x31\xd6\x5e\x2d\x0b\x8a\xa4\x08\x43\x8e\x49\x54\x2f\x05\xf4\x1c\x57\x6d\xf7\x0e\x3c\xaf\x5b\xb8\x22\x7d\x48\x30\x94\xae\x58"));
("X25519", `X25519 (Mirage_crypto_ec.X25519.secret_of_octets "\x4c\x6d\xb7\xcf\x93\x5b\xcf\x84\x02\x61\x78\xd4\x0c\x95\x6a\xf0\x9d\x8e\x36\x32\x03\x49\x0d\x2c\x41\x62\x5a\xcb\x68\xb9\x31\xa4" |> Result.get_ok |> fst,
"\xca\x19\x19\x3c\xf5\xc0\xb3\x8c\x61\xaa\x01\xc1\x72\xb2\xe9\x3d\x16\xf7\x50\xd0\x84\x62\x77\xad\x32\x2d\xe5\xe4\xfb\x33\x24\x29"));
]
let bm name f = (name, fun () -> f name)
let benchmarks = [
bm "rsa-generate" (fun name ->
count name (fun bits -> Mirage_crypto_pk.Rsa.generate ~bits ())
string_of_int [1024;2048;4096]) ;
bm "rsa-encrypt" (fun name ->
count name (fun key -> Mirage_crypto_pk.Rsa.(encrypt ~key:(pub_of_priv key) msg_str))
(fun k -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024;rsa_2048;rsa_4096]) ;
bm "rsa-decrypt" (fun name ->
count name (fun (key, msg) -> Mirage_crypto_pk.Rsa.(decrypt ~key msg))
(fun (k, _) -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024,enc_1024 ; rsa_2048,enc_2048 ; rsa_4096,enc_4096]) ;
bm "rsa-pkcs1-encrypt" (fun name ->
count name (fun key -> Mirage_crypto_pk.Rsa.(PKCS1.encrypt ~key:(pub_of_priv key) msg_str))
(fun k -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024;rsa_2048;rsa_4096]) ;
bm "rsa-pkcs1-decrypt" (fun name ->
count name (fun (key, msg) -> Mirage_crypto_pk.Rsa.(PKCS1.decrypt ~key msg))
(fun (k, _) -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024,pkcs1_enc_1024 () ; rsa_2048,pkcs1_enc_2048 () ; rsa_4096,pkcs1_enc_4096 ()]) ;
bm "rsa-pkcs1-sign" (fun name ->
count name (fun key -> Mirage_crypto_pk.Rsa.PKCS1.sign ~hash:`SHA256 ~key (`Message msg_str))
(fun k -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024;rsa_2048;rsa_4096]) ;
bm "rsa-pkcs1-verify" (fun name ->
count name (fun (key, signature) ->
Mirage_crypto_pk.Rsa.(PKCS1.verify ~hashp:(fun _ -> true) ~key:(pub_of_priv key) ~signature (`Message msg_str)))
(fun (k, _) -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024,pkcs1_sig_1024 () ; rsa_2048,pkcs1_sig_2048 () ; rsa_4096,pkcs1_sig_4096 ()]) ;
bm "rsa-pss-sign" (fun name ->
count name (fun key -> PSS.sign ~key (`Message msg_str))
(fun k -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024;rsa_2048;rsa_4096]) ;
bm "rsa-pss-verify" (fun name ->
count name (fun (key, signature) ->
PSS.verify ~key:(Mirage_crypto_pk.Rsa.pub_of_priv key) ~signature (`Message msg_str))
(fun (k, _) -> string_of_int (Mirage_crypto_pk.Rsa.priv_bits k))
[rsa_1024,pss_sig_1024 () ; rsa_2048,pss_sig_2048 () ; rsa_4096,pss_sig_4096 ()]) ;
bm "dsa-generate" (fun name ->
count name (fun ks -> Mirage_crypto_pk.Dsa.generate ks)
(function `Fips1024 -> "1024" | `Fips2048 -> "2048" | `Fips3072 -> "3072" | `Exactly (l, _) -> string_of_int l)
[`Fips1024;`Fips2048;`Fips3072]);
bm "dsa-sign" (fun name ->
count name (fun key -> Mirage_crypto_pk.Dsa.sign ~key msg_str)
(fun k -> string_of_int (Z.numbits k.p))
[dsa_1024;dsa_2048;dsa_3072]);
bm "dsa-verify" (fun name ->
count name (fun (key, signature) ->
Mirage_crypto_pk.Dsa.(verify ~key:(pub_of_priv key) signature msg_str))
(fun (k, _) -> string_of_int (Z.numbits k.p))
[dsa_1024,dsa_sig_1024 () ; dsa_2048,dsa_sig_2048 () ; dsa_3072,dsa_sig_3072 ()]);
bm "ecdsa-generate" (fun name ->
let open Mirage_crypto_ec in
count name
(fun (_, x) -> match x with
| `P256 _ -> P256.Dsa.generate () |> ignore
| `P384 _ -> P384.Dsa.generate () |> ignore
| `P521 _ -> P521.Dsa.generate () |> ignore
| `Ed25519 _ -> Ed25519.generate () |> ignore
)
fst ecdsas);
bm "ecdsa-sign" (fun name ->
let open Mirage_crypto_ec in
count name (fun (_, x) -> match x with
| `P256 (key, _) -> P256.Dsa.sign ~key msg_str_32
| `P384 (key, _) -> P384.Dsa.sign ~key msg_str_48
| `P521 (key, _) -> P521.Dsa.sign ~key msg_str_65
| `Ed25519 (key, _) -> Ed25519.sign ~key msg_str, ""
)
fst ecdsas);
bm "ecdsa-verify" (fun name ->
let open Mirage_crypto_ec in
count name (fun (_, x) -> match x with
| `P256 (key, signature) -> P256.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_32)
| `P384 (key, signature) -> P384.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_48)
| `P521 (key, signature) -> P521.Dsa.(verify ~key:(pub_of_priv key) signature msg_str_65)
| `Ed25519 (key, signature) -> Ed25519.(verify ~key:(pub_of_priv key) signature ~msg:msg_str)
) fst ecdsas);
bm "dh-secret" (fun name ->
count name (fun (_, group) -> Mirage_crypto_pk.Dh.gen_key group)
fst dh_groups);
bm "dh-share" (fun name ->
count name (fun (_, (sec, share)) ->
Mirage_crypto_pk.Dh.shared sec share)
(fun ((g, _), _) -> g) dh_secrets);
bm "ecdh-secret" (fun name ->
let open Mirage_crypto_ec in
count name (fun (_, x) -> match x with
| `P256 _ -> P256.Dh.gen_key () |> ignore
| `P384 _ -> P384.Dh.gen_key () |> ignore
| `P521 _ -> P521.Dh.gen_key () |> ignore
| `X25519 _ -> X25519.gen_key () |> ignore)
fst ecdh_shares);
bm "ecdh-share" (fun name ->
let open Mirage_crypto_ec in
count name (fun (_, x) -> match x with
| `P256 (sec, share) -> P256.Dh.key_exchange sec share |> Result.get_ok |> ignore
| `P384 (sec, share) -> P384.Dh.key_exchange sec share |> Result.get_ok |> ignore
| `P521 (sec, share) -> P521.Dh.key_exchange sec share |> Result.get_ok |> ignore
| `X25519 (sec, share) -> X25519.key_exchange sec share |> Result.get_ok |> ignore)
fst ecdh_shares);
bm "chacha20-poly1305" (fun name ->
let key = Chacha20.of_secret (Mirage_crypto_rng.generate 32)
and nonce = Mirage_crypto_rng.generate 8 in
throughput_into ~add:Chacha20.tag_size name
(fun dst cs -> Chacha20.authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs))) ;
bm "chacha20-poly1305-unsafe" (fun name ->
let key = Chacha20.of_secret (Mirage_crypto_rng.generate 32)
and nonce = Mirage_crypto_rng.generate 8 in
throughput_into ~add:Chacha20.tag_size name
(fun dst cs -> Chacha20.unsafe_authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs))) ;
bm "aes-128-ecb" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 16) in
throughput_into name
(fun dst cs -> AES.ECB.encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-192-ecb" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 24) in
throughput_into name (fun dst cs -> AES.ECB.encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-192-ecb-unsafe" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 24) in
throughput_into name (fun dst cs -> AES.ECB.unsafe_encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-256-ecb" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 32) in
throughput_into name (fun dst cs -> AES.ECB.encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-256-ecb-unsafe" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 32) in
throughput_into name (fun dst cs -> AES.ECB.unsafe_encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-ecb-unsafe" (fun name ->
let key = AES.ECB.of_secret (Mirage_crypto_rng.generate 16) in
throughput_into name
(fun dst cs -> AES.ECB.unsafe_encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-cbc-e" (fun name ->
let key = AES.CBC.of_secret (Mirage_crypto_rng.generate 16)
and iv = Mirage_crypto_rng.generate 16 in
throughput_into name
(fun dst cs -> AES.CBC.encrypt_into ~key ~iv cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-cbc-e-unsafe" (fun name ->
let key = AES.CBC.of_secret (Mirage_crypto_rng.generate 16)
and iv = Mirage_crypto_rng.generate 16 in
throughput_into name
(fun dst cs -> AES.CBC.unsafe_encrypt_into ~key ~iv cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-cbc-e-unsafe-inplace" (fun name ->
let key = AES.CBC.of_secret (Mirage_crypto_rng.generate 16)
and iv = Mirage_crypto_rng.generate 16 in
throughput name
(fun cs ->
let b = Bytes.unsafe_of_string cs in
AES.CBC.unsafe_encrypt_into_inplace ~key ~iv b ~dst_off:0 (String.length cs))) ;
bm "aes-128-cbc-d" (fun name ->
let key = AES.CBC.of_secret (Mirage_crypto_rng.generate 16)
and iv = Mirage_crypto_rng.generate 16 in
throughput_into name
(fun dst cs -> AES.CBC.decrypt_into ~key ~iv cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-cbc-d-unsafe" (fun name ->
let key = AES.CBC.of_secret (Mirage_crypto_rng.generate 16)
and iv = Mirage_crypto_rng.generate 16 in
throughput_into name
(fun dst cs -> AES.CBC.unsafe_decrypt_into ~key ~iv cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-ctr" (fun name ->
let key = Mirage_crypto_rng.generate 16 |> AES.CTR.of_secret
and ctr = Mirage_crypto_rng.generate 16 |> AES.CTR.ctr_of_octets in
throughput_into name (fun dst cs -> AES.CTR.encrypt_into ~key ~ctr cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-ctr-unsafe" (fun name ->
let key = Mirage_crypto_rng.generate 16 |> AES.CTR.of_secret
and ctr = Mirage_crypto_rng.generate 16 |> AES.CTR.ctr_of_octets in
throughput_into name (fun dst cs -> AES.CTR.unsafe_encrypt_into ~key ~ctr cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "aes-128-gcm" (fun name ->
let key = AES.GCM.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 12 in
throughput_into ~add:AES.GCM.tag_size name
(fun dst cs -> AES.GCM.authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs)));
bm "aes-128-gcm-unsafe" (fun name ->
let key = AES.GCM.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 12 in
throughput_into ~add:AES.GCM.tag_size name
(fun dst cs -> AES.GCM.unsafe_authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs)));
bm "aes-128-ghash" (fun name ->
let key = AES.GCM.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 12 in
throughput_into ~add:AES.GCM.tag_size name
(fun dst cs -> AES.GCM.authenticate_encrypt_into ~key ~nonce ~adata:cs "" ~src_off:0 dst ~dst_off:0 ~tag_off:0 0));
bm "aes-128-ghash-unsafe" (fun name ->
let key = AES.GCM.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 12 in
throughput_into ~add:AES.GCM.tag_size name
(fun dst cs -> AES.GCM.unsafe_authenticate_encrypt_into ~key ~nonce ~adata:cs "" ~src_off:0 dst ~dst_off:0 ~tag_off:0 0));
bm "aes-128-ccm" (fun name ->
let key = AES.CCM16.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 10 in
throughput_into ~add:AES.CCM16.tag_size name
(fun dst cs -> AES.CCM16.authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs)));
bm "aes-128-ccm-unsafe" (fun name ->
let key = AES.CCM16.of_secret (Mirage_crypto_rng.generate 16)
and nonce = Mirage_crypto_rng.generate 10 in
throughput_into ~add:AES.CCM16.tag_size name
(fun dst cs -> AES.CCM16.unsafe_authenticate_encrypt_into ~key ~nonce cs ~src_off:0 dst ~dst_off:0 ~tag_off:(String.length cs) (String.length cs)));
bm "d3des-ecb" (fun name ->
let key = DES.ECB.of_secret (Mirage_crypto_rng.generate 24) in
throughput_into name (fun dst cs -> DES.ECB.encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "d3des-ecb-unsafe" (fun name ->
let key = DES.ECB.of_secret (Mirage_crypto_rng.generate 24) in
throughput_into name (fun dst cs -> DES.ECB.unsafe_encrypt_into ~key cs ~src_off:0 dst ~dst_off:0 (String.length cs))) ;
bm "fortuna" (fun name ->
begin[@alert "-deprecated"]
Mirage_crypto_rng_unix.initialize (module Mirage_crypto_rng.Fortuna);
throughput name (fun buf ->
let buf = Bytes.unsafe_of_string buf in
Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))
end);
bm "getentropy" (fun name ->
Mirage_crypto_rng_unix.use_getentropy ();
throughput name (fun buf ->
let buf = Bytes.unsafe_of_string buf in
Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))) ;
bm "urandom" (fun name ->
Mirage_crypto_rng_unix.use_dev_urandom ();
throughput name (fun buf ->
let buf = Bytes.unsafe_of_string buf in
Mirage_crypto_rng.generate_into buf ~off:0 (Bytes.length buf))) ;
]
let help () =
Printf.printf "available benchmarks:\n ";
List.iter (fun (n, _) -> Printf.printf "%s " n) benchmarks ;
Printf.printf "\n%!"
let runv fs =
Format.printf "accel: %a\n%!"
(fun ppf -> List.iter @@ fun x ->
Format.fprintf ppf "%s " @@
match x with `XOR -> "XOR" | `AES -> "AES" | `GHASH -> "GHASH")
accelerated;
Time.warmup () ;
List.iter (fun f -> f ()) fs
let () =
let seed = "abcd" in
let g = Mirage_crypto_rng.(create ~seed (module Fortuna)) in
Mirage_crypto_rng.set_default_generator g;
match Array.to_list Sys.argv with
| _::(_::_ as args) -> begin
try
let fs =
args |> List.map @@ fun n ->
snd (benchmarks |> List.find @@ fun (n1, _) -> n = n1) in
runv fs
with Not_found -> help ()
end
| _ -> help ()
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
ml
|
mirage-crypto-2.0.2/config/cfg.ml
|
let () =
let c = Configurator.V1.create "mirage-crypto" in
let ccomp_type_opt = Configurator.V1.ocaml_config_var c "ccomp_type" in
let arch =
let defines =
Configurator.V1.C_define.import
c
~includes:[]
[("__x86_64__", Switch); ("__i386__", Switch); ("__powerpc64__", Switch);
("__s390x__", Switch); ("__aarch64__", Switch);
("_WIN64", Switch); ("_WIN32", Switch)]
in
match defines with
| (_, Switch true) :: _ -> `x86_64
| _ :: (_, Switch true) :: _ -> `x86
| _ :: _ :: (_, Switch true) :: _ -> `ppc64
| _ :: _ :: _ :: (_, Switch true) :: _ -> `s390x
| _ :: _ :: _ :: _ :: (_, Switch true) :: _ -> `arm64
| _ :: _ :: _ :: _ :: _ :: (_, Switch true) :: _ -> `x86_64
| _ :: _ :: _ :: _ :: _ :: _ :: (_, Switch true) :: _ -> `x86
| _ -> `unknown
in
let os =
let defines =
Configurator.V1.C_define.import
c
~includes:[]
[("__APPLE__", Switch)]
in
match defines with
| (_, Switch true) :: _ -> `macos
| _ -> `unknown
in
let accelerate_flags =
match arch, ccomp_type_opt with
| `x86_64, Some "msvc" -> [ "-DACCELERATE" ]
| `x86_64, _ -> [ "-DACCELERATE"; "-mssse3"; "-maes"; "-mpclmul" ]
| _ -> []
in
let ent_flags =
match arch, ccomp_type_opt with
| (`x86_64 | `x86), Some "msvc" -> [ "-DENTROPY" ]
| (`x86_64 | `x86), _ -> [ "-DENTROPY"; "-mrdrnd"; "-mrdseed" ]
| _ -> []
in
let std_flags =
match ccomp_type_opt with
| Some "msvc" -> ["/Wall"]
| _ -> ["--std=c11"; "-Wall"; "-Wextra"; "-Wpedantic"; "-O3"]
in
let warn_flags =
(* See #178, there may be false positives on ppc&s390 with no-stringop-overflow *)
match arch, ccomp_type_opt with
| _, Some "msvc" -> [ "/WX" ]
| (`ppc64, _) | (`s390x, _) -> [ "-Wno-stringop-overflow"; "-Werror" ]
| _ -> [ "-Werror" ]
in
let no_instcombine_on_macos = match arch, os with
| `arm64, `macos ->
let res = Configurator.V1.Process.run c "cc" ["-dumpversion"] in
if String.trim res.stdout = "14.0.3" then
["-mllvm"; "--instcombine-max-iterations=0"]
(* macOS instcombine miscompilation with clang 14.0.3 *)
else
[]
| _ -> []
in
let flags = std_flags @ no_instcombine_on_macos @ ent_flags in
let opt_flags = flags @ accelerate_flags in
Configurator.V1.Flags.write_sexp "cflags_optimized.sexp" opt_flags;
Configurator.V1.Flags.write_sexp "cflags.sexp" flags;
Configurator.V1.Flags.write_sexp "cflags_warn.sexp" warn_flags
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
dune
|
mirage-crypto-2.0.2/config/dune
|
(executables
(names cfg)
(libraries dune-configurator))
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
dune
|
mirage-crypto-2.0.2/ec/dune
|
(library
(name mirage_crypto_ec)
(public_name mirage-crypto-ec)
(libraries eqaf mirage-crypto-rng digestif)
(foreign_stubs
(language c)
(names p256_stubs np256_stubs p384_stubs np384_stubs p521_stubs np521_stubs
curve25519_stubs)
(include_dirs ../src/native)
(flags
(:standard -DNDEBUG)
(:include cflags_optimized.sexp))))
(env
(dev
(c_flags (:include cflags_warn.sexp))))
(include_subdirs unqualified)
(rule
(targets cflags_optimized.sexp cflags_warn.sexp)
(action
(run ../config/cfg.exe)))
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
dune
|
mirage-crypto-2.0.2/ec/gen_tables/dune
|
(include_subdirs no)
(executable
(name gen_tables)
(libraries mirage_crypto_ec))
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
ml
|
mirage-crypto-2.0.2/ec/gen_tables/gen_tables.ml
|
open Format
let print_header name =
printf
{|
/*
Pre-computed %d-bit multiples of the generator point G for the curve %s,
used for speeding up its scalar multiplication in point_operations.h.
Generated by %s
*/|}
Sys.word_size name Sys.argv.(0)
let pp_array elem_fmt fmt arr =
let fout = fprintf fmt in
let len = Array.length arr in
fout "@[<2>{@\n";
for i = 0 to len - 1 do
elem_fmt fmt arr.(i);
if i < len - 1 then printf ",@ " else printf ""
done;
fout "@]@,}"
let div_round_up a b = (a / b) + if a mod b = 0 then 0 else 1
let pp_string_words ~wordsize fmt str =
assert (String.length str * 8 mod wordsize = 0);
let limbs = String.length str * 8 / wordsize in
(* Truncate at the beginning (little-endian) *)
let bytes = Bytes.unsafe_of_string str in
(* let bytes = rev_str_bytes str in *)
fprintf fmt "@[<2>{@\n";
for i = 0 to limbs - 1 do
let index = i * (wordsize / 8) in
(if wordsize = 64 then
let w = Bytes.get_int64_le bytes index in
fprintf fmt "%#016Lx" w
else
let w = Bytes.get_int32_le bytes index in
fprintf fmt "%#08lx" w);
if i < limbs - 1 then printf ",@ " else printf ""
done;
fprintf fmt "@]@,}"
let check_shape tables =
let fe_len = String.length tables.(0).(0).(0) in
let table_len = fe_len * 2 in
assert (Array.length tables = table_len);
Array.iter
(fun x ->
assert (Array.length x = 15);
Array.iter
(fun x ->
assert (Array.length x = 3);
Array.iter (fun x -> assert (String.length x = fe_len)) x)
x)
tables
let print_tables tables ~wordsize =
let fe_len = String.length tables.(0).(0).(0) in
printf "@[<2>static WORD generator_table[%d][15][3][LIMBS] = @," (fe_len * 2);
pp_array
(pp_array (pp_array (pp_string_words ~wordsize)))
std_formatter tables;
printf "@];@,"
let print_toplevel name wordsize (module P : Mirage_crypto_ec.Dh_dsa) =
let tables = P.Dsa.Precompute.generator_tables () in
assert (wordsize = Sys.word_size);
check_shape tables;
print_header name;
if wordsize = 64 then
printf
"@[<v>#ifndef ARCH_64BIT@,\
#error \"Cannot use 64-bit tables on a 32-bit architecture\"@,\
#endif@,\
@]"
else
printf
"@[<v>#ifdef ARCH_64BIT@,\
#error \"Cannot use 32-bit tables on a 64-bit architecture\"@,\
#endif@,\
@]";
print_tables ~wordsize tables
let curves =
Mirage_crypto_ec.
[
("p256", (module P256 : Dh_dsa));
("p384", (module P384));
("p521", (module P521));
]
let usage () =
printf "Usage: gen_tables [%a] [64 | 32]@."
(pp_print_list
~pp_sep:(fun fmt () -> pp_print_string fmt " | ")
pp_print_string)
(List.map fst curves)
let go =
let name, curve, wordsize =
try
let name, curve =
List.find (fun (name, _) -> name = Sys.argv.(1)) curves
in
(name, curve, int_of_string Sys.argv.(2))
with _ ->
usage ();
exit 1
in
print_toplevel name wordsize curve
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
ml
|
mirage-crypto-2.0.2/ec/mirage_crypto_ec.ml
|
type error = [
| `Invalid_format
| `Invalid_length
| `Invalid_range
| `Not_on_curve
| `At_infinity
| `Low_order
]
let error_to_string = function
| `Invalid_format -> "invalid format"
| `Not_on_curve -> "point is not on curve"
| `At_infinity -> "point is at infinity"
| `Invalid_length -> "invalid length"
| `Invalid_range -> "invalid range"
| `Low_order -> "low order"
let pp_error fmt e =
Format.fprintf fmt "Cannot parse point: %s" (error_to_string e)
let rev_string buf =
let len = String.length buf in
let res = Bytes.create len in
for i = 0 to len - 1 do
Bytes.set res (len - 1 - i) (String.get buf i)
done ;
Bytes.unsafe_to_string res
exception Message_too_long
let bit_at buf i =
let byte_num = i / 8 in
let bit_num = i mod 8 in
let byte = String.get_uint8 buf byte_num in
byte land (1 lsl bit_num) <> 0
module type Dh = sig
type secret
val secret_of_octets : ?compress:bool -> string ->
(secret * string, error) result
val secret_to_octets : secret -> string
val gen_key : ?compress:bool -> ?g:Mirage_crypto_rng.g -> unit ->
secret * string
val key_exchange : secret -> string -> (string, error) result
end
module type Dsa = sig
type priv
type pub
val byte_length : int
val bit_length : int
val priv_of_octets : string -> (priv, error) result
val priv_to_octets : priv -> string
val pub_of_octets : string -> (pub, error) result
val pub_to_octets : ?compress:bool -> pub -> string
val pub_of_priv : priv -> pub
val generate : ?g:Mirage_crypto_rng.g -> unit -> priv * pub
val sign : key:priv -> ?k:string -> string -> string * string
val verify : key:pub -> string * string -> string -> bool
module K_gen (H : Digestif.S) : sig
val generate : key:priv -> string -> string
end
module Precompute : sig
val generator_tables : unit -> string array array array
end
end
module type Dh_dsa = sig
module Dh : Dh
module Dsa : Dsa
end
type field_element = string
type out_field_element = bytes
module type Parameters = sig
val a : field_element
val b : field_element
val g_x : field_element
val g_y : field_element
val p : field_element
val n : field_element
val pident: string
val byte_length : int
val bit_length : int
val fe_length : int
val first_byte_bits : int option
end
type point = { f_x : field_element; f_y : field_element; f_z : field_element }
type out_point = { m_f_x : out_field_element; m_f_y : out_field_element; m_f_z : out_field_element }
type scalar = Scalar of string
module type Foreign = sig
val mul : out_field_element -> field_element -> field_element -> unit
val sub : out_field_element -> field_element -> field_element -> unit
val add : out_field_element -> field_element -> field_element -> unit
val to_montgomery : out_field_element -> field_element -> unit
val from_octets : out_field_element -> string -> unit
val set_one : out_field_element -> unit
val nz : field_element -> bool
val sqr : out_field_element -> field_element -> unit
val from_montgomery : out_field_element -> field_element -> unit
val to_octets : bytes -> field_element -> unit
val inv : out_field_element -> field_element -> unit
val select_c : out_field_element -> bool -> field_element -> field_element -> unit
val double_c : out_point -> point -> unit
val add_c : out_point -> point -> point -> unit
val scalar_mult_base_c : out_point -> string -> unit
end
module type Field_element = sig
val mul : field_element -> field_element -> field_element
val sub : field_element -> field_element -> field_element
val add : field_element -> field_element -> field_element
val from_montgomery : field_element -> field_element
val zero : field_element
val one : field_element
val nz : field_element -> bool
val sqr : field_element -> field_element
val inv : field_element -> field_element
val select : bool -> then_:field_element -> else_:field_element -> field_element
val from_be_octets : string -> field_element
val to_octets : field_element -> string
val double_point : point -> point
val add_point : point -> point -> point
val scalar_mult_base_point : scalar -> point
end
module Make_field_element (P : Parameters) (F : Foreign) : Field_element = struct
let b_uts b = Bytes.unsafe_to_string b
let create () = Bytes.create P.fe_length
let mul a b =
let tmp = create () in
F.mul tmp a b;
b_uts tmp
let sub a b =
let tmp = create () in
F.sub tmp a b;
b_uts tmp
let add a b =
let tmp = create () in
F.add tmp a b;
b_uts tmp
let from_montgomery a =
let tmp = create () in
F.from_montgomery tmp a;
b_uts tmp
let zero =
let b = Bytes.make P.fe_length '\000' in
b_uts b
let one =
let fe = create () in
F.set_one fe;
b_uts fe
let nz a = F.nz a
let sqr a =
let tmp = create () in
F.sqr tmp a;
b_uts tmp
let inv a =
let tmp = create () in
F.inv tmp a;
b_uts tmp
let select bit ~then_ ~else_ =
let tmp = create () in
F.select_c tmp bit then_ else_;
b_uts tmp
let from_be_octets buf =
let buf_rev = rev_string buf in
let tmp = create () in
F.from_octets tmp buf_rev;
F.to_montgomery tmp (b_uts tmp);
b_uts tmp
let create_octets () =
Bytes.create P.byte_length
let to_octets fe =
let tmp = create_octets () in
F.to_octets tmp fe;
b_uts tmp
let out_point () = {
m_f_x = create ();
m_f_y = create ();
m_f_z = create ();
}
let out_p_to_p p = {
f_x = b_uts p.m_f_x ;
f_y = b_uts p.m_f_y ;
f_z = b_uts p.m_f_z ;
}
let double_point p =
let tmp = out_point () in
F.double_c tmp p;
out_p_to_p tmp
let add_point a b =
let tmp = out_point () in
F.add_c tmp a b;
out_p_to_p tmp
let scalar_mult_base_point (Scalar d) =
let tmp = out_point () in
F.scalar_mult_base_c tmp d;
out_p_to_p tmp
end
module type Point = sig
val at_infinity : unit -> point
val is_infinity : point -> bool
val add : point -> point -> point
val double : point -> point
val of_octets : string -> (point, error) result
val to_octets : compress:bool -> point -> string
val to_affine_raw : point -> (field_element * field_element) option
val x_of_finite_point : point -> string
val params_g : point
val select : bool -> then_:point -> else_:point -> point
val scalar_mult_base : scalar -> point
end
module Make_point (P : Parameters) (F : Foreign) : Point = struct
module Fe = Make_field_element(P)(F)
let at_infinity () =
let f_x = Fe.one in
let f_y = Fe.one in
let f_z = Fe.zero in
{ f_x; f_y; f_z }
let is_infinity (p : point) = not (Fe.nz p.f_z)
let is_solution_to_curve_equation =
let a = Fe.from_be_octets P.a in
let b = Fe.from_be_octets P.b in
fun ~x ~y ->
let x3 = Fe.mul x x in
let x3 = Fe.mul x3 x in
let ax = Fe.mul a x in
let y2 = Fe.mul y y in
let sum = Fe.add x3 ax in
let sum = Fe.add sum b in
let sum = Fe.sub sum y2 in
not (Fe.nz sum)
let check_coordinate buf =
(* ensure buf < p: *)
match Eqaf.compare_be_with_len ~len:P.byte_length buf P.p >= 0 with
| true -> None
| exception Invalid_argument _ -> None
| false -> Some (Fe.from_be_octets buf)
(** Convert coordinates to a finite point ensuring:
- x < p
- y < p
- y^2 = ax^3 + ax + b
*)
let validate_finite_point ~x ~y =
match (check_coordinate x, check_coordinate y) with
| Some f_x, Some f_y ->
if is_solution_to_curve_equation ~x:f_x ~y:f_y then
let f_z = Fe.one in
Ok { f_x; f_y; f_z }
else Error `Not_on_curve
| _ -> Error `Invalid_range
let to_affine_raw p =
if is_infinity p then
None
else
let z1 = Fe.from_montgomery p.f_z in
let z2 = Fe.inv z1 in
let z1 = Fe.sqr z2 in
let z1 = Fe.from_montgomery z1 in
let x = Fe.mul p.f_x z1 in
let z1 = Fe.mul z1 z2 in
let y = Fe.mul p.f_y z1 in
Some (x, y)
let to_affine p =
Option.map (fun (x, y) -> Fe.to_octets x, Fe.to_octets y)
(to_affine_raw p)
let to_octets ~compress p =
let buf =
match to_affine p with
| None -> String.make 1 '\000'
| Some (x, y) ->
let len_x = String.length x and len_y = String.length y in
let res = Bytes.create (1 + len_x + len_y) in
Bytes.set res 0 '\004' ;
let rev_x = rev_string x and rev_y = rev_string y in
Bytes.unsafe_blit_string rev_x 0 res 1 len_x ;
Bytes.unsafe_blit_string rev_y 0 res (1 + len_x) len_y ;
Bytes.unsafe_to_string res
in
if compress then
let out = Bytes.create (P.byte_length + 1) in
let ident =
2 + (String.get_uint8 buf (P.byte_length * 2)) land 1
in
Bytes.unsafe_blit_string buf 1 out 1 P.byte_length;
Bytes.set_uint8 out 0 ident;
Bytes.unsafe_to_string out
else
buf
let double p = Fe.double_point p
let add p q = Fe.add_point p q
let x_of_finite_point p =
match to_affine p with None -> assert false | Some (x, _) -> rev_string x
let params_g =
match validate_finite_point ~x:P.g_x ~y:P.g_y with
| Ok p -> p
| Error _ -> assert false
let select bit ~then_ ~else_ =
{
f_x = Fe.select bit ~then_:then_.f_x ~else_:else_.f_x;
f_y = Fe.select bit ~then_:then_.f_y ~else_:else_.f_y;
f_z = Fe.select bit ~then_:then_.f_z ~else_:else_.f_z;
}
let pow x exp =
let r0 = ref Fe.one in
let r1 = ref x in
for i = P.byte_length * 8 - 1 downto 0 do
let bit = bit_at exp i in
let multiplied = Fe.mul !r0 !r1 in
let r0_sqr = Fe.sqr !r0 in
let r1_sqr = Fe.sqr !r1 in
r0 := Fe.select bit ~then_:multiplied ~else_:r0_sqr;
r1 := Fe.select bit ~then_:r1_sqr ~else_:multiplied;
done;
!r0
let decompress =
(* When p = 4*k+3, as is the case of NIST-P256, there is an efficient square
root algorithm to recover the y, as follows:
Given the compact representation of Q as x,
y2 = x^3 + a*x + b
y' = y2^((p+1)/4)
y = min(y',p-y')
Q=(x,y) is the canonical representation of the point
*)
let pident = P.pident (* (Params.p + 1) / 4*) in
let a = Fe.from_be_octets P.a in
let b = Fe.from_be_octets P.b in
let p = Fe.from_be_octets P.p in
fun pk ->
let x = Fe.from_be_octets (String.sub pk 1 P.byte_length) in
let x3 = Fe.mul x x in
let x3 = Fe.mul x3 x in (* x3 *)
let ax = Fe.mul a x in (* ax *)
let sum = Fe.add x3 ax in
let sum = Fe.add sum b in (* y^2 *)
let y = pow sum pident in (* https://tools.ietf.org/id/draft-jivsov-ecc-compact-00.xml#sqrt point 4.3*)
let y' = Fe.sub p y in
let y = Fe.from_montgomery y in
let y_struct = Fe.to_octets y in (* number must not be in montgomery domain*)
let y_struct = rev_string y_struct in
let y' = Fe.from_montgomery y' in
let y_struct2 = Fe.to_octets y' in (* number must not be in montgomery domain*)
let y_struct2 = rev_string y_struct2 in
let ident = String.get_uint8 pk 0 in
let signY =
2 + (String.get_uint8 y_struct (P.byte_length - 1)) land 1
in
let res = if Int.equal signY ident then y_struct else y_struct2 in
let out = Bytes.create ((P.byte_length * 2) + 1) in
Bytes.set out 0 '\004';
Bytes.unsafe_blit_string pk 1 out 1 P.byte_length;
Bytes.unsafe_blit_string res 0 out (P.byte_length + 1) P.byte_length;
Bytes.unsafe_to_string out
let of_octets buf =
let len = P.byte_length in
if String.length buf = 0 then
Error `Invalid_format
else
let of_octets buf =
let x = String.sub buf 1 len in
let y = String.sub buf (1 + len) len in
validate_finite_point ~x ~y
in
match String.get_uint8 buf 0 with
| 0x00 when String.length buf = 1 ->
Ok (at_infinity ())
| 0x02 | 0x03 when String.length P.pident > 0 ->
let decompressed = decompress buf in
of_octets decompressed
| 0x04 when String.length buf = 1 + len + len ->
of_octets buf
| 0x00 | 0x04 -> Error `Invalid_length
| _ -> Error `Invalid_format
let scalar_mult_base = Fe.scalar_mult_base_point
end
module type Scalar = sig
val not_zero : string -> bool
val is_in_range : string -> bool
val of_octets : string -> (scalar, error) result
val to_octets : scalar -> string
val scalar_mult : scalar -> point -> point
val scalar_mult_base : scalar -> point
val generator_tables : unit -> field_element array array array
end
module Make_scalar (Param : Parameters) (P : Point) : Scalar = struct
let not_zero =
let zero = String.make Param.byte_length '\000' in
fun buf -> not (Eqaf.equal buf zero)
let is_in_range buf =
not_zero buf
&& Eqaf.compare_be_with_len ~len:Param.byte_length Param.n buf > 0
let of_octets buf =
match is_in_range buf with
| exception Invalid_argument _ -> Error `Invalid_length
| true -> Ok (Scalar (rev_string buf))
| false -> Error `Invalid_range
let to_octets (Scalar buf) = rev_string buf
(* Branchless Montgomery ladder method *)
let scalar_mult (Scalar s) p =
let r0 = ref (P.at_infinity ()) in
let r1 = ref p in
for i = Param.byte_length * 8 - 1 downto 0 do
let bit = bit_at s i in
let sum = P.add !r0 !r1 in
let r0_double = P.double !r0 in
let r1_double = P.double !r1 in
r0 := P.select bit ~then_:sum ~else_:r0_double;
r1 := P.select bit ~then_:r1_double ~else_:sum
done;
!r0
(* Specialization of [scalar_mult d p] when [p] is the generator *)
let scalar_mult_base = P.scalar_mult_base
(* Pre-compute multiples of the generator point
returns the tables along with the number of significant bytes *)
let generator_tables () =
let len = Param.fe_length * 2 in
let one_table _ = Array.init 15 (fun _ -> P.at_infinity ()) in
let table = Array.init len one_table in
let base = ref P.params_g in
for i = 0 to len - 1 do
table.(i).(0) <- !base;
for j = 1 to 14 do
table.(i).(j) <- P.add !base table.(i).(j - 1)
done;
base := P.double !base;
base := P.double !base;
base := P.double !base;
base := P.double !base
done;
let convert {f_x; f_y; f_z} = [|f_x; f_y; f_z|] in
Array.map (Array.map convert) table
end
module Make_dh (Param : Parameters) (P : Point) (S : Scalar) : Dh = struct
let point_of_octets c =
match P.of_octets c with
| Ok p when not (P.is_infinity p) -> Ok p
| Ok _ -> Error `At_infinity
| Error _ as e -> e
let point_to_octets = P.to_octets
type secret = scalar
let share ?(compress = false) private_key =
let public_key = S.scalar_mult_base private_key in
point_to_octets ~compress public_key
let secret_of_octets ?compress s =
match S.of_octets s with
| Ok p -> Ok (p, share ?compress p)
| Error _ as e -> e
let secret_to_octets s =
S.to_octets s
let rec generate_private_key ?g () =
let candidate = Mirage_crypto_rng.generate ?g Param.byte_length in
match S.of_octets candidate with
| Ok secret -> secret
| Error _ -> generate_private_key ?g ()
let gen_key ?compress ?g () =
let private_key = generate_private_key ?g () in
private_key, share ?compress private_key
let key_exchange secret received =
match point_of_octets received with
| Error _ as err -> err
| Ok shared -> Ok (P.x_of_finite_point (S.scalar_mult secret shared))
end
module type Foreign_n = sig
val mul : out_field_element -> field_element -> field_element -> unit
val add : out_field_element -> field_element -> field_element -> unit
val inv : out_field_element -> field_element -> unit
val one : out_field_element -> unit
val from_bytes : out_field_element -> string -> unit
val to_bytes : bytes -> field_element -> unit
val from_montgomery : out_field_element -> field_element -> unit
val to_montgomery : out_field_element -> field_element -> unit
end
module type Fn = sig
val from_be_octets : string -> field_element
val to_be_octets : field_element -> string
val mul : field_element -> field_element -> field_element
val add : field_element -> field_element -> field_element
val inv : field_element -> field_element
val one : field_element
val from_montgomery : field_element -> field_element
val to_montgomery : field_element -> field_element
end
module Make_Fn (P : Parameters) (F : Foreign_n) : Fn = struct
let b_uts = Bytes.unsafe_to_string
let create () = Bytes.create P.fe_length
let create_octets () = Bytes.create P.byte_length
let from_be_octets v =
let v' = create () in
F.from_bytes v' (rev_string v);
F.to_montgomery v' (b_uts v');
b_uts v'
let to_be_octets v =
let buf = create_octets () in
F.to_bytes buf v;
rev_string (b_uts buf)
let mul a b =
let tmp = create () in
F.mul tmp a b;
b_uts tmp
let add a b =
let tmp = create () in
F.add tmp a b;
b_uts tmp
let inv a =
let tmp = create () in
F.inv tmp a;
F.to_montgomery tmp (b_uts tmp);
b_uts tmp
let one =
let tmp = create () in
F.one tmp;
b_uts tmp
let from_montgomery a =
let tmp = create () in
F.from_montgomery tmp a;
b_uts tmp
let to_montgomery a =
let tmp = create () in
F.to_montgomery tmp a;
b_uts tmp
end
module Make_dsa (Param : Parameters) (F : Fn) (P : Point) (S : Scalar) (H : Digestif.S) = struct
type priv = scalar
let byte_length = Param.byte_length
let bit_length = Param.bit_length
let priv_of_octets= S.of_octets
let priv_to_octets = S.to_octets
let padded msg =
let l = String.length msg in
let bl = Param.byte_length in
let first_byte_ok () =
match Param.first_byte_bits with
| None -> true
| Some m -> (String.get_uint8 msg 0) land (0xFF land (lnot m)) = 0
in
if l > bl || (l = bl && not (first_byte_ok ())) then
raise Message_too_long
else if l = bl then
msg
else
( let res = Bytes.make bl '\000' in
Bytes.unsafe_blit_string msg 0 res (bl - l) l ;
Bytes.unsafe_to_string res )
(* RFC 6979: compute a deterministic k *)
module K_gen (H : Digestif.S) = struct
let drbg : 'a Mirage_crypto_rng.generator =
let module M = Mirage_crypto_rng.Hmac_drbg (H) in (module M)
let g ~key msg =
let g = Mirage_crypto_rng.create ~strict:true drbg in
Mirage_crypto_rng.reseed ~g (S.to_octets key ^ msg);
g
(* Defined in RFC 6979 sec 2.3.2 with
- blen = 8 * Param.byte_length
- qlen = Param.bit_length *)
let bits2int r =
(* keep qlen *leftmost* bits *)
let shift = (8 * Param.byte_length) - Param.bit_length in
if shift = 0 then
Bytes.unsafe_to_string r
else
(* Assuming shift is < 8 *)
let r' = Bytes.create Param.byte_length in
let p = ref 0x00 in
for i = 0 to Param.byte_length - 1 do
let x = Bytes.get_uint8 r i in
let v = (x lsr shift) lor (!p lsl (8 - shift)) in
p := x;
Bytes.set_uint8 r' i v
done;
Bytes.unsafe_to_string r'
(* take qbit length, and ensure it is suitable for ECDSA (> 0 & < n) *)
let gen g =
let rec go () =
let b = Bytes.create Param.byte_length in
Mirage_crypto_rng.generate_into ~g b Param.byte_length;
(* truncate to the desired number of bits *)
let r = bits2int b in
if S.is_in_range r then r else go ()
in
go ()
let generate ~key buf = gen (g ~key (padded buf))
end
module K_gen_default = K_gen(H)
type pub = point
let pub_of_octets = P.of_octets
let pub_to_octets ?(compress = false) pk = P.to_octets ~compress pk
let generate ?g () =
(* FIPS 186-4 B 4.2 *)
let d =
let rec one () =
match S.of_octets (Mirage_crypto_rng.generate ?g Param.byte_length) with
| Ok x -> x
| Error _ -> one ()
in
one ()
in
let q = S.scalar_mult_base d in
(d, q)
let x_of_finite_point_mod_n p =
match P.to_affine_raw p with
| None -> None
| Some (x, _) ->
let x = F.to_montgomery x in
let x = F.mul x F.one in
let x = F.from_montgomery x in
Some (F.to_be_octets x)
let sign ~key ?k msg =
let msg = padded msg in
let e = F.from_be_octets msg in
let g = K_gen_default.g ~key msg in
let rec do_sign g =
let again () =
match k with
| None -> do_sign g
| Some _ -> invalid_arg "k not suitable"
in
let k' = match k with None -> K_gen_default.gen g | Some k -> k in
let ksc = match S.of_octets k' with
| Ok ksc -> ksc
| Error _ -> invalid_arg "k not in range" (* if no k is provided, this cannot happen since K_gen_*.gen already preserves the Scalar invariants *)
in
let point = S.scalar_mult_base ksc in
match x_of_finite_point_mod_n point with
| None -> again ()
| Some r ->
let r_mon = F.from_be_octets r in
let kmon = F.from_be_octets k' in
let kinv = F.inv kmon in
let dmon = F.from_be_octets (S.to_octets key) in
let rd = F.mul r_mon dmon in
let cmon = F.add e rd in
let smon = F.mul kinv cmon in
let s = F.from_montgomery smon in
let s = F.to_be_octets s in
if S.not_zero s && S.not_zero r then
r, s
else
again ()
in
do_sign g
let pub_of_priv priv = S.scalar_mult_base priv
let verify ~key (r, s) msg =
try
let r = padded r and s = padded s in
if not (S.is_in_range r && S.is_in_range s) then
false
else
let msg = padded msg in
let z = F.from_be_octets msg in
let s_mon = F.from_be_octets s in
let s_inv = F.inv s_mon in
let u1 = F.mul z s_inv in
let r_mon = F.from_be_octets r in
let u2 = F.mul r_mon s_inv in
let u1 = F.from_montgomery u1 in
let u2 = F.from_montgomery u2 in
match
S.of_octets (F.to_be_octets u1),
S.of_octets (F.to_be_octets u2)
with
| Ok u1, Ok u2 ->
let point =
P.add
(S.scalar_mult_base u1)
(S.scalar_mult u2 key)
in
begin match x_of_finite_point_mod_n point with
| None -> false (* point is infinity *)
| Some r' -> String.equal r r'
end
| Error _, _ | _, Error _ -> false
with
| Message_too_long -> false
module Precompute = struct
let generator_tables = S.generator_tables
end
end
module P256 : Dh_dsa = struct
module Params = struct
let a = "\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC"
let b = "\x5A\xC6\x35\xD8\xAA\x3A\x93\xE7\xB3\xEB\xBD\x55\x76\x98\x86\xBC\x65\x1D\x06\xB0\xCC\x53\xB0\xF6\x3B\xCE\x3C\x3E\x27\xD2\x60\x4B"
let g_x = "\x6B\x17\xD1\xF2\xE1\x2C\x42\x47\xF8\xBC\xE6\xE5\x63\xA4\x40\xF2\x77\x03\x7D\x81\x2D\xEB\x33\xA0\xF4\xA1\x39\x45\xD8\x98\xC2\x96"
let g_y = "\x4F\xE3\x42\xE2\xFE\x1A\x7F\x9B\x8E\xE7\xEB\x4A\x7C\x0F\x9E\x16\x2B\xCE\x33\x57\x6B\x31\x5E\xCE\xCB\xB6\x40\x68\x37\xBF\x51\xF5"
let p = "\xFF\xFF\xFF\xFF\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
let n = "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBC\xE6\xFA\xAD\xA7\x17\x9E\x84\xF3\xB9\xCA\xC2\xFC\x63\x25\x51"
let pident = "\x3F\xFF\xFF\xFF\xC0\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" |> rev_string (* (Params.p + 1) / 4*)
let byte_length = 32
let bit_length = 256
let fe_length = 32
let first_byte_bits = None
end
module Foreign = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_p256_mul" [@@noalloc]
external sub : out_field_element -> field_element -> field_element -> unit = "mc_p256_sub" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_p256_add" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_p256_to_montgomery" [@@noalloc]
external from_octets : out_field_element -> string -> unit = "mc_p256_from_bytes" [@@noalloc]
external set_one : out_field_element -> unit = "mc_p256_set_one" [@@noalloc]
external nz : field_element -> bool = "mc_p256_nz" [@@noalloc]
external sqr : out_field_element -> field_element -> unit = "mc_p256_sqr" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_p256_from_montgomery" [@@noalloc]
external to_octets : bytes -> field_element -> unit = "mc_p256_to_bytes" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_p256_inv" [@@noalloc]
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = "mc_p256_select" [@@noalloc]
external double_c : out_point -> point -> unit = "mc_p256_point_double" [@@noalloc]
external add_c : out_point -> point -> point -> unit = "mc_p256_point_add" [@@noalloc]
external scalar_mult_base_c : out_point -> string -> unit = "mc_p256_scalar_mult_base" [@@noalloc]
end
module Foreign_n = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_np256_mul" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_np256_add" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_np256_inv" [@@noalloc]
external one : out_field_element -> unit = "mc_np256_one" [@@noalloc]
external from_bytes : out_field_element -> string -> unit = "mc_np256_from_bytes" [@@noalloc]
external to_bytes : bytes -> field_element -> unit = "mc_np256_to_bytes" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_np256_from_montgomery" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_np256_to_montgomery" [@@noalloc]
end
module P = Make_point(Params)(Foreign)
module S = Make_scalar(Params)(P)
module Dh = Make_dh(Params)(P)(S)
module Fn = Make_Fn(Params)(Foreign_n)
module Dsa = Make_dsa(Params)(Fn)(P)(S)(Digestif.SHA256)
end
module P384 : Dh_dsa = struct
module Params = struct
let a = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFC"
let b = "\xB3\x31\x2F\xA7\xE2\x3E\xE7\xE4\x98\x8E\x05\x6B\xE3\xF8\x2D\x19\x18\x1D\x9C\x6E\xFE\x81\x41\x12\x03\x14\x08\x8F\x50\x13\x87\x5A\xC6\x56\x39\x8D\x8A\x2E\xD1\x9D\x2A\x85\xC8\xED\xD3\xEC\x2A\xEF"
let g_x = "\xAA\x87\xCA\x22\xBE\x8B\x05\x37\x8E\xB1\xC7\x1E\xF3\x20\xAD\x74\x6E\x1D\x3B\x62\x8B\xA7\x9B\x98\x59\xF7\x41\xE0\x82\x54\x2A\x38\x55\x02\xF2\x5D\xBF\x55\x29\x6C\x3A\x54\x5E\x38\x72\x76\x0A\xB7"
let g_y =
"\x36\x17\xde\x4a\x96\x26\x2c\x6f\x5d\x9e\x98\xbf\x92\x92\xdc\x29\xf8\xf4\x1d\xbd\x28\x9a\x14\x7c\xe9\xda\x31\x13\xb5\xf0\xb8\xc0\x0a\x60\xb1\xce\x1d\x7e\x81\x9d\x7a\x43\x1d\x7c\x90\xea\x0e\x5f"
let p = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
let n = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC7\x63\x4D\x81\xF4\x37\x2D\xDF\x58\x1A\x0D\xB2\x48\xB0\xA7\x7A\xEC\xEC\x19\x6A\xCC\xC5\x29\x73"
let pident = "\x3F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xBF\xFF\xFF\xFF\xC0\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00" |> rev_string (* (Params.p + 1) / 4*)
let byte_length = 48
let bit_length = 384
let fe_length = 48
let first_byte_bits = None
end
module Foreign = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_p384_mul" [@@noalloc]
external sub : out_field_element -> field_element -> field_element -> unit = "mc_p384_sub" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_p384_add" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_p384_to_montgomery" [@@noalloc]
external from_octets : out_field_element -> string -> unit = "mc_p384_from_bytes" [@@noalloc]
external set_one : out_field_element -> unit = "mc_p384_set_one" [@@noalloc]
external nz : field_element -> bool = "mc_p384_nz" [@@noalloc]
external sqr : out_field_element -> field_element -> unit = "mc_p384_sqr" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_p384_from_montgomery" [@@noalloc]
external to_octets : bytes -> field_element -> unit = "mc_p384_to_bytes" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_p384_inv" [@@noalloc]
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = "mc_p384_select" [@@noalloc]
external double_c : out_point -> point -> unit = "mc_p384_point_double" [@@noalloc]
external add_c : out_point -> point -> point -> unit = "mc_p384_point_add" [@@noalloc]
external scalar_mult_base_c : out_point -> string -> unit = "mc_p384_scalar_mult_base" [@@noalloc]
end
module Foreign_n = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_np384_mul" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_np384_add" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_np384_inv" [@@noalloc]
external one : out_field_element -> unit = "mc_np384_one" [@@noalloc]
external from_bytes : out_field_element -> string -> unit = "mc_np384_from_bytes" [@@noalloc]
external to_bytes : bytes -> field_element -> unit = "mc_np384_to_bytes" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_np384_from_montgomery" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_np384_to_montgomery" [@@noalloc]
end
module P = Make_point(Params)(Foreign)
module S = Make_scalar(Params)(P)
module Dh = Make_dh(Params)(P)(S)
module Fn = Make_Fn(Params)(Foreign_n)
module Dsa = Make_dsa(Params)(Fn)(P)(S)(Digestif.SHA384)
end
module P521 : Dh_dsa = struct
module Params = struct
let a = "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFC"
let b = "\x00\x51\x95\x3E\xB9\x61\x8E\x1C\x9A\x1F\x92\x9A\x21\xA0\xB6\x85\x40\xEE\xA2\xDA\x72\x5B\x99\xB3\x15\xF3\xB8\xB4\x89\x91\x8E\xF1\x09\xE1\x56\x19\x39\x51\xEC\x7E\x93\x7B\x16\x52\xC0\xBD\x3B\xB1\xBF\x07\x35\x73\xDF\x88\x3D\x2C\x34\xF1\xEF\x45\x1F\xD4\x6B\x50\x3F\x00"
let g_x =
"\x00\xC6\x85\x8E\x06\xB7\x04\x04\xE9\xCD\x9E\x3E\xCB\x66\x23\x95\xB4\x42\x9C\x64\x81\x39\x05\x3F\xB5\x21\xF8\x28\xAF\x60\x6B\x4D\x3D\xBA\xA1\x4B\x5E\x77\xEF\xE7\x59\x28\xFE\x1D\xC1\x27\xA2\xFF\xA8\xDE\x33\x48\xB3\xC1\x85\x6A\x42\x9B\xF9\x7E\x7E\x31\xC2\xE5\xBD\x66"
let g_y =
"\x01\x18\x39\x29\x6a\x78\x9a\x3b\xc0\x04\x5c\x8a\x5f\xb4\x2c\x7d\x1b\xd9\x98\xf5\x44\x49\x57\x9b\x44\x68\x17\xaf\xbd\x17\x27\x3e\x66\x2c\x97\xee\x72\x99\x5e\xf4\x26\x40\xc5\x50\xb9\x01\x3f\xad\x07\x61\x35\x3c\x70\x86\xa2\x72\xc2\x40\x88\xbe\x94\x76\x9f\xd1\x66\x50"
let p = "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
let n = "\x01\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFA\x51\x86\x87\x83\xBF\x2F\x96\x6B\x7F\xCC\x01\x48\xF7\x09\xA5\xD0\x3B\xB5\xC9\xB8\x89\x9C\x47\xAE\xBB\x6F\xB7\x1E\x91\x38\x64\x09"
let pident = "\x01\x7f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |> rev_string
let byte_length = 66
let bit_length = 521
let fe_length = if Sys.word_size == 64 then 72 else 68 (* TODO: is this congruent with C code? *)
let first_byte_bits = Some 0x01
end
module Foreign = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_p521_mul" [@@noalloc]
external sub : out_field_element -> field_element -> field_element -> unit = "mc_p521_sub" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_p521_add" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_p521_to_montgomery" [@@noalloc]
external from_octets : out_field_element -> string -> unit = "mc_p521_from_bytes" [@@noalloc]
external set_one : out_field_element -> unit = "mc_p521_set_one" [@@noalloc]
external nz : field_element -> bool = "mc_p521_nz" [@@noalloc]
external sqr : out_field_element -> field_element -> unit = "mc_p521_sqr" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_p521_from_montgomery" [@@noalloc]
external to_octets : bytes -> field_element -> unit = "mc_p521_to_bytes" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_p521_inv" [@@noalloc]
external select_c : out_field_element -> bool -> field_element -> field_element -> unit = "mc_p521_select" [@@noalloc]
external double_c : out_point -> point -> unit = "mc_p521_point_double" [@@noalloc]
external add_c : out_point -> point -> point -> unit = "mc_p521_point_add" [@@noalloc]
external scalar_mult_base_c : out_point -> string -> unit = "mc_p521_scalar_mult_base" [@@noalloc]
end
module Foreign_n = struct
external mul : out_field_element -> field_element -> field_element -> unit = "mc_np521_mul" [@@noalloc]
external add : out_field_element -> field_element -> field_element -> unit = "mc_np521_add" [@@noalloc]
external inv : out_field_element -> field_element -> unit = "mc_np521_inv" [@@noalloc]
external one : out_field_element -> unit = "mc_np521_one" [@@noalloc]
external from_bytes : out_field_element -> string -> unit = "mc_np521_from_bytes" [@@noalloc]
external to_bytes : bytes -> field_element -> unit = "mc_np521_to_bytes" [@@noalloc]
external from_montgomery : out_field_element -> field_element -> unit = "mc_np521_from_montgomery" [@@noalloc]
external to_montgomery : out_field_element -> field_element -> unit = "mc_np521_to_montgomery" [@@noalloc]
end
module P = Make_point(Params)(Foreign)
module S = Make_scalar(Params)(P)
module Dh = Make_dh(Params)(P)(S)
module Fn = Make_Fn(Params)(Foreign_n)
module Dsa = Make_dsa(Params)(Fn)(P)(S)(Digestif.SHA512)
end
module X25519 = struct
(* RFC 7748 *)
external x25519_scalar_mult_generic : bytes -> string -> string -> unit = "mc_x25519_scalar_mult_generic" [@@noalloc]
let key_len = 32
let scalar_mult in_ base =
let out = Bytes.create key_len in
x25519_scalar_mult_generic out in_ base;
Bytes.unsafe_to_string out
type secret = string
let basepoint = String.init key_len (function 0 -> '\009' | _ -> '\000')
let public priv = scalar_mult priv basepoint
let gen_key ?compress:_ ?g () =
let secret = Mirage_crypto_rng.generate ?g key_len in
secret, public secret
let secret_of_octets ?compress:_ s =
if String.length s = key_len then
Ok (s, public s)
else
Error `Invalid_length
let secret_to_octets s = s
let is_zero =
let zero = String.make key_len '\000' in
fun buf -> String.equal zero buf
let key_exchange secret public =
if String.length public = key_len then
let res = scalar_mult secret public in
if is_zero res then Error `Low_order else Ok res
else
Error `Invalid_length
end
module Ed25519 = struct
external scalar_mult_base_to_bytes : bytes -> string -> unit = "mc_25519_scalar_mult_base" [@@noalloc]
external reduce_l : bytes -> unit = "mc_25519_reduce_l" [@@noalloc]
external muladd : bytes -> string -> string -> string -> unit = "mc_25519_muladd" [@@noalloc]
external double_scalar_mult : bytes -> string -> string -> string -> bool = "mc_25519_double_scalar_mult" [@@noalloc]
external pub_ok : string -> bool = "mc_25519_pub_ok" [@@noalloc]
let key_len = 32
let scalar_mult_base_to_bytes p =
let tmp = Bytes.create key_len in
scalar_mult_base_to_bytes tmp p;
Bytes.unsafe_to_string tmp
let muladd a b c =
let tmp = Bytes.create key_len in
muladd tmp a b c;
Bytes.unsafe_to_string tmp
let double_scalar_mult a b c =
let tmp = Bytes.create key_len in
let s = double_scalar_mult tmp a b c in
s, Bytes.unsafe_to_string tmp
type pub = string
type priv = string
let sha512 datas =
let open Digestif.SHA512 in
let buf = Bytes.create digest_size in
let ctx = List.fold_left (feed_string ?off:None ?len:None) empty datas in
get_into_bytes ctx buf;
buf
(* RFC 8032 *)
let public secret =
(* section 5.1.5 *)
(* step 1 *)
let h = sha512 [ secret ] in
(* step 2 *)
let s, rest =
Bytes.sub h 0 key_len,
Bytes.unsafe_to_string (Bytes.sub h key_len (Bytes.length h - key_len))
in
Bytes.set_uint8 s 0 ((Bytes.get_uint8 s 0) land 248);
Bytes.set_uint8 s 31 (((Bytes.get_uint8 s 31) land 127) lor 64);
let s = Bytes.unsafe_to_string s in
(* step 3 and 4 *)
let public = scalar_mult_base_to_bytes s in
public, (s, rest)
let pub_of_priv secret = fst (public secret)
let priv_of_octets buf =
if String.length buf = key_len then Ok buf else Error `Invalid_length
let priv_to_octets (priv : priv) = priv
let pub_of_octets buf =
if String.length buf = key_len then
if pub_ok buf then
Ok buf
else
Error `Not_on_curve
else
Error `Invalid_length
let pub_to_octets pub = pub
let generate ?g () =
let secret = Mirage_crypto_rng.generate ?g key_len in
secret, pub_of_priv secret
let sign ~key msg =
(* section 5.1.6 *)
let pub, (s, prefix) = public key in
let r = sha512 [ prefix; msg ] in
reduce_l r;
let r = Bytes.unsafe_to_string r in
let r_big = scalar_mult_base_to_bytes r in
let k = sha512 [ r_big; pub; msg] in
reduce_l k;
let k = Bytes.unsafe_to_string k in
let s_out = muladd k s r in
let res = Bytes.create (key_len + key_len) in
Bytes.unsafe_blit_string r_big 0 res 0 key_len ;
Bytes.unsafe_blit_string s_out 0 res key_len key_len ;
Bytes.unsafe_to_string res
let verify ~key signature ~msg =
(* section 5.1.7 *)
if String.length signature = 2 * key_len then
let r, s =
String.sub signature 0 key_len,
String.sub signature key_len key_len
in
let s_smaller_l =
(* check s within 0 <= s < L *)
let s' = Bytes.make (key_len * 2) '\000' in
Bytes.unsafe_blit_string s 0 s' 0 key_len;
reduce_l s';
let s' = Bytes.unsafe_to_string s' in
let s'' = s ^ String.make key_len '\000' in
String.equal s'' s'
in
if s_smaller_l then begin
let k = sha512 [ r ; key ; msg ] in
reduce_l k;
let k = Bytes.unsafe_to_string k in
let success, r' = double_scalar_mult k key s in
success && String.equal r r'
end else
false
else
false
end
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
mli
|
mirage-crypto-2.0.2/ec/mirage_crypto_ec.mli
|
(** {1 Elliptic curve cryptography} *)
(** Mirage-crypto-ec implements public key cryptography with named elliptic
curves. Ephemeral key exchanges with {{!Dh}Diffie-Hellman} and
{{!Dsa}digital signatures (ECDSA)} are implemented.
The arithmetic operations uses code generated by
{{:https://github.com/mit-plv/fiat-crypto}fiat-crypto} which is proven to
consume a constant amount of time, independent of the input values.
*)
type error = [
| `Invalid_range
| `Invalid_format
| `Invalid_length
| `Not_on_curve
| `At_infinity
| `Low_order
]
(** The type for errors. *)
val pp_error : Format.formatter -> error -> unit
(** Pretty printer for errors *)
exception Message_too_long
(** Raised if the provided message is too long for the curve. *)
(** Diffie-Hellman key exchange. *)
module type Dh = sig
type secret
(** Type for private keys. *)
val secret_of_octets : ?compress:bool -> string ->
(secret * string, error) result
(** [secret_of_octets ~compress secret] decodes the provided buffer as
{!secret}. If [compress] is provided and [true] (defaults to [false]),
the shared part will be compressed. May result in an error if the buffer
had an invalid length or was not in bounds. *)
val secret_to_octets : secret -> string
(** [secret_to_octets secret] encodes the provided secret into a freshly
allocated buffer. *)
val gen_key : ?compress:bool -> ?g:Mirage_crypto_rng.g -> unit ->
secret * string
(** [gen_key ~compress ~g ()] generates a private and a public key for
Ephemeral Diffie-Hellman. If [compress] is provided and [true] (defaults
to [false]), the shared part will be compressed. The returned key pair
MUST only be used for a single key exchange.
The generated private key is checked to be greater than zero and lower
than the group order meaning the public key cannot be the point at
inifinity. *)
val key_exchange : secret -> string -> (string, error) result
(** [key_exchange secret received_public_key] performs Diffie-Hellman key
exchange using your secret and the data received from the other party.
Returns the shared secret or an error if the received data is wrongly
encoded, doesn't represent a point on the curve or represent the point
at infinity.
The shared secret is returned as is i.e. not stripped from leading 0x00
bytes.
The public key encoding is described
{{:http://www.secg.org/sec1-v2.pdf}in SEC 1} from SECG. *)
end
(** Digital signature algorithm. *)
module type Dsa = sig
type priv
(** The type for private keys. *)
type pub
(** The type for public keys. *)
val byte_length : int
(** [byte_length] is the size of a ECDSA signature in bytes. *)
val bit_length : int
(** [bit_length] is the number of significant bits in a ECDSA signature *)
(** {2 Serialisation} *)
val priv_of_octets : string -> (priv, error) result
(** [priv_of_octets buf] decodes a private key from the buffer [buf]. If the
provided data is invalid, an error is returned. *)
val priv_to_octets : priv -> string
(** [priv_to_octets p] encode the private key [p] to a buffer. *)
val pub_of_octets : string -> (pub, error) result
(** [pub_of_octets buf] decodes a public key from the buffer [buf]. If the
provided data is invalid, an error is returned. *)
val pub_to_octets : ?compress:bool -> pub -> string
(** [pub_to_octets ~compress p] encodes the public key [p] into a buffer.
If [compress] is provided and [true] (default [false]), the compressed
representation is returned. *)
(** {2 Deriving the public key} *)
val pub_of_priv : priv -> pub
(** [pub_of_priv p] extracts the public key from the private key [p]. *)
(** {2 Key generation} *)
val generate : ?g:Mirage_crypto_rng.g -> unit -> priv * pub
(** [generate ~g ()] generates a key pair. *)
(** {2 Cryptographic operations} *)
val sign : key:priv -> ?k:string -> string -> string * string
(** [sign ~key ~k digest] signs the message [digest] using the private
[key]. The [digest] is not processed further - it should be the hash of
the message to sign. If [k] is not provided, it is computed using the
deterministic construction from RFC 6979. The result is a pair of [r]
and [s].
Warning: there {{:https://www.hertzbleed.com/2h2b.pdf}are}
{{:https://www.hertzbleed.com/hertzbleed.pdf}attacks} that recover the
private key from a power and timing analysis of the RFC 6979 computation
of [k] - thus it is advised to provide a good nonce ([k]) explicitly,
which is independent of key and digest.
@raise Invalid_argument if [k] is not suitable or not in range.
@raise Message_too_long if the bit size of [msg] exceeds the curve. *)
val verify : key:pub -> string * string -> string -> bool
(** [verify ~key (r, s) digest] verifies the signature [r, s] on the message
[digest] with the public [key]. The return value is [true] if verification
was successful, [false] otherwise. If the message has more bits than the
group order, the result is false. *)
(** [K_gen] can be instantiated over a hashing module to obtain an RFC6979
compliant [k]-generator for that hash. *)
module K_gen (H : Digestif.S) : sig
val generate : key:priv -> string -> string
(** [generate ~key digest] deterministically takes the given private key
and message digest to a [k] suitable for seeding the signing process. *)
end
(** {2 Misc} *)
(** Operations to precompute useful data meant to be hardcoded in
[mirage-crypto-ec] before compilation *)
module Precompute : sig
val generator_tables : unit -> string array array array
(** Return an array of shape (Fe_length * 2, 15, 3) containing multiples of
the generator point for the curve. Useful only to bootstrap tables
necessary for scalar multiplication. *)
end
end
(** Elliptic curve with Diffie-Hellman and DSA. *)
module type Dh_dsa = sig
(** Diffie-Hellman key exchange. *)
module Dh : Dh
(** Digital signature algorithm. *)
module Dsa : Dsa
end
(** The NIST P-256 curve, also known as SECP256R1. *)
module P256 : Dh_dsa
(** The NIST P-384 curve, also known as SECP384R1. *)
module P384 : Dh_dsa
(** The NIST P-521 curve, also known as SECP521R1. *)
module P521 : Dh_dsa
(** Curve 25519 Diffie-Hellman, also known as X25519. *)
module X25519 : Dh
(** Curve 25519 DSA, also known as Ed25519. *)
module Ed25519 : sig
type priv
(** The type for private keys. *)
type pub
(** The type for public keys. *)
(** {2 Serialisation} *)
val priv_of_octets : string -> (priv, error) result
(** [priv_of_octets buf] decodes a private key from the buffer [buf]. If the
provided data is invalid, an error is returned. *)
val priv_to_octets : priv -> string
(** [priv_to_octets p] encode the private key [p] to a buffer. *)
val pub_of_octets : string -> (pub, error) result
(** [pub_of_octets buf] decodes a public key from the buffer [buf]. If the
provided data is invalid, an error is returned. *)
val pub_to_octets : pub -> string
(** [pub_to_octets p] encodes the public key [p] into a buffer. *)
(** {2 Deriving the public key} *)
val pub_of_priv : priv -> pub
(** [pub_of_priv p] extracts the public key from the private key [p]. *)
(** {2 Key generation} *)
val generate : ?g:Mirage_crypto_rng.g -> unit -> priv * pub
(** [generate ~g ()] generates a key pair. *)
(** {2 Cryptographic operations} *)
val sign : key:priv -> string -> string
(** [sign ~key msg] signs the message [msg] using the private [key]. The
result is the concatenation of [r] and [s], as specified in RFC 8032. *)
val verify : key:pub -> string -> msg:string -> bool
(** [verify ~key signature msg] verifies the [signature] on the message
[msg] with the public [key]. The return value is [true] if verification
was successful, [false] otherwise. *)
end
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
h
|
mirage-crypto-2.0.2/ec/native/curve25519_32.h
|
/* Autogenerated: '../fiat-crypto/src/ExtractionOCaml/unsaturated_solinas' --static --use-value-barrier --inline-internal 25519 32 '(auto)' '2^255 - 19' carry_mul carry_square carry add sub opp selectznz to_bytes from_bytes carry_scmul121666 */
/* curve description: 25519 */
/* machine_wordsize = 32 (from "32") */
/* requested operations: carry_mul, carry_square, carry, add, sub, opp, selectznz, to_bytes, from_bytes, carry_scmul121666 */
/* n = 10 (from "(auto)") */
/* s-c = 2^255 - [(1, 19)] (from "2^255 - 19") */
/* tight_bounds_multiplier = 1 (from "") */
/* */
/* Computed values: */
/* carry_chain = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1] */
/* eval z = z[0] + (z[1] << 26) + (z[2] << 51) + (z[3] << 77) + (z[4] << 102) + (z[5] << 128) + (z[6] << 153) + (z[7] << 179) + (z[8] << 204) + (z[9] << 230) */
/* bytes_eval z = z[0] + (z[1] << 8) + (z[2] << 16) + (z[3] << 24) + (z[4] << 32) + (z[5] << 40) + (z[6] << 48) + (z[7] << 56) + (z[8] << 64) + (z[9] << 72) + (z[10] << 80) + (z[11] << 88) + (z[12] << 96) + (z[13] << 104) + (z[14] << 112) + (z[15] << 120) + (z[16] << 128) + (z[17] << 136) + (z[18] << 144) + (z[19] << 152) + (z[20] << 160) + (z[21] << 168) + (z[22] << 176) + (z[23] << 184) + (z[24] << 192) + (z[25] << 200) + (z[26] << 208) + (z[27] << 216) + (z[28] << 224) + (z[29] << 232) + (z[30] << 240) + (z[31] << 248) */
/* balance = [0x7ffffda, 0x3fffffe, 0x7fffffe, 0x3fffffe, 0x7fffffe, 0x3fffffe, 0x7fffffe, 0x3fffffe, 0x7fffffe, 0x3fffffe] */
#include <stdint.h>
typedef unsigned char fiat_25519_uint1;
typedef signed char fiat_25519_int1;
#if defined(__GNUC__) || defined(__clang__)
# define FIAT_25519_FIAT_INLINE __inline__
#else
# define FIAT_25519_FIAT_INLINE
#endif
/* The type fiat_25519_loose_field_element is a field element with loose bounds. */
/* Bounds: [[0x0 ~> 0xc000000], [0x0 ~> 0x6000000], [0x0 ~> 0xc000000], [0x0 ~> 0x6000000], [0x0 ~> 0xc000000], [0x0 ~> 0x6000000], [0x0 ~> 0xc000000], [0x0 ~> 0x6000000], [0x0 ~> 0xc000000], [0x0 ~> 0x6000000]] */
typedef uint32_t fiat_25519_loose_field_element[10];
/* The type fiat_25519_tight_field_element is a field element with tight bounds. */
/* Bounds: [[0x0 ~> 0x4000000], [0x0 ~> 0x2000000], [0x0 ~> 0x4000000], [0x0 ~> 0x2000000], [0x0 ~> 0x4000000], [0x0 ~> 0x2000000], [0x0 ~> 0x4000000], [0x0 ~> 0x2000000], [0x0 ~> 0x4000000], [0x0 ~> 0x2000000]] */
typedef uint32_t fiat_25519_tight_field_element[10];
#if (-1 & 3) != 3
#error "This code only works on a two's complement system"
#endif
#if !defined(FIAT_25519_NO_ASM) && (defined(__GNUC__) || defined(__clang__))
static __inline__ uint32_t fiat_25519_value_barrier_u32(uint32_t a) {
__asm__("" : "+r"(a) : /* no inputs */);
return a;
}
#else
# define fiat_25519_value_barrier_u32(x) (x)
#endif
/*
* The function fiat_25519_addcarryx_u26 is an addition with carry.
*
* Postconditions:
* out1 = (arg1 + arg2 + arg3) mod 2^26
* out2 = ⌊(arg1 + arg2 + arg3) / 2^26⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x3ffffff]
* arg3: [0x0 ~> 0x3ffffff]
* Output Bounds:
* out1: [0x0 ~> 0x3ffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_addcarryx_u26(uint32_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint32_t arg2, uint32_t arg3) {
uint32_t x1;
uint32_t x2;
fiat_25519_uint1 x3;
x1 = ((arg1 + arg2) + arg3);
x2 = (x1 & UINT32_C(0x3ffffff));
x3 = (fiat_25519_uint1)(x1 >> 26);
*out1 = x2;
*out2 = x3;
}
/*
* The function fiat_25519_subborrowx_u26 is a subtraction with borrow.
*
* Postconditions:
* out1 = (-arg1 + arg2 + -arg3) mod 2^26
* out2 = -⌊(-arg1 + arg2 + -arg3) / 2^26⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x3ffffff]
* arg3: [0x0 ~> 0x3ffffff]
* Output Bounds:
* out1: [0x0 ~> 0x3ffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_subborrowx_u26(uint32_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint32_t arg2, uint32_t arg3) {
int32_t x1;
fiat_25519_int1 x2;
uint32_t x3;
x1 = ((int32_t)(arg2 - arg1) - (int32_t)arg3);
x2 = (fiat_25519_int1)(x1 >> 26);
x3 = (x1 & UINT32_C(0x3ffffff));
*out1 = x3;
*out2 = (fiat_25519_uint1)(0x0 - x2);
}
/*
* The function fiat_25519_addcarryx_u25 is an addition with carry.
*
* Postconditions:
* out1 = (arg1 + arg2 + arg3) mod 2^25
* out2 = ⌊(arg1 + arg2 + arg3) / 2^25⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x1ffffff]
* arg3: [0x0 ~> 0x1ffffff]
* Output Bounds:
* out1: [0x0 ~> 0x1ffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_addcarryx_u25(uint32_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint32_t arg2, uint32_t arg3) {
uint32_t x1;
uint32_t x2;
fiat_25519_uint1 x3;
x1 = ((arg1 + arg2) + arg3);
x2 = (x1 & UINT32_C(0x1ffffff));
x3 = (fiat_25519_uint1)(x1 >> 25);
*out1 = x2;
*out2 = x3;
}
/*
* The function fiat_25519_subborrowx_u25 is a subtraction with borrow.
*
* Postconditions:
* out1 = (-arg1 + arg2 + -arg3) mod 2^25
* out2 = -⌊(-arg1 + arg2 + -arg3) / 2^25⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x1ffffff]
* arg3: [0x0 ~> 0x1ffffff]
* Output Bounds:
* out1: [0x0 ~> 0x1ffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_subborrowx_u25(uint32_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint32_t arg2, uint32_t arg3) {
int32_t x1;
fiat_25519_int1 x2;
uint32_t x3;
x1 = ((int32_t)(arg2 - arg1) - (int32_t)arg3);
x2 = (fiat_25519_int1)(x1 >> 25);
x3 = (x1 & UINT32_C(0x1ffffff));
*out1 = x3;
*out2 = (fiat_25519_uint1)(0x0 - x2);
}
/*
* The function fiat_25519_cmovznz_u32 is a single-word conditional move.
*
* Postconditions:
* out1 = (if arg1 = 0 then arg2 else arg3)
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0xffffffff]
* arg3: [0x0 ~> 0xffffffff]
* Output Bounds:
* out1: [0x0 ~> 0xffffffff]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_cmovznz_u32(uint32_t* out1, fiat_25519_uint1 arg1, uint32_t arg2, uint32_t arg3) {
fiat_25519_uint1 x1;
uint32_t x2;
uint32_t x3;
x1 = (!(!arg1));
x2 = ((fiat_25519_int1)(0x0 - x1) & UINT32_C(0xffffffff));
x3 = ((fiat_25519_value_barrier_u32(x2) & arg3) | (fiat_25519_value_barrier_u32((~x2)) & arg2));
*out1 = x3;
}
/*
* The function fiat_25519_carry_mul multiplies two field elements and reduces the result.
*
* Postconditions:
* eval out1 mod m = (eval arg1 * eval arg2) mod m
*
*/
static void fiat_25519_carry_mul(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1, const fiat_25519_loose_field_element arg2) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
uint64_t x6;
uint64_t x7;
uint64_t x8;
uint64_t x9;
uint64_t x10;
uint64_t x11;
uint64_t x12;
uint64_t x13;
uint64_t x14;
uint64_t x15;
uint64_t x16;
uint64_t x17;
uint64_t x18;
uint64_t x19;
uint64_t x20;
uint64_t x21;
uint64_t x22;
uint64_t x23;
uint64_t x24;
uint64_t x25;
uint64_t x26;
uint64_t x27;
uint64_t x28;
uint64_t x29;
uint64_t x30;
uint64_t x31;
uint64_t x32;
uint64_t x33;
uint64_t x34;
uint64_t x35;
uint64_t x36;
uint64_t x37;
uint64_t x38;
uint64_t x39;
uint64_t x40;
uint64_t x41;
uint64_t x42;
uint64_t x43;
uint64_t x44;
uint64_t x45;
uint64_t x46;
uint64_t x47;
uint64_t x48;
uint64_t x49;
uint64_t x50;
uint64_t x51;
uint64_t x52;
uint64_t x53;
uint64_t x54;
uint64_t x55;
uint64_t x56;
uint64_t x57;
uint64_t x58;
uint64_t x59;
uint64_t x60;
uint64_t x61;
uint64_t x62;
uint64_t x63;
uint64_t x64;
uint64_t x65;
uint64_t x66;
uint64_t x67;
uint64_t x68;
uint64_t x69;
uint64_t x70;
uint64_t x71;
uint64_t x72;
uint64_t x73;
uint64_t x74;
uint64_t x75;
uint64_t x76;
uint64_t x77;
uint64_t x78;
uint64_t x79;
uint64_t x80;
uint64_t x81;
uint64_t x82;
uint64_t x83;
uint64_t x84;
uint64_t x85;
uint64_t x86;
uint64_t x87;
uint64_t x88;
uint64_t x89;
uint64_t x90;
uint64_t x91;
uint64_t x92;
uint64_t x93;
uint64_t x94;
uint64_t x95;
uint64_t x96;
uint64_t x97;
uint64_t x98;
uint64_t x99;
uint64_t x100;
uint64_t x101;
uint64_t x102;
uint32_t x103;
uint64_t x104;
uint64_t x105;
uint64_t x106;
uint64_t x107;
uint64_t x108;
uint64_t x109;
uint64_t x110;
uint64_t x111;
uint64_t x112;
uint64_t x113;
uint64_t x114;
uint32_t x115;
uint64_t x116;
uint64_t x117;
uint32_t x118;
uint64_t x119;
uint64_t x120;
uint32_t x121;
uint64_t x122;
uint64_t x123;
uint32_t x124;
uint64_t x125;
uint64_t x126;
uint32_t x127;
uint64_t x128;
uint64_t x129;
uint32_t x130;
uint64_t x131;
uint64_t x132;
uint32_t x133;
uint64_t x134;
uint64_t x135;
uint32_t x136;
uint64_t x137;
uint64_t x138;
uint32_t x139;
uint64_t x140;
uint64_t x141;
uint32_t x142;
uint32_t x143;
uint32_t x144;
fiat_25519_uint1 x145;
uint32_t x146;
uint32_t x147;
x1 = ((uint64_t)(arg1[9]) * ((arg2[9]) * UINT8_C(0x26)));
x2 = ((uint64_t)(arg1[9]) * ((arg2[8]) * UINT8_C(0x13)));
x3 = ((uint64_t)(arg1[9]) * ((arg2[7]) * UINT8_C(0x26)));
x4 = ((uint64_t)(arg1[9]) * ((arg2[6]) * UINT8_C(0x13)));
x5 = ((uint64_t)(arg1[9]) * ((arg2[5]) * UINT8_C(0x26)));
x6 = ((uint64_t)(arg1[9]) * ((arg2[4]) * UINT8_C(0x13)));
x7 = ((uint64_t)(arg1[9]) * ((arg2[3]) * UINT8_C(0x26)));
x8 = ((uint64_t)(arg1[9]) * ((arg2[2]) * UINT8_C(0x13)));
x9 = ((uint64_t)(arg1[9]) * ((arg2[1]) * UINT8_C(0x26)));
x10 = ((uint64_t)(arg1[8]) * ((arg2[9]) * UINT8_C(0x13)));
x11 = ((uint64_t)(arg1[8]) * ((arg2[8]) * UINT8_C(0x13)));
x12 = ((uint64_t)(arg1[8]) * ((arg2[7]) * UINT8_C(0x13)));
x13 = ((uint64_t)(arg1[8]) * ((arg2[6]) * UINT8_C(0x13)));
x14 = ((uint64_t)(arg1[8]) * ((arg2[5]) * UINT8_C(0x13)));
x15 = ((uint64_t)(arg1[8]) * ((arg2[4]) * UINT8_C(0x13)));
x16 = ((uint64_t)(arg1[8]) * ((arg2[3]) * UINT8_C(0x13)));
x17 = ((uint64_t)(arg1[8]) * ((arg2[2]) * UINT8_C(0x13)));
x18 = ((uint64_t)(arg1[7]) * ((arg2[9]) * UINT8_C(0x26)));
x19 = ((uint64_t)(arg1[7]) * ((arg2[8]) * UINT8_C(0x13)));
x20 = ((uint64_t)(arg1[7]) * ((arg2[7]) * UINT8_C(0x26)));
x21 = ((uint64_t)(arg1[7]) * ((arg2[6]) * UINT8_C(0x13)));
x22 = ((uint64_t)(arg1[7]) * ((arg2[5]) * UINT8_C(0x26)));
x23 = ((uint64_t)(arg1[7]) * ((arg2[4]) * UINT8_C(0x13)));
x24 = ((uint64_t)(arg1[7]) * ((arg2[3]) * UINT8_C(0x26)));
x25 = ((uint64_t)(arg1[6]) * ((arg2[9]) * UINT8_C(0x13)));
x26 = ((uint64_t)(arg1[6]) * ((arg2[8]) * UINT8_C(0x13)));
x27 = ((uint64_t)(arg1[6]) * ((arg2[7]) * UINT8_C(0x13)));
x28 = ((uint64_t)(arg1[6]) * ((arg2[6]) * UINT8_C(0x13)));
x29 = ((uint64_t)(arg1[6]) * ((arg2[5]) * UINT8_C(0x13)));
x30 = ((uint64_t)(arg1[6]) * ((arg2[4]) * UINT8_C(0x13)));
x31 = ((uint64_t)(arg1[5]) * ((arg2[9]) * UINT8_C(0x26)));
x32 = ((uint64_t)(arg1[5]) * ((arg2[8]) * UINT8_C(0x13)));
x33 = ((uint64_t)(arg1[5]) * ((arg2[7]) * UINT8_C(0x26)));
x34 = ((uint64_t)(arg1[5]) * ((arg2[6]) * UINT8_C(0x13)));
x35 = ((uint64_t)(arg1[5]) * ((arg2[5]) * UINT8_C(0x26)));
x36 = ((uint64_t)(arg1[4]) * ((arg2[9]) * UINT8_C(0x13)));
x37 = ((uint64_t)(arg1[4]) * ((arg2[8]) * UINT8_C(0x13)));
x38 = ((uint64_t)(arg1[4]) * ((arg2[7]) * UINT8_C(0x13)));
x39 = ((uint64_t)(arg1[4]) * ((arg2[6]) * UINT8_C(0x13)));
x40 = ((uint64_t)(arg1[3]) * ((arg2[9]) * UINT8_C(0x26)));
x41 = ((uint64_t)(arg1[3]) * ((arg2[8]) * UINT8_C(0x13)));
x42 = ((uint64_t)(arg1[3]) * ((arg2[7]) * UINT8_C(0x26)));
x43 = ((uint64_t)(arg1[2]) * ((arg2[9]) * UINT8_C(0x13)));
x44 = ((uint64_t)(arg1[2]) * ((arg2[8]) * UINT8_C(0x13)));
x45 = ((uint64_t)(arg1[1]) * ((arg2[9]) * UINT8_C(0x26)));
x46 = ((uint64_t)(arg1[9]) * (arg2[0]));
x47 = ((uint64_t)(arg1[8]) * (arg2[1]));
x48 = ((uint64_t)(arg1[8]) * (arg2[0]));
x49 = ((uint64_t)(arg1[7]) * (arg2[2]));
x50 = ((uint64_t)(arg1[7]) * ((arg2[1]) * 0x2));
x51 = ((uint64_t)(arg1[7]) * (arg2[0]));
x52 = ((uint64_t)(arg1[6]) * (arg2[3]));
x53 = ((uint64_t)(arg1[6]) * (arg2[2]));
x54 = ((uint64_t)(arg1[6]) * (arg2[1]));
x55 = ((uint64_t)(arg1[6]) * (arg2[0]));
x56 = ((uint64_t)(arg1[5]) * (arg2[4]));
x57 = ((uint64_t)(arg1[5]) * ((arg2[3]) * 0x2));
x58 = ((uint64_t)(arg1[5]) * (arg2[2]));
x59 = ((uint64_t)(arg1[5]) * ((arg2[1]) * 0x2));
x60 = ((uint64_t)(arg1[5]) * (arg2[0]));
x61 = ((uint64_t)(arg1[4]) * (arg2[5]));
x62 = ((uint64_t)(arg1[4]) * (arg2[4]));
x63 = ((uint64_t)(arg1[4]) * (arg2[3]));
x64 = ((uint64_t)(arg1[4]) * (arg2[2]));
x65 = ((uint64_t)(arg1[4]) * (arg2[1]));
x66 = ((uint64_t)(arg1[4]) * (arg2[0]));
x67 = ((uint64_t)(arg1[3]) * (arg2[6]));
x68 = ((uint64_t)(arg1[3]) * ((arg2[5]) * 0x2));
x69 = ((uint64_t)(arg1[3]) * (arg2[4]));
x70 = ((uint64_t)(arg1[3]) * ((arg2[3]) * 0x2));
x71 = ((uint64_t)(arg1[3]) * (arg2[2]));
x72 = ((uint64_t)(arg1[3]) * ((arg2[1]) * 0x2));
x73 = ((uint64_t)(arg1[3]) * (arg2[0]));
x74 = ((uint64_t)(arg1[2]) * (arg2[7]));
x75 = ((uint64_t)(arg1[2]) * (arg2[6]));
x76 = ((uint64_t)(arg1[2]) * (arg2[5]));
x77 = ((uint64_t)(arg1[2]) * (arg2[4]));
x78 = ((uint64_t)(arg1[2]) * (arg2[3]));
x79 = ((uint64_t)(arg1[2]) * (arg2[2]));
x80 = ((uint64_t)(arg1[2]) * (arg2[1]));
x81 = ((uint64_t)(arg1[2]) * (arg2[0]));
x82 = ((uint64_t)(arg1[1]) * (arg2[8]));
x83 = ((uint64_t)(arg1[1]) * ((arg2[7]) * 0x2));
x84 = ((uint64_t)(arg1[1]) * (arg2[6]));
x85 = ((uint64_t)(arg1[1]) * ((arg2[5]) * 0x2));
x86 = ((uint64_t)(arg1[1]) * (arg2[4]));
x87 = ((uint64_t)(arg1[1]) * ((arg2[3]) * 0x2));
x88 = ((uint64_t)(arg1[1]) * (arg2[2]));
x89 = ((uint64_t)(arg1[1]) * ((arg2[1]) * 0x2));
x90 = ((uint64_t)(arg1[1]) * (arg2[0]));
x91 = ((uint64_t)(arg1[0]) * (arg2[9]));
x92 = ((uint64_t)(arg1[0]) * (arg2[8]));
x93 = ((uint64_t)(arg1[0]) * (arg2[7]));
x94 = ((uint64_t)(arg1[0]) * (arg2[6]));
x95 = ((uint64_t)(arg1[0]) * (arg2[5]));
x96 = ((uint64_t)(arg1[0]) * (arg2[4]));
x97 = ((uint64_t)(arg1[0]) * (arg2[3]));
x98 = ((uint64_t)(arg1[0]) * (arg2[2]));
x99 = ((uint64_t)(arg1[0]) * (arg2[1]));
x100 = ((uint64_t)(arg1[0]) * (arg2[0]));
x101 = (x100 + (x45 + (x44 + (x42 + (x39 + (x35 + (x30 + (x24 + (x17 + x9)))))))));
x102 = (x101 >> 26);
x103 = (uint32_t)(x101 & UINT32_C(0x3ffffff));
x104 = (x91 + (x82 + (x74 + (x67 + (x61 + (x56 + (x52 + (x49 + (x47 + x46)))))))));
x105 = (x92 + (x83 + (x75 + (x68 + (x62 + (x57 + (x53 + (x50 + (x48 + x1)))))))));
x106 = (x93 + (x84 + (x76 + (x69 + (x63 + (x58 + (x54 + (x51 + (x10 + x2)))))))));
x107 = (x94 + (x85 + (x77 + (x70 + (x64 + (x59 + (x55 + (x18 + (x11 + x3)))))))));
x108 = (x95 + (x86 + (x78 + (x71 + (x65 + (x60 + (x25 + (x19 + (x12 + x4)))))))));
x109 = (x96 + (x87 + (x79 + (x72 + (x66 + (x31 + (x26 + (x20 + (x13 + x5)))))))));
x110 = (x97 + (x88 + (x80 + (x73 + (x36 + (x32 + (x27 + (x21 + (x14 + x6)))))))));
x111 = (x98 + (x89 + (x81 + (x40 + (x37 + (x33 + (x28 + (x22 + (x15 + x7)))))))));
x112 = (x99 + (x90 + (x43 + (x41 + (x38 + (x34 + (x29 + (x23 + (x16 + x8)))))))));
x113 = (x102 + x112);
x114 = (x113 >> 25);
x115 = (uint32_t)(x113 & UINT32_C(0x1ffffff));
x116 = (x114 + x111);
x117 = (x116 >> 26);
x118 = (uint32_t)(x116 & UINT32_C(0x3ffffff));
x119 = (x117 + x110);
x120 = (x119 >> 25);
x121 = (uint32_t)(x119 & UINT32_C(0x1ffffff));
x122 = (x120 + x109);
x123 = (x122 >> 26);
x124 = (uint32_t)(x122 & UINT32_C(0x3ffffff));
x125 = (x123 + x108);
x126 = (x125 >> 25);
x127 = (uint32_t)(x125 & UINT32_C(0x1ffffff));
x128 = (x126 + x107);
x129 = (x128 >> 26);
x130 = (uint32_t)(x128 & UINT32_C(0x3ffffff));
x131 = (x129 + x106);
x132 = (x131 >> 25);
x133 = (uint32_t)(x131 & UINT32_C(0x1ffffff));
x134 = (x132 + x105);
x135 = (x134 >> 26);
x136 = (uint32_t)(x134 & UINT32_C(0x3ffffff));
x137 = (x135 + x104);
x138 = (x137 >> 25);
x139 = (uint32_t)(x137 & UINT32_C(0x1ffffff));
x140 = (x138 * UINT8_C(0x13));
x141 = (x103 + x140);
x142 = (uint32_t)(x141 >> 26);
x143 = (uint32_t)(x141 & UINT32_C(0x3ffffff));
x144 = (x142 + x115);
x145 = (fiat_25519_uint1)(x144 >> 25);
x146 = (x144 & UINT32_C(0x1ffffff));
x147 = (x145 + x118);
out1[0] = x143;
out1[1] = x146;
out1[2] = x147;
out1[3] = x121;
out1[4] = x124;
out1[5] = x127;
out1[6] = x130;
out1[7] = x133;
out1[8] = x136;
out1[9] = x139;
}
/*
* The function fiat_25519_carry_square squares a field element and reduces the result.
*
* Postconditions:
* eval out1 mod m = (eval arg1 * eval arg1) mod m
*
*/
static void fiat_25519_carry_square(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint64_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
uint64_t x11;
uint32_t x12;
uint32_t x13;
uint32_t x14;
uint32_t x15;
uint32_t x16;
uint32_t x17;
uint32_t x18;
uint64_t x19;
uint64_t x20;
uint64_t x21;
uint64_t x22;
uint64_t x23;
uint64_t x24;
uint64_t x25;
uint64_t x26;
uint64_t x27;
uint64_t x28;
uint64_t x29;
uint64_t x30;
uint64_t x31;
uint64_t x32;
uint64_t x33;
uint64_t x34;
uint64_t x35;
uint64_t x36;
uint64_t x37;
uint64_t x38;
uint64_t x39;
uint64_t x40;
uint64_t x41;
uint64_t x42;
uint64_t x43;
uint64_t x44;
uint64_t x45;
uint64_t x46;
uint64_t x47;
uint64_t x48;
uint64_t x49;
uint64_t x50;
uint64_t x51;
uint64_t x52;
uint64_t x53;
uint64_t x54;
uint64_t x55;
uint64_t x56;
uint64_t x57;
uint64_t x58;
uint64_t x59;
uint64_t x60;
uint64_t x61;
uint64_t x62;
uint64_t x63;
uint64_t x64;
uint64_t x65;
uint64_t x66;
uint64_t x67;
uint64_t x68;
uint64_t x69;
uint64_t x70;
uint64_t x71;
uint64_t x72;
uint64_t x73;
uint64_t x74;
uint64_t x75;
uint32_t x76;
uint64_t x77;
uint64_t x78;
uint64_t x79;
uint64_t x80;
uint64_t x81;
uint64_t x82;
uint64_t x83;
uint64_t x84;
uint64_t x85;
uint64_t x86;
uint64_t x87;
uint32_t x88;
uint64_t x89;
uint64_t x90;
uint32_t x91;
uint64_t x92;
uint64_t x93;
uint32_t x94;
uint64_t x95;
uint64_t x96;
uint32_t x97;
uint64_t x98;
uint64_t x99;
uint32_t x100;
uint64_t x101;
uint64_t x102;
uint32_t x103;
uint64_t x104;
uint64_t x105;
uint32_t x106;
uint64_t x107;
uint64_t x108;
uint32_t x109;
uint64_t x110;
uint64_t x111;
uint32_t x112;
uint64_t x113;
uint64_t x114;
uint32_t x115;
uint32_t x116;
uint32_t x117;
fiat_25519_uint1 x118;
uint32_t x119;
uint32_t x120;
x1 = ((arg1[9]) * UINT8_C(0x13));
x2 = (x1 * 0x2);
x3 = ((arg1[9]) * 0x2);
x4 = ((arg1[8]) * UINT8_C(0x13));
x5 = ((uint64_t)x4 * 0x2);
x6 = ((arg1[8]) * 0x2);
x7 = ((arg1[7]) * UINT8_C(0x13));
x8 = (x7 * 0x2);
x9 = ((arg1[7]) * 0x2);
x10 = ((arg1[6]) * UINT8_C(0x13));
x11 = ((uint64_t)x10 * 0x2);
x12 = ((arg1[6]) * 0x2);
x13 = ((arg1[5]) * UINT8_C(0x13));
x14 = ((arg1[5]) * 0x2);
x15 = ((arg1[4]) * 0x2);
x16 = ((arg1[3]) * 0x2);
x17 = ((arg1[2]) * 0x2);
x18 = ((arg1[1]) * 0x2);
x19 = ((uint64_t)(arg1[9]) * (x1 * 0x2));
x20 = ((uint64_t)(arg1[8]) * x2);
x21 = ((uint64_t)(arg1[8]) * x4);
x22 = ((arg1[7]) * ((uint64_t)x2 * 0x2));
x23 = ((arg1[7]) * x5);
x24 = ((uint64_t)(arg1[7]) * (x7 * 0x2));
x25 = ((uint64_t)(arg1[6]) * x2);
x26 = ((arg1[6]) * x5);
x27 = ((uint64_t)(arg1[6]) * x8);
x28 = ((uint64_t)(arg1[6]) * x10);
x29 = ((arg1[5]) * ((uint64_t)x2 * 0x2));
x30 = ((arg1[5]) * x5);
x31 = ((arg1[5]) * ((uint64_t)x8 * 0x2));
x32 = ((arg1[5]) * x11);
x33 = ((uint64_t)(arg1[5]) * (x13 * 0x2));
x34 = ((uint64_t)(arg1[4]) * x2);
x35 = ((arg1[4]) * x5);
x36 = ((uint64_t)(arg1[4]) * x8);
x37 = ((arg1[4]) * x11);
x38 = ((uint64_t)(arg1[4]) * x14);
x39 = ((uint64_t)(arg1[4]) * (arg1[4]));
x40 = ((arg1[3]) * ((uint64_t)x2 * 0x2));
x41 = ((arg1[3]) * x5);
x42 = ((arg1[3]) * ((uint64_t)x8 * 0x2));
x43 = ((uint64_t)(arg1[3]) * x12);
x44 = ((uint64_t)(arg1[3]) * (x14 * 0x2));
x45 = ((uint64_t)(arg1[3]) * x15);
x46 = ((uint64_t)(arg1[3]) * ((arg1[3]) * 0x2));
x47 = ((uint64_t)(arg1[2]) * x2);
x48 = ((arg1[2]) * x5);
x49 = ((uint64_t)(arg1[2]) * x9);
x50 = ((uint64_t)(arg1[2]) * x12);
x51 = ((uint64_t)(arg1[2]) * x14);
x52 = ((uint64_t)(arg1[2]) * x15);
x53 = ((uint64_t)(arg1[2]) * x16);
x54 = ((uint64_t)(arg1[2]) * (arg1[2]));
x55 = ((arg1[1]) * ((uint64_t)x2 * 0x2));
x56 = ((uint64_t)(arg1[1]) * x6);
x57 = ((uint64_t)(arg1[1]) * (x9 * 0x2));
x58 = ((uint64_t)(arg1[1]) * x12);
x59 = ((uint64_t)(arg1[1]) * (x14 * 0x2));
x60 = ((uint64_t)(arg1[1]) * x15);
x61 = ((uint64_t)(arg1[1]) * (x16 * 0x2));
x62 = ((uint64_t)(arg1[1]) * x17);
x63 = ((uint64_t)(arg1[1]) * ((arg1[1]) * 0x2));
x64 = ((uint64_t)(arg1[0]) * x3);
x65 = ((uint64_t)(arg1[0]) * x6);
x66 = ((uint64_t)(arg1[0]) * x9);
x67 = ((uint64_t)(arg1[0]) * x12);
x68 = ((uint64_t)(arg1[0]) * x14);
x69 = ((uint64_t)(arg1[0]) * x15);
x70 = ((uint64_t)(arg1[0]) * x16);
x71 = ((uint64_t)(arg1[0]) * x17);
x72 = ((uint64_t)(arg1[0]) * x18);
x73 = ((uint64_t)(arg1[0]) * (arg1[0]));
x74 = (x73 + (x55 + (x48 + (x42 + (x37 + x33)))));
x75 = (x74 >> 26);
x76 = (uint32_t)(x74 & UINT32_C(0x3ffffff));
x77 = (x64 + (x56 + (x49 + (x43 + x38))));
x78 = (x65 + (x57 + (x50 + (x44 + (x39 + x19)))));
x79 = (x66 + (x58 + (x51 + (x45 + x20))));
x80 = (x67 + (x59 + (x52 + (x46 + (x22 + x21)))));
x81 = (x68 + (x60 + (x53 + (x25 + x23))));
x82 = (x69 + (x61 + (x54 + (x29 + (x26 + x24)))));
x83 = (x70 + (x62 + (x34 + (x30 + x27))));
x84 = (x71 + (x63 + (x40 + (x35 + (x31 + x28)))));
x85 = (x72 + (x47 + (x41 + (x36 + x32))));
x86 = (x75 + x85);
x87 = (x86 >> 25);
x88 = (uint32_t)(x86 & UINT32_C(0x1ffffff));
x89 = (x87 + x84);
x90 = (x89 >> 26);
x91 = (uint32_t)(x89 & UINT32_C(0x3ffffff));
x92 = (x90 + x83);
x93 = (x92 >> 25);
x94 = (uint32_t)(x92 & UINT32_C(0x1ffffff));
x95 = (x93 + x82);
x96 = (x95 >> 26);
x97 = (uint32_t)(x95 & UINT32_C(0x3ffffff));
x98 = (x96 + x81);
x99 = (x98 >> 25);
x100 = (uint32_t)(x98 & UINT32_C(0x1ffffff));
x101 = (x99 + x80);
x102 = (x101 >> 26);
x103 = (uint32_t)(x101 & UINT32_C(0x3ffffff));
x104 = (x102 + x79);
x105 = (x104 >> 25);
x106 = (uint32_t)(x104 & UINT32_C(0x1ffffff));
x107 = (x105 + x78);
x108 = (x107 >> 26);
x109 = (uint32_t)(x107 & UINT32_C(0x3ffffff));
x110 = (x108 + x77);
x111 = (x110 >> 25);
x112 = (uint32_t)(x110 & UINT32_C(0x1ffffff));
x113 = (x111 * UINT8_C(0x13));
x114 = (x76 + x113);
x115 = (uint32_t)(x114 >> 26);
x116 = (uint32_t)(x114 & UINT32_C(0x3ffffff));
x117 = (x115 + x88);
x118 = (fiat_25519_uint1)(x117 >> 25);
x119 = (x117 & UINT32_C(0x1ffffff));
x120 = (x118 + x91);
out1[0] = x116;
out1[1] = x119;
out1[2] = x120;
out1[3] = x94;
out1[4] = x97;
out1[5] = x100;
out1[6] = x103;
out1[7] = x106;
out1[8] = x109;
out1[9] = x112;
}
/*
* The function fiat_25519_carry reduces a field element.
*
* Postconditions:
* eval out1 mod m = eval arg1 mod m
*
*/
static void fiat_25519_carry(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
uint32_t x11;
uint32_t x12;
uint32_t x13;
uint32_t x14;
uint32_t x15;
uint32_t x16;
uint32_t x17;
uint32_t x18;
uint32_t x19;
uint32_t x20;
uint32_t x21;
uint32_t x22;
x1 = (arg1[0]);
x2 = ((x1 >> 26) + (arg1[1]));
x3 = ((x2 >> 25) + (arg1[2]));
x4 = ((x3 >> 26) + (arg1[3]));
x5 = ((x4 >> 25) + (arg1[4]));
x6 = ((x5 >> 26) + (arg1[5]));
x7 = ((x6 >> 25) + (arg1[6]));
x8 = ((x7 >> 26) + (arg1[7]));
x9 = ((x8 >> 25) + (arg1[8]));
x10 = ((x9 >> 26) + (arg1[9]));
x11 = ((x1 & UINT32_C(0x3ffffff)) + ((x10 >> 25) * UINT8_C(0x13)));
x12 = ((fiat_25519_uint1)(x11 >> 26) + (x2 & UINT32_C(0x1ffffff)));
x13 = (x11 & UINT32_C(0x3ffffff));
x14 = (x12 & UINT32_C(0x1ffffff));
x15 = ((fiat_25519_uint1)(x12 >> 25) + (x3 & UINT32_C(0x3ffffff)));
x16 = (x4 & UINT32_C(0x1ffffff));
x17 = (x5 & UINT32_C(0x3ffffff));
x18 = (x6 & UINT32_C(0x1ffffff));
x19 = (x7 & UINT32_C(0x3ffffff));
x20 = (x8 & UINT32_C(0x1ffffff));
x21 = (x9 & UINT32_C(0x3ffffff));
x22 = (x10 & UINT32_C(0x1ffffff));
out1[0] = x13;
out1[1] = x14;
out1[2] = x15;
out1[3] = x16;
out1[4] = x17;
out1[5] = x18;
out1[6] = x19;
out1[7] = x20;
out1[8] = x21;
out1[9] = x22;
}
/*
* The function fiat_25519_add adds two field elements.
*
* Postconditions:
* eval out1 mod m = (eval arg1 + eval arg2) mod m
*
*/
static void fiat_25519_add(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1, const fiat_25519_tight_field_element arg2) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
x1 = ((arg1[0]) + (arg2[0]));
x2 = ((arg1[1]) + (arg2[1]));
x3 = ((arg1[2]) + (arg2[2]));
x4 = ((arg1[3]) + (arg2[3]));
x5 = ((arg1[4]) + (arg2[4]));
x6 = ((arg1[5]) + (arg2[5]));
x7 = ((arg1[6]) + (arg2[6]));
x8 = ((arg1[7]) + (arg2[7]));
x9 = ((arg1[8]) + (arg2[8]));
x10 = ((arg1[9]) + (arg2[9]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
out1[5] = x6;
out1[6] = x7;
out1[7] = x8;
out1[8] = x9;
out1[9] = x10;
}
/*
* The function fiat_25519_sub subtracts two field elements.
*
* Postconditions:
* eval out1 mod m = (eval arg1 - eval arg2) mod m
*
*/
static void fiat_25519_sub(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1, const fiat_25519_tight_field_element arg2) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
x1 = ((UINT32_C(0x7ffffda) + (arg1[0])) - (arg2[0]));
x2 = ((UINT32_C(0x3fffffe) + (arg1[1])) - (arg2[1]));
x3 = ((UINT32_C(0x7fffffe) + (arg1[2])) - (arg2[2]));
x4 = ((UINT32_C(0x3fffffe) + (arg1[3])) - (arg2[3]));
x5 = ((UINT32_C(0x7fffffe) + (arg1[4])) - (arg2[4]));
x6 = ((UINT32_C(0x3fffffe) + (arg1[5])) - (arg2[5]));
x7 = ((UINT32_C(0x7fffffe) + (arg1[6])) - (arg2[6]));
x8 = ((UINT32_C(0x3fffffe) + (arg1[7])) - (arg2[7]));
x9 = ((UINT32_C(0x7fffffe) + (arg1[8])) - (arg2[8]));
x10 = ((UINT32_C(0x3fffffe) + (arg1[9])) - (arg2[9]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
out1[5] = x6;
out1[6] = x7;
out1[7] = x8;
out1[8] = x9;
out1[9] = x10;
}
/*
* The function fiat_25519_opp negates a field element.
*
* Postconditions:
* eval out1 mod m = -eval arg1 mod m
*
*/
static void fiat_25519_opp(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
x1 = (UINT32_C(0x7ffffda) - (arg1[0]));
x2 = (UINT32_C(0x3fffffe) - (arg1[1]));
x3 = (UINT32_C(0x7fffffe) - (arg1[2]));
x4 = (UINT32_C(0x3fffffe) - (arg1[3]));
x5 = (UINT32_C(0x7fffffe) - (arg1[4]));
x6 = (UINT32_C(0x3fffffe) - (arg1[5]));
x7 = (UINT32_C(0x7fffffe) - (arg1[6]));
x8 = (UINT32_C(0x3fffffe) - (arg1[7]));
x9 = (UINT32_C(0x7fffffe) - (arg1[8]));
x10 = (UINT32_C(0x3fffffe) - (arg1[9]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
out1[5] = x6;
out1[6] = x7;
out1[7] = x8;
out1[8] = x9;
out1[9] = x10;
}
/*
* The function fiat_25519_selectznz is a multi-limb conditional select.
*
* Postconditions:
* out1 = (if arg1 = 0 then arg2 else arg3)
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [[0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]]
* arg3: [[0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]]
* Output Bounds:
* out1: [[0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff], [0x0 ~> 0xffffffff]]
*/
static void fiat_25519_selectznz(uint32_t out1[10], fiat_25519_uint1 arg1, const uint32_t arg2[10], const uint32_t arg3[10]) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
fiat_25519_cmovznz_u32(&x1, arg1, (arg2[0]), (arg3[0]));
fiat_25519_cmovznz_u32(&x2, arg1, (arg2[1]), (arg3[1]));
fiat_25519_cmovznz_u32(&x3, arg1, (arg2[2]), (arg3[2]));
fiat_25519_cmovznz_u32(&x4, arg1, (arg2[3]), (arg3[3]));
fiat_25519_cmovznz_u32(&x5, arg1, (arg2[4]), (arg3[4]));
fiat_25519_cmovznz_u32(&x6, arg1, (arg2[5]), (arg3[5]));
fiat_25519_cmovznz_u32(&x7, arg1, (arg2[6]), (arg3[6]));
fiat_25519_cmovznz_u32(&x8, arg1, (arg2[7]), (arg3[7]));
fiat_25519_cmovznz_u32(&x9, arg1, (arg2[8]), (arg3[8]));
fiat_25519_cmovznz_u32(&x10, arg1, (arg2[9]), (arg3[9]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
out1[5] = x6;
out1[6] = x7;
out1[7] = x8;
out1[8] = x9;
out1[9] = x10;
}
/*
* The function fiat_25519_to_bytes serializes a field element to bytes in little-endian order.
*
* Postconditions:
* out1 = map (λ x, ⌊((eval arg1 mod m) mod 2^(8 * (x + 1))) / 2^(8 * x)⌋) [0..31]
*
* Output Bounds:
* out1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
*/
static void fiat_25519_to_bytes(uint8_t out1[32], const fiat_25519_tight_field_element arg1) {
uint32_t x1;
fiat_25519_uint1 x2;
uint32_t x3;
fiat_25519_uint1 x4;
uint32_t x5;
fiat_25519_uint1 x6;
uint32_t x7;
fiat_25519_uint1 x8;
uint32_t x9;
fiat_25519_uint1 x10;
uint32_t x11;
fiat_25519_uint1 x12;
uint32_t x13;
fiat_25519_uint1 x14;
uint32_t x15;
fiat_25519_uint1 x16;
uint32_t x17;
fiat_25519_uint1 x18;
uint32_t x19;
fiat_25519_uint1 x20;
uint32_t x21;
uint32_t x22;
fiat_25519_uint1 x23;
uint32_t x24;
fiat_25519_uint1 x25;
uint32_t x26;
fiat_25519_uint1 x27;
uint32_t x28;
fiat_25519_uint1 x29;
uint32_t x30;
fiat_25519_uint1 x31;
uint32_t x32;
fiat_25519_uint1 x33;
uint32_t x34;
fiat_25519_uint1 x35;
uint32_t x36;
fiat_25519_uint1 x37;
uint32_t x38;
fiat_25519_uint1 x39;
uint32_t x40;
fiat_25519_uint1 x41;
uint32_t x42;
uint32_t x43;
uint32_t x44;
uint32_t x45;
uint32_t x46;
uint32_t x47;
uint32_t x48;
uint32_t x49;
uint8_t x50;
uint32_t x51;
uint8_t x52;
uint32_t x53;
uint8_t x54;
uint8_t x55;
uint32_t x56;
uint8_t x57;
uint32_t x58;
uint8_t x59;
uint32_t x60;
uint8_t x61;
uint8_t x62;
uint32_t x63;
uint8_t x64;
uint32_t x65;
uint8_t x66;
uint32_t x67;
uint8_t x68;
uint8_t x69;
uint32_t x70;
uint8_t x71;
uint32_t x72;
uint8_t x73;
uint32_t x74;
uint8_t x75;
uint8_t x76;
uint32_t x77;
uint8_t x78;
uint32_t x79;
uint8_t x80;
uint32_t x81;
uint8_t x82;
uint8_t x83;
uint8_t x84;
uint32_t x85;
uint8_t x86;
uint32_t x87;
uint8_t x88;
fiat_25519_uint1 x89;
uint32_t x90;
uint8_t x91;
uint32_t x92;
uint8_t x93;
uint32_t x94;
uint8_t x95;
uint8_t x96;
uint32_t x97;
uint8_t x98;
uint32_t x99;
uint8_t x100;
uint32_t x101;
uint8_t x102;
uint8_t x103;
uint32_t x104;
uint8_t x105;
uint32_t x106;
uint8_t x107;
uint32_t x108;
uint8_t x109;
uint8_t x110;
uint32_t x111;
uint8_t x112;
uint32_t x113;
uint8_t x114;
uint32_t x115;
uint8_t x116;
uint8_t x117;
fiat_25519_subborrowx_u26(&x1, &x2, 0x0, (arg1[0]), UINT32_C(0x3ffffed));
fiat_25519_subborrowx_u25(&x3, &x4, x2, (arg1[1]), UINT32_C(0x1ffffff));
fiat_25519_subborrowx_u26(&x5, &x6, x4, (arg1[2]), UINT32_C(0x3ffffff));
fiat_25519_subborrowx_u25(&x7, &x8, x6, (arg1[3]), UINT32_C(0x1ffffff));
fiat_25519_subborrowx_u26(&x9, &x10, x8, (arg1[4]), UINT32_C(0x3ffffff));
fiat_25519_subborrowx_u25(&x11, &x12, x10, (arg1[5]), UINT32_C(0x1ffffff));
fiat_25519_subborrowx_u26(&x13, &x14, x12, (arg1[6]), UINT32_C(0x3ffffff));
fiat_25519_subborrowx_u25(&x15, &x16, x14, (arg1[7]), UINT32_C(0x1ffffff));
fiat_25519_subborrowx_u26(&x17, &x18, x16, (arg1[8]), UINT32_C(0x3ffffff));
fiat_25519_subborrowx_u25(&x19, &x20, x18, (arg1[9]), UINT32_C(0x1ffffff));
fiat_25519_cmovznz_u32(&x21, x20, 0x0, UINT32_C(0xffffffff));
fiat_25519_addcarryx_u26(&x22, &x23, 0x0, x1, (x21 & UINT32_C(0x3ffffed)));
fiat_25519_addcarryx_u25(&x24, &x25, x23, x3, (x21 & UINT32_C(0x1ffffff)));
fiat_25519_addcarryx_u26(&x26, &x27, x25, x5, (x21 & UINT32_C(0x3ffffff)));
fiat_25519_addcarryx_u25(&x28, &x29, x27, x7, (x21 & UINT32_C(0x1ffffff)));
fiat_25519_addcarryx_u26(&x30, &x31, x29, x9, (x21 & UINT32_C(0x3ffffff)));
fiat_25519_addcarryx_u25(&x32, &x33, x31, x11, (x21 & UINT32_C(0x1ffffff)));
fiat_25519_addcarryx_u26(&x34, &x35, x33, x13, (x21 & UINT32_C(0x3ffffff)));
fiat_25519_addcarryx_u25(&x36, &x37, x35, x15, (x21 & UINT32_C(0x1ffffff)));
fiat_25519_addcarryx_u26(&x38, &x39, x37, x17, (x21 & UINT32_C(0x3ffffff)));
fiat_25519_addcarryx_u25(&x40, &x41, x39, x19, (x21 & UINT32_C(0x1ffffff)));
x42 = (x40 << 6);
x43 = (x38 << 4);
x44 = (x36 << 3);
x45 = (x34 * (uint32_t)0x2);
x46 = (x30 << 6);
x47 = (x28 << 5);
x48 = (x26 << 3);
x49 = (x24 << 2);
x50 = (uint8_t)(x22 & UINT8_C(0xff));
x51 = (x22 >> 8);
x52 = (uint8_t)(x51 & UINT8_C(0xff));
x53 = (x51 >> 8);
x54 = (uint8_t)(x53 & UINT8_C(0xff));
x55 = (uint8_t)(x53 >> 8);
x56 = (x49 + (uint32_t)x55);
x57 = (uint8_t)(x56 & UINT8_C(0xff));
x58 = (x56 >> 8);
x59 = (uint8_t)(x58 & UINT8_C(0xff));
x60 = (x58 >> 8);
x61 = (uint8_t)(x60 & UINT8_C(0xff));
x62 = (uint8_t)(x60 >> 8);
x63 = (x48 + (uint32_t)x62);
x64 = (uint8_t)(x63 & UINT8_C(0xff));
x65 = (x63 >> 8);
x66 = (uint8_t)(x65 & UINT8_C(0xff));
x67 = (x65 >> 8);
x68 = (uint8_t)(x67 & UINT8_C(0xff));
x69 = (uint8_t)(x67 >> 8);
x70 = (x47 + (uint32_t)x69);
x71 = (uint8_t)(x70 & UINT8_C(0xff));
x72 = (x70 >> 8);
x73 = (uint8_t)(x72 & UINT8_C(0xff));
x74 = (x72 >> 8);
x75 = (uint8_t)(x74 & UINT8_C(0xff));
x76 = (uint8_t)(x74 >> 8);
x77 = (x46 + (uint32_t)x76);
x78 = (uint8_t)(x77 & UINT8_C(0xff));
x79 = (x77 >> 8);
x80 = (uint8_t)(x79 & UINT8_C(0xff));
x81 = (x79 >> 8);
x82 = (uint8_t)(x81 & UINT8_C(0xff));
x83 = (uint8_t)(x81 >> 8);
x84 = (uint8_t)(x32 & UINT8_C(0xff));
x85 = (x32 >> 8);
x86 = (uint8_t)(x85 & UINT8_C(0xff));
x87 = (x85 >> 8);
x88 = (uint8_t)(x87 & UINT8_C(0xff));
x89 = (fiat_25519_uint1)(x87 >> 8);
x90 = (x45 + (uint32_t)x89);
x91 = (uint8_t)(x90 & UINT8_C(0xff));
x92 = (x90 >> 8);
x93 = (uint8_t)(x92 & UINT8_C(0xff));
x94 = (x92 >> 8);
x95 = (uint8_t)(x94 & UINT8_C(0xff));
x96 = (uint8_t)(x94 >> 8);
x97 = (x44 + (uint32_t)x96);
x98 = (uint8_t)(x97 & UINT8_C(0xff));
x99 = (x97 >> 8);
x100 = (uint8_t)(x99 & UINT8_C(0xff));
x101 = (x99 >> 8);
x102 = (uint8_t)(x101 & UINT8_C(0xff));
x103 = (uint8_t)(x101 >> 8);
x104 = (x43 + (uint32_t)x103);
x105 = (uint8_t)(x104 & UINT8_C(0xff));
x106 = (x104 >> 8);
x107 = (uint8_t)(x106 & UINT8_C(0xff));
x108 = (x106 >> 8);
x109 = (uint8_t)(x108 & UINT8_C(0xff));
x110 = (uint8_t)(x108 >> 8);
x111 = (x42 + (uint32_t)x110);
x112 = (uint8_t)(x111 & UINT8_C(0xff));
x113 = (x111 >> 8);
x114 = (uint8_t)(x113 & UINT8_C(0xff));
x115 = (x113 >> 8);
x116 = (uint8_t)(x115 & UINT8_C(0xff));
x117 = (uint8_t)(x115 >> 8);
out1[0] = x50;
out1[1] = x52;
out1[2] = x54;
out1[3] = x57;
out1[4] = x59;
out1[5] = x61;
out1[6] = x64;
out1[7] = x66;
out1[8] = x68;
out1[9] = x71;
out1[10] = x73;
out1[11] = x75;
out1[12] = x78;
out1[13] = x80;
out1[14] = x82;
out1[15] = x83;
out1[16] = x84;
out1[17] = x86;
out1[18] = x88;
out1[19] = x91;
out1[20] = x93;
out1[21] = x95;
out1[22] = x98;
out1[23] = x100;
out1[24] = x102;
out1[25] = x105;
out1[26] = x107;
out1[27] = x109;
out1[28] = x112;
out1[29] = x114;
out1[30] = x116;
out1[31] = x117;
}
/*
* The function fiat_25519_from_bytes deserializes a field element from bytes in little-endian order.
*
* Postconditions:
* eval out1 mod m = bytes_eval arg1 mod m
*
* Input Bounds:
* arg1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
*/
static void fiat_25519_from_bytes(fiat_25519_tight_field_element out1, const uint8_t arg1[32]) {
uint32_t x1;
uint32_t x2;
uint32_t x3;
uint32_t x4;
uint32_t x5;
uint32_t x6;
uint32_t x7;
uint32_t x8;
uint32_t x9;
uint32_t x10;
uint32_t x11;
uint32_t x12;
uint32_t x13;
uint32_t x14;
uint32_t x15;
uint8_t x16;
uint32_t x17;
uint32_t x18;
uint32_t x19;
uint32_t x20;
uint32_t x21;
uint32_t x22;
uint32_t x23;
uint32_t x24;
uint32_t x25;
uint32_t x26;
uint32_t x27;
uint32_t x28;
uint32_t x29;
uint32_t x30;
uint32_t x31;
uint8_t x32;
uint32_t x33;
uint32_t x34;
uint32_t x35;
uint32_t x36;
uint8_t x37;
uint32_t x38;
uint32_t x39;
uint32_t x40;
uint32_t x41;
uint8_t x42;
uint32_t x43;
uint32_t x44;
uint32_t x45;
uint32_t x46;
uint8_t x47;
uint32_t x48;
uint32_t x49;
uint32_t x50;
uint32_t x51;
uint8_t x52;
uint32_t x53;
uint32_t x54;
uint32_t x55;
uint32_t x56;
uint32_t x57;
uint32_t x58;
uint32_t x59;
uint8_t x60;
uint32_t x61;
uint32_t x62;
uint32_t x63;
uint32_t x64;
uint8_t x65;
uint32_t x66;
uint32_t x67;
uint32_t x68;
uint32_t x69;
uint8_t x70;
uint32_t x71;
uint32_t x72;
uint32_t x73;
uint32_t x74;
uint8_t x75;
uint32_t x76;
uint32_t x77;
uint32_t x78;
x1 = ((uint32_t)(arg1[31]) << 18);
x2 = ((uint32_t)(arg1[30]) << 10);
x3 = ((uint32_t)(arg1[29]) << 2);
x4 = ((uint32_t)(arg1[28]) << 20);
x5 = ((uint32_t)(arg1[27]) << 12);
x6 = ((uint32_t)(arg1[26]) << 4);
x7 = ((uint32_t)(arg1[25]) << 21);
x8 = ((uint32_t)(arg1[24]) << 13);
x9 = ((uint32_t)(arg1[23]) << 5);
x10 = ((uint32_t)(arg1[22]) << 23);
x11 = ((uint32_t)(arg1[21]) << 15);
x12 = ((uint32_t)(arg1[20]) << 7);
x13 = ((uint32_t)(arg1[19]) << 24);
x14 = ((uint32_t)(arg1[18]) << 16);
x15 = ((uint32_t)(arg1[17]) << 8);
x16 = (arg1[16]);
x17 = ((uint32_t)(arg1[15]) << 18);
x18 = ((uint32_t)(arg1[14]) << 10);
x19 = ((uint32_t)(arg1[13]) << 2);
x20 = ((uint32_t)(arg1[12]) << 19);
x21 = ((uint32_t)(arg1[11]) << 11);
x22 = ((uint32_t)(arg1[10]) << 3);
x23 = ((uint32_t)(arg1[9]) << 21);
x24 = ((uint32_t)(arg1[8]) << 13);
x25 = ((uint32_t)(arg1[7]) << 5);
x26 = ((uint32_t)(arg1[6]) << 22);
x27 = ((uint32_t)(arg1[5]) << 14);
x28 = ((uint32_t)(arg1[4]) << 6);
x29 = ((uint32_t)(arg1[3]) << 24);
x30 = ((uint32_t)(arg1[2]) << 16);
x31 = ((uint32_t)(arg1[1]) << 8);
x32 = (arg1[0]);
x33 = (x31 + (uint32_t)x32);
x34 = (x30 + x33);
x35 = (x29 + x34);
x36 = (x35 & UINT32_C(0x3ffffff));
x37 = (uint8_t)(x35 >> 26);
x38 = (x28 + (uint32_t)x37);
x39 = (x27 + x38);
x40 = (x26 + x39);
x41 = (x40 & UINT32_C(0x1ffffff));
x42 = (uint8_t)(x40 >> 25);
x43 = (x25 + (uint32_t)x42);
x44 = (x24 + x43);
x45 = (x23 + x44);
x46 = (x45 & UINT32_C(0x3ffffff));
x47 = (uint8_t)(x45 >> 26);
x48 = (x22 + (uint32_t)x47);
x49 = (x21 + x48);
x50 = (x20 + x49);
x51 = (x50 & UINT32_C(0x1ffffff));
x52 = (uint8_t)(x50 >> 25);
x53 = (x19 + (uint32_t)x52);
x54 = (x18 + x53);
x55 = (x17 + x54);
x56 = (x15 + (uint32_t)x16);
x57 = (x14 + x56);
x58 = (x13 + x57);
x59 = (x58 & UINT32_C(0x1ffffff));
x60 = (uint8_t)(x58 >> 25);
x61 = (x12 + (uint32_t)x60);
x62 = (x11 + x61);
x63 = (x10 + x62);
x64 = (x63 & UINT32_C(0x3ffffff));
x65 = (uint8_t)(x63 >> 26);
x66 = (x9 + (uint32_t)x65);
x67 = (x8 + x66);
x68 = (x7 + x67);
x69 = (x68 & UINT32_C(0x1ffffff));
x70 = (uint8_t)(x68 >> 25);
x71 = (x6 + (uint32_t)x70);
x72 = (x5 + x71);
x73 = (x4 + x72);
x74 = (x73 & UINT32_C(0x3ffffff));
x75 = (uint8_t)(x73 >> 26);
x76 = (x3 + (uint32_t)x75);
x77 = (x2 + x76);
x78 = (x1 + x77);
out1[0] = x36;
out1[1] = x41;
out1[2] = x46;
out1[3] = x51;
out1[4] = x55;
out1[5] = x59;
out1[6] = x64;
out1[7] = x69;
out1[8] = x74;
out1[9] = x78;
}
/*
* The function fiat_25519_carry_scmul_121666 multiplies a field element by 121666 and reduces the result.
*
* Postconditions:
* eval out1 mod m = (121666 * eval arg1) mod m
*
*/
static void fiat_25519_carry_scmul_121666(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
uint64_t x6;
uint64_t x7;
uint64_t x8;
uint64_t x9;
uint64_t x10;
uint32_t x11;
uint32_t x12;
uint64_t x13;
uint32_t x14;
uint32_t x15;
uint64_t x16;
uint32_t x17;
uint32_t x18;
uint64_t x19;
uint32_t x20;
uint32_t x21;
uint64_t x22;
uint32_t x23;
uint32_t x24;
uint64_t x25;
uint32_t x26;
uint32_t x27;
uint64_t x28;
uint32_t x29;
uint32_t x30;
uint64_t x31;
uint32_t x32;
uint32_t x33;
uint64_t x34;
uint32_t x35;
uint32_t x36;
uint64_t x37;
uint32_t x38;
uint32_t x39;
uint32_t x40;
uint32_t x41;
fiat_25519_uint1 x42;
uint32_t x43;
uint32_t x44;
fiat_25519_uint1 x45;
uint32_t x46;
uint32_t x47;
x1 = ((uint64_t)UINT32_C(0x1db42) * (arg1[9]));
x2 = ((uint64_t)UINT32_C(0x1db42) * (arg1[8]));
x3 = ((uint64_t)UINT32_C(0x1db42) * (arg1[7]));
x4 = ((uint64_t)UINT32_C(0x1db42) * (arg1[6]));
x5 = ((uint64_t)UINT32_C(0x1db42) * (arg1[5]));
x6 = ((uint64_t)UINT32_C(0x1db42) * (arg1[4]));
x7 = ((uint64_t)UINT32_C(0x1db42) * (arg1[3]));
x8 = ((uint64_t)UINT32_C(0x1db42) * (arg1[2]));
x9 = ((uint64_t)UINT32_C(0x1db42) * (arg1[1]));
x10 = ((uint64_t)UINT32_C(0x1db42) * (arg1[0]));
x11 = (uint32_t)(x10 >> 26);
x12 = (uint32_t)(x10 & UINT32_C(0x3ffffff));
x13 = (x11 + x9);
x14 = (uint32_t)(x13 >> 25);
x15 = (uint32_t)(x13 & UINT32_C(0x1ffffff));
x16 = (x14 + x8);
x17 = (uint32_t)(x16 >> 26);
x18 = (uint32_t)(x16 & UINT32_C(0x3ffffff));
x19 = (x17 + x7);
x20 = (uint32_t)(x19 >> 25);
x21 = (uint32_t)(x19 & UINT32_C(0x1ffffff));
x22 = (x20 + x6);
x23 = (uint32_t)(x22 >> 26);
x24 = (uint32_t)(x22 & UINT32_C(0x3ffffff));
x25 = (x23 + x5);
x26 = (uint32_t)(x25 >> 25);
x27 = (uint32_t)(x25 & UINT32_C(0x1ffffff));
x28 = (x26 + x4);
x29 = (uint32_t)(x28 >> 26);
x30 = (uint32_t)(x28 & UINT32_C(0x3ffffff));
x31 = (x29 + x3);
x32 = (uint32_t)(x31 >> 25);
x33 = (uint32_t)(x31 & UINT32_C(0x1ffffff));
x34 = (x32 + x2);
x35 = (uint32_t)(x34 >> 26);
x36 = (uint32_t)(x34 & UINT32_C(0x3ffffff));
x37 = (x35 + x1);
x38 = (uint32_t)(x37 >> 25);
x39 = (uint32_t)(x37 & UINT32_C(0x1ffffff));
x40 = (x38 * UINT8_C(0x13));
x41 = (x12 + x40);
x42 = (fiat_25519_uint1)(x41 >> 26);
x43 = (x41 & UINT32_C(0x3ffffff));
x44 = (x42 + x15);
x45 = (fiat_25519_uint1)(x44 >> 25);
x46 = (x44 & UINT32_C(0x1ffffff));
x47 = (x45 + x18);
out1[0] = x43;
out1[1] = x46;
out1[2] = x47;
out1[3] = x21;
out1[4] = x24;
out1[5] = x27;
out1[6] = x30;
out1[7] = x33;
out1[8] = x36;
out1[9] = x39;
}
|
mirage-crypto
|
2.0.2
|
ISC
|
https://github.com/mirage/mirage-crypto
|
git+https://github.com/mirage/mirage-crypto.git
|
h
|
mirage-crypto-2.0.2/ec/native/curve25519_64.h
|
/* Autogenerated: '../fiat-crypto/src/ExtractionOCaml/unsaturated_solinas' --static --use-value-barrier --inline-internal 25519 64 '(auto)' '2^255 - 19' carry_mul carry_square carry add sub opp selectznz to_bytes from_bytes carry_scmul121666 */
/* curve description: 25519 */
/* machine_wordsize = 64 (from "64") */
/* requested operations: carry_mul, carry_square, carry, add, sub, opp, selectznz, to_bytes, from_bytes, carry_scmul121666 */
/* n = 5 (from "(auto)") */
/* s-c = 2^255 - [(1, 19)] (from "2^255 - 19") */
/* tight_bounds_multiplier = 1 (from "") */
/* */
/* Computed values: */
/* carry_chain = [0, 1, 2, 3, 4, 0, 1] */
/* eval z = z[0] + (z[1] << 51) + (z[2] << 102) + (z[3] << 153) + (z[4] << 204) */
/* bytes_eval z = z[0] + (z[1] << 8) + (z[2] << 16) + (z[3] << 24) + (z[4] << 32) + (z[5] << 40) + (z[6] << 48) + (z[7] << 56) + (z[8] << 64) + (z[9] << 72) + (z[10] << 80) + (z[11] << 88) + (z[12] << 96) + (z[13] << 104) + (z[14] << 112) + (z[15] << 120) + (z[16] << 128) + (z[17] << 136) + (z[18] << 144) + (z[19] << 152) + (z[20] << 160) + (z[21] << 168) + (z[22] << 176) + (z[23] << 184) + (z[24] << 192) + (z[25] << 200) + (z[26] << 208) + (z[27] << 216) + (z[28] << 224) + (z[29] << 232) + (z[30] << 240) + (z[31] << 248) */
/* balance = [0xfffffffffffda, 0xffffffffffffe, 0xffffffffffffe, 0xffffffffffffe, 0xffffffffffffe] */
#include <stdint.h>
typedef unsigned char fiat_25519_uint1;
typedef signed char fiat_25519_int1;
#if defined(__GNUC__) || defined(__clang__)
# define FIAT_25519_FIAT_EXTENSION __extension__
# define FIAT_25519_FIAT_INLINE __inline__
#else
# define FIAT_25519_FIAT_EXTENSION
# define FIAT_25519_FIAT_INLINE
#endif
FIAT_25519_FIAT_EXTENSION typedef signed __int128 fiat_25519_int128;
FIAT_25519_FIAT_EXTENSION typedef unsigned __int128 fiat_25519_uint128;
/* The type fiat_25519_loose_field_element is a field element with loose bounds. */
/* Bounds: [[0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000], [0x0 ~> 0x18000000000000]] */
typedef uint64_t fiat_25519_loose_field_element[5];
/* The type fiat_25519_tight_field_element is a field element with tight bounds. */
/* Bounds: [[0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000], [0x0 ~> 0x8000000000000]] */
typedef uint64_t fiat_25519_tight_field_element[5];
#if (-1 & 3) != 3
#error "This code only works on a two's complement system"
#endif
#if !defined(FIAT_25519_NO_ASM) && (defined(__GNUC__) || defined(__clang__))
static __inline__ uint64_t fiat_25519_value_barrier_u64(uint64_t a) {
__asm__("" : "+r"(a) : /* no inputs */);
return a;
}
#else
# define fiat_25519_value_barrier_u64(x) (x)
#endif
/*
* The function fiat_25519_addcarryx_u51 is an addition with carry.
*
* Postconditions:
* out1 = (arg1 + arg2 + arg3) mod 2^51
* out2 = ⌊(arg1 + arg2 + arg3) / 2^51⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x7ffffffffffff]
* arg3: [0x0 ~> 0x7ffffffffffff]
* Output Bounds:
* out1: [0x0 ~> 0x7ffffffffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_addcarryx_u51(uint64_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
uint64_t x1;
uint64_t x2;
fiat_25519_uint1 x3;
x1 = ((arg1 + arg2) + arg3);
x2 = (x1 & UINT64_C(0x7ffffffffffff));
x3 = (fiat_25519_uint1)(x1 >> 51);
*out1 = x2;
*out2 = x3;
}
/*
* The function fiat_25519_subborrowx_u51 is a subtraction with borrow.
*
* Postconditions:
* out1 = (-arg1 + arg2 + -arg3) mod 2^51
* out2 = -⌊(-arg1 + arg2 + -arg3) / 2^51⌋
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0x7ffffffffffff]
* arg3: [0x0 ~> 0x7ffffffffffff]
* Output Bounds:
* out1: [0x0 ~> 0x7ffffffffffff]
* out2: [0x0 ~> 0x1]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_subborrowx_u51(uint64_t* out1, fiat_25519_uint1* out2, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
int64_t x1;
fiat_25519_int1 x2;
uint64_t x3;
x1 = ((int64_t)(arg2 - (int64_t)arg1) - (int64_t)arg3);
x2 = (fiat_25519_int1)(x1 >> 51);
x3 = (x1 & UINT64_C(0x7ffffffffffff));
*out1 = x3;
*out2 = (fiat_25519_uint1)(0x0 - x2);
}
/*
* The function fiat_25519_cmovznz_u64 is a single-word conditional move.
*
* Postconditions:
* out1 = (if arg1 = 0 then arg2 else arg3)
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [0x0 ~> 0xffffffffffffffff]
* arg3: [0x0 ~> 0xffffffffffffffff]
* Output Bounds:
* out1: [0x0 ~> 0xffffffffffffffff]
*/
static FIAT_25519_FIAT_INLINE void fiat_25519_cmovznz_u64(uint64_t* out1, fiat_25519_uint1 arg1, uint64_t arg2, uint64_t arg3) {
fiat_25519_uint1 x1;
uint64_t x2;
uint64_t x3;
x1 = (!(!arg1));
x2 = ((fiat_25519_int1)(0x0 - x1) & UINT64_C(0xffffffffffffffff));
x3 = ((fiat_25519_value_barrier_u64(x2) & arg3) | (fiat_25519_value_barrier_u64((~x2)) & arg2));
*out1 = x3;
}
/*
* The function fiat_25519_carry_mul multiplies two field elements and reduces the result.
*
* Postconditions:
* eval out1 mod m = (eval arg1 * eval arg2) mod m
*
*/
static void fiat_25519_carry_mul(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1, const fiat_25519_loose_field_element arg2) {
fiat_25519_uint128 x1;
fiat_25519_uint128 x2;
fiat_25519_uint128 x3;
fiat_25519_uint128 x4;
fiat_25519_uint128 x5;
fiat_25519_uint128 x6;
fiat_25519_uint128 x7;
fiat_25519_uint128 x8;
fiat_25519_uint128 x9;
fiat_25519_uint128 x10;
fiat_25519_uint128 x11;
fiat_25519_uint128 x12;
fiat_25519_uint128 x13;
fiat_25519_uint128 x14;
fiat_25519_uint128 x15;
fiat_25519_uint128 x16;
fiat_25519_uint128 x17;
fiat_25519_uint128 x18;
fiat_25519_uint128 x19;
fiat_25519_uint128 x20;
fiat_25519_uint128 x21;
fiat_25519_uint128 x22;
fiat_25519_uint128 x23;
fiat_25519_uint128 x24;
fiat_25519_uint128 x25;
fiat_25519_uint128 x26;
uint64_t x27;
uint64_t x28;
fiat_25519_uint128 x29;
fiat_25519_uint128 x30;
fiat_25519_uint128 x31;
fiat_25519_uint128 x32;
fiat_25519_uint128 x33;
uint64_t x34;
uint64_t x35;
fiat_25519_uint128 x36;
uint64_t x37;
uint64_t x38;
fiat_25519_uint128 x39;
uint64_t x40;
uint64_t x41;
fiat_25519_uint128 x42;
uint64_t x43;
uint64_t x44;
uint64_t x45;
uint64_t x46;
uint64_t x47;
uint64_t x48;
uint64_t x49;
fiat_25519_uint1 x50;
uint64_t x51;
uint64_t x52;
x1 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[4]) * UINT8_C(0x13)));
x2 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[3]) * UINT8_C(0x13)));
x3 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[2]) * UINT8_C(0x13)));
x4 = ((fiat_25519_uint128)(arg1[4]) * ((arg2[1]) * UINT8_C(0x13)));
x5 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[4]) * UINT8_C(0x13)));
x6 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[3]) * UINT8_C(0x13)));
x7 = ((fiat_25519_uint128)(arg1[3]) * ((arg2[2]) * UINT8_C(0x13)));
x8 = ((fiat_25519_uint128)(arg1[2]) * ((arg2[4]) * UINT8_C(0x13)));
x9 = ((fiat_25519_uint128)(arg1[2]) * ((arg2[3]) * UINT8_C(0x13)));
x10 = ((fiat_25519_uint128)(arg1[1]) * ((arg2[4]) * UINT8_C(0x13)));
x11 = ((fiat_25519_uint128)(arg1[4]) * (arg2[0]));
x12 = ((fiat_25519_uint128)(arg1[3]) * (arg2[1]));
x13 = ((fiat_25519_uint128)(arg1[3]) * (arg2[0]));
x14 = ((fiat_25519_uint128)(arg1[2]) * (arg2[2]));
x15 = ((fiat_25519_uint128)(arg1[2]) * (arg2[1]));
x16 = ((fiat_25519_uint128)(arg1[2]) * (arg2[0]));
x17 = ((fiat_25519_uint128)(arg1[1]) * (arg2[3]));
x18 = ((fiat_25519_uint128)(arg1[1]) * (arg2[2]));
x19 = ((fiat_25519_uint128)(arg1[1]) * (arg2[1]));
x20 = ((fiat_25519_uint128)(arg1[1]) * (arg2[0]));
x21 = ((fiat_25519_uint128)(arg1[0]) * (arg2[4]));
x22 = ((fiat_25519_uint128)(arg1[0]) * (arg2[3]));
x23 = ((fiat_25519_uint128)(arg1[0]) * (arg2[2]));
x24 = ((fiat_25519_uint128)(arg1[0]) * (arg2[1]));
x25 = ((fiat_25519_uint128)(arg1[0]) * (arg2[0]));
x26 = (x25 + (x10 + (x9 + (x7 + x4))));
x27 = (uint64_t)(x26 >> 51);
x28 = (uint64_t)(x26 & UINT64_C(0x7ffffffffffff));
x29 = (x21 + (x17 + (x14 + (x12 + x11))));
x30 = (x22 + (x18 + (x15 + (x13 + x1))));
x31 = (x23 + (x19 + (x16 + (x5 + x2))));
x32 = (x24 + (x20 + (x8 + (x6 + x3))));
x33 = (x27 + x32);
x34 = (uint64_t)(x33 >> 51);
x35 = (uint64_t)(x33 & UINT64_C(0x7ffffffffffff));
x36 = (x34 + x31);
x37 = (uint64_t)(x36 >> 51);
x38 = (uint64_t)(x36 & UINT64_C(0x7ffffffffffff));
x39 = (x37 + x30);
x40 = (uint64_t)(x39 >> 51);
x41 = (uint64_t)(x39 & UINT64_C(0x7ffffffffffff));
x42 = (x40 + x29);
x43 = (uint64_t)(x42 >> 51);
x44 = (uint64_t)(x42 & UINT64_C(0x7ffffffffffff));
x45 = (x43 * UINT8_C(0x13));
x46 = (x28 + x45);
x47 = (x46 >> 51);
x48 = (x46 & UINT64_C(0x7ffffffffffff));
x49 = (x47 + x35);
x50 = (fiat_25519_uint1)(x49 >> 51);
x51 = (x49 & UINT64_C(0x7ffffffffffff));
x52 = (x50 + x38);
out1[0] = x48;
out1[1] = x51;
out1[2] = x52;
out1[3] = x41;
out1[4] = x44;
}
/*
* The function fiat_25519_carry_square squares a field element and reduces the result.
*
* Postconditions:
* eval out1 mod m = (eval arg1 * eval arg1) mod m
*
*/
static void fiat_25519_carry_square(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
uint64_t x6;
uint64_t x7;
uint64_t x8;
fiat_25519_uint128 x9;
fiat_25519_uint128 x10;
fiat_25519_uint128 x11;
fiat_25519_uint128 x12;
fiat_25519_uint128 x13;
fiat_25519_uint128 x14;
fiat_25519_uint128 x15;
fiat_25519_uint128 x16;
fiat_25519_uint128 x17;
fiat_25519_uint128 x18;
fiat_25519_uint128 x19;
fiat_25519_uint128 x20;
fiat_25519_uint128 x21;
fiat_25519_uint128 x22;
fiat_25519_uint128 x23;
fiat_25519_uint128 x24;
uint64_t x25;
uint64_t x26;
fiat_25519_uint128 x27;
fiat_25519_uint128 x28;
fiat_25519_uint128 x29;
fiat_25519_uint128 x30;
fiat_25519_uint128 x31;
uint64_t x32;
uint64_t x33;
fiat_25519_uint128 x34;
uint64_t x35;
uint64_t x36;
fiat_25519_uint128 x37;
uint64_t x38;
uint64_t x39;
fiat_25519_uint128 x40;
uint64_t x41;
uint64_t x42;
uint64_t x43;
uint64_t x44;
uint64_t x45;
uint64_t x46;
uint64_t x47;
fiat_25519_uint1 x48;
uint64_t x49;
uint64_t x50;
x1 = ((arg1[4]) * UINT8_C(0x13));
x2 = (x1 * 0x2);
x3 = ((arg1[4]) * 0x2);
x4 = ((arg1[3]) * UINT8_C(0x13));
x5 = (x4 * 0x2);
x6 = ((arg1[3]) * 0x2);
x7 = ((arg1[2]) * 0x2);
x8 = ((arg1[1]) * 0x2);
x9 = ((fiat_25519_uint128)(arg1[4]) * x1);
x10 = ((fiat_25519_uint128)(arg1[3]) * x2);
x11 = ((fiat_25519_uint128)(arg1[3]) * x4);
x12 = ((fiat_25519_uint128)(arg1[2]) * x2);
x13 = ((fiat_25519_uint128)(arg1[2]) * x5);
x14 = ((fiat_25519_uint128)(arg1[2]) * (arg1[2]));
x15 = ((fiat_25519_uint128)(arg1[1]) * x2);
x16 = ((fiat_25519_uint128)(arg1[1]) * x6);
x17 = ((fiat_25519_uint128)(arg1[1]) * x7);
x18 = ((fiat_25519_uint128)(arg1[1]) * (arg1[1]));
x19 = ((fiat_25519_uint128)(arg1[0]) * x3);
x20 = ((fiat_25519_uint128)(arg1[0]) * x6);
x21 = ((fiat_25519_uint128)(arg1[0]) * x7);
x22 = ((fiat_25519_uint128)(arg1[0]) * x8);
x23 = ((fiat_25519_uint128)(arg1[0]) * (arg1[0]));
x24 = (x23 + (x15 + x13));
x25 = (uint64_t)(x24 >> 51);
x26 = (uint64_t)(x24 & UINT64_C(0x7ffffffffffff));
x27 = (x19 + (x16 + x14));
x28 = (x20 + (x17 + x9));
x29 = (x21 + (x18 + x10));
x30 = (x22 + (x12 + x11));
x31 = (x25 + x30);
x32 = (uint64_t)(x31 >> 51);
x33 = (uint64_t)(x31 & UINT64_C(0x7ffffffffffff));
x34 = (x32 + x29);
x35 = (uint64_t)(x34 >> 51);
x36 = (uint64_t)(x34 & UINT64_C(0x7ffffffffffff));
x37 = (x35 + x28);
x38 = (uint64_t)(x37 >> 51);
x39 = (uint64_t)(x37 & UINT64_C(0x7ffffffffffff));
x40 = (x38 + x27);
x41 = (uint64_t)(x40 >> 51);
x42 = (uint64_t)(x40 & UINT64_C(0x7ffffffffffff));
x43 = (x41 * UINT8_C(0x13));
x44 = (x26 + x43);
x45 = (x44 >> 51);
x46 = (x44 & UINT64_C(0x7ffffffffffff));
x47 = (x45 + x33);
x48 = (fiat_25519_uint1)(x47 >> 51);
x49 = (x47 & UINT64_C(0x7ffffffffffff));
x50 = (x48 + x36);
out1[0] = x46;
out1[1] = x49;
out1[2] = x50;
out1[3] = x39;
out1[4] = x42;
}
/*
* The function fiat_25519_carry reduces a field element.
*
* Postconditions:
* eval out1 mod m = eval arg1 mod m
*
*/
static void fiat_25519_carry(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
uint64_t x6;
uint64_t x7;
uint64_t x8;
uint64_t x9;
uint64_t x10;
uint64_t x11;
uint64_t x12;
x1 = (arg1[0]);
x2 = ((x1 >> 51) + (arg1[1]));
x3 = ((x2 >> 51) + (arg1[2]));
x4 = ((x3 >> 51) + (arg1[3]));
x5 = ((x4 >> 51) + (arg1[4]));
x6 = ((x1 & UINT64_C(0x7ffffffffffff)) + ((x5 >> 51) * UINT8_C(0x13)));
x7 = ((fiat_25519_uint1)(x6 >> 51) + (x2 & UINT64_C(0x7ffffffffffff)));
x8 = (x6 & UINT64_C(0x7ffffffffffff));
x9 = (x7 & UINT64_C(0x7ffffffffffff));
x10 = ((fiat_25519_uint1)(x7 >> 51) + (x3 & UINT64_C(0x7ffffffffffff)));
x11 = (x4 & UINT64_C(0x7ffffffffffff));
x12 = (x5 & UINT64_C(0x7ffffffffffff));
out1[0] = x8;
out1[1] = x9;
out1[2] = x10;
out1[3] = x11;
out1[4] = x12;
}
/*
* The function fiat_25519_add adds two field elements.
*
* Postconditions:
* eval out1 mod m = (eval arg1 + eval arg2) mod m
*
*/
static void fiat_25519_add(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1, const fiat_25519_tight_field_element arg2) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
x1 = ((arg1[0]) + (arg2[0]));
x2 = ((arg1[1]) + (arg2[1]));
x3 = ((arg1[2]) + (arg2[2]));
x4 = ((arg1[3]) + (arg2[3]));
x5 = ((arg1[4]) + (arg2[4]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
}
/*
* The function fiat_25519_sub subtracts two field elements.
*
* Postconditions:
* eval out1 mod m = (eval arg1 - eval arg2) mod m
*
*/
static void fiat_25519_sub(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1, const fiat_25519_tight_field_element arg2) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
x1 = ((UINT64_C(0xfffffffffffda) + (arg1[0])) - (arg2[0]));
x2 = ((UINT64_C(0xffffffffffffe) + (arg1[1])) - (arg2[1]));
x3 = ((UINT64_C(0xffffffffffffe) + (arg1[2])) - (arg2[2]));
x4 = ((UINT64_C(0xffffffffffffe) + (arg1[3])) - (arg2[3]));
x5 = ((UINT64_C(0xffffffffffffe) + (arg1[4])) - (arg2[4]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
}
/*
* The function fiat_25519_opp negates a field element.
*
* Postconditions:
* eval out1 mod m = -eval arg1 mod m
*
*/
static void fiat_25519_opp(fiat_25519_loose_field_element out1, const fiat_25519_tight_field_element arg1) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
x1 = (UINT64_C(0xfffffffffffda) - (arg1[0]));
x2 = (UINT64_C(0xffffffffffffe) - (arg1[1]));
x3 = (UINT64_C(0xffffffffffffe) - (arg1[2]));
x4 = (UINT64_C(0xffffffffffffe) - (arg1[3]));
x5 = (UINT64_C(0xffffffffffffe) - (arg1[4]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
}
/*
* The function fiat_25519_selectznz is a multi-limb conditional select.
*
* Postconditions:
* out1 = (if arg1 = 0 then arg2 else arg3)
*
* Input Bounds:
* arg1: [0x0 ~> 0x1]
* arg2: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
* arg3: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
* Output Bounds:
* out1: [[0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff], [0x0 ~> 0xffffffffffffffff]]
*/
static void fiat_25519_selectznz(uint64_t out1[5], fiat_25519_uint1 arg1, const uint64_t arg2[5], const uint64_t arg3[5]) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
fiat_25519_cmovznz_u64(&x1, arg1, (arg2[0]), (arg3[0]));
fiat_25519_cmovznz_u64(&x2, arg1, (arg2[1]), (arg3[1]));
fiat_25519_cmovznz_u64(&x3, arg1, (arg2[2]), (arg3[2]));
fiat_25519_cmovznz_u64(&x4, arg1, (arg2[3]), (arg3[3]));
fiat_25519_cmovznz_u64(&x5, arg1, (arg2[4]), (arg3[4]));
out1[0] = x1;
out1[1] = x2;
out1[2] = x3;
out1[3] = x4;
out1[4] = x5;
}
/*
* The function fiat_25519_to_bytes serializes a field element to bytes in little-endian order.
*
* Postconditions:
* out1 = map (λ x, ⌊((eval arg1 mod m) mod 2^(8 * (x + 1))) / 2^(8 * x)⌋) [0..31]
*
* Output Bounds:
* out1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
*/
static void fiat_25519_to_bytes(uint8_t out1[32], const fiat_25519_tight_field_element arg1) {
uint64_t x1;
fiat_25519_uint1 x2;
uint64_t x3;
fiat_25519_uint1 x4;
uint64_t x5;
fiat_25519_uint1 x6;
uint64_t x7;
fiat_25519_uint1 x8;
uint64_t x9;
fiat_25519_uint1 x10;
uint64_t x11;
uint64_t x12;
fiat_25519_uint1 x13;
uint64_t x14;
fiat_25519_uint1 x15;
uint64_t x16;
fiat_25519_uint1 x17;
uint64_t x18;
fiat_25519_uint1 x19;
uint64_t x20;
fiat_25519_uint1 x21;
uint64_t x22;
uint64_t x23;
uint64_t x24;
uint64_t x25;
uint8_t x26;
uint64_t x27;
uint8_t x28;
uint64_t x29;
uint8_t x30;
uint64_t x31;
uint8_t x32;
uint64_t x33;
uint8_t x34;
uint64_t x35;
uint8_t x36;
uint8_t x37;
uint64_t x38;
uint8_t x39;
uint64_t x40;
uint8_t x41;
uint64_t x42;
uint8_t x43;
uint64_t x44;
uint8_t x45;
uint64_t x46;
uint8_t x47;
uint64_t x48;
uint8_t x49;
uint8_t x50;
uint64_t x51;
uint8_t x52;
uint64_t x53;
uint8_t x54;
uint64_t x55;
uint8_t x56;
uint64_t x57;
uint8_t x58;
uint64_t x59;
uint8_t x60;
uint64_t x61;
uint8_t x62;
uint64_t x63;
uint8_t x64;
fiat_25519_uint1 x65;
uint64_t x66;
uint8_t x67;
uint64_t x68;
uint8_t x69;
uint64_t x70;
uint8_t x71;
uint64_t x72;
uint8_t x73;
uint64_t x74;
uint8_t x75;
uint64_t x76;
uint8_t x77;
uint8_t x78;
uint64_t x79;
uint8_t x80;
uint64_t x81;
uint8_t x82;
uint64_t x83;
uint8_t x84;
uint64_t x85;
uint8_t x86;
uint64_t x87;
uint8_t x88;
uint64_t x89;
uint8_t x90;
uint8_t x91;
fiat_25519_subborrowx_u51(&x1, &x2, 0x0, (arg1[0]), UINT64_C(0x7ffffffffffed));
fiat_25519_subborrowx_u51(&x3, &x4, x2, (arg1[1]), UINT64_C(0x7ffffffffffff));
fiat_25519_subborrowx_u51(&x5, &x6, x4, (arg1[2]), UINT64_C(0x7ffffffffffff));
fiat_25519_subborrowx_u51(&x7, &x8, x6, (arg1[3]), UINT64_C(0x7ffffffffffff));
fiat_25519_subborrowx_u51(&x9, &x10, x8, (arg1[4]), UINT64_C(0x7ffffffffffff));
fiat_25519_cmovznz_u64(&x11, x10, 0x0, UINT64_C(0xffffffffffffffff));
fiat_25519_addcarryx_u51(&x12, &x13, 0x0, x1, (x11 & UINT64_C(0x7ffffffffffed)));
fiat_25519_addcarryx_u51(&x14, &x15, x13, x3, (x11 & UINT64_C(0x7ffffffffffff)));
fiat_25519_addcarryx_u51(&x16, &x17, x15, x5, (x11 & UINT64_C(0x7ffffffffffff)));
fiat_25519_addcarryx_u51(&x18, &x19, x17, x7, (x11 & UINT64_C(0x7ffffffffffff)));
fiat_25519_addcarryx_u51(&x20, &x21, x19, x9, (x11 & UINT64_C(0x7ffffffffffff)));
x22 = (x20 << 4);
x23 = (x18 * (uint64_t)0x2);
x24 = (x16 << 6);
x25 = (x14 << 3);
x26 = (uint8_t)(x12 & UINT8_C(0xff));
x27 = (x12 >> 8);
x28 = (uint8_t)(x27 & UINT8_C(0xff));
x29 = (x27 >> 8);
x30 = (uint8_t)(x29 & UINT8_C(0xff));
x31 = (x29 >> 8);
x32 = (uint8_t)(x31 & UINT8_C(0xff));
x33 = (x31 >> 8);
x34 = (uint8_t)(x33 & UINT8_C(0xff));
x35 = (x33 >> 8);
x36 = (uint8_t)(x35 & UINT8_C(0xff));
x37 = (uint8_t)(x35 >> 8);
x38 = (x25 + (uint64_t)x37);
x39 = (uint8_t)(x38 & UINT8_C(0xff));
x40 = (x38 >> 8);
x41 = (uint8_t)(x40 & UINT8_C(0xff));
x42 = (x40 >> 8);
x43 = (uint8_t)(x42 & UINT8_C(0xff));
x44 = (x42 >> 8);
x45 = (uint8_t)(x44 & UINT8_C(0xff));
x46 = (x44 >> 8);
x47 = (uint8_t)(x46 & UINT8_C(0xff));
x48 = (x46 >> 8);
x49 = (uint8_t)(x48 & UINT8_C(0xff));
x50 = (uint8_t)(x48 >> 8);
x51 = (x24 + (uint64_t)x50);
x52 = (uint8_t)(x51 & UINT8_C(0xff));
x53 = (x51 >> 8);
x54 = (uint8_t)(x53 & UINT8_C(0xff));
x55 = (x53 >> 8);
x56 = (uint8_t)(x55 & UINT8_C(0xff));
x57 = (x55 >> 8);
x58 = (uint8_t)(x57 & UINT8_C(0xff));
x59 = (x57 >> 8);
x60 = (uint8_t)(x59 & UINT8_C(0xff));
x61 = (x59 >> 8);
x62 = (uint8_t)(x61 & UINT8_C(0xff));
x63 = (x61 >> 8);
x64 = (uint8_t)(x63 & UINT8_C(0xff));
x65 = (fiat_25519_uint1)(x63 >> 8);
x66 = (x23 + (uint64_t)x65);
x67 = (uint8_t)(x66 & UINT8_C(0xff));
x68 = (x66 >> 8);
x69 = (uint8_t)(x68 & UINT8_C(0xff));
x70 = (x68 >> 8);
x71 = (uint8_t)(x70 & UINT8_C(0xff));
x72 = (x70 >> 8);
x73 = (uint8_t)(x72 & UINT8_C(0xff));
x74 = (x72 >> 8);
x75 = (uint8_t)(x74 & UINT8_C(0xff));
x76 = (x74 >> 8);
x77 = (uint8_t)(x76 & UINT8_C(0xff));
x78 = (uint8_t)(x76 >> 8);
x79 = (x22 + (uint64_t)x78);
x80 = (uint8_t)(x79 & UINT8_C(0xff));
x81 = (x79 >> 8);
x82 = (uint8_t)(x81 & UINT8_C(0xff));
x83 = (x81 >> 8);
x84 = (uint8_t)(x83 & UINT8_C(0xff));
x85 = (x83 >> 8);
x86 = (uint8_t)(x85 & UINT8_C(0xff));
x87 = (x85 >> 8);
x88 = (uint8_t)(x87 & UINT8_C(0xff));
x89 = (x87 >> 8);
x90 = (uint8_t)(x89 & UINT8_C(0xff));
x91 = (uint8_t)(x89 >> 8);
out1[0] = x26;
out1[1] = x28;
out1[2] = x30;
out1[3] = x32;
out1[4] = x34;
out1[5] = x36;
out1[6] = x39;
out1[7] = x41;
out1[8] = x43;
out1[9] = x45;
out1[10] = x47;
out1[11] = x49;
out1[12] = x52;
out1[13] = x54;
out1[14] = x56;
out1[15] = x58;
out1[16] = x60;
out1[17] = x62;
out1[18] = x64;
out1[19] = x67;
out1[20] = x69;
out1[21] = x71;
out1[22] = x73;
out1[23] = x75;
out1[24] = x77;
out1[25] = x80;
out1[26] = x82;
out1[27] = x84;
out1[28] = x86;
out1[29] = x88;
out1[30] = x90;
out1[31] = x91;
}
/*
* The function fiat_25519_from_bytes deserializes a field element from bytes in little-endian order.
*
* Postconditions:
* eval out1 mod m = bytes_eval arg1 mod m
*
* Input Bounds:
* arg1: [[0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0xff], [0x0 ~> 0x7f]]
*/
static void fiat_25519_from_bytes(fiat_25519_tight_field_element out1, const uint8_t arg1[32]) {
uint64_t x1;
uint64_t x2;
uint64_t x3;
uint64_t x4;
uint64_t x5;
uint64_t x6;
uint64_t x7;
uint64_t x8;
uint64_t x9;
uint64_t x10;
uint64_t x11;
uint64_t x12;
uint64_t x13;
uint64_t x14;
uint64_t x15;
uint64_t x16;
uint64_t x17;
uint64_t x18;
uint64_t x19;
uint64_t x20;
uint64_t x21;
uint64_t x22;
uint64_t x23;
uint64_t x24;
uint64_t x25;
uint64_t x26;
uint64_t x27;
uint64_t x28;
uint64_t x29;
uint64_t x30;
uint64_t x31;
uint8_t x32;
uint64_t x33;
uint64_t x34;
uint64_t x35;
uint64_t x36;
uint64_t x37;
uint64_t x38;
uint64_t x39;
uint8_t x40;
uint64_t x41;
uint64_t x42;
uint64_t x43;
uint64_t x44;
uint64_t x45;
uint64_t x46;
uint64_t x47;
uint8_t x48;
uint64_t x49;
uint64_t x50;
uint64_t x51;
uint64_t x52;
uint64_t x53;
uint64_t x54;
uint64_t x55;
uint64_t x56;
uint8_t x57;
uint64_t x58;
uint64_t x59;
uint64_t x60;
uint64_t x61;
uint64_t x62;
uint64_t x63;
uint64_t x64;
uint8_t x65;
uint64_t x66;
uint64_t x67;
uint64_t x68;
uint64_t x69;
uint64_t x70;
uint64_t x71;
x1 = ((uint64_t)(arg1[31]) << 44);
x2 = ((uint64_t)(arg1[30]) << 36);
x3 = ((uint64_t)(arg1[29]) << 28);
x4 = ((uint64_t)(arg1[28]) << 20);
x5 = ((uint64_t)(arg1[27]) << 12);
x6 = ((uint64_t)(arg1[26]) << 4);
x7 = ((uint64_t)(arg1[25]) << 47);
x8 = ((uint64_t)(arg1[24]) << 39);
x9 = ((uint64_t)(arg1[23]) << 31);
x10 = ((uint64_t)(arg1[22]) << 23);
x11 = ((uint64_t)(arg1[21]) << 15);
x12 = ((uint64_t)(arg1[20]) << 7);
x13 = ((uint64_t)(arg1[19]) << 50);
x14 = ((uint64_t)(arg1[18]) << 42);
x15 = ((uint64_t)(arg1[17]) << 34);
x16 = ((uint64_t)(arg1[16]) << 26);
x17 = ((uint64_t)(arg1[15]) << 18);
x18 = ((uint64_t)(arg1[14]) << 10);
x19 = ((uint64_t)(arg1[13]) << 2);
x20 = ((uint64_t)(arg1[12]) << 45);
x21 = ((uint64_t)(arg1[11]) << 37);
x22 = ((uint64_t)(arg1[10]) << 29);
x23 = ((uint64_t)(arg1[9]) << 21);
x24 = ((uint64_t)(arg1[8]) << 13);
x25 = ((uint64_t)(arg1[7]) << 5);
x26 = ((uint64_t)(arg1[6]) << 48);
x27 = ((uint64_t)(arg1[5]) << 40);
x28 = ((uint64_t)(arg1[4]) << 32);
x29 = ((uint64_t)(arg1[3]) << 24);
x30 = ((uint64_t)(arg1[2]) << 16);
x31 = ((uint64_t)(arg1[1]) << 8);
x32 = (arg1[0]);
x33 = (x31 + (uint64_t)x32);
x34 = (x30 + x33);
x35 = (x29 + x34);
x36 = (x28 + x35);
x37 = (x27 + x36);
x38 = (x26 + x37);
x39 = (x38 & UINT64_C(0x7ffffffffffff));
x40 = (uint8_t)(x38 >> 51);
x41 = (x25 + (uint64_t)x40);
x42 = (x24 + x41);
x43 = (x23 + x42);
x44 = (x22 + x43);
x45 = (x21 + x44);
x46 = (x20 + x45);
x47 = (x46 & UINT64_C(0x7ffffffffffff));
x48 = (uint8_t)(x46 >> 51);
x49 = (x19 + (uint64_t)x48);
x50 = (x18 + x49);
x51 = (x17 + x50);
x52 = (x16 + x51);
x53 = (x15 + x52);
x54 = (x14 + x53);
x55 = (x13 + x54);
x56 = (x55 & UINT64_C(0x7ffffffffffff));
x57 = (uint8_t)(x55 >> 51);
x58 = (x12 + (uint64_t)x57);
x59 = (x11 + x58);
x60 = (x10 + x59);
x61 = (x9 + x60);
x62 = (x8 + x61);
x63 = (x7 + x62);
x64 = (x63 & UINT64_C(0x7ffffffffffff));
x65 = (uint8_t)(x63 >> 51);
x66 = (x6 + (uint64_t)x65);
x67 = (x5 + x66);
x68 = (x4 + x67);
x69 = (x3 + x68);
x70 = (x2 + x69);
x71 = (x1 + x70);
out1[0] = x39;
out1[1] = x47;
out1[2] = x56;
out1[3] = x64;
out1[4] = x71;
}
/*
* The function fiat_25519_carry_scmul_121666 multiplies a field element by 121666 and reduces the result.
*
* Postconditions:
* eval out1 mod m = (121666 * eval arg1) mod m
*
*/
static void fiat_25519_carry_scmul_121666(fiat_25519_tight_field_element out1, const fiat_25519_loose_field_element arg1) {
fiat_25519_uint128 x1;
fiat_25519_uint128 x2;
fiat_25519_uint128 x3;
fiat_25519_uint128 x4;
fiat_25519_uint128 x5;
uint64_t x6;
uint64_t x7;
fiat_25519_uint128 x8;
uint64_t x9;
uint64_t x10;
fiat_25519_uint128 x11;
uint64_t x12;
uint64_t x13;
fiat_25519_uint128 x14;
uint64_t x15;
uint64_t x16;
fiat_25519_uint128 x17;
uint64_t x18;
uint64_t x19;
uint64_t x20;
uint64_t x21;
fiat_25519_uint1 x22;
uint64_t x23;
uint64_t x24;
fiat_25519_uint1 x25;
uint64_t x26;
uint64_t x27;
x1 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[4]));
x2 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[3]));
x3 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[2]));
x4 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[1]));
x5 = ((fiat_25519_uint128)UINT32_C(0x1db42) * (arg1[0]));
x6 = (uint64_t)(x5 >> 51);
x7 = (uint64_t)(x5 & UINT64_C(0x7ffffffffffff));
x8 = (x6 + x4);
x9 = (uint64_t)(x8 >> 51);
x10 = (uint64_t)(x8 & UINT64_C(0x7ffffffffffff));
x11 = (x9 + x3);
x12 = (uint64_t)(x11 >> 51);
x13 = (uint64_t)(x11 & UINT64_C(0x7ffffffffffff));
x14 = (x12 + x2);
x15 = (uint64_t)(x14 >> 51);
x16 = (uint64_t)(x14 & UINT64_C(0x7ffffffffffff));
x17 = (x15 + x1);
x18 = (uint64_t)(x17 >> 51);
x19 = (uint64_t)(x17 & UINT64_C(0x7ffffffffffff));
x20 = (x18 * UINT8_C(0x13));
x21 = (x7 + x20);
x22 = (fiat_25519_uint1)(x21 >> 51);
x23 = (x21 & UINT64_C(0x7ffffffffffff));
x24 = (x22 + x10);
x25 = (fiat_25519_uint1)(x24 >> 51);
x26 = (x24 & UINT64_C(0x7ffffffffffff));
x27 = (x25 + x13);
out1[0] = x23;
out1[1] = x26;
out1[2] = x27;
out1[3] = x16;
out1[4] = x19;
}
|
End of preview. Expand
in Data Studio
This is a dataset automatically generated from ocaml/opam:archive. You can find more information about this dataset in this blogpost.
- Downloads last month
- 283