From 6096592a2221ad2137fb2e307d062a5134328163 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Thu, 8 Feb 2018 18:59:43 +0100 Subject: [PATCH] Vendors: add `ocaml-bip39` --- vendors/ocaml-bip39/LICENSE.md | 13 + vendors/ocaml-bip39/bip39.opam | 22 + vendors/ocaml-bip39/gen/english.txt | 2048 ++++++++++++++++++++++++++ vendors/ocaml-bip39/src/bip39.ml | 140 ++ vendors/ocaml-bip39/src/bip39.mli | 56 + vendors/ocaml-bip39/src/generator.ml | 44 + vendors/ocaml-bip39/src/jbuild | 17 + vendors/ocaml-bip39/test/jbuild | 8 + vendors/ocaml-bip39/test/test.ml | 138 ++ 9 files changed, 2486 insertions(+) create mode 100644 vendors/ocaml-bip39/LICENSE.md create mode 100644 vendors/ocaml-bip39/bip39.opam create mode 100644 vendors/ocaml-bip39/gen/english.txt create mode 100644 vendors/ocaml-bip39/src/bip39.ml create mode 100644 vendors/ocaml-bip39/src/bip39.mli create mode 100644 vendors/ocaml-bip39/src/generator.ml create mode 100644 vendors/ocaml-bip39/src/jbuild create mode 100644 vendors/ocaml-bip39/test/jbuild create mode 100644 vendors/ocaml-bip39/test/test.ml diff --git a/vendors/ocaml-bip39/LICENSE.md b/vendors/ocaml-bip39/LICENSE.md new file mode 100644 index 000000000..52b5f8cd7 --- /dev/null +++ b/vendors/ocaml-bip39/LICENSE.md @@ -0,0 +1,13 @@ +Copyright (c) 2017 Vincent Bernardoff + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/vendors/ocaml-bip39/bip39.opam b/vendors/ocaml-bip39/bip39.opam new file mode 100644 index 000000000..a964beb45 --- /dev/null +++ b/vendors/ocaml-bip39/bip39.opam @@ -0,0 +1,22 @@ +opam-version: "1.2" +name: "bip39" +version: "1.0" +maintainer: "Vincent Bernardoff " +authors: ["Vincent Bernardoff "] +homepage: "https://github.com/vbmithr/ocaml-bip39" +doc: "https://vbmithr.github.io/ocaml-bip39/doc" +license: "ISC" +dev-repo: "https://github.com/vbmithr/ocaml-bip39.git" +bug-reports: "https://github.com/vbmithr/ocaml-bip39/issues" +tags: [] +available: [ ocaml-version >= "4.03.0" ] +build: [ "jbuilder" "build" "-j" jobs "-p" name "@install" ] +depends: [ + "jbuilder" {build & >= "1.0+beta17"} + "base" {build & >= "v0.10.0"} + "stdio" {build & >= "v0.10.0"} + "nocrypto" {>= "0.5.4"} + "pbkdf" {>= "0.2.0"} + "hex" { "test" } + "alcotest" { "test" } +] diff --git a/vendors/ocaml-bip39/gen/english.txt b/vendors/ocaml-bip39/gen/english.txt new file mode 100644 index 000000000..942040ed5 --- /dev/null +++ b/vendors/ocaml-bip39/gen/english.txt @@ -0,0 +1,2048 @@ +abandon +ability +able +about +above +absent +absorb +abstract +absurd +abuse +access +accident +account +accuse +achieve +acid +acoustic +acquire +across +act +action +actor +actress +actual +adapt +add +addict +address +adjust +admit +adult +advance +advice +aerobic +affair +afford +afraid +again +age +agent +agree +ahead +aim +air +airport +aisle +alarm +album +alcohol +alert +alien +all +alley +allow +almost +alone +alpha +already +also +alter +always +amateur +amazing +among +amount +amused +analyst +anchor +ancient +anger +angle +angry +animal +ankle +announce +annual +another +answer +antenna +antique +anxiety +any +apart +apology +appear +apple +approve +april +arch +arctic +area +arena +argue +arm +armed +armor +army +around +arrange +arrest +arrive +arrow +art +artefact +artist +artwork +ask +aspect +assault +asset +assist +assume +asthma +athlete +atom +attack +attend +attitude +attract +auction +audit +august +aunt +author +auto +autumn +average +avocado +avoid +awake +aware +away +awesome +awful +awkward +axis +baby +bachelor +bacon +badge +bag +balance +balcony +ball +bamboo +banana +banner +bar +barely +bargain +barrel +base +basic +basket +battle +beach +bean +beauty +because +become +beef +before +begin +behave +behind +believe +below +belt +bench +benefit +best +betray +better +between +beyond +bicycle +bid +bike +bind +biology +bird +birth +bitter +black +blade +blame +blanket +blast +bleak +bless +blind +blood +blossom +blouse +blue +blur +blush +board +boat +body +boil +bomb +bone +bonus +book +boost +border +boring +borrow +boss +bottom +bounce +box +boy +bracket +brain +brand +brass +brave +bread +breeze +brick +bridge +brief +bright +bring +brisk +broccoli +broken +bronze +broom +brother +brown +brush +bubble +buddy +budget +buffalo +build +bulb +bulk +bullet +bundle +bunker +burden +burger +burst +bus +business +busy +butter +buyer +buzz +cabbage +cabin +cable +cactus +cage +cake +call +calm +camera +camp +can +canal +cancel +candy +cannon +canoe +canvas +canyon +capable +capital +captain +car +carbon +card +cargo +carpet +carry +cart +case +cash +casino +castle +casual +cat +catalog +catch +category +cattle +caught +cause +caution +cave +ceiling +celery +cement +census +century +cereal +certain +chair +chalk +champion +change +chaos +chapter +charge +chase +chat +cheap +check +cheese +chef +cherry +chest +chicken +chief +child +chimney +choice +choose +chronic +chuckle +chunk +churn +cigar +cinnamon +circle +citizen +city +civil +claim +clap +clarify +claw +clay +clean +clerk +clever +click +client +cliff +climb +clinic +clip +clock +clog +close +cloth +cloud +clown +club +clump +cluster +clutch +coach +coast +coconut +code +coffee +coil +coin +collect +color +column +combine +come +comfort +comic +common +company +concert +conduct +confirm +congress +connect +consider +control +convince +cook +cool +copper +copy +coral +core +corn +correct +cost +cotton +couch +country +couple +course +cousin +cover +coyote +crack +cradle +craft +cram +crane +crash +crater +crawl +crazy +cream +credit +creek +crew +cricket +crime +crisp +critic +crop +cross +crouch +crowd +crucial +cruel +cruise +crumble +crunch +crush +cry +crystal +cube +culture +cup +cupboard +curious +current +curtain +curve +cushion +custom +cute +cycle +dad +damage +damp +dance +danger +daring +dash +daughter +dawn +day +deal +debate +debris +decade +december +decide +decline +decorate +decrease +deer +defense +define +defy +degree +delay +deliver +demand +demise +denial +dentist +deny +depart +depend +deposit +depth +deputy +derive +describe +desert +design +desk +despair +destroy +detail +detect +develop +device +devote +diagram +dial +diamond +diary +dice +diesel +diet +differ +digital +dignity +dilemma +dinner +dinosaur +direct +dirt +disagree +discover +disease +dish +dismiss +disorder +display +distance +divert +divide +divorce +dizzy +doctor +document +dog +doll +dolphin +domain +donate +donkey +donor +door +dose +double +dove +draft +dragon +drama +drastic +draw +dream +dress +drift +drill +drink +drip +drive +drop +drum +dry +duck +dumb +dune +during +dust +dutch +duty +dwarf +dynamic +eager +eagle +early +earn +earth +easily +east +easy +echo +ecology +economy +edge +edit +educate +effort +egg +eight +either +elbow +elder +electric +elegant +element +elephant +elevator +elite +else +embark +embody +embrace +emerge +emotion +employ +empower +empty +enable +enact +end +endless +endorse +enemy +energy +enforce +engage +engine +enhance +enjoy +enlist +enough +enrich +enroll +ensure +enter +entire +entry +envelope +episode +equal +equip +era +erase +erode +erosion +error +erupt +escape +essay +essence +estate +eternal +ethics +evidence +evil +evoke +evolve +exact +example +excess +exchange +excite +exclude +excuse +execute +exercise +exhaust +exhibit +exile +exist +exit +exotic +expand +expect +expire +explain +expose +express +extend +extra +eye +eyebrow +fabric +face +faculty +fade +faint +faith +fall +false +fame +family +famous +fan +fancy +fantasy +farm +fashion +fat +fatal +father +fatigue +fault +favorite +feature +february +federal +fee +feed +feel +female +fence +festival +fetch +fever +few +fiber +fiction +field +figure +file +film +filter +final +find +fine +finger +finish +fire +firm +first +fiscal +fish +fit +fitness +fix +flag +flame +flash +flat +flavor +flee +flight +flip +float +flock +floor +flower +fluid +flush +fly +foam +focus +fog +foil +fold +follow +food +foot +force +forest +forget +fork +fortune +forum +forward +fossil +foster +found +fox +fragile +frame +frequent +fresh +friend +fringe +frog +front +frost +frown +frozen +fruit +fuel +fun +funny +furnace +fury +future +gadget +gain +galaxy +gallery +game +gap +garage +garbage +garden +garlic +garment +gas +gasp +gate +gather +gauge +gaze +general +genius +genre +gentle +genuine +gesture +ghost +giant +gift +giggle +ginger +giraffe +girl +give +glad +glance +glare +glass +glide +glimpse +globe +gloom +glory +glove +glow +glue +goat +goddess +gold +good +goose +gorilla +gospel +gossip +govern +gown +grab +grace +grain +grant +grape +grass +gravity +great +green +grid +grief +grit +grocery +group +grow +grunt +guard +guess +guide +guilt +guitar +gun +gym +habit +hair +half +hammer +hamster +hand +happy +harbor +hard +harsh +harvest +hat +have +hawk +hazard +head +health +heart +heavy +hedgehog +height +hello +helmet +help +hen +hero +hidden +high +hill +hint +hip +hire +history +hobby +hockey +hold +hole +holiday +hollow +home +honey +hood +hope +horn +horror +horse +hospital +host +hotel +hour +hover +hub +huge +human +humble +humor +hundred +hungry +hunt +hurdle +hurry +hurt +husband +hybrid +ice +icon +idea +identify +idle +ignore +ill +illegal +illness +image +imitate +immense +immune +impact +impose +improve +impulse +inch +include +income +increase +index +indicate +indoor +industry +infant +inflict +inform +inhale +inherit +initial +inject +injury +inmate +inner +innocent +input +inquiry +insane +insect +inside +inspire +install +intact +interest +into +invest +invite +involve +iron +island +isolate +issue +item +ivory +jacket +jaguar +jar +jazz +jealous +jeans +jelly +jewel +job +join +joke +journey +joy +judge +juice +jump +jungle +junior +junk +just +kangaroo +keen +keep +ketchup +key +kick +kid +kidney +kind +kingdom +kiss +kit +kitchen +kite +kitten +kiwi +knee +knife +knock +know +lab +label +labor +ladder +lady +lake +lamp +language +laptop +large +later +latin +laugh +laundry +lava +law +lawn +lawsuit +layer +lazy +leader +leaf +learn +leave +lecture +left +leg +legal +legend +leisure +lemon +lend +length +lens +leopard +lesson +letter +level +liar +liberty +library +license +life +lift +light +like +limb +limit +link +lion +liquid +list +little +live +lizard +load +loan +lobster +local +lock +logic +lonely +long +loop +lottery +loud +lounge +love +loyal +lucky +luggage +lumber +lunar +lunch +luxury +lyrics +machine +mad +magic +magnet +maid +mail +main +major +make +mammal +man +manage +mandate +mango +mansion +manual +maple +marble +march +margin +marine +market +marriage +mask +mass +master +match +material +math +matrix +matter +maximum +maze +meadow +mean +measure +meat +mechanic +medal +media +melody +melt +member +memory +mention +menu +mercy +merge +merit +merry +mesh +message +metal +method +middle +midnight +milk +million +mimic +mind +minimum +minor +minute +miracle +mirror +misery +miss +mistake +mix +mixed +mixture +mobile +model +modify +mom +moment +monitor +monkey +monster +month +moon +moral +more +morning +mosquito +mother +motion +motor +mountain +mouse +move +movie +much +muffin +mule +multiply +muscle +museum +mushroom +music +must +mutual +myself +mystery +myth +naive +name +napkin +narrow +nasty +nation +nature +near +neck +need +negative +neglect +neither +nephew +nerve +nest +net +network +neutral +never +news +next +nice +night +noble +noise +nominee +noodle +normal +north +nose +notable +note +nothing +notice +novel +now +nuclear +number +nurse +nut +oak +obey +object +oblige +obscure +observe +obtain +obvious +occur +ocean +october +odor +off +offer +office +often +oil +okay +old +olive +olympic +omit +once +one +onion +online +only +open +opera +opinion +oppose +option +orange +orbit +orchard +order +ordinary +organ +orient +original +orphan +ostrich +other +outdoor +outer +output +outside +oval +oven +over +own +owner +oxygen +oyster +ozone +pact +paddle +page +pair +palace +palm +panda +panel +panic +panther +paper +parade +parent +park +parrot +party +pass +patch +path +patient +patrol +pattern +pause +pave +payment +peace +peanut +pear +peasant +pelican +pen +penalty +pencil +people +pepper +perfect +permit +person +pet +phone +photo +phrase +physical +piano +picnic +picture +piece +pig +pigeon +pill +pilot +pink +pioneer +pipe +pistol +pitch +pizza +place +planet +plastic +plate +play +please +pledge +pluck +plug +plunge +poem +poet +point +polar +pole +police +pond +pony +pool +popular +portion +position +possible +post +potato +pottery +poverty +powder +power +practice +praise +predict +prefer +prepare +present +pretty +prevent +price +pride +primary +print +priority +prison +private +prize +problem +process +produce +profit +program +project +promote +proof +property +prosper +protect +proud +provide +public +pudding +pull +pulp +pulse +pumpkin +punch +pupil +puppy +purchase +purity +purpose +purse +push +put +puzzle +pyramid +quality +quantum +quarter +question +quick +quit +quiz +quote +rabbit +raccoon +race +rack +radar +radio +rail +rain +raise +rally +ramp +ranch +random +range +rapid +rare +rate +rather +raven +raw +razor +ready +real +reason +rebel +rebuild +recall +receive +recipe +record +recycle +reduce +reflect +reform +refuse +region +regret +regular +reject +relax +release +relief +rely +remain +remember +remind +remove +render +renew +rent +reopen +repair +repeat +replace +report +require +rescue +resemble +resist +resource +response +result +retire +retreat +return +reunion +reveal +review +reward +rhythm +rib +ribbon +rice +rich +ride +ridge +rifle +right +rigid +ring +riot +ripple +risk +ritual +rival +river +road +roast +robot +robust +rocket +romance +roof +rookie +room +rose +rotate +rough +round +route +royal +rubber +rude +rug +rule +run +runway +rural +sad +saddle +sadness +safe +sail +salad +salmon +salon +salt +salute +same +sample +sand +satisfy +satoshi +sauce +sausage +save +say +scale +scan +scare +scatter +scene +scheme +school +science +scissors +scorpion +scout +scrap +screen +script +scrub +sea +search +season +seat +second +secret +section +security +seed +seek +segment +select +sell +seminar +senior +sense +sentence +series +service +session +settle +setup +seven +shadow +shaft +shallow +share +shed +shell +sheriff +shield +shift +shine +ship +shiver +shock +shoe +shoot +shop +short +shoulder +shove +shrimp +shrug +shuffle +shy +sibling +sick +side +siege +sight +sign +silent +silk +silly +silver +similar +simple +since +sing +siren +sister +situate +six +size +skate +sketch +ski +skill +skin +skirt +skull +slab +slam +sleep +slender +slice +slide +slight +slim +slogan +slot +slow +slush +small +smart +smile +smoke +smooth +snack +snake +snap +sniff +snow +soap +soccer +social +sock +soda +soft +solar +soldier +solid +solution +solve +someone +song +soon +sorry +sort +soul +sound +soup +source +south +space +spare +spatial +spawn +speak +special +speed +spell +spend +sphere +spice +spider +spike +spin +spirit +split +spoil +sponsor +spoon +sport +spot +spray +spread +spring +spy +square +squeeze +squirrel +stable +stadium +staff +stage +stairs +stamp +stand +start +state +stay +steak +steel +stem +step +stereo +stick +still +sting +stock +stomach +stone +stool +story +stove +strategy +street +strike +strong +struggle +student +stuff +stumble +style +subject +submit +subway +success +such +sudden +suffer +sugar +suggest +suit +summer +sun +sunny +sunset +super +supply +supreme +sure +surface +surge +surprise +surround +survey +suspect +sustain +swallow +swamp +swap +swarm +swear +sweet +swift +swim +swing +switch +sword +symbol +symptom +syrup +system +table +tackle +tag +tail +talent +talk +tank +tape +target +task +taste +tattoo +taxi +teach +team +tell +ten +tenant +tennis +tent +term +test +text +thank +that +theme +then +theory +there +they +thing +this +thought +three +thrive +throw +thumb +thunder +ticket +tide +tiger +tilt +timber +time +tiny +tip +tired +tissue +title +toast +tobacco +today +toddler +toe +together +toilet +token +tomato +tomorrow +tone +tongue +tonight +tool +tooth +top +topic +topple +torch +tornado +tortoise +toss +total +tourist +toward +tower +town +toy +track +trade +traffic +tragic +train +transfer +trap +trash +travel +tray +treat +tree +trend +trial +tribe +trick +trigger +trim +trip +trophy +trouble +truck +true +truly +trumpet +trust +truth +try +tube +tuition +tumble +tuna +tunnel +turkey +turn +turtle +twelve +twenty +twice +twin +twist +two +type +typical +ugly +umbrella +unable +unaware +uncle +uncover +under +undo +unfair +unfold +unhappy +uniform +unique +unit +universe +unknown +unlock +until +unusual +unveil +update +upgrade +uphold +upon +upper +upset +urban +urge +usage +use +used +useful +useless +usual +utility +vacant +vacuum +vague +valid +valley +valve +van +vanish +vapor +various +vast +vault +vehicle +velvet +vendor +venture +venue +verb +verify +version +very +vessel +veteran +viable +vibrant +vicious +victory +video +view +village +vintage +violin +virtual +virus +visa +visit +visual +vital +vivid +vocal +voice +void +volcano +volume +vote +voyage +wage +wagon +wait +walk +wall +walnut +want +warfare +warm +warrior +wash +wasp +waste +water +wave +way +wealth +weapon +wear +weasel +weather +web +wedding +weekend +weird +welcome +west +wet +whale +what +wheat +wheel +when +where +whip +whisper +wide +width +wife +wild +will +win +window +wine +wing +wink +winner +winter +wire +wisdom +wise +wish +witness +wolf +woman +wonder +wood +wool +word +work +world +worry +worth +wrap +wreck +wrestle +wrist +write +wrong +yard +year +yellow +you +young +youth +zebra +zero +zone +zoo diff --git a/vendors/ocaml-bip39/src/bip39.ml b/vendors/ocaml-bip39/src/bip39.ml new file mode 100644 index 000000000..d7a4ae44d --- /dev/null +++ b/vendors/ocaml-bip39/src/bip39.ml @@ -0,0 +1,140 @@ +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff. All rights reserved. + Distributed under the ISC license, see terms at the end of the file. + ---------------------------------------------------------------------------*) + +open StdLabels + +let acceptable_num_words = [12 ; 15 ; 18 ; 21 ; 24] + +type entropy = { + bytes : Cstruct.t ; + length : int ; + digest_length : int ; + num_words : int ; +} + +let entropy_of_bytes bytes = + match Cstruct.len bytes with + | 16 -> Some { bytes ; length = 16 ; digest_length = 4 ; num_words = 12 } + | 20 -> Some { bytes ; length = 20 ; digest_length = 5 ; num_words = 15 } + | 24 -> Some { bytes ; length = 24 ; digest_length = 6 ; num_words = 18 } + | 28 -> Some { bytes ; length = 28 ; digest_length = 7 ; num_words = 21 } + | 32 -> Some { bytes ; length = 32 ; digest_length = 8 ; num_words = 24 } + | _ -> None + +type t = int list + +let index_of_word word = + let index = ref (-1) in + try + List.iteri English.words ~f:begin fun i w -> + if String.compare word w = 0 then (index := i ; raise Exit) + end ; + None + with Exit -> Some !index + +let of_words words = + try + List.fold_right words ~init:(0, []) ~f:begin fun word (count, acc) -> + match index_of_word word with + | Some i -> (succ count, i :: acc) + | _ -> raise Exit + end |> fun (count, x) -> + if List.(mem count ~set:acceptable_num_words) then Some x + else None + with Exit -> None + +let of_indices idxs = + try + List.fold_right idxs ~init:(0, []) ~f:begin fun i (count, acc) -> + if i < 0 || i > 2047 then raise Exit + else (succ count, i :: acc) + end |> fun (count, x) -> + if List.(mem count ~set:acceptable_num_words) then Some x + else None + with Exit -> None + +let to_words = List.map ~f:(List.nth English.words) +let to_indices t = t + +let pp ppf t = + let open Format in + let words = to_words t in + let pp_mnemonic = + pp_print_list + ~pp_sep:(fun fmt () -> fprintf fmt " ") + pp_print_string in + fprintf ppf "%a" pp_mnemonic words + +let show t = + Format.asprintf "%a" pp t + +let int_of_bits bits = + snd @@ List.fold_right bits ~init:(0, 0) ~f:begin fun b (i, res) -> + succ i, if b then res lor (1 lsl i) else res + end + +let bits_of_char c = + let b = Char.code c in + let res = ref [] in + for i = 0 to 7 do + res := (b land (1 lsl i) <> 0) :: !res + done ; + !res + +let bits_of_bytes bytes = + let acc = ref [] in + String.iter bytes ~f:begin fun c -> + acc := List.rev_append (bits_of_char c) !acc + end ; + List.rev !acc + +let list_sub l n = + let rec inner acc n l = + if n > 0 then match l with + | h :: tl -> inner (h :: acc) (pred n) tl + | _ -> invalid_arg "Bip39.list_sub" + else List.rev acc + in inner [] n l + +let pack l pack_len = + let rec inner (sub_acc_len, sub_acc, acc) = function + | [] -> if sub_acc <> [] then List.rev sub_acc :: acc else acc + | h :: tl -> + if sub_acc_len = pack_len then + inner (1, [h], List.rev sub_acc :: acc) tl + else inner (succ sub_acc_len, h :: sub_acc, acc) tl + in + List.rev (inner (0, [], []) l) + +let of_entropy entropy = + match entropy_of_bytes entropy with + | None -> invalid_arg "Bip39.of_entropy: wrong entropy length" + | Some { bytes ; digest_length ; _ } -> + let digest = Cstruct.get_char (Nocrypto.Hash.SHA256.digest entropy) 0 in + let digest = list_sub (bits_of_char digest) digest_length in + let entropy = bits_of_bytes (Cstruct.to_string bytes) @ digest in + List.map (pack entropy 11) ~f:int_of_bits + +let to_seed ?(passphrase="") t = + let words = to_words t in + let password = Cstruct.of_string (String.concat ~sep:" " words) in + let salt = Cstruct.of_string ("mnemonic" ^ passphrase) in + Pbkdf.pbkdf2 ~prf:`SHA512 ~password ~salt ~count:2048 ~dk_len:64l + +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + ---------------------------------------------------------------------------*) diff --git a/vendors/ocaml-bip39/src/bip39.mli b/vendors/ocaml-bip39/src/bip39.mli new file mode 100644 index 000000000..fc2b4bd56 --- /dev/null +++ b/vendors/ocaml-bip39/src/bip39.mli @@ -0,0 +1,56 @@ +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff. All rights reserved. + Distributed under the ISC license, see terms at the end of the file. + ---------------------------------------------------------------------------*) + +type t +(** Abstract type of a mnemonic *) + +val pp : Format.formatter -> t -> unit +val show : t -> string + +val index_of_word : string -> int option +(** [find_index word] is [Some i] where is is the index of [word] in + the BIP39 word list, or [None] if no such word is in the list. *) + +val of_indices : int list -> t option +(** [of_indices idxs] is [Some mnemonic] if indices are all in range + [0-2047] or [None] otherwise. *) + +val to_indices : t -> int list +(** [to_indices t] is the list of indices corresponding to [t]. *) + +val of_words : string list -> t option +(** [of_words words] is [Some mnemonic] if [words] is a list + containing a valids number of valid english words. *) + +val to_words : t -> string list +(** [to_words mnemonic] is the list of words corresponding to + [mnemonic]. *) + +val of_entropy : Cstruct.t -> t +(** [of_entropy bytes] is the mnemonic derived from [bytes]. + + @raises [Invalid_argument] is [List.length bytes] is not in { 16, + 20, 24, 28, 32 }. *) + +val to_seed : ?passphrase:string -> t -> Cstruct.t +(** [to_seed ?passphrase mnemonic] is 64 bytes derived from a BIP39 + mnemonic [mnemonic], using the optional passphrase [passphrase] if + provided. *) + +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + ---------------------------------------------------------------------------*) diff --git a/vendors/ocaml-bip39/src/generator.ml b/vendors/ocaml-bip39/src/generator.ml new file mode 100644 index 000000000..cc1c51c61 --- /dev/null +++ b/vendors/ocaml-bip39/src/generator.ml @@ -0,0 +1,44 @@ +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff. All rights reserved. + Distributed under the ISC license, see terms at the end of the file. + ---------------------------------------------------------------------------*) + +open Base +open Stdio + +let pp_print_quoted_string ppf str = + let open Caml.Format in + fprintf ppf "\"%s\"" str + +let pp_print_quoted_string_list ppf strs = + let open Caml.Format in + pp_print_list ~pp_sep:(fun ppf () -> pp_print_string ppf ";") + pp_print_quoted_string ppf strs + +let gen ml = + let txt = "../gen/" ^ (Caml.Filename.remove_extension ml) ^ ".txt" in + let words = In_channel.read_lines txt in + Out_channel.with_file + ~binary:false ~append:false ~fail_if_exists:false ml ~f:begin fun oc -> + let ppf = Caml.Format.formatter_of_out_channel oc in + Caml.Format.fprintf ppf "let words = [%a]@." pp_print_quoted_string_list words + end + +let () = + Array.to_list Caml.Sys.argv |> List.tl_exn |> List.iter ~f:gen + +(*--------------------------------------------------------------------------- + Copyright (c) 2017 Vincent Bernardoff + + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + ---------------------------------------------------------------------------*) diff --git a/vendors/ocaml-bip39/src/jbuild b/vendors/ocaml-bip39/src/jbuild new file mode 100644 index 000000000..a99b61ca8 --- /dev/null +++ b/vendors/ocaml-bip39/src/jbuild @@ -0,0 +1,17 @@ +(jbuild_version 1) + +(library + ((name bip39) + (public_name bip39) + (modules (english bip39)) + (libraries (nocrypto pbkdf)))) + +(rule + ((targets (english.ml)) + (deps (generator.exe (files_recursively_in ../gen))) + (action (run ${<} ${@})))) + +(executable + ((name generator) + (modules (generator)) + (libraries (stdio)))) diff --git a/vendors/ocaml-bip39/test/jbuild b/vendors/ocaml-bip39/test/jbuild new file mode 100644 index 000000000..a0943da51 --- /dev/null +++ b/vendors/ocaml-bip39/test/jbuild @@ -0,0 +1,8 @@ +(executable + ((name test) + (libraries (bip39 hex alcotest)))) + +(alias + ((name runtest-bip39) + (deps (test.exe)) + (action (run ${<})))) diff --git a/vendors/ocaml-bip39/test/test.ml b/vendors/ocaml-bip39/test/test.ml new file mode 100644 index 000000000..2e169e416 --- /dev/null +++ b/vendors/ocaml-bip39/test/test.ml @@ -0,0 +1,138 @@ +open Alcotest + +type vector = { + entropy : Hex.t ; + words : string ; + seed : Hex.t ; +} + +let vectors = [ + { entropy = `Hex "00000000000000000000000000000000" ; + words = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about" ; + seed = `Hex "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04" ; + } ; + { entropy = `Hex "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f" ; + words = "legal winner thank year wave sausage worth useful legal winner thank yellow" ; + seed = `Hex "2e8905819b8723fe2c1d161860e5ee1830318dbf49a83bd451cfb8440c28bd6fa457fe1296106559a3c80937a1c1069be3a3a5bd381ee6260e8d9739fce1f607" ; + } ; + { entropy = `Hex "80808080808080808080808080808080" ; + words = "letter advice cage absurd amount doctor acoustic avoid letter advice cage above" ; + seed = `Hex "d71de856f81a8acc65e6fc851a38d4d7ec216fd0796d0a6827a3ad6ed5511a30fa280f12eb2e47ed2ac03b5c462a0358d18d69fe4f985ec81778c1b370b652a8" ; + } ; + { entropy = `Hex "ffffffffffffffffffffffffffffffff" ; + words = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong" ; + seed = `Hex "ac27495480225222079d7be181583751e86f571027b0497b5b5d11218e0a8a13332572917f0f8e5a589620c6f15b11c61dee327651a14c34e18231052e48c069" ; + } ; + { entropy = `Hex "000000000000000000000000000000000000000000000000" ; + words = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon agent" ; + seed = `Hex "035895f2f481b1b0f01fcf8c289c794660b289981a78f8106447707fdd9666ca06da5a9a565181599b79f53b844d8a71dd9f439c52a3d7b3e8a79c906ac845fa" ; + } ; + { entropy = `Hex "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f" ; + words = "legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal will" ; + seed = `Hex "f2b94508732bcbacbcc020faefecfc89feafa6649a5491b8c952cede496c214a0c7b3c392d168748f2d4a612bada0753b52a1c7ac53c1e93abd5c6320b9e95dd" ; + } ; + { entropy = `Hex "808080808080808080808080808080808080808080808080" ; + words = "letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter always" ; + seed = `Hex "107d7c02a5aa6f38c58083ff74f04c607c2d2c0ecc55501dadd72d025b751bc27fe913ffb796f841c49b1d33b610cf0e91d3aa239027f5e99fe4ce9e5088cd65" ; + } ; + { entropy = `Hex "ffffffffffffffffffffffffffffffffffffffffffffffff" ; + words = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo when" ; + seed = `Hex "0cd6e5d827bb62eb8fc1e262254223817fd068a74b5b449cc2f667c3f1f985a76379b43348d952e2265b4cd129090758b3e3c2c49103b5051aac2eaeb890a528" ; + } ; + { entropy = `Hex "0000000000000000000000000000000000000000000000000000000000000000" ; + words = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art" ; + seed = `Hex "bda85446c68413707090a52022edd26a1c9462295029f2e60cd7c4f2bbd3097170af7a4d73245cafa9c3cca8d561a7c3de6f5d4a10be8ed2a5e608d68f92fcc8" ; + } ; + { entropy = `Hex "7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f7f" ; + words = "legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth useful legal winner thank year wave sausage worth title" ; + seed = `Hex "bc09fca1804f7e69da93c2f2028eb238c227f2e9dda30cd63699232578480a4021b146ad717fbb7e451ce9eb835f43620bf5c514db0f8add49f5d121449d3e87" ; + } ; + { entropy = `Hex "8080808080808080808080808080808080808080808080808080808080808080" ; + words = "letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic avoid letter advice cage absurd amount doctor acoustic bless" ; + seed = `Hex "c0c519bd0e91a2ed54357d9d1ebef6f5af218a153624cf4f2da911a0ed8f7a09e2ef61af0aca007096df430022f7a2b6fb91661a9589097069720d015e4e982f" ; + } ; + { entropy = `Hex "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ; + words = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo vote" ; + seed = `Hex "dd48c104698c30cfe2b6142103248622fb7bb0ff692eebb00089b32d22484e1613912f0a5b694407be899ffd31ed3992c456cdf60f5d4564b8ba3f05a69890ad" ; + } ; + { entropy = `Hex "9e885d952ad362caeb4efe34a8e91bd2" ; + words = "ozone drill grab fiber curtain grace pudding thank cruise elder eight picnic" ; + seed = `Hex "274ddc525802f7c828d8ef7ddbcdc5304e87ac3535913611fbbfa986d0c9e5476c91689f9c8a54fd55bd38606aa6a8595ad213d4c9c9f9aca3fb217069a41028" ; + } ; + { entropy = `Hex "6610b25967cdcca9d59875f5cb50b0ea75433311869e930b" ; + words = "gravity machine north sort system female filter attitude volume fold club stay feature office ecology stable narrow fog" ; + seed = `Hex "628c3827a8823298ee685db84f55caa34b5cc195a778e52d45f59bcf75aba68e4d7590e101dc414bc1bbd5737666fbbef35d1f1903953b66624f910feef245ac" ; + } ; + { entropy = `Hex "68a79eaca2324873eacc50cb9c6eca8cc68ea5d936f98787c60c7ebc74e6ce7c" ; + words = "hamster diagram private dutch cause delay private meat slide toddler razor book happy fancy gospel tennis maple dilemma loan word shrug inflict delay length" ; + seed = `Hex "64c87cde7e12ecf6704ab95bb1408bef047c22db4cc7491c4271d170a1b213d20b385bc1588d9c7b38f1b39d415665b8a9030c9ec653d75e65f847d8fc1fc440" ; + } ; + { entropy = `Hex "c0ba5a8e914111210f2bd131f3d5e08d" ; + words = "scheme spot photo card baby mountain device kick cradle pact join borrow" ; + seed = `Hex "ea725895aaae8d4c1cf682c1bfd2d358d52ed9f0f0591131b559e2724bb234fca05aa9c02c57407e04ee9dc3b454aa63fbff483a8b11de949624b9f1831a9612" ; + } ; + { entropy = `Hex "6d9be1ee6ebd27a258115aad99b7317b9c8d28b6d76431c3" ; + words = "horn tenant knee talent sponsor spell gate clip pulse soap slush warm silver nephew swap uncle crack brave" ; + seed = `Hex "fd579828af3da1d32544ce4db5c73d53fc8acc4ddb1e3b251a31179cdb71e853c56d2fcb11aed39898ce6c34b10b5382772db8796e52837b54468aeb312cfc3d" ; + } ; + { entropy = `Hex "9f6a2878b2520799a44ef18bc7df394e7061a224d2c33cd015b157d746869863" ; + words = "panda eyebrow bullet gorilla call smoke muffin taste mesh discover soft ostrich alcohol speed nation flash devote level hobby quick inner drive ghost inside" ; + seed = `Hex "72be8e052fc4919d2adf28d5306b5474b0069df35b02303de8c1729c9538dbb6fc2d731d5f832193cd9fb6aeecbc469594a70e3dd50811b5067f3b88b28c3e8d" ; + } ; + { entropy = `Hex "23db8160a31d3e0dca3688ed941adbf3" ; + words = "cat swing flag economy stadium alone churn speed unique patch report train" ; + seed = `Hex "deb5f45449e615feff5640f2e49f933ff51895de3b4381832b3139941c57b59205a42480c52175b6efcffaa58a2503887c1e8b363a707256bdd2b587b46541f5" ; + } ; + { entropy = `Hex "8197a4a47f0425faeaa69deebc05ca29c0a5b5cc76ceacc0" ; + words = "light rule cinnamon wrap drastic word pride squirrel upgrade then income fatal apart sustain crack supply proud access" ; + seed = `Hex "4cbdff1ca2db800fd61cae72a57475fdc6bab03e441fd63f96dabd1f183ef5b782925f00105f318309a7e9c3ea6967c7801e46c8a58082674c860a37b93eda02" ; + } ; + { entropy = `Hex "066dca1a2bb7e8a1db2832148ce9933eea0f3ac9548d793112d9a95c9407efad" ; + words = "all hour make first leader extend hole alien behind guard gospel lava path output census museum junior mass reopen famous sing advance salt reform" ; + seed = `Hex "26e975ec644423f4a4c4f4215ef09b4bd7ef924e85d1d17c4cf3f136c2863cf6df0a475045652c57eb5fb41513ca2a2d67722b77e954b4b3fc11f7590449191d" ; + } ; + { entropy = `Hex "f30f8c1da665478f49b001d94c5fc452" ; + words = "vessel ladder alter error federal sibling chat ability sun glass valve picture" ; + seed = `Hex "2aaa9242daafcee6aa9d7269f17d4efe271e1b9a529178d7dc139cd18747090bf9d60295d0ce74309a78852a9caadf0af48aae1c6253839624076224374bc63f" ; + } ; + { entropy = `Hex "c10ec20dc3cd9f652c7fac2f1230f7a3c828389a14392f05" ; + words = "scissors invite lock maple supreme raw rapid void congress muscle digital elegant little brisk hair mango congress clump" ; + seed = `Hex "7b4a10be9d98e6cba265566db7f136718e1398c71cb581e1b2f464cac1ceedf4f3e274dc270003c670ad8d02c4558b2f8e39edea2775c9e232c7cb798b069e88" ; + } ; + { entropy = `Hex "f585c11aec520db57dd353c69554b21a89b20fb0650966fa0a9d6f74fd989d8f" ; + words = "void come effort suffer camp survey warrior heavy shoot primary clutch crush open amazing screen patrol group space point ten exist slush involve unfold" ; + seed = `Hex "01f5bced59dec48e362f2c45b5de68b9fd6c92c6634f44d6d40aab69056506f0e35524a518034ddc1192e1dacd32c1ed3eaa3c3b131c88ed8e7e54c49a5d0998" ; + } ; +] + +let pp_diff ppf (l1, l2) = + match List.length l1, List.length l2 with + | n, m when n <> m -> + Format.fprintf ppf "Mnemonic size differs: %d vs %d" n m ; + | _ -> + ignore @@ ListLabels.fold_left2 l1 l2 ~init:0 ~f:begin fun i w1 w2 -> + if w1 <> w2 then begin + Format.fprintf ppf "At position %d, words differ: %s <> %s" + i w1 w2 + end ; + succ i + end + +let vectors () = + ListLabels.iteri vectors ~f:begin fun i { entropy ; words ; seed } -> + let words = String.split_on_char ' ' words in + let mnemonic = Bip39.of_entropy (Cstruct.of_string (Hex.to_string entropy)) in + let words_computed = Bip39.to_words mnemonic in + assert (words = words_computed) ; + let seed_computed = Bip39.to_seed ~passphrase:"TREZOR" mnemonic in + assert ((Hex.to_cstruct seed) = seed_computed) + end + +let basic = [ + "vectors", `Quick, vectors ; +] + +let () = + Alcotest.run "bip39" [ + "basic", basic ; + ]