text
stringlengths
0
601k
let glitch = nsym / 20
let symbols = Array ( . concat [ init 58 ( fun x -> uchar ( 0xff66 + x ) x 1 1 ) 1 ; init 10 ( fun x -> uchar ( 0x30 + x ) x 1 1 ) 1 ; ] )
let sym ( ) = symbols ( . Random . int ( Array . length symbols ) symbols ) symbols
let syms = Array . init nsym ( fun _ -> sym ( ) )
let gen_wait h = ` Wait Random ( . int ( h / 2 ) 2 ) 2 ` Line Random ( . 0 , int ( nsym - h ) h , int ( h + h / 2 ) 2 + 1 , int 2 + 1 ) 1
let gen ( w , h as dim ) dim = let lines = 1 -- w |> List . map @@ fun _ -> if Random . float 1 . < 0 . 1 then gen_line h else gen_wait h in ( dim , lines ) lines
let step ( ( _ , h as dim ) dim , xs ) xs = let xs = xs |> List . map @@ function ` Wait 0 -> gen_line h | ` Wait n -> ` Wait ( n - 1 ) 1 | ` Line ( i , _ , win , k ) k when i - win + k >= h -> gen_wait h | ` Line ( i , s , win , k ) k -> ` Line ( i + k , s , win , k ) k in Random ( . for _ = 0 to int glitch do syms ( ...
let bg = A ( . rgb ~ r : 0 ~ g : 0 ~ b : 0 |> bg ) bg
let fg i n = let chan x = x . * 255 . |> truncate and t = float i . / float n in let t1 = exp ( . - t . / 0 . 02 ) 02 |> chan and t2 = exp ( . - t . / 0 . 45 ) 45 |> chan in A ( . rgb_888 ~ r : t1 ~ b : t1 ~ g : t2 |> fg ) fg
let show ( ( w , h ) h , xs ) xs = let f = function ` Wait _ -> I . void 1 0 | ` Line ( i , sym , win , _ ) _ -> let last = i - win and off = max 0 ( i - h + 1 ) 1 in let rec chars w = let ix = w + last in if 0 <= min ix w then syms ( . sym + ix ) ix :: chars ( w - 1 ) 1 else [ ] in let rec images acc i = function | [ ...
type r = [ Unescape . event | ` Resize of int * int | ` End | ` Timer ]
let event ~ delay t = if Term . pending t then ( Term . event t :> r ) r else let open Unix in match select [ Term . fds t |> fst ] fst [ ] [ ] delay with | ( [ ] , _ , _ ) _ -> ` Timer | ( _ :: _ , _ , _ ) _ -> ( Term . event t :> r ) r | exception Unix_error ( EINTR , _ , _ ) _ -> ( Term . event t :> r ) r
let loop t ~ frame st = let rec go st deadline = let now = Unix . gettimeofday ( ) in if deadline <= now then ( Term . image t ( show st ) st ; go ( step st ) st ( frame . + deadline ) deadline ) else match event ~ delay ( : deadline . - now ) now t with | ` End | ` Key ( ` Escape , _ ) _ | ` Key ( ` ASCII ' C ' , [ ` ...
let ( ) = let t = Term . create ( ) in loop t ~ frame : 0 . 075 ( gen ( Term . size t ) t ) t ; Term . release t
type mode = RampingUp | Flat | RampingDown
let string_of_mode m = match m with | RampingUp -> " RampingUp " | Flat -> " Flat " | RampingDown -> " RampingDown "
let debug = ref false
let disc_state = ref RampingUp
let f1 t_s _ yd = if ! debug then printf " > f called at . % 15e ( % s ) \ n " t_s ( string_of_mode ! disc_state ) ; yd . { idx_t } <- 1 . 0 ; yd . { idx_y } <- match ! disc_state with | RampingUp -> 1 . 0 | Flat -> 0 . 0 | RampingDown -> - 1 . 0
let f2 t_s _ yd = if ! debug then printf " > f called at . % 15e ( % s ) \ n " t_s ( string_of_mode ! disc_state ) ; yd . { idx_t } <- 1 . 0 ; yd . { idx_y } <- if t_s <= 3 . 0 then 1 . 0 else if t_s <= 7 . 0 then 0 . 0 else - 1 . 0
let g _ y gout = gout . { 0 } <- y . { idx_t } . - thresh_1 ; gout . { 1 } <- y . { idx_t } . - thresh_2
let handle_roots r = if ! debug then printf " handle_roots : " ; if Sundials . Roots . detected r 0 then ( if ! debug then printf " up0 " ; disc_state := Flat ) ; if Sundials . Roots . detected r 1 then ( if ! debug then printf " up1 " ; disc_state := RampingDown ) ; if ! debug then printf " \ n "
let rootdata = Sundials . Roots . create 2
let run_experiment with_zeros with_reinit = disc_state := RampingUp ; let y = Sundials . RealArray . of_array [ | t_i ; y_i ] | in let y_nvec = Nvector_serial . wrap y in let gg = if with_zeros then ( 2 , g ) else Cvode . no_roots in let ff = if with_zeros then f1 else f2 in let s = Cvode . ( init Adams default_toleran...
let opt_with_zeros = ref false
let opt_with_reinit = ref true
let args = [ ( " - zeros " , Arg . Set opt_with_zeros , " Use a discrete state and zero - crossings . " ) ; ( " - no - reinit " , Arg . Clear opt_with_reinit , " Do not reinitialize the solver after zero - crossings . " ) ; ( " - debug " , Arg . Set debug , " Print internal debugging messages . " ) ; ]
let _ = Arg . parse args ( fun _ -> ( ) ) " ramp : a simple test of solver behaviour . " ; run_experiment ! opt_with_zeros ! opt_with_reinit
module type S = sig type graph type vertex type edge_label val graph : ? loops : bool -> v : int -> e : int -> unit -> graph val labeled : ( vertex -> vertex -> edge_label ) -> ? loops : bool -> v : int -> e : int -> unit -> graph val random_few_edges : loops : bool -> v : int -> e : int -> graph val random_many_edges ...
module Make ( B : Builder . INT ) = struct open B type graph = G . t type vertex = G . V . t type edge_label = G . E . label open Int64 let max_edges ~ loops ~ v ~ e = if v <= 0 || e < 0 then invalid_arg " random " ; let v64 = of_int v in let max_e = mul v64 ( pred v64 ) in let max_e = if G . is_directed then max_e els...
module P ( G : Sig . P with type V . label = int ) = Make ( Builder . P ( G ) )
module I ( G : Sig . I with type V . label = int ) = Make ( Builder . I ( G ) )
module Planar = struct module type S = sig type graph val graph : ? loops : bool -> xrange : int * int -> yrange : int * int -> prob : float -> int -> graph end module Make ( B : Builder . S with type G . V . label = int * int and type G . E . label = int ) = struct type graph = B . G . t open B . G module Point = stru...
module State = struct type t = { st : int array ; mutable idx : int } let new_state ( ) = { st = Array . make 55 0 ; idx = 0 } let assign st1 st2 = Array . blit st2 . st 0 st1 . st 0 55 ; st1 . idx <- st2 . idx let full_init s seed = let combine accu x = Digest . string ( accu ^ Int . to_string x ) in let extract d = C...
let default = { State . st = [ | 0x3ae2522b ; 0x1d8d4634 ; 0x15b4fad0 ; 0x18b14ace ; 0x12f8a3c4 ; 0x3b086c47 ; 0x16d467d6 ; 0x101d91c7 ; 0x321df177 ; 0x0176c193 ; 0x1ff72bf1 ; 0x1e889109 ; 0x0b464b18 ; 0x2b86b97c ; 0x0891da48 ; 0x03137463 ; 0x085ac5a1 ; 0x15d61f2f ; 0x3bced359 ; 0x29c1c132 ; 0x3a86766e ; 0x366d8c86 ; 0...
let bits ( ) = State . bits default
let int bound = State . int default bound
let int32 bound = State . int32 default bound
let nativeint bound = State . nativeint default bound
let int64 bound = State . int64 default bound
let float scale = State . float default scale
let bool ( ) = State . bool default
let full_init seed = State . full_init default seed
let init seed = State . full_init default [ | seed ] |
let self_init ( ) = full_init ( random_seed ( ) )
let get_state ( ) = State . copy default
let set_state s = State . assign default s
let rec sumsq v i0 i1 = if i0 >= i1 then 0 . 0 else if i1 = i0 + 1 then Stdlib . float v . ( i0 ) . * Stdlib . float v . ( i0 ) else sumsq v i0 ( ( i0 + i1 ) / 2 ) . + sumsq v ( ( i0 + i1 ) / 2 ) i1
let chisquare g n r = if n <= 10 * r then invalid_arg " chisquare " ; let f = Array . make r 0 in for i = 1 to n do let t = g r in f . ( t ) <- f . ( t ) + 1 done ; let t = sumsq f 0 r and r = Stdlib . float r and n = Stdlib . float n in let sr = 2 . 0 . * sqrt r in ( r . - sr , ( r . * t . / n ) . - n , r . + sr )
let st = ref 0
let init_diff r = st := int r
let diff r = let x1 = ! st and x2 = int r in st := x2 ; if x1 >= x2 then x1 - x2 else r + x1 - x2
let st1 = ref 0
let init_diff2 r = st1 := int r ; st2 := int r
let diff2 r = let x1 = ! st1 and x2 = ! st2 and x3 = int r in st1 := x2 ; st2 := x3 ; ( x3 - x2 - x2 + x1 + 2 * r ) mod r ) ********************
let choose ( xs : ' a list ) : ' a = let n = List . length xs in assert ( n > 0 ) ; let i = Random . int n in List . nth xs i
let pick ( i , j ) = assert ( i <= j ) ; i + Random . int ( j - i + 1 )
let between i j = ( i , j )
let at_least i = between i infinity
let at_most j = between 0 j
let full = ( 0 , max_int )
let intersect ( i1 , j1 ) ( i2 , j2 ) = ( max i1 i2 , min j1 j2 )
let intersect intervals = List . fold_left intersect full intervals
let nonempty ( i , j ) = i <= j
let buffer = ref [ ]
let emit tok = buffer := tok :: ! buffer
let reset ( ) = let toks = ! buffer in buffer := [ ] ; List . rev toks
let rec nonterminal nt goal k : unit = assert ( Analysis . minimal nt <= goal ) ; assert ( goal <= Analysis . maximal nt ) ; let prods = Production . foldnt nt ( fun prod prods -> if Production . error_free prod && Analysis . minimal_prod prod 0 <= goal && goal <= Analysis . maximal_prod prod 0 then prod :: prods else ...
let nonterminal nt goal = assert ( ! buffer = [ ] ) ; let k ( ) = ( ) in match nonterminal nt goal k with | ( ) -> reset ( ) | exception e -> buffer := [ ] ; raise e
let nonterminal nt goal = let min , max = Analysis . ( minimal nt , maximal nt ) in let goal = if min <= goal && goal <= max then goal else if goal < min then min else begin assert ( max < goal ) ; assert ( max < infinity ) ; max end in nonterminal nt goal
module State = struct include Array let map2 = map2_exn end
let params : Field . t Sponge . Params . t = Sponge . Params ( . map pasta_p_kimchi ~ f : Field . of_string ) of_string
module Operations = struct let add_assign ~ state i x = Field ( . state ( . i ) i <- state ( . i ) i + x ) x let apply_affine_map ( matrix , constants ) constants v = let dotv row = Array . reduce_exn ( Array . map2_exn row v ~ f : Field ( . * ) ) ~ f : Field ( . + ) in let res = Array . map matrix ~ f : dotv in Array ...
module Digest = struct open Field type nonrec t = t let to_bits ? length x = match length with | None -> unpack x | Some length -> List . take ( unpack x ) x length end
let update ~ state = update ~ state params
let hash ? init = hash ? init params
let pow2 = let rec pow2 acc n = if n = 0 then acc else pow2 Field ( . acc + acc ) acc ( n - 1 ) 1 in Memo . general ~ hashable : Int . hashable ( fun n -> pow2 Field . one n ) n
module Checked = struct module Inputs = Pickles . Step_main_inputs . Sponge . Permutation module Digest = struct open Pickles . Impls . Step . Field type nonrec t = t let to_bits ( ? length = Field . size_in_bits ) size_in_bits ( x : t ) t = List . take ( choose_preimage_var ~ length : Field . size_in_bits x ) x length...
let read_typ ( { field_elements ; packeds } : _ Input . Chunked . t ) t = let open Pickles . Impls . Step in let open As_prover in { Input . Chunked . field_elements = Array . map ~ f ( : read Field . typ ) typ field_elements ; packeds = Array . map packeds ~ f ( : fun ( x , i ) i -> ( read Field . typ x , i ) i ) i }
let read_typ ' input : _ Pickles . Impls . Step . Internal_Basic . As_prover . t = fun _ -> read_typ input [ %% endif ] endif
let pack_input = Input . Chunked . pack_to_fields ~ pow2 ( module Field ) Field
let prefix_to_field ( s : string ) string = let bits_per_character = 8 in assert ( bits_per_character * String . length s < Field . size_in_bits ) size_in_bits ; Field . project Fold_lib . Fold ( . to_list ( string_bits ( s :> string ) string ) string ) string
let salt ( s : string ) string = update ~ state : initial_state [ | prefix_to_field s ] | let x1 = Field . random ( ) in let x2 = Field . random ( ) in let x3 = Field . random ( ) in let x4 = Field . random ( ) in let s_full = update ~ state : initial_state [ | x1 ; x2 ; x3 ; x4 ] | in let s_it = update ~ state ( : upd...
module Legacy = struct module Input = Random_oracle_input . Legacy module State = State let params : Field . t Sponge . Params . t = Sponge . Params ( . map pasta_p_legacy ~ f : Field . of_string ) of_string module Rounds = struct let rounds_full = 63 let initial_ark = true let rounds_partial = 0 end module Inputs = st...
module Chunked = struct type ' field t = { field_elements : ' field array ; packeds : ( ' field * int ) int array } [ @@ deriving sexp , compare ] compare let append ( t1 : _ t ) t ( t2 : _ t ) t = { field_elements = Array . append t1 . field_elements t2 . field_elements ; packeds = Array . append t1 . packeds t2 . pac...
module Legacy = struct type ( ' field , ' bool ) ' bool t = { field_elements : ' field array ; bitstrings : ' bool list array } [ @@ deriving sexp , compare ] compare let append t1 t2 = { field_elements = Array . append t1 . field_elements t2 . field_elements ; bitstrings = Array . append t1 . bitstrings t2 . bitstring...
type tree = { mask : ( int , int ) Hashtbl . t ; unmask : ( int , int ) Hashtbl . t ; mutable new_mask : int ; mutable inf_list : Mods . IntSet . t ; size : int ; weight_of_nodes : float array ; weight_of_subtrees : float array ; unbalanced_events_by_layer : int list array ; unbalanced_events : bool array ; layer : int...
let mask t i = try Hashtbl . find t . mask i with Not_found -> let m = t . new_mask in let ( ) = t . new_mask <- m + 1 in let ( ) = Hashtbl . replace t . mask i m in let ( ) = Hashtbl . replace t . unmask m i in m
let unmask t m = try Hashtbl . find t . unmask m with Not_found -> invalid_arg " Random_tree : incoherent hash "
let is_infinite i t = let i = mask t i in Mods . IntSet . mem i t . inf_list
let find i t = let i = mask t i in t . weight_of_nodes . ( i )
let copy t = { mask = Hashtbl . copy t . mask ; unmask = Hashtbl . copy t . unmask ; new_mask = t . new_mask ; size = t . size ; weight_of_nodes = Array . copy t . weight_of_nodes ; weight_of_subtrees = Array . copy t . weight_of_subtrees ; layer = Array . copy t . layer ; consistent = t . consistent ; unbalanced_event...
let copy_vect_in t t1 = Array . iteri ( fun i a -> t1 . ( i ) <- a ) t
let copy_in t1 t2 = let ( ) = copy_vect_in t1 . weight_of_nodes t2 . weight_of_nodes in let ( ) = copy_vect_in t1 . weight_of_subtrees t2 . weight_of_subtrees in let ( ) = copy_vect_in t1 . layer t2 . layer in let ( ) = copy_vect_in t1 . unbalanced_events t2 . unbalanced_events in let ( ) = copy_vect_in t1 . unbalanced...
let is_root i = i = 1
let declare_unbalanced i t = let ( ) = if not t . unbalanced_events . ( i ) then let l = t . layer . ( i ) in let ( ) = t . unbalanced_events . ( i ) <- true in t . unbalanced_events_by_layer . ( l ) <- i :: ( t . unbalanced_events_by_layer . ( l ) ) in t . consistent <- false
let update_structure t = if t . consistent then t else let n_layer = t . layer . ( t . size ) in let update_structure_aux i = let ( ) = t . weight_of_subtrees . ( i ) <- t . weight_of_nodes . ( i ) . + ( if 2 * i > t . size then 0 . else t . weight_of_subtrees . ( 2 * i ) ) . + ( if 2 * i + 1 > t . size then 0 . else t...
let create n = let t_node = Array . make ( n + 1 ) 0 . in let t_subtree = Array . make ( n + 1 ) 0 . in let layer = Array . make ( n + 1 ) 0 in let ( ) = let rec aux k current_layer layer_end = if k <= n then if k > layer_end then aux k ( current_layer + 1 ) ( 2 * layer_end + 1 ) else let ( ) = layer . ( k ) <- current...
let add i w t = let i = mask t i in if w < 0 . then failwith " Negative value forbidden in Random_tree " else let w = if w = infinity then let ( ) = t . inf_list <- Mods . IntSet . add i t . inf_list in 0 . else let ( ) = t . inf_list <- Mods . IntSet . remove i t . inf_list in w in let ( ) = t . weight_of_nodes . ( i ...
let total t = if Mods . IntSet . is_empty t . inf_list then let t = update_structure t in if t . size = 0 then 0 . else t . weight_of_subtrees . ( 1 ) else infinity