Skip to content

Commit 56150eb

Browse files
committed
Added new functions to try replace the select k indices one.
1 parent 67b18cd commit 56150eb

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

mithril-stm/src/aggregate_signature/basic_verifier.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,118 @@ impl BasicVerifier {
172172
Err(AggregationError::NotEnoughSignatures(count, params.k))
173173
}
174174

175+
176+
// Modification of the function select_valid_signatures_for_k_indices
177+
// to try to improve readability
178+
// Still not outputing the same results, more tests need to be done
179+
pub fn modified_select_valid_signatures_for_k_indices(
180+
total_stake: &Stake,
181+
params: &Parameters,
182+
msg: &[u8],
183+
sigs: &[SingleSignatureWithRegisteredParty],
184+
) -> Result<Vec<SingleSignatureWithRegisteredParty>, AggregationError> {
185+
186+
let (idx_by_mtidx, btm) = Self::get_k_indices(
187+
total_stake,
188+
params,
189+
msg,
190+
sigs,
191+
);
192+
193+
let vec_single_sig = Self::valid_signatures_from_k_indices(
194+
&params,
195+
idx_by_mtidx,
196+
btm,
197+
);
198+
vec_single_sig
199+
}
200+
201+
202+
// Get a set of k unique indices connected to indices in the MT
203+
// Also creates a more explicit link between MT index and signature to reuse later
204+
pub fn get_k_indices<'a>(
205+
total_stake: &Stake,
206+
params: &Parameters,
207+
msg: &[u8],
208+
sigs: &'a [SingleSignatureWithRegisteredParty],
209+
) -> (HashMap<Index, Index>, BTreeMap<Index, &'a SingleSignatureWithRegisteredParty>) {
210+
let mut sig_by_mt_index: BTreeMap<Index, &SingleSignatureWithRegisteredParty> =
211+
BTreeMap::new();
212+
let mut indices_by_mt_idx: HashMap<Index, Index> = HashMap::new();
213+
214+
for sig_reg in sigs.iter() {
215+
if sig_reg
216+
.sig
217+
.basic_verify(
218+
params,
219+
&sig_reg.reg_party.0,
220+
&sig_reg.reg_party.1,
221+
msg,
222+
total_stake,
223+
)
224+
.is_err()
225+
{
226+
continue;
227+
}
228+
for index in sig_reg.sig.indexes.iter() {
229+
if let Some(mt_idx) = indices_by_mt_idx.get(index) {
230+
if let Some(prev_sig) = sig_by_mt_index.get(mt_idx) {
231+
if prev_sig.sig.sigma < sig_reg.sig.sigma {
232+
continue
233+
} else {
234+
indices_by_mt_idx.insert(*index, sig_reg.sig.signer_index);
235+
sig_by_mt_index.insert(sig_reg.sig.signer_index, &sig_reg);
236+
}
237+
}
238+
} else {
239+
if (indices_by_mt_idx.len() as u64) < params.k {
240+
indices_by_mt_idx.insert(*index, sig_reg.sig.signer_index);
241+
sig_by_mt_index.insert(sig_reg.sig.signer_index, &sig_reg);
242+
}
243+
}
244+
}
245+
}
246+
(indices_by_mt_idx, sig_by_mt_index)
247+
}
248+
249+
// From a set of k unique indices together with Merkle Tree indices
250+
// Creates a vector of signatures with correct indices
251+
pub fn valid_signatures_from_k_indices(
252+
params: &Parameters,
253+
list_k_valid_indices: HashMap<Index, Index>,
254+
sig_by_mt_index: BTreeMap<Index, &SingleSignatureWithRegisteredParty>,
255+
) -> Result<Vec<SingleSignatureWithRegisteredParty>, AggregationError> {
256+
257+
let mut valid_idx_for_mt_idx: HashMap<u64, Vec<u64>> = HashMap::new();
258+
259+
for (&valid_idx, &mt_id) in list_k_valid_indices.iter() {
260+
if let Some(val) = valid_idx_for_mt_idx.get_mut(&mt_id) {
261+
val.push(valid_idx);
262+
} else {
263+
valid_idx_for_mt_idx.insert(mt_id, vec![valid_idx]);
264+
}
265+
}
266+
let mut uniques_sig: Vec<SingleSignatureWithRegisteredParty> = Vec::new();
267+
let mut count_idx = 0;
268+
for (mt_idx, indices) in valid_idx_for_mt_idx.into_iter() {
269+
let mut single_sig = if let Some(sig) = sig_by_mt_index.get(&mt_idx) {
270+
(*sig).clone()
271+
} else {
272+
// Change the error
273+
return Err(AggregationError::NotEnoughSignatures(0, params.k));
274+
};
275+
count_idx += indices.len() as u64;
276+
single_sig.sig.indexes = indices;
277+
uniques_sig.push(single_sig);
278+
279+
}
280+
281+
if count_idx >= params.k {
282+
return Ok(uniques_sig);
283+
}
284+
Err(AggregationError::NotEnoughSignatures(count_idx, params.k))
285+
}
286+
175287
/// Given a slice of `sig_reg_list`, this function returns a new list of `sig_reg_list` with only valid indices.
176288
/// In case of conflict (having several signatures for the same index)
177289
/// it selects the smallest signature (i.e. takes the signature with the smallest scalar).

0 commit comments

Comments
 (0)