123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(*****************************************************************************)(* *)(* Copyright (c) 2020-2021 Danny Willems <be.danny.willems@gmail.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. *)(* *)(*****************************************************************************)moduletypeRAW_UNCOMPRESSED=sigincludeElliptic_curve_sig.RAW_BASEvalbuild_from_components:Bytes.t->Bytes.t->Bytes.toptionendmoduletypeRAW_COMPRESSED=sigincludeElliptic_curve_sig.RAW_BASEendmoduletypeBASE=sigincludeElliptic_curve_sig.TendmoduletypeUNCOMPRESSED=sigincludeBASE(** Create a point from the coordinates. If the point is not on the curve,
[None] is return. The points must be given modulo the order of Fq. To create
the point at infinity, use [zero ()] *)valof_z_opt:x:Z.t->y:Z.t->toptionendmoduletypeCOMPRESSED=sigincludeBASEendmoduleMakeBase(Scalar:Fr.T)(Stubs:Elliptic_curve_sig.RAW_BASE):BASEwithmoduleScalar=Scalar=structexceptionNot_on_curveofBytes.ttypet=Bytes.tletsize_in_bytes=Stubs.size_in_bytesmoduleScalar=Scalarletempty()=Bytes.makesize_in_bytes'\000'letcheck_bytesbs=ifBytes.lengthbs=size_in_bytesthenStubs.check_bytesbselsefalseletof_bytes_optbs=ifcheck_bytesbsthenSome(Bytes.copybs)elseNoneletof_bytes_exn(g:Bytes.t):t=ifcheck_bytesgthenBytes.copygelseraise(Not_on_curveg)letto_bytesg=gletzero=letres=Stubs.zero()inresletone=letres=Stubs.one()inresletrandom?state()=ignorestate;letres=Stubs.random()inresletaddg1g2=assert(Bytes.lengthg1=size_in_bytes);assert(Bytes.lengthg2=size_in_bytes);letres=Stubs.addg1g2inassert(Bytes.lengthres=size_in_bytes);resletnegateg=assert(Bytes.lengthg=size_in_bytes);letres=Stubs.negateginassert(Bytes.lengthres=size_in_bytes);resleteqg1g2=assert(Bytes.lengthg1=size_in_bytes);assert(Bytes.lengthg2=size_in_bytes);Stubs.eqg1g2letis_zerog=assert(Bytes.lengthg=size_in_bytes);Stubs.is_zerogletdoubleg=assert(Bytes.lengthg=size_in_bytes);letres=Stubs.doubleginassert(Bytes.lengthres=size_in_bytes);resletmul(g:t)(a:Scalar.t):t=assert(Bytes.lengthg=size_in_bytes);assert(Bytes.length(Scalar.to_bytesa)=Scalar.size_in_bytes);letres=Stubs.mulg(Scalar.to_bytesa)inassert(Bytes.lengthres=size_in_bytes);resendmoduleMakeUncompressed(Scalar:Fr.T)(Stubs:RAW_UNCOMPRESSED):UNCOMPRESSEDwithtypeScalar.t=Scalar.t=structincludeMakeBase(Scalar)(Stubs)letof_z_opt~x~y=letx_bytes=Bytes.make48'\000'inletx=Bytes.of_string(Z.to_bitsx)inBytes.blitx0x_bytes0(min(Bytes.lengthx)48);lety_bytes=Bytes.make48'\000'inlety=Bytes.of_string(Z.to_bitsy)inBytes.blity0y_bytes0(min(Bytes.lengthy)48);letres=Stubs.build_from_componentsx_bytesy_bytesinmatchreswithNone->None|Someres->Some(of_bytes_exnres)endmoduleMakeCompressed(Scalar:Fr.T)(Stubs:RAW_COMPRESSED):COMPRESSEDwithtypeScalar.t=Scalar.t=structincludeMakeBase(Scalar)(Stubs)end