Skip to content

Commit eff8976

Browse files
committed
config to option, add new_empty on state
1 parent 18e2c43 commit eff8976

3 files changed

Lines changed: 54 additions & 18 deletions

File tree

crates/cac/protocol/src/evaluator/state.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@ use std::collections::HashMap;
22

33
use mosaic_cac_types::{DepositId, MsgId, Seed};
44

5-
use super::artifact::EvaluatorArtifactStore;
65
use crate::evaluator::deposit::DepositState;
76

87
#[derive(Debug)]
98
#[expect(dead_code)]
10-
pub struct State<S: EvaluatorArtifactStore> {
11-
pub(crate) config: Config,
9+
pub struct State<S> {
10+
pub(crate) config: Option<Config>,
1211
pub(crate) context: Context,
1312
pub(crate) step: Step,
1413
pub(crate) deposits: HashMap<DepositId, DepositState>,
1514
pub(crate) artifact_store: S,
1615
}
1716

17+
impl<S> State<S> {
18+
pub fn new_empty(artifact_store: S) -> Self {
19+
Self {
20+
config: None,
21+
context: Context::default(),
22+
step: Step::Uninit,
23+
deposits: HashMap::default(),
24+
artifact_store,
25+
}
26+
}
27+
}
28+
1829
/// Immutable state that is set during init and never updated
1930
#[derive(Debug)]
2031
#[expect(dead_code)]

crates/cac/protocol/src/garbler/state.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,31 @@ use mosaic_cac_types::{
66
};
77
use mosaic_common::constants::N_EVAL_CIRCUITS;
88

9-
use super::{artifact::GarblerArtifactStore, deposit::DepositState};
9+
use super::deposit::DepositState;
1010

1111
#[derive(Debug)]
12-
pub struct State<S: GarblerArtifactStore> {
13-
pub(crate) config: Config,
12+
pub struct State<S> {
13+
pub(crate) config: Option<Config>,
1414
pub(crate) context: Context,
1515
pub(crate) step: Step,
1616
pub(crate) deposits: HashMap<DepositId, DepositState>,
1717
pub(crate) artifact_store: S,
1818
}
1919

20+
impl<S> State<S> {
21+
pub fn new_empty(artifact_store: S) -> Self {
22+
Self {
23+
config: None,
24+
context: Context::default(),
25+
step: Step::Uninit,
26+
deposits: HashMap::new(),
27+
artifact_store,
28+
}
29+
}
30+
}
31+
2032
/// Immutable state that is set during init and never updated
21-
#[derive(Debug)]
33+
#[derive(Debug, Clone, Copy)]
2234
pub struct Config {
2335
pub(crate) seed: Seed,
2436
pub(crate) setup_inputs: SetupInputs,

crates/cac/protocol/src/garbler/stf.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
2727
match state.step {
2828
Step::Uninit => {
2929
// state update
30-
state.config = Config {
30+
state.config = Some(Config {
3131
seed: data.seed,
3232
setup_inputs: data.setup_inputs,
33-
};
33+
});
3434
state.step = Step::GeneratingPolynomials;
3535

3636
// generate actions
37-
actions.push(Action::GeneratePolynomials(state.config.seed));
37+
let seed = state.config.expect("just set").seed;
38+
actions.push(Action::GeneratePolynomials(seed));
3839
}
3940
_ => return Err(SMError::UnexpectedInput),
4041
}
@@ -71,7 +72,8 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
7172
state.step = Step::GeneratingTableCommitments;
7273

7374
// generate actions
74-
let seeds = generate_garbling_table_seeds(state.config.seed);
75+
let config = require_config(&state)?;
76+
let seeds = generate_garbling_table_seeds(config.seed);
7577
actions.push(Action::GenerateTableCommitments(
7678
Box::new(seeds),
7779
input_shares,
@@ -135,13 +137,14 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
135137
let msg_id = challenge_msg.id();
136138
let (input_shares, output_shares) =
137139
state.artifact_store.load_shares().await?;
138-
let seeds = generate_garbling_table_seeds(state.config.seed);
140+
let config = require_config(&state)?;
141+
let seeds = generate_garbling_table_seeds(config.seed);
139142
let challenge_response_msg = create_challenge_response_msg(
140143
challenge_msg.challenge_indices.as_ref(),
141144
input_shares,
142145
output_shares,
143146
seeds,
144-
state.config.setup_inputs,
147+
config.setup_inputs,
145148
);
146149
state
147150
.artifact_store
@@ -186,7 +189,8 @@ pub(crate) async fn stf<S: GarblerArtifactStore>(
186189
let eval_commitments =
187190
get_eval_commitments(&eval_indices, garbling_table_commitments.as_ref());
188191

189-
let garbling_seeds = generate_garbling_table_seeds(state.config.seed);
192+
let config = require_config(&state)?;
193+
let garbling_seeds = generate_garbling_table_seeds(config.seed);
190194
let eval_seeds = get_eval_seeds(&eval_indices, &garbling_seeds);
191195

192196
state.step = Step::TransferringGarblingTables {
@@ -449,14 +453,16 @@ pub(crate) async fn restore<S: GarblerArtifactStore>(state: &State<S>) -> SMResu
449453
match &state.step {
450454
Step::Uninit => {}
451455
Step::GeneratingPolynomials => {
452-
actions.push(Action::GeneratePolynomials(state.config.seed));
456+
let config = require_config(state)?;
457+
actions.push(Action::GeneratePolynomials(config.seed));
453458
}
454459
Step::GeneratingShares => {
455460
let polynomials = state.artifact_store.load_polynomials().await?;
456461
actions.push(Action::GenerateShares(polynomials));
457462
}
458463
Step::GeneratingTableCommitments => {
459-
let seeds = generate_garbling_table_seeds(state.config.seed);
464+
let config = require_config(state)?;
465+
let seeds = generate_garbling_table_seeds(config.seed);
460466
let (input_shares, output_shares) = state.artifact_store.load_shares().await?;
461467
actions.push(Action::GenerateTableCommitments(
462468
Box::new(seeds),
@@ -485,13 +491,14 @@ pub(crate) async fn restore<S: GarblerArtifactStore>(state: &State<S>) -> SMResu
485491
};
486492
let challenge_indices = state.artifact_store.load_challenge_indices().await?;
487493
let (input_shares, output_shares) = state.artifact_store.load_shares().await?;
488-
let seeds = generate_garbling_table_seeds(state.config.seed);
494+
let config = require_config(state)?;
495+
let seeds = generate_garbling_table_seeds(config.seed);
489496
let challenge_response_msg = create_challenge_response_msg(
490497
challenge_indices.as_ref(),
491498
input_shares,
492499
output_shares,
493500
seeds,
494-
state.config.setup_inputs,
501+
config.setup_inputs,
495502
);
496503

497504
// sanity check
@@ -610,6 +617,12 @@ pub(crate) async fn restore<S: GarblerArtifactStore>(state: &State<S>) -> SMResu
610617
Ok(actions)
611618
}
612619

620+
fn require_config<S>(state: &State<S>) -> SMResult<Config> {
621+
state
622+
.config
623+
.ok_or_else(|| SMError::StateInconsistency("expected config to not be None"))
624+
}
625+
613626
#[expect(unused_variables)]
614627
fn generate_garbling_table_seeds(base_seed: Seed) -> AllGarblingSeeds {
615628
todo!()

0 commit comments

Comments
 (0)