123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188(*****************************************************************************)(* *)(* 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. *)(* *)(*****************************************************************************)moduleFf_sig=Ff_sigmoduletypeCURVE=sigexceptionNot_on_curveofBytes.t(** The type of the element on the curve and in the prime subgroup. The point
is given in jacobian coordinates *)typet(** An element on the curve and in the prime subgroup, in affine coordinates *)typeaffine(** [affine_of_jacobian p] creates a new value of type [affine] representing
the point [p] in affine coordinates *)valaffine_of_jacobian:t->affine(** [jacobian_of_affine p] creates a new value of type [t] representing the
point [p] in jacobian coordinates *)valjacobian_of_affine:affine->t(** Contiguous C array containing points in affine coordinates *)typeaffine_array(** [to_affine_array pts] builds a contiguous C array and populate it with the
points [pts] in affine coordinates. Use it with
{!pippenger_with_affine_array} to get better performance. *)valto_affine_array:tarray->affine_array(** Build a OCaml array of [t] values from the contiguous C array *)valof_affine_array:affine_array->tarray(** Return the number of elements in the array *)valsize_of_affine_array:affine_array->int(** Actual number of bytes allocated for a value of type t *)valsize_in_memory:int(** Size in bytes for the compressed representation *)valcompressed_size_in_bytes:int(** The size of a point representation, in bytes *)valsize_in_bytes:intmoduleScalar:Ff_sig.PRIMEwithtypet=Fr.t(** Check if a point, represented as a byte array, is on the curve **)valcheck_bytes:Bytes.t->bool(** Attempt to construct a point from a byte array of length {!size_in_bytes}. *)valof_bytes_opt:Bytes.t->toption(** Attempt to construct a point from a byte array of length {!size_in_bytes}.
Raise {!Not_on_curve} if the point is not on the curve *)valof_bytes_exn:Bytes.t->t(** Allocates a new point from a byte of length [size_in_bytes / 2] array
representing a point in compressed form. *)valof_compressed_bytes_opt:Bytes.t->toption(** Allocates a new point from a byte array of length [size_in_bytes / 2]
representing a point in compressed form. Raise {!Not_on_curve} if the
point is not on the curve. *)valof_compressed_bytes_exn:Bytes.t->t(** Return a representation in bytes *)valto_bytes:t->Bytes.t(** Return a compressed bytes representation *)valto_compressed_bytes:t->Bytes.t(** Zero of the elliptic curve *)valzero:t(** A fixed generator of the elliptic curve *)valone:t(** Return [true] if the given element is zero *)valis_zero:t->bool(** [copy x] return a fresh copy of [x] *)valcopy:t->t(** Generate a random element. The element is on the curve and in the prime
subgroup. *)valrandom:?state:Random.State.t->unit->t(** Return the addition of two element *)valadd:t->t->tvaladd_inplace:t->t->unitvaladd_bulk:tlist->t(** [double g] returns [2g] *)valdouble:t->t(** Return the opposite of the element *)valnegate:t->t(** Return [true] if the two elements are algebraically the same *)valeq:t->t->bool(** Multiply an element by a scalar *)valmul:t->Scalar.t->tvalmul_inplace:t->Scalar.t->unitvalhash_to_curve:Bytes.t->Bytes.t->t(** [pippenger ?start ?len pts scalars] computes the multi scalar
exponentiation/multiplication. The scalars are given in [scalars] and the
points in [pts]. If [pts] and [scalars] are not of the same length,
perform the computation on the first [n] points where [n] is the smallest
size. Arguments [start] and [len] can be used to take advantages of
multicore OCaml. Default value for [start] (resp. [len]) is [0] (resp. the
length of the array [scalars]).
@raise Invalid_argument if [start] or [len] would infer out of bounds
array access.
Perform allocations on the C heap to convert scalars to bytes and to
convert the points [pts] in affine coordinates as values of type [t] are
in jacobian coordinates.
{b Warning.} Undefined behavior if the point to infinity is in the array *)valpippenger:?start:int->?len:int->tarray->Scalar.tarray->t(** [pippenger_with_affine_array ?start ?len pts scalars] computes the multi
scalar exponentiation/multiplication. The scalars are given in [scalars]
and the points in [pts]. If [pts] and [scalars] are not of the same
length, perform the computation on the first [n] points where [n] is the
smallest size. The differences with {!pippenger} are 1. the points are
loaded in a contiguous C array to speed up the access to the elements by
relying on the CPU cache 2. and the points are in affine coordinates, the
form expected by the algorithm implementation, avoiding new allocations
and field inversions required to convert from jacobian (representation of
a points of type [t], as expected by {!pippenger}) to affine coordinates.
Expect a speed improvement around 20% compared to {!pippenger}, and less
allocation on the C heap. A value of [affine_array] can be built using
{!to_affine_array}. Arguments [start] and [len] can be used to take
advantages of multicore OCaml. Default value for [start] (resp. [len]) is
[0] (resp. the length of the array [scalars]).
@raise Invalid_argument if [start] or [len] would infer out of bounds
array access.
Perform allocations on the C heap to convert scalars to bytes.
{b Warning.} Undefined behavior if the point to infinity is in the array *)valpippenger_with_affine_array:?start:int->?len:int->affine_array->Scalar.tarray->tendmoduleFr=FrmoduleG1=G1moduleG2=G2moduleGT=GtmoduleFq12=Fq12modulePairing=Pairingexternalbuilt_with_blst_portable_stubs:unit->bool="caml_built_with_blst_portable_stubs"letbuilt_with_blst_portable=built_with_blst_portable_stubs()