Skip to content

Commit fe95be7

Browse files
committed
secret mul as a macro
1 parent d20c20b commit fe95be7

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

src/ietf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ impl<S: IetfSuite> Prover<S> for Secret<S> {
9191
fn prove(&self, input: Input<S>, output: Output<S>, ad: impl AsRef<[u8]>) -> Proof<S> {
9292
let k = S::nonce(&self.scalar, input);
9393

94-
let k_b = utils::mul_secret::<S>(S::generator(), k).into_affine();
95-
let k_h = utils::mul_secret::<S>(input.0, k).into_affine();
94+
let k_b = smul!(S::generator(), k).into_affine();
95+
let k_h = smul!(input.0, k).into_affine();
9696

9797
let c = S::challenge(
9898
&[&self.public.0, &input.0, &output.0, &k_b, &k_h],

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ pub mod ring;
3737
#[cfg(test)]
3838
mod testing;
3939

40-
// Re-export stuff that may be useful downstream.
41-
#[doc(hidden)]
40+
use codec::Codec;
4241
pub mod reexports {
4342
pub use ark_ec;
4443
pub use ark_ff;
@@ -47,14 +46,12 @@ pub mod reexports {
4746
}
4847

4948
pub type AffinePoint<S> = <S as Suite>::Affine;
50-
5149
pub type BaseField<S> = <AffinePoint<S> as AffineRepr>::BaseField;
5250
pub type ScalarField<S> = <AffinePoint<S> as AffineRepr>::ScalarField;
5351
pub type CurveConfig<S> = <AffinePoint<S> as AffineRepr>::Config;
5452

5553
pub type HashOutput<S> = digest::Output<<S as Suite>::Hasher>;
5654

57-
pub use codec::Codec;
5855

5956
#[derive(Debug)]
6057
pub enum Error {
@@ -242,7 +239,7 @@ impl<S: Suite> Secret<S> {
242239

243240
/// Get the VRF output point relative to input.
244241
pub fn output(&self, input: Input<S>) -> Output<S> {
245-
Output(utils::mul_secret::<S>(input.0, self.scalar).into_affine())
242+
Output(smul!(input.0, self.scalar).into_affine())
246243
}
247244
}
248245

src/pedersen.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ impl<S: PedersenSuite> Prover<S> for Secret<S> {
8989
let kb = S::nonce(&blinding, input);
9090

9191
// Yb = x*G + b*B
92-
let xg = utils::mul_secret::<S>(S::generator(), self.scalar);
93-
let bb = utils::mul_secret::<S>(S::BLINDING_BASE, blinding);
92+
let xg = smul!(S::generator(), self.scalar);
93+
let bb = smul!(S::BLINDING_BASE, blinding);
9494
let pk_com = (xg + bb).into_affine();
9595

9696
// R = k*G + kb*B
97-
let kg = utils::mul_secret::<S>(S::generator(), k);
98-
let kbb = utils::mul_secret::<S>(S::BLINDING_BASE, kb);
97+
let kg = smul!(S::generator(), k);
98+
let kbb = smul!(S::BLINDING_BASE, kb);
9999
let r = (kg + kbb).into_affine();
100100

101101
// Ok = k*I
102-
let ok = utils::mul_secret::<S>(input.0, k).into_affine();
102+
let ok = smul!(input.0, k).into_affine();
103103

104104
// c = Hash(Yb, I, O, R, Ok, ad)
105105
let c = S::challenge(&[&pk_com, &input.0, &output.0, &r, &ok], ad.as_ref());

src/ring.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ pub(crate) mod testing {
734734
while !pks.is_empty() {
735735
let chunk_len = 1 + random_val::<usize>(Some(rng)) % 5;
736736
let chunk = pks.drain(..pks.len().min(chunk_len)).collect::<Vec<_>>();
737-
println!("Appending {} items", chunk.len());
738737
vk_builder.append(&chunk[..], &loader).unwrap();
739738
assert_eq!(vk_builder.free_slots(), pks.len());
740739
}

src/utils/mod.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,35 @@ pub use common::*;
88
/// Twisted Edwards to Short Weierstrass mapping.
99
pub use te_sw_map::*;
1010

11-
use crate::{AffinePoint, ScalarField, Suite};
12-
use ark_ec::AffineRepr;
13-
14-
type Projective<S> = <AffinePoint<S> as AffineRepr>::Group;
15-
16-
/// Point scalar multiplication with secret splitting.
11+
/// Point scalar multiplication with optional secret splitting.
1712
///
1813
/// Secret scalar split into the sum of two scalars, which randomly mutate but
1914
/// retain the same sum. Incurs 2x penalty in scalar multiplications, but provides
2015
/// side channel defenses.
21-
#[cfg(feature = "secret-split")]
22-
#[inline(always)]
23-
pub(crate) fn mul_secret<S: Suite>(p: AffinePoint<S>, s: ScalarField<S>) -> Projective<S> {
24-
use ark_std::UniformRand;
25-
let mut rng = ark_std::rand::rngs::OsRng;
26-
let x1 = ScalarField::<S>::rand(&mut rng);
27-
let x2 = s - x1;
28-
p * x1 + p * x2
29-
}
16+
///
17+
/// Note: actual secret splitting is enabled via the `secret-split` feature.
18+
mod secret_split {
19+
#[cfg(feature = "secret-split")]
20+
#[doc(hidden)]
21+
#[macro_export]
22+
macro_rules! smul {
23+
($p:expr, $s:expr) => {{
24+
#[inline(always)]
25+
fn get_rand<T: ark_std::UniformRand>(_: &T) -> T {
26+
T::rand(&mut ark_std::rand::rngs::OsRng)
27+
}
28+
let x1 = get_rand(&$s);
29+
let x2 = $s - x1;
30+
$p * x1 + $p * x2
31+
}};
32+
}
3033

31-
/// Point scalar multiplication with no secret splitting.
32-
#[cfg(not(feature = "secret-split"))]
33-
#[inline(always)]
34-
pub(crate) fn mul_secret<S: Suite>(p: AffinePoint<S>, s: ScalarField<S>) -> Projective<S> {
35-
p * s
34+
#[cfg(not(feature = "secret-split"))]
35+
#[doc(hidden)]
36+
#[macro_export]
37+
macro_rules! smul {
38+
($p:expr, $s:expr) => {
39+
$p * $s
40+
};
41+
}
3642
}

0 commit comments

Comments
 (0)