Skip to content

Commit

Permalink
evaluate pickles dummy stuff lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Aug 16, 2023
1 parent 78de73e commit 7d9fa28
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/lib/pickles/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let pad_messages_for_next_wrap_proof
(messages_for_next_wrap_proofs :
local_max_proofs_verifieds
H1.T(Proof_.Messages_for_next_proof_over_same_field.Wrap).t ) =
let dummy_chals = Dummy.Ipa.Wrap.challenges in
let dummy_chals = Lazy.force Dummy.Ipa.Wrap.challenges in
let module Messages =
H1.T (Proof_.Messages_for_next_proof_over_same_field.Wrap) in
let module Maxes = H1.T (Nat) in
Expand Down
49 changes: 29 additions & 20 deletions src/lib/pickles/dummy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,62 @@ open Pickles_types
open Backend
open Composition_types
open Common
open Lazy.Let_syntax

let wrap_domains = Common.wrap_domains

let evals =
let open Plonk_types in
let e =
Evals.map (Evaluation_lengths.create ~of_int:Fn.id) ~f:(fun n ->
let a () = Array.create ~len:n (Ro.tock ()) in
(a (), a ()) )
in
let ex =
{ All_evals.With_public_input.evals = e
; public_input = (Ro.tock (), Ro.tock ())
}
in
{ All_evals.ft_eval1 = Ro.tock (); evals = ex }
lazy
(let open Plonk_types in
let e =
Evals.map (Evaluation_lengths.create ~of_int:Fn.id) ~f:(fun n ->
let a () = Array.create ~len:n (Ro.tock ()) in
(a (), a ()) )
in
let ex =
{ All_evals.With_public_input.evals = e
; public_input = (Ro.tock (), Ro.tock ())
}
in
{ All_evals.ft_eval1 = Ro.tock (); evals = ex })

let evals_combined =
let%map evals = evals in
Plonk_types.All_evals.map evals ~f1:Fn.id
~f2:(Array.reduce_exn ~f:Backend.Tock.Field.( + ))

module Ipa = struct
module Wrap = struct
let challenges =
Vector.init Tock.Rounds.n ~f:(fun _ ->
let prechallenge = Ro.scalar_chal () in
{ Bulletproof_challenge.prechallenge } )
lazy
(Vector.init Tock.Rounds.n ~f:(fun _ ->
let prechallenge = Ro.scalar_chal () in
{ Bulletproof_challenge.prechallenge } ) )

let challenges_computed =
let%map challenges = challenges in
Vector.map challenges ~f:(fun { prechallenge } : Tock.Field.t ->
Ipa.Wrap.compute_challenge prechallenge )

let sg =
lazy (time "dummy wrap sg" (fun () -> Ipa.Wrap.compute_sg challenges))
let%map challenges = challenges in
time "dummy wrap sg" (fun () -> Ipa.Wrap.compute_sg challenges)
end

module Step = struct
let challenges =
Vector.init Tick.Rounds.n ~f:(fun _ ->
let prechallenge = Ro.scalar_chal () in
{ Bulletproof_challenge.prechallenge } )
lazy
(Vector.init Tick.Rounds.n ~f:(fun _ ->
let prechallenge = Ro.scalar_chal () in
{ Bulletproof_challenge.prechallenge } ) )

let challenges_computed =
let%map challenges = challenges in
Vector.map challenges ~f:(fun { prechallenge } : Tick.Field.t ->
Ipa.Step.compute_challenge prechallenge )

let sg =
lazy (time "dummy wrap sg" (fun () -> Ipa.Step.compute_sg challenges))
let%map challenges = challenges in
time "dummy wrap sg" (fun () -> Ipa.Step.compute_sg challenges)
end
end
6 changes: 6 additions & 0 deletions src/lib/pickles/dummy.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ module Ipa : sig
Composition_types.Bulletproof_challenge.t
, Pickles_types.Nat.z Backend.Tock.Rounds.plus_n )
Pickles_types.Vector.t
lazy_t

val challenges_computed :
( Backend.Tock.Field.t
, Pickles_types.Nat.z Backend.Tock.Rounds.plus_n )
Pickles_types.Vector.t
lazy_t

val sg : (Pasta_bindings.Fp.t * Pasta_bindings.Fp.t) lazy_t
end
Expand All @@ -23,11 +25,13 @@ module Ipa : sig
Composition_types.Bulletproof_challenge.t
, Pickles_types.Nat.z Backend.Tick.Rounds.plus_n )
Pickles_types.Vector.t
lazy_t

val challenges_computed :
( Backend.Tick.Field.t
, Pickles_types.Nat.z Backend.Tick.Rounds.plus_n )
Pickles_types.Vector.t
lazy_t

val sg : (Pasta_bindings.Fq.t * Pasta_bindings.Fq.t) lazy_t
end
Expand All @@ -43,9 +47,11 @@ val evals :
( Backend.Tock.Field.t
, Backend.Tock.Field.t array )
Pickles_types.Plonk_types.All_evals.t
lazy_t

(** [evals_combined] is a constant *)
val evals_combined :
( Backend.Tock.Field.t
, Backend.Tock.Field.t )
Pickles_types.Plonk_types.All_evals.t
lazy_t
31 changes: 17 additions & 14 deletions src/lib/pickles/proof.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ let dummy (type w h r) (_w : w Nat.t) (h : h Nat.t)
; domain_log2 =
Branch_data.Domain_log2.of_int_exn domain_log2
}
; bulletproof_challenges = Dummy.Ipa.Step.challenges
; bulletproof_challenges = Lazy.force Dummy.Ipa.Step.challenges
; plonk =
{ alpha = scalar_chal ()
; beta = chal ()
Expand All @@ -163,15 +163,16 @@ let dummy (type w h r) (_w : w Nat.t) (h : h Nat.t)
; messages_for_next_wrap_proof =
{ challenge_polynomial_commitment = Lazy.force Dummy.Ipa.Step.sg
; old_bulletproof_challenges =
Vector.init h ~f:(fun _ -> Dummy.Ipa.Wrap.challenges)
Vector.init h ~f:(fun _ ->
Lazy.force Dummy.Ipa.Wrap.challenges )
}
}
; messages_for_next_step_proof =
{ app_state = ()
; old_bulletproof_challenges =
(* Not sure if this should be w or h honestly ...*)
Vector.init most_recent_width ~f:(fun _ ->
Dummy.Ipa.Step.challenges )
Lazy.force Dummy.Ipa.Step.challenges )
(* TODO: Should this be wrap? *)
; challenge_polynomial_commitments =
Vector.init most_recent_width ~f:(fun _ ->
Expand All @@ -186,17 +187,19 @@ let dummy (type w h r) (_w : w Nat.t) (h : h Nat.t)
; lookup = None
}
; openings =
{ proof =
{ lr =
Array.init (Nat.to_int Tock.Rounds.n) ~f:(fun _ -> (g0, g0))
; z_1 = Ro.tock ()
; z_2 = Ro.tock ()
; delta = g0
; challenge_polynomial_commitment = g0
}
; evals = Dummy.evals.evals.evals
; ft_eval1 = Dummy.evals.ft_eval1
}
(let evals = Lazy.force Dummy.evals in
{ proof =
{ lr =
Array.init (Nat.to_int Tock.Rounds.n) ~f:(fun _ ->
(g0, g0) )
; z_1 = Ro.tock ()
; z_2 = Ro.tock ()
; delta = g0
; challenge_polynomial_commitment = g0
}
; evals = evals.evals.evals
; ft_eval1 = evals.ft_eval1
} )
}
; prev_evals =
(let e =
Expand Down
7 changes: 4 additions & 3 deletions src/lib/pickles/step.ml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ struct
(Vector.map
t.statement.messages_for_next_step_proof
.old_bulletproof_challenges ~f:Ipa.Step.compute_challenges )
Local_max_proofs_verified.n Dummy.Ipa.Step.challenges_computed
Local_max_proofs_verified.n
(Lazy.force Dummy.Ipa.Step.challenges_computed)
; wrap_proof =
{ opening =
{ t.proof.openings.proof with challenge_polynomial_commitment }
Expand Down Expand Up @@ -720,7 +721,7 @@ struct
{ challenge_polynomial_commitment = Lazy.force Dummy.Ipa.Step.sg
; old_bulletproof_challenges =
Vector.init Max_proofs_verified.n ~f:(fun _ ->
Dummy.Ipa.Wrap.challenges_computed )
Lazy.force Dummy.Ipa.Wrap.challenges_computed )
}
in
Wrap_hack.hash_messages_for_next_wrap_proof Max_proofs_verified.n t
Expand Down Expand Up @@ -889,7 +890,7 @@ struct
; evals =
{ With_public_input.evals = es; public_input = x_hat }
} ) )
lte Max_proofs_verified.n Dummy.evals
lte Max_proofs_verified.n (Lazy.force Dummy.evals)
}
, Option.value_exn !return_value
, Option.value_exn !auxiliary_value
Expand Down
5 changes: 3 additions & 2 deletions src/lib/pickles/unfinalized.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ module Constant = struct
}
in
let evals =
Plonk_types.Evals.to_in_circuit Dummy.evals_combined.evals.evals
Plonk_types.Evals.to_in_circuit
(Lazy.force Dummy.evals_combined).evals.evals
in
let env =
let module Env_bool = struct
Expand Down Expand Up @@ -138,7 +139,7 @@ module Constant = struct
}
; combined_inner_product = Shifted_value (tock ())
; xi = Scalar_challenge.create one_chal
; bulletproof_challenges = Dummy.Ipa.Wrap.challenges
; bulletproof_challenges = Lazy.force Dummy.Ipa.Wrap.challenges
; b = Shifted_value (tock ())
}
; should_finalize = false
Expand Down
40 changes: 21 additions & 19 deletions src/lib/pickles/wrap_hack.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open Core_kernel
open Backend
open Pickles_types
open Lazy.Let_syntax

(* The actual "accumulator" for the wrap proof contains a vector of elements,
each of which is a vector of bulletproof challenges.
Expand All @@ -25,7 +26,7 @@ module Padded_length = Nat.N2

(* Pad up to length 2 by preprending dummy values. *)
let pad_vector (type a) ~dummy (v : (a, _) Vector.t) =
Vector.extend_front_exn v Padded_length.n dummy
Vector.extend_front_exn v Padded_length.n (Lazy.force dummy)

(* Specialized padding function. *)
let pad_challenges (chalss : (_ Vector.t, _) Vector.t) =
Expand All @@ -35,10 +36,11 @@ let pad_challenges (chalss : (_ Vector.t, _) Vector.t) =
let pad_accumulator (xs : (Tock.Proof.Challenge_polynomial.t, _) Vector.t) =
pad_vector xs
~dummy:
{ Tock.Proof.Challenge_polynomial.commitment =
Lazy.force Dummy.Ipa.Wrap.sg
; challenges = Vector.to_array Dummy.Ipa.Wrap.challenges_computed
}
(let%map chals = Dummy.Ipa.Wrap.challenges_computed in
{ Tock.Proof.Challenge_polynomial.commitment =
Lazy.force Dummy.Ipa.Wrap.sg
; challenges = Vector.to_array chals
} )
|> Vector.to_list

(* Hash the me only, padding first. *)
Expand Down Expand Up @@ -82,31 +84,31 @@ module Checked = struct
let pad_challenges (chalss : (_ Vector.t, _) Vector.t) =
pad_vector
~dummy:
(Vector.map ~f:Impls.Wrap.Field.constant
Dummy.Ipa.Wrap.challenges_computed )
(let%map chals = Dummy.Ipa.Wrap.challenges_computed in
Vector.map ~f:Impls.Wrap.Field.constant chals )
chalss

let pad_commitments (commitments : _ Vector.t) =
pad_vector
~dummy:
(Tuple_lib.Double.map ~f:Impls.Step.Field.constant
(Lazy.force Dummy.Ipa.Wrap.sg) )
(let%map sg = Dummy.Ipa.Wrap.sg in
Tuple_lib.Double.map ~f:Impls.Step.Field.constant sg )
commitments

(* We precompute the sponge states that would result from absorbing
0, 1, or 2 dummy challenge vectors. This is used to speed up hashing
inside the circuit. *)
let dummy_messages_for_next_wrap_proof_sponge_states =
lazy
(let module S = Tock_field_sponge.Field in
let full_state s = (S.state s, s.sponge_state) in
let sponge = S.create Tock_field_sponge.params in
let s0 = full_state sponge in
Vector.iter ~f:(S.absorb sponge) Dummy.Ipa.Wrap.challenges_computed ;
let s1 = full_state sponge in
Vector.iter ~f:(S.absorb sponge) Dummy.Ipa.Wrap.challenges_computed ;
let s2 = full_state sponge in
[| s0; s1; s2 |] )
let module S = Tock_field_sponge.Field in
let full_state s = (S.state s, s.sponge_state) in
let sponge = S.create Tock_field_sponge.params in
let s0 = full_state sponge in
let%map chals = Dummy.Ipa.Wrap.challenges_computed in
Vector.iter ~f:(S.absorb sponge) chals ;
let s1 = full_state sponge in
Vector.iter ~f:(S.absorb sponge) chals ;
let s2 = full_state sponge in
[| s0; s1; s2 |]

let hash_constant_messages_for_next_wrap_proof =
hash_messages_for_next_wrap_proof
Expand Down

0 comments on commit 7d9fa28

Please sign in to comment.