text
stringlengths
12
786k
module Time_zone_info = struct include Time_zone_info let of_sexp = Of_sexp_utils . wrap_of_sexp Of_sexp . tz_info_of_sexp let to_sexp = To_sexp . sexp_of_tz_info end
module Utils = struct let ptime_span_of_span = Ptime_utils . ptime_span_of_span let ptime_of_timestamp = Ptime_utils . ptime_of_timestamp let span_of_ptime_span = Ptime_utils . span_of_ptime_span let timestamp_of_ptime = Ptime_utils . timestamp_of_ptime let day_count_of_year = day_count_of_year let day_co...
let local ( ) : string list = match Sys . getenv_opt " TZ " with | Some name -> [ name ] | None -> ( if CCIO . File . exists " / system / bin / getprop " then ( let ic = Unix . open_process_in " getprop " in let name = CCIO . read_all ic in close_in ic ; [ name ] ) e...
type rsa_secret = { p : Z . t ; q : Z . t }
type ciphertext = { nonce : Crypto_box . nonce ; payload : bytes }
let unlocked_value_to_symmetric_key unlocked_value = let kdf_key = " Tezoskdftimelockv0 " in let to_hash = Z . to_string unlocked_value in let hash = Blake2B . ( to_bytes @@ hash_string ~ key : kdf_key [ to_hash ] ) in Crypto_box . Secretbox . unsafe_of_bytes hash
let random_z size = Hacl . Rand . gen size |> Bytes . to_string |> Z . of_bits
let random_prime_z size = let rec aux ( ) = let trial = random_z size in if Z . probab_prime trial 25 = 0 then aux ( ) else trial in aux ( )
let gen_rsa_keys ( ) = let size = size_modulus / ( 2 * 8 ) in let p = random_prime_z size in let q = random_prime_z size in ( Z . ( p * q ) , { p ; q } )
let gen_locked_value rsa_public = Z . erem ( random_z ( ( size_modulus / 8 ) + 16 ) ) rsa_public
let hash_to_prime rsa_public ~ time value key = let personalization = Bytes . of_string " \ 032 " in let s = String . concat " \ xff \ x00 \ xff \ x00 \ xff \ x00 \ xff \ x00 " ( Int . to_string time :: List . map Z . to_bits [ rsa_public ; value ; key ] ) in let ( Hacl . ...
let prove_without_secret rsa_public ~ time locked_value unlocked_value = let l = hash_to_prime rsa_public ~ time locked_value unlocked_value in let pow = Z . ( pow ( of_int 2 ) time / l ) in Z . powm locked_value pow rsa_public
let prove_with_secret secret ~ time locked_value unlocked_value = let rsa_public = Z . ( secret . p * secret . q ) in let l = hash_to_prime rsa_public ~ time locked_value unlocked_value in let phi = Z . ( ( secret . p - one ) * ( secret . q - one ) ) in let pow = Z . ( pow ( ...
let verify_time_lock rsa_public ~ time locked_value unlocked_value proof = let l = hash_to_prime rsa_public ~ time locked_value unlocked_value in let r = Z . ( powm ( of_int 2 ) ( Z . of_int time ) l ) in unlocked_value = Z . ( powm proof l rsa_public * powm locked_value r rsa_public mod rsa...
let unlock_with_secret secret ( ~ time : int ) ( locked_value : locked_value ) = let phi = Z . ( ( secret . p - one ) * ( secret . q - one ) ) in let e = Z . powm ( Z . of_int 2 ) ( Z . of_int time ) phi in Z . powm locked_value e Z . ( secret . p * secret . ...
let unlock_and_prove_with_secret secret ( ~ time : int ) ( locked_value : locked_value ) = let unlocked_value = unlock_with_secret secret ~ time locked_value in let pi = prove_with_secret secret ~ time locked_value unlocked_value in ( unlocked_value , pi )
let locked_value_to_symmetric_key_with_secret secret ( ~ time : int ) ( locked_value : locked_value ) : symmetric_key = unlocked_value_to_symmetric_key ( unlock_with_secret secret ~ time locked_value )
let unlock_and_prove_without_secret rsa_public ~ time locked_value = let rec aux time v = if time = 0 then v else aux Int . ( pred time ) Z . ( v * v mod rsa_public ) in let unlocked_value = aux time locked_value in let pi = prove_without_secret rsa_public ~ time locked_value unlocked_value in ( ...
let locked_value_to_symmetric_key_with_proof ( rsa_public : rsa_public ) ( ~ time : int ) locked_value unlocked_value proof = if verify_time_lock rsa_public ~ time locked_value unlocked_value proof then Some ( unlocked_value_to_symmetric_key unlocked_value ) else None
let encrypt symmetric_key plaintext = let nonce = Crypto_box . random_nonce ( ) in { nonce ; payload = Crypto_box . Secretbox . secretbox symmetric_key plaintext nonce ; }
let decrypt symmetric_key ciphertext = Crypto_box . Secretbox . secretbox_open symmetric_key ciphertext . payload ciphertext . nonce
type chest_key = { unlocked_value : unlocked_value ; proof : time_lock_proof }
type chest = { locked_value : locked_value ; rsa_public : rsa_public ; ciphertext : ciphertext ; }
let chest_key_encoding = let open Data_encoding in def " timelock . chest_key " @@ conv ( fun chest_key -> ( chest_key . unlocked_value , chest_key . proof ) ) ( fun ( unlocked_value , proof ) -> { unlocked_value ; proof } ) ( obj2 ( req " unlocked_value " Data_encoding ....
let ciphertext_encoding = let open Data_encoding in def " timelock . ciphertext " @@ conv_with_guard ( fun ciphertext -> ( ciphertext . nonce , ciphertext . payload ) ) ( fun ( nonce , payload ) -> if Bytes . length payload <= Crypto_box . tag_length then Error " The ciphertext ha...
let min_rsa_modulus = Z . ( shift_left ( of_int 2 ) 2000 )
let chest_encoding = let open Data_encoding in def " timelock . chest " @@ conv_with_guard ( fun chest -> ( chest . locked_value , chest . rsa_public , chest . ciphertext ) ) ( fun ( locked_value , rsa_public , ciphertext ) -> if Z . Compare . ( locked_value < Z . zero ||...
type opening_result = Correct of Bytes . t | Bogus_cipher | Bogus_opening
let open_chest chest chest_key ~ time = if time < 0 then failwith " Timelock : trying to open with a negative time " else let sym_key_opt = locked_value_to_symmetric_key_with_proof chest . rsa_public ~ time chest . locked_value chest_key . unlocked_value chest_key . proof in match sym_key_opt with |...
let create_chest_and_chest_key ~ payload ~ time = let ( rsa_public , rsa_secret ) = gen_rsa_keys ( ) in let locked_value = gen_locked_value rsa_public in let ( unlocked_value , proof ) = unlock_and_prove_with_secret rsa_secret ~ time locked_value in let sym_key = unlocked_value_to_symmetric_key ...
let create_chest_key chest ~ time = let ( unlocked_value , proof ) = unlock_and_prove_without_secret chest . rsa_public ~ time chest . locked_value in { unlocked_value ; proof }
let get_plaintext_size chest = Bytes . length chest . ciphertext . payload - Crypto_box . tag_length
let gen_random_bytes_unsafe size = Bytes . init size ( fun _ -> Char . chr ( Random . int 256 ) )
let gen_random_z_unsafe size = gen_random_bytes_unsafe size |> Bytes . to_string |> Z . of_bits
let gen_random_prime_unsafe size = gen_random_z_unsafe size |> Z . nextprime
let gen_rsa_keys_unsafe ( ) = let size = size_modulus / ( 2 * 8 ) in let p = gen_random_prime_unsafe size in let q = gen_random_prime_unsafe size in ( Z . ( p * q ) , { p ; q } )
let gen_locked_value_unsafe rsa_public = Z . erem ( gen_random_z_unsafe ( size_modulus / 8 ) ) rsa_public
let encrypt_unsafe symmetric_key plaintext = let nonce = Data_encoding . Binary . of_bytes_exn Crypto_box . nonce_encoding ( gen_random_bytes_unsafe Crypto_box . nonce_size ) in { nonce ; payload = Crypto_box . Secretbox . secretbox symmetric_key plaintext nonce ; }
let chest_sampler ~ rng_state ~ plaintext_size ~ time = Random . set_state rng_state ; let plaintext = gen_random_bytes_unsafe plaintext_size in let ( rsa_public , rsa_secret ) = gen_rsa_keys_unsafe ( ) in let locked_value = gen_locked_value_unsafe rsa_public in let ( unlocked_value , proof ) ...
module Alarm_timeout = struct type t = unit let with_timeout ~ timeout ? on_timeout ~ do_ = let old_handler = ref Sys . Signal_default in let old_timeout = ref 0 in let on_timeout _sigalrm = match on_timeout with | None -> raise Timeout | Some f -> f ( ) ; raise Timeout in Utils . with_context ...
module Select_timeout = struct type t = { timeout : float ; } let create timeout = { timeout = Unix . gettimeofday ( ) . + timeout } let with_timeout ~ timeout ? on_timeout ~ do_ = let t = create ( float timeout ) in try do_ t with Timeout as exn -> match on_timeout with | None -> ra...
module type S = sig type t val with_timeout : timeout : int -> ? on_timeout ( : unit -> unit ) -> do_ ( : t -> ' a ) -> ' a val check_timeout : t -> unit type in_channel val in_channel_of_descr : Unix . file_descr -> in_channel val descr_of_in_channel : in_channel -> Unix . file_descr v...
let select = ( module Select_timeout : S )
let alarm = ( module Alarm_timeout : S )
let read_connection ~ timeout ? on_timeout ~ reader sockaddr = with_timeout ~ timeout ? on_timeout ~ do_ ( : fun timeout -> let ( tic , oc ) = open_connection ~ timeout sockaddr in try reader timeout tic oc with exn -> close_out oc ; raise exn )
module type Time_intf = sig type t module Span : sig type t val to_time_ns_span : t -> Time_ns . Span . t val ( - ) : t -> t -> t end module Controller : sig type t end val now : Controller . t -> t val diff : t -> t -> Span . t end
module Timeout_intf ( Time : Time_intf ) Time_intf = struct module type S = sig type ' a t val create : Time . Controller . t -> Time . Span . t -> f ( : Time . t -> ' a ) ' a -> ' a t val to_deferred : ' a t -> ' a Async_kernel . Deferred . t val peek : ' a t -> ' a option val cancel ...
module Make ( Time : Time_intf ) Time_intf : Timeout_intf ( Timeout_intfTime ) Timeout_intfTime . S = struct type ' a t = { deferred : ' a Deferred . t ; cancel : ' a -> unit ; start_time : Time . t ; span : Time . Span . t ; ctrl : Time . Controller . t } let create ctrl span ~ ...
module Core_time = Make ( struct include ( Core_kernel . Time : module type of Core_kernel . Time with module Span := Core_kernel . Time . Span and type underlying = float ) module Controller = struct type t = unit end module Span = struct include Core_kernel . Time . Span let to_time_ns_span = Fn . c...
module Core_time_ns = Make ( struct include ( Core_kernel . Time_ns : module type of Core_kernel . Time_ns with module Span := Core_kernel . Time_ns . Span ) module Controller = struct type t = unit end module Span = struct include Core_kernel . Time_ns . Span let to_time_ns_span = Fn . id end let dif...
let query_uri = let base_uri = Uri . of_string " http :// api . duckduckgo . com /? format = json " in fun query -> Uri . add_query_param base_uri ( " q " , [ query ] )
let get_definition_from_json json = match Yojson . Safe . from_string json with | ` Assoc kv_list -> begin match List . Assoc . find kv_list " Definition " with | None | Some ( ` String " " ) -> Or_error . error_string " No definition found " | Some s -> Ok ( Yojson . Safe . ...
let get_definition ~ timeout word = let get = Cohttp . Client . call ` GET ( query_uri word ) >>= fun ( _ , body ) -> Pipe . to_list body >>| fun strings -> get_definition_from_json ( String . concat strings ) in match timeout with | None -> get | Some timeout -> let timeout = Clock...
let run_one_search ~ timeout search_string = get_definition ~ timeout search_string >>| fun result -> printf " %- 10s : % s \ n " search_string ( match result with | Ok x -> x | Error err -> " { " ^ Error . to_string_hum err ^ " } " )
let run_many_searches ~ parallel ~ timeout search_strings = Deferred . List . iter search_strings ~ f ( : run_one_search ~ timeout ) ~ how ( : if parallel then ` Parallel else ` Sequential )
let ( ) = Command . async_basic ~ summary " : Retrieve definitions from duckduckgo search engine " Command . Spec . ( empty +> flag " - timeout " ( optional time_span ) ~ doc " : Whether to run queries in parallel " +> flag " - parallel " no_arg ~ doc " : Run queries in para...
type tkTimer = int = " camltk_add_timer " = " camltk_rem_timer "
type t = tkTimer * cbid
let add ~ ms ~ callback = if ! Protocol . debug then begin prerr_string " Timer . add " ; flush stderr ; end ; let id = new_function_id ( ) in if ! Protocol . debug then begin prerr_string " id " ; = prerr_cbid id ; flush stderr ; end ; let wrapped _ = clear_callback id ; call...
let set ~ ms ~ callback = ignore ( add ~ ms ~ callback ) ; ;
let remove ( tkTimer , id ) = internal_rem_timer tkTimer ; clear_callback id
type ' a range = ' a Range . range
let resolve_exn ? search_using_tz time = match resolve ? search_using_tz time with | Ok s -> s | Error msg -> raise ( Resolution_error msg )
let of_sexp = Of_sexp . ( wrap_of_sexp of_sexp )
module Utils = struct let flatten_month_ranges ( months : int range Seq . t ) : int Seq . t option = try Some ( Month_ranges . Flatten . flatten months ) with Range . Range_is_invalid -> None let flatten_month_range_list ( months : int range list ) : int list option = try Some ( Month_ra...
type t = { mutable main : int ; mutable sub : int ; mutable div_c : int ; mutable tima : Uint8 . t ; mutable div : Uint8 . t ; mutable tma : Uint8 . t ; mutable tac : Uint8 . t ; }
let make ( ) = { main = 0 ; sub = 0 ; div_c = 0 ; div = Uint8 . zero ; tima = Uint8 . zero ; tma = Uint8 . zero ; tac = Uint8 . zero ; }
let get_mode { tac ; _ } = if Uint8 . code tac land 4 != 0 then match Uint8 . code tac land 3 with | 0 -> Some 64 | 1 -> Some 1 | 2 -> Some 4 | 3 -> Some 16 | _ -> None else None
let tick t m_cycles = t . sub <- t . sub + m_cycles ; if t . sub >= 4 then begin t . main <- succ t . main ; t . sub <- t . sub - 4 ; t . div_c <- succ t . div_c ; if t . div_c == 16 then begin t . div <- Uint8 . succ t . div ; t . div_c <- 0 end end ; match get_mo...
let config = { Scheduler . Config . concurrency = 1 ; display = { verbosity = Short ; status_line = false } ; rpc = None ; stats = None } Scheduler . Run . go ~ on_event ( : fun _ _ -> ( ) ) config ( fun ( ) -> let now ( ) = Unix . gettimeofday ( ) in let s...
let int64_pow b n = let rec loop b n acc = if n = 0 then acc else loop Int64 . ( mul b b ) ( n lsr 1 ) ( if n land 1 = 0 then acc else Int64 . ( mul b acc ) ) in loop b n 1L
let pp = Ptime . pp_human ~ frac_s : 6 ( )
let of_string x = let d_ps_of_intlit intlit = let sec = Int64 . of_string intlit in let d = Int64 . div sec 86_400L in let ps = Int64 . ( mul ( rem sec 86_400L ) 1_000_000_000_000L ) in ( Int64 . to_int d , ps ) in match match String . split_on_char ' . ' x with | [ _ ] -> P...
let to_string ts = let d , ps = Ptime . Span . to_d_ps ( Ptime . diff ts Ptime . epoch ) in let sec = Int64 . ( add ( mul ( of_int d ) 86_400L ) ( div ps 1_000_000_000_000L ) ) in let subsec = Int64 . ( rem ps 1_000_000_000_000L ) in Printf . sprintf " % Ld . % 06Ld ...
let of_yojson json = match match json with | ` Int x -> Ptime . of_span ( Ptime . Span . of_int_s x ) | ` Intlit x -> of_string x | ` String x -> of_string x | _ -> None with | Some ts -> Result . Ok ts | None -> Result . Error " Couldn ' t parse timestamp "
let to_yojson ts = ` String ( to_string ts )
module type Extend_zone = Timezone_intf . Extend_zone
module Zone_cache = struct type z = { mutable full : bool ; basedir : string ; table : t String . Table . t } let the_one_and_only = { full = false ; basedir = Option . value ( Sys . getenv_opt " TZDIR " ) ~ default " :/ usr / share / zoneinfo " / ; table = String . Tab...
let find zone = let zone = match zone with | " utc " -> " UTC " | " gmt " -> " GMT " | " chi " -> " America / Chicago " | " nyc " -> " America / New_York " | " hkg " -> " Asia / Hong_Kong " | " lon " | " ldn " -> " Europe / London " | " tyo " ...
let find_exn zone = match find zone with | None -> Error . raise_s [ % message " unknown zone " ( zone : string ) ] | Some z -> z ; ;
let local = let local_zone_name = Sys . getenv_opt " TZ " in let load ( ) = match local_zone_name with | Some zone_name -> find_exn zone_name | None -> let localtime_t = input_tz_file ~ zonename " :/ etc / localtime " ~ filename " :/ etc / localtime " in ( match Zone_cache . find_or_...
module Stable = struct include Core_kernel_private . Time_zone . Stable module V1 = struct type nonrec t = t let t_of_sexp sexp = match sexp with | Sexp . Atom " Local " -> Lazy . force local | Sexp . Atom name -> ( try if String . is_prefix name ~ prefix " : GMT " - || String . is_p...
module Private = struct module Zone_cache = Zone_cache end
module type Extend_zone = sig type t include Identifiable . S with type t := t val find : string -> t option val find_exn : string -> t val local : t Lazy . t val initialized_zones : unit -> ( string * t ) list val init : unit -> unit end
module type Timezone = sig module type Extend_zone = Extend_zone include Core_kernel_private . Time_zone . S with type t = Time . Zone . t include Extend_zone with type t := t module Stable : sig module V1 : sig type nonrec t = t [ @@ deriving bin_io , compare , hash , sexp ] end include Core_...
type unary_op = | Not | Shift of Timedesc . Span . t | Lengthen of Timedesc . Span . t | With_tz of Timedesc . Time_zone . t
type chunking = [ ` Disjoint_intervals | ` By_duration of Timedesc . Span . t | ` By_duration_drop_partial of Timedesc . Span . t | ` At_year_boundary | ` At_month_boundary ]
type chunked_unary_op_on_t = | Chunk_disjoint_interval | Chunk_at_year_boundary | Chunk_at_month_boundary | Chunk_by_duration of { chunk_size : Timedesc . Span . t ; drop_partial : bool ; }
type chunked_unary_op_on_chunked = | Drop of int | Take of int | Take_nth of int | Nth of int | Chunk_again of chunked_unary_op_on_t
type t = | Empty | All | Intervals of ( Timedesc . Span . t * Timedesc . Span . t ) Seq . t | ISO_week_pattern of Int_set . t * Int_set . t | Pattern of Pattern . t | Unary_op of unary_op * t | Inter_seq of t Seq . t | Union_seq of t Seq . t | Pattern_intervals of { mode : [ ` ...
module type S = sig type t include Binable . S with type t := t include Comparable . S with type t := t include Hashable . S with type t := t include Sexpable . S with type t := t include Stringable . S with type t := t module Span : sig type t include Binable . S with type t := t include Comparable...
module type S_kernel_without_zone = Time0_intf . S
module type S_kernel = Time_intf . S
module Stable = struct include Time_float0 . Stable module With_utc_sexp = struct module V2 = struct type nonrec t = t [ @@ deriving bin_io , compare , hash ] let sexp_of_t t = [ % sexp ( to_string_abs_parts t ~ zone : Zone . utc : string list ) ] let t_of_sexp sexp = try match sexp with ...
module Absolute = struct type underlying = Float . t include ( Float : sig type t = float [ @@ deriving bin_io , hash , typerep ] include Comparable . S_common with type t := t include module type of struct include Float . O end end ) include Float . Robust_compare . Make ( struct let robu...
module Date_and_ofday = struct type t = float let of_synthetic_span_since_epoch span = Span . to_sec span let to_synthetic_span_since_epoch t = Span . of_sec t let of_date_ofday date ofday = let days = Float . of_int ( Date0 . Days . diff ( Date0 . Days . of_date date ) Date0 . Days . unix...
let next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval = if Span . ( <= ) interval Span . zero then failwiths ~ here [ :% here ] " Time . next_multiple got nonpositive interval " interval [ % sexp_of : Span . t ] ; let base_to_after = diff after base in if Span . (...
let next_multiple ( ? can_equal_after = false ) ~ base ~ after ~ interval ( ) = next_multiple_internal ~ can_equal_after ~ base ~ after ~ interval ; ;
let prev_multiple ( ? can_equal_before = false ) ~ base ~ before ~ interval ( ) = next_multiple_internal ~ can_equal_after ( : not can_equal_before ) ~ base ~ after ( : sub before interval ) ~ interval ; ;
let now ( ) = let float_ns = Time_now . nanoseconds_since_unix_epoch ( ) |> Int63 . to_float in of_span_since_epoch ( Span . of_sec ( float_ns . * 1E - 9 ) ) ; ;