package octez-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file xor.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module Nat = struct
  include Nativeint

  let ( lxor ) = Nativeint.logxor
end

module type BUFFER = sig
  type t

  val length : t -> int

  val sub : t -> int -> int -> t

  val copy : t -> t

  val benat_to_cpu : t -> int -> nativeint

  val cpu_to_benat : t -> int -> nativeint -> unit
end

let imin (a : int) (b : int) = if a < b then a else b

module Make (B : BUFFER) = struct
  let size_of_long = Sys.word_size / 8

  (* XXX(dinosaure): I'm not sure about this code. May be we don't need the
     first loop and the _optimization_ is irrelevant. *)
  let xor_into src src_off dst dst_off n =
    let n = ref n in
    let i = ref 0 in
    while !n >= size_of_long do
      B.cpu_to_benat
        dst
        (dst_off + !i)
        Nat.(
          B.benat_to_cpu dst (dst_off + !i)
          lxor B.benat_to_cpu src (src_off + !i)) ;
      n := !n - size_of_long ;
      i := !i + size_of_long
    done ;
    while !n > 0 do
      B.cpu_to_benat
        dst
        (dst_off + !i)
        Nat.(
          B.benat_to_cpu src (src_off + !i)
          lxor B.benat_to_cpu dst (dst_off + !i)) ;
      incr i ;
      decr n
    done

  let xor_into a b n =
    if n > imin (B.length a) (B.length b) then
      raise (Invalid_argument "Baijiu.Xor.xor_inrot: buffers to small")
    else xor_into a 0 b 0 n

  let xor a b =
    let l = imin (B.length a) (B.length b) in
    let r = B.copy (B.sub b 0 l) in
    xor_into a r l ;
    r
end

module Bytes = Make (Digestif_by)
module Bigstring = Make (Digestif_bi)
OCaml

Innovation. Community. Security.