11use bitvec:: array:: BitArray ;
22use mosaic_cac_types:: {
3- AdaptorMsg , AllGarblingSeeds , AllGarblingTableCommitments , ChallengeIndices , ChallengeMsg ,
4- ChallengeResponseMsg , CommitMsg , EvalGarblingSeeds , EvalGarblingTableCommitments ,
5- EvaluationIndices , HasMsgId , InputShares , OutputShares , ReservedDepositInputShares ,
3+ AdaptorMsg , AllGarblingSeeds , AllGarblingTableCommitments , AllPolynomials , ChallengeIndices ,
4+ ChallengeMsg , ChallengeResponseMsg , CommitMsg , EvalGarblingSeeds , EvalGarblingTableCommitments ,
5+ EvaluationIndices , HasMsgId , Index , InputShares , OutputShares , ReservedDepositInputShares ,
66 ReservedInputShares , ReservedWithdrawalInputShares , Seed , SetupInputs ,
77 state_machine:: garbler:: {
88 Action , AdaptorVerificationData , CompleteAdaptorSignaturesData , GarblerDepositInitData ,
99 Input ,
1010 } ,
1111} ;
12+ use mosaic_common:: constants:: N_CIRCUITS ;
1213
1314use super :: {
1415 artifact:: GarblerArtifactStore ,
@@ -31,73 +32,114 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
3132 seed : data. seed ,
3233 setup_inputs : data. setup_inputs ,
3334 } ) ;
34- state. step = Step :: GeneratingPolynomials ;
3535
36- // generate actions
37- let seed = state. config . expect ( "just set" ) . seed ;
38- actions. push ( Action :: GeneratePolynomials ( seed) ) ;
36+ let polynomials = generate_polynomials ( data. seed ) ;
37+
38+ state. artifact_store . save_polynomials ( & polynomials) . await ?;
39+ state. step = Step :: GeneratingPolynomialCommitments ;
40+
41+ // Get polynomials directly from db
42+ actions. push ( Action :: GeneratePolynomialCommitments ) ;
3943 }
4044 _ => return Err ( SMError :: UnexpectedInput ) ,
4145 }
4246 }
43- Input :: PolynomialsGenerated ( polynomials , commitments) => {
47+ Input :: PolynomialCommitmentsGenerated ( commitments) => {
4448 match state. step {
45- Step :: GeneratingPolynomials => {
49+ Step :: GeneratingPolynomialCommitments => {
4650 // state update
47- state. artifact_store . save_polynomials ( & polynomials) . await ?;
4851 state
4952 . artifact_store
5053 . save_polynomial_commitments ( & commitments)
5154 . await ?;
52- state. step = Step :: GeneratingShares ;
55+ state. step = Step :: GeneratingShares {
56+ generated : BitArray :: ZERO ,
57+ } ;
5358
5459 // generate actions
55- actions. push ( Action :: GenerateShares ( polynomials) ) ;
60+ for idx in 0 ..N_CIRCUITS {
61+ let index = Index :: new ( idx + 1 ) . expect ( "valid index" ) ;
62+ actions. push ( Action :: GenerateShares ( index) ) ;
63+ }
5664 }
5765 _ => return Err ( SMError :: UnexpectedInput ) ,
5866 }
5967 }
60- Input :: SharesGenerated ( input_shares, output_shares) => {
61- match state. step {
62- Step :: GeneratingShares => {
68+ Input :: SharesGenerated ( index, input_shares, output_shares) => {
69+ match & mut state. step {
70+ Step :: GeneratingShares { generated } => {
71+ let idx = index. get ( ) . checked_sub ( 1 ) . ok_or_else ( || {
72+ // not expecting reserved (0) index
73+ SMError :: InvalidInputData
74+ } ) ?;
75+ if generated[ idx] {
76+ // already have this data
77+ return Err ( SMError :: InvalidInputData ) ;
78+ }
79+
6380 // state update
81+ generated. set ( idx, true ) ;
6482 state
6583 . artifact_store
66- . save_shares ( input_shares. as_ref ( ) , output_shares. as_ref ( ) )
84+ . save_shares_for_index ( index , input_shares. as_ref ( ) , output_shares. as_ref ( ) )
6785 . await ?;
6886
69- state. step = Step :: GeneratingTableCommitments ;
87+ if generated. all ( ) {
88+ let config = require_config ( state) ?;
89+ let seeds = Box :: new ( generate_garbling_table_seeds ( config. seed ) ) ;
7090
71- // generate actions
72- let config = require_config ( state) ?;
73- let seeds = generate_garbling_table_seeds ( config. seed ) ;
74- actions. push ( Action :: GenerateTableCommitments (
75- Box :: new ( seeds) ,
76- input_shares,
77- output_shares,
78- ) ) ;
91+ // generate actions
92+ for idx in 0 ..N_CIRCUITS {
93+ let index = Index :: new ( idx + 1 ) . expect ( "valid index" ) ;
94+ let seed = seeds[ idx] ;
95+ actions. push ( Action :: GenerateTableCommitment ( index, seed) ) ;
96+ }
97+
98+ state. step = Step :: GeneratingTableCommitments {
99+ seeds,
100+ generated : BitArray :: ZERO ,
101+ } ;
102+ }
79103 }
80104 _ => return Err ( SMError :: UnexpectedInput ) ,
81105 }
82106 }
83- Input :: TableCommitmentsGenerated ( garbling_table_commitments) => {
84- match state. step {
85- Step :: GeneratingTableCommitments => {
107+ Input :: TableCommitmentGenerated ( index, commitment) => {
108+ match & mut state. step {
109+ Step :: GeneratingTableCommitments { generated, .. } => {
110+ let idx = index. get ( ) . checked_sub ( 1 ) . ok_or_else ( || {
111+ // not expecting reserved (0) index
112+ SMError :: InvalidInputData
113+ } ) ?;
114+ if generated[ idx] {
115+ // already have this data
116+ return Err ( SMError :: InvalidInputData ) ;
117+ }
118+
86119 // state update
120+ generated. set ( idx, true ) ;
87121 state
88122 . artifact_store
89- . save_garbling_table_commitments ( garbling_table_commitments . as_ref ( ) )
123+ . save_garbling_table_commitment ( index , & commitment )
90124 . await ?;
91- state. step = Step :: SendingCommit ;
92125
93- // generate actions
94- let polynomial_commitments =
95- state. artifact_store . load_polynomial_commitments ( ) . await ?;
96- let commit_msg = CommitMsg {
97- polynomial_commitments,
98- garbling_table_commitments,
99- } ;
100- actions. push ( Action :: SendCommitMsg ( commit_msg) ) ;
126+ if generated. all ( ) {
127+ state. step = Step :: SendingCommit ;
128+
129+ // generate actions
130+ let polynomial_commitments =
131+ state. artifact_store . load_polynomial_commitments ( ) . await ?;
132+ let garbling_table_commitments = state
133+ . artifact_store
134+ . load_all_garbling_table_commitments ( )
135+ . await ?;
136+ let commit_msg = CommitMsg {
137+ polynomial_commitments,
138+ garbling_table_commitments,
139+ } ;
140+ actions. push ( Action :: SendCommitMsg ( commit_msg) ) ;
141+ }
142+ // else stay on same step and wait for all table commitments to be generated
101143 }
102144 _ => return Err ( SMError :: UnexpectedInput ) ,
103145 }
@@ -181,24 +223,26 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
181223
182224 let garbling_table_commitments = state
183225 . artifact_store
184- . load_garbling_table_commitments ( )
226+ . load_all_garbling_table_commitments ( )
185227 . await ?;
186- let eval_commitments =
187- get_eval_commitments ( & eval_indices, garbling_table_commitments. as_ref ( ) ) ;
228+ let eval_commitments = Box :: new ( get_eval_commitments (
229+ & eval_indices,
230+ garbling_table_commitments. as_ref ( ) ,
231+ ) ) ;
188232
189233 let config = require_config ( state) ?;
190234 let garbling_seeds = generate_garbling_table_seeds ( config. seed ) ;
191- let eval_seeds = get_eval_seeds ( & eval_indices, & garbling_seeds) ;
192-
193- state. step = Step :: TransferringGarblingTables {
194- eval_seeds : Box :: new ( eval_seeds) ,
195- eval_commitments : Box :: new ( eval_commitments) ,
196- transferred : BitArray :: ZERO ,
197- } ;
235+ let eval_seeds = Box :: new ( get_eval_seeds ( & eval_indices, & garbling_seeds) ) ;
198236
199237 for seed in eval_seeds. as_ref ( ) {
200238 actions. push ( Action :: TransferGarblingTable ( * seed) ) ;
201239 }
240+
241+ state. step = Step :: TransferringGarblingTables {
242+ eval_seeds,
243+ eval_commitments,
244+ transferred : BitArray :: ZERO ,
245+ } ;
202246 }
203247 _ => return Err ( SMError :: UnexpectedInput ) ,
204248 } ,
@@ -449,29 +493,33 @@ pub(crate) async fn restore<S: GarblerArtifactStore>(state: &State<S>) -> SMResu
449493
450494 match & state. step {
451495 Step :: Uninit => { }
452- Step :: GeneratingPolynomials => {
453- let config = require_config ( state) ?;
454- actions. push ( Action :: GeneratePolynomials ( config. seed ) ) ;
496+ Step :: GeneratingPolynomialCommitments => {
497+ actions. push ( Action :: GeneratePolynomialCommitments ) ;
455498 }
456- Step :: GeneratingShares => {
457- let polynomials = state. artifact_store . load_polynomials ( ) . await ?;
458- actions. push ( Action :: GenerateShares ( polynomials) ) ;
499+ Step :: GeneratingShares { generated } => {
500+ for idx in 0 ..N_CIRCUITS {
501+ if generated[ idx] {
502+ continue ;
503+ }
504+ let index = Index :: new ( idx + 1 ) . expect ( "valid index" ) ;
505+ actions. push ( Action :: GenerateShares ( index) ) ;
506+ }
459507 }
460- Step :: GeneratingTableCommitments => {
461- let config = require_config ( state ) ? ;
462- let seeds = generate_garbling_table_seeds ( config . seed ) ;
463- let ( input_shares , output_shares ) = state . artifact_store . load_shares ( ) . await ? ;
464- actions . push ( Action :: GenerateTableCommitments (
465- Box :: new ( seeds ) ,
466- input_shares ,
467- output_shares ,
468- ) ) ;
508+ Step :: GeneratingTableCommitments { seeds , generated } => {
509+ for idx in 0 .. N_CIRCUITS {
510+ if generated [ idx ] {
511+ continue ;
512+ }
513+ let index = Index :: new ( idx + 1 ) . expect ( "valid index" ) ;
514+ let seed = seeds [ idx ] ;
515+ actions . push ( Action :: GenerateTableCommitment ( index , seed ) ) ;
516+ }
469517 }
470518 Step :: SendingCommit => {
471519 let polynomial_commitments = state. artifact_store . load_polynomial_commitments ( ) . await ?;
472520 let garbling_table_commitments = state
473521 . artifact_store
474- . load_garbling_table_commitments ( )
522+ . load_all_garbling_table_commitments ( )
475523 . await ?;
476524 let commit_msg = CommitMsg {
477525 polynomial_commitments,
@@ -621,6 +669,11 @@ fn require_config<S>(state: &State<S>) -> SMResult<&Config> {
621669 . ok_or_else ( || SMError :: StateInconsistency ( "expected config to not be None" ) )
622670}
623671
672+ #[ expect( unused_variables) ]
673+ fn generate_polynomials ( seed : Seed ) -> AllPolynomials {
674+ todo ! ( )
675+ }
676+
624677#[ expect( unused_variables) ]
625678fn generate_garbling_table_seeds ( base_seed : Seed ) -> AllGarblingSeeds {
626679 todo ! ( )
0 commit comments