Skip to content

Commit cb5540e

Browse files
committed
Merge branch 'ax/crypto-update' into ax/reshare-core
2 parents c7149ea + 3482f0d commit cb5540e

File tree

3 files changed

+104
-60
lines changed

3 files changed

+104
-60
lines changed

timeboost-crypto/src/feldman.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use derive_more::{Deref, From, IntoIterator};
99
use rayon::prelude::*;
1010
use serde::{Deserialize, Serialize};
1111
use serde_with::serde_as;
12-
use std::{iter::successors, num::NonZeroU32};
12+
use std::{iter::successors, num::NonZeroUsize};
1313

1414
use crate::{
1515
interpolation::{interpolate, interpolate_in_exponent},
@@ -23,22 +23,22 @@ pub struct FeldmanVss<C: CurveGroup>(PhantomData<C>);
2323
#[derive(Debug, Clone, Copy)]
2424
pub struct FeldmanVssPublicParam {
2525
// reconstruction threshold t
26-
pub t: NonZeroU32,
26+
pub t: NonZeroUsize,
2727
// total number of nodes
28-
pub n: NonZeroU32,
28+
pub n: NonZeroUsize,
2929
}
3030

3131
impl FeldmanVssPublicParam {
32-
pub fn new(t: NonZeroU32, n: NonZeroU32) -> Self {
32+
pub fn new(t: NonZeroUsize, n: NonZeroUsize) -> Self {
3333
Self { t, n }
3434
}
3535

3636
pub fn threshold(&self) -> usize {
37-
self.t.get() as usize
37+
self.t.get()
3838
}
3939

4040
pub fn num_nodes(&self) -> usize {
41-
self.n.get() as usize
41+
self.n.get()
4242
}
4343
}
4444

@@ -51,7 +51,7 @@ impl<C: CurveGroup> FeldmanVss<C> {
5151
) -> (DensePolynomial<C::ScalarField>, FeldmanCommitment<C>) {
5252
// sample random polynomial of degree t-1 (s.t. any t evaluations can interpolate this poly)
5353
// f(X) = Sum a_i * X^i
54-
let mut poly = DensePolynomial::<C::ScalarField>::rand(pp.t.get() as usize - 1, rng);
54+
let mut poly = DensePolynomial::<C::ScalarField>::rand(pp.t.get() - 1, rng);
5555
// f(0) = a_0 set to the secret, this index access will never panic since t>0
5656
poly.coeffs[0] = secret;
5757

@@ -67,7 +67,7 @@ impl<C: CurveGroup> FeldmanVss<C> {
6767
pp: &FeldmanVssPublicParam,
6868
poly: &DensePolynomial<C::ScalarField>,
6969
) -> impl Iterator<Item = C::ScalarField> {
70-
(0..pp.n.get()).map(|node_idx| poly.evaluate(&(node_idx + 1).into()))
70+
(0..pp.n.get()).map(|node_idx| poly.evaluate(&((node_idx + 1) as u64).into()))
7171
}
7272

7373
/// same as [`Self::compute_shares()`], but output an iterator of bytes
@@ -86,8 +86,8 @@ impl<C: CurveGroup> FeldmanVss<C> {
8686
node_idx: usize,
8787
commitment: &[C::Affine],
8888
) -> Result<C, VssError> {
89-
let n = pp.n.get() as usize;
90-
let t = pp.t.get() as usize;
89+
let n = pp.n.get();
90+
let t = pp.t.get();
9191

9292
// input validation
9393
if node_idx >= n {
@@ -155,8 +155,8 @@ impl<C: CurveGroup> VerifiableSecretSharing for FeldmanVss<C> {
155155
shares: impl Iterator<Item = (usize, Self::SecretShare)>,
156156
) -> Result<Self::Secret, VssError> {
157157
let shares = shares.collect::<Vec<_>>();
158-
let n = pp.n.get() as usize;
159-
let t = pp.t.get() as usize;
158+
let n = pp.n.get();
159+
let t = pp.t.get();
160160
// input validation
161161
if shares.len() != t {
162162
return Err(VssError::MismatchedSharesCount(t, shares.len()));
@@ -316,8 +316,8 @@ mod tests {
316316
let n_usize = n as usize;
317317
let t_usize = t as usize;
318318

319-
let n = NonZeroU32::new(n).unwrap();
320-
let t = NonZeroU32::new(t).unwrap();
319+
let n = NonZeroUsize::new(n as usize).unwrap();
320+
let t = NonZeroUsize::new(t as usize).unwrap();
321321
let pp = FeldmanVssPublicParam::new(t, n);
322322

323323
let (shares, commitment) = FeldmanVss::<C>::share(&pp, rng, secret);

timeboost-crypto/src/mre.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ pub struct Ciphertext<C: CurveGroup, H: Digest = sha2::Sha256> {
174174
/// - `aad` is the associated data
175175
/// - `C` is the DL group, `H` is the choice of H_enc whose output space = message space
176176
/// - preprocess messages to pad them to proper length before passing in
177-
pub fn encrypt<C, H, R>(
178-
recipients: &[EncryptionKey<C>],
177+
pub fn encrypt<'a, C, H, R, I>(
178+
recipients: I,
179179
messages: &[Vec<u8>],
180180
aad: &[u8],
181181
rng: &mut R,
@@ -184,14 +184,17 @@ where
184184
C: CurveGroup,
185185
H: Digest,
186186
R: Rng + CryptoRng,
187+
I: IntoIterator<Item = &'a EncryptionKey<C>>,
188+
I::IntoIter: ExactSizeIterator,
187189
{
188190
// input validation
189-
if recipients.is_empty() || messages.is_empty() {
191+
let recipients_iter = recipients.into_iter();
192+
if messages.is_empty() {
190193
return Err(MultiRecvEncError::EmptyInput);
191194
}
192-
if recipients.len() != messages.len() {
195+
if recipients_iter.len() != messages.len() {
193196
return Err(MultiRecvEncError::MismatchedInputLength(
194-
recipients.len(),
197+
recipients_iter.len(),
195198
messages.len(),
196199
));
197200
}
@@ -210,8 +213,7 @@ where
210213
let epk = C::generator().mul(&esk);
211214

212215
// generate recipient-specific ciphertext parts
213-
let cts = recipients
214-
.iter()
216+
let cts = recipients_iter
215217
.zip(messages.iter())
216218
.enumerate()
217219
.map(|(idx, (pk, msg))| {
@@ -228,7 +230,7 @@ where
228230

229231
// TODO(alex): use SIMD vectorized XOR when `std::simd` move out of nightly,
230232
// or rayon as an intermediate improvement
231-
let ct = Output::<H>::from_iter(k.iter().zip(msg).map(|(ki, m)| ki ^ m));
233+
let ct = Output::<H>::from_iter(k.iter().zip(msg.iter()).map(|(ki, m)| ki ^ m));
232234
Ok(ct)
233235
})
234236
.collect::<Result<Vec<_>, MultiRecvEncError>>()?;
@@ -276,7 +278,7 @@ impl From<ark_serialize::SerializationError> for MultiRecvEncError {
276278

277279
#[cfg(test)]
278280
mod tests {
279-
use std::iter::repeat_with;
281+
use std::{collections::BTreeMap, iter::repeat_with};
280282

281283
use ark_bls12_381::G1Projective;
282284
use ark_std::rand;
@@ -290,8 +292,12 @@ mod tests {
290292
let n = 10; // num of recipients
291293
let recv_sks: Vec<DecryptionKey<G1Projective>> =
292294
repeat_with(|| DecryptionKey::rand(rng)).take(n).collect();
293-
let recv_pks: Vec<EncryptionKey<G1Projective>> =
294-
recv_sks.iter().map(EncryptionKey::from).collect();
295+
// collecting into a BTreeSet to demonstrate flexible encrypt() input type
296+
let recv_pks: BTreeMap<usize, EncryptionKey<G1Projective>> = recv_sks
297+
.iter()
298+
.enumerate()
299+
.map(|(i, sk)| (i, EncryptionKey::from(sk)))
300+
.collect();
295301
let labeled_sks: Vec<LabeledDecryptionKey<G1Projective>> = recv_sks
296302
.into_iter()
297303
.enumerate()
@@ -302,7 +308,7 @@ mod tests {
302308
.collect::<Vec<_>>();
303309
let aad = b"Alice";
304310

305-
let mre_ct = encrypt::<G1Projective, H, _>(&recv_pks, &msgs, aad, rng).unwrap();
311+
let mre_ct = encrypt::<G1Projective, H, _, _>(recv_pks.values(), &msgs, aad, rng).unwrap();
306312
for i in 0..n {
307313
let ct = mre_ct.get_recipient_ct(i).unwrap();
308314
assert_eq!(

0 commit comments

Comments
 (0)