-
Notifications
You must be signed in to change notification settings - Fork 4
/
universal_setup_compiler.rs
103 lines (93 loc) · 3.35 KB
/
universal_setup_compiler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use crate::compiler::IVLS;
use crate::ivls::data_structures::Commitment;
use crate::{
building_blocks::mt::MT,
gadgets::UInt64,
ivls::{
history::VerifiableHistory,
state::VerifiableState,
transition_function::{VerifiableTransitionFunction, VerifiableTransitionFunctionConfig},
},
ledger_system::transition_function::TransitionFunction,
Error, PhantomData,
};
use ark_pcd::UniversalSetupPCD;
use ark_std::rand::rngs::StdRng;
use ark_std::rand::{CryptoRng, RngCore, SeedableRng};
/// compiler for universal setup IVLS
pub struct UniversalSetupIVLSCompiler<VC: VerifiableTransitionFunctionConfig>
where
VC::I: UniversalSetupPCD<VC::F>,
{
vc_phantom: PhantomData<VC>,
}
/// public parameters for universal setup IVLS
pub struct UniversalSetupIVLSPP<VC: VerifiableTransitionFunctionConfig>
where
VC::I: UniversalSetupPCD<VC::F>,
{
/// the PCD public parameters
pub pp_pcd: <VC::I as UniversalSetupPCD<VC::F>>::PublicParameters,
/// the seed used to sample Merkle tree parameters (which would support any addr/data types)
pub pp_mt_seed: [u8; 32],
}
impl<VC: VerifiableTransitionFunctionConfig> UniversalSetupIVLSCompiler<VC>
where
VC::I: UniversalSetupPCD<VC::F>,
{
/// IVLS.setup (universal)
pub fn universal_setup<R: RngCore + CryptoRng>(
setup_bound: <VC::I as UniversalSetupPCD<VC::F>>::PredicateBound,
rng: &mut R,
) -> Result<UniversalSetupIVLSPP<VC>, Error> {
let pp_pcd = <VC::I as UniversalSetupPCD<VC::F>>::universal_setup::<R>(&setup_bound, rng)?;
let mut pp_mt_seed = [0u8; 32];
rng.fill_bytes(&mut pp_mt_seed);
Ok(UniversalSetupIVLSPP { pp_pcd, pp_mt_seed })
}
/// IVLS.make_sfh
pub fn make_sfh<R: RngCore + CryptoRng>(
pp: &UniversalSetupIVLSPP<VC>,
rng: &mut R,
) -> Result<IVLS<VC>, Error> {
let mut setup_rng = StdRng::from_seed(pp.pp_mt_seed);
let pp_mt = (
<VC::MTState as MT<
VC::F,
<VC::TF as TransitionFunction<VC::F>>::Addr,
<VC::TF as TransitionFunction<VC::F>>::AddrVar,
>>::setup(&mut setup_rng)?,
<VC::MTHistory as MT<VC::F, u64, UInt64<VC::F>>>::setup(&mut setup_rng)?,
);
let empty_tree_state =
VC::MTState::new::<<VC::TF as TransitionFunction<VC::F>>::Data>(&pp_mt.0)?;
let empty_tree_history = VC::MTHistory::new::<Commitment<VC>>(&pp_mt.1)?;
let empty_digest = (
VC::MTState::root(&pp_mt.0, &empty_tree_state)?,
VC::MTHistory::root(&pp_mt.1, &empty_tree_history)?,
);
let p = VerifiableTransitionFunction::<VC> {
pp_mt: pp_mt.clone(),
empty_digest: empty_digest.clone(),
ipk: None,
ivk: None,
};
let (ipk, ivk) = <VC::I as UniversalSetupPCD<VC::F>>::index::<
VerifiableTransitionFunction<VC>,
R,
>(&pp.pp_pcd, &p, rng)?;
Ok(IVLS::<VC> {
vf: VerifiableTransitionFunction::<VC> {
pp_mt: pp_mt.clone(),
empty_digest,
ipk: Some(ipk),
ivk: Some(ivk.clone()),
},
vs: VerifiableState::<VC> {
pp_mt: pp_mt.clone(),
ivk,
},
vh: VerifiableHistory::<VC> { pp_mt },
})
}
}