123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)moduletypeZN=sigtypetincludeS.B58_DATAwithtypet:=tincludeS.ENCODERwithtypet:=tvalzero:tvalone:tvaln:Z.tval(+):t->t->tval(*):t->t->tval(-):t->t->tval(=):t->t->boolvalof_int:int->tvalof_Z:Z.t->tvalto_Z:t->Z.tvalof_bits_exn:String.t->tvalto_bits:t->String.tvalpow:t->Z.t->tvalinv:t->toptionendmoduletypeINT=sigvaln:Z.tendmoduleMakeZn(N:INT)(B:sigvalb58_prefix:stringend):ZN=structtypet=Z.tletn=N.nletmax_char_length=2*Z.numbitsnletzero=Z.zeroletone=Z.oneletof_Zr=Z.(eremrn)letto_Za=aletof_intu=u|>Z.of_int|>of_Zletto_bitsh=h|>Zplus.serialize|>funs->String.subs0(String.lengths-1)letof_bits_exnbits=(* Do not process oversized inputs. *)ifCompare.Int.(String.lengthbits>max_char_length)thenfailwith"input too long"else(* Make sure the input is in the range [0, N.n-1]. Do not reduce modulo
N.n for free! *)letx=Zplus.deserializebitsinifZplus.(x<zero||x>=N.n)thenfailwith"out of range"elseof_Zxletpowax=Z.powmaZ.(eremx(subnone))nlet(+)xy=Z.(erem(addxy)n)let(*)xy=Z.(erem(mulxy)n)let(-)xy=Z.(erem(subxy)n)let(=)xy=Z.equalxyletinva=Zplus.invertanlettitle=Format.sprintf"Znz.Make(%s)"(Z.to_stringN.n)letname=Format.sprintf"An element of Z/nZ for n = %s"(Z.to_stringN.n)typeBase58.data+=Dataoftletb58check_encoding=Base58.register_encoding~prefix:B.b58_prefix~length:32~to_raw:to_bits~of_raw:(funs->trySome(of_bits_exns)with_->None)~wrap:(funx->Datax)includeHelpers.MakeB58(structtypenonrect=tletname=nameletb58check_encoding=b58check_encodingend)includeHelpers.MakeEncoder(structtypenonrect=tletname=namelettitle=titleletraw_encoding=Data_encoding.(convto_bitsof_bits_exnstring)letto_b58check=to_b58checkletto_short_b58check=to_short_b58checkletof_b58check=of_b58checkletof_b58check_opt=of_b58check_optletof_b58check_exn=of_b58check_exnend)end