Skip to content

Commit

Permalink
algebra: authenticated-scalar: Implement pow method
Browse files Browse the repository at this point in the history
  • Loading branch information
joeykraut committed Oct 20, 2023
1 parent 637ed00 commit 07b88a1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/algebra/scalar/authenticated_scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,25 @@ impl<C: CurveGroup> AuthenticatedScalarResult<C> {
// m_i^-1 * r_i = (x_i^-1 * r_i^-1) * r_i = x_i^-1
AuthenticatedScalarResult::batch_mul_public(&shared_scalars, &inverted_openings)
}

/// Compute the exponentiation of the given value
///
/// via recursive squaring
pub fn pow(&self, exp: u64) -> Self {
if exp == 0 {
return self.fabric().zero_authenticated();
} else if exp == 1 {
return self.clone();
}

let recursive = self.pow(exp / 2);
let mut res = &recursive * &recursive;

if exp % 2 == 1 {
res = res * self.clone();
}
res
}
}

/// Opening implementations
Expand Down Expand Up @@ -1206,7 +1225,7 @@ mod tests {
use ark_poly::{EvaluationDomain, Radix2EvaluationDomain};
use futures::future;
use itertools::Itertools;
use rand::{thread_rng, Rng};
use rand::{thread_rng, Rng, RngCore};

use crate::{
algebra::{poly_test_helpers::TestPolyField, scalar::Scalar, AuthenticatedScalarResult},
Expand Down Expand Up @@ -1337,6 +1356,26 @@ mod tests {
assert_eq!(res.unwrap(), expected_res)
}

/// Tests exponentiation
#[tokio::test]
async fn test_pow() {
let mut rng = thread_rng();
let exp = rng.next_u64();
let value = Scalar::<TestCurve>::random(&mut rng);

let expected_res = value.pow(exp);

let (res, _) = execute_mock_mpc(|fabric| async move {
let shared_value = fabric.share_scalar(value, PARTY0 /* sender */);
let res = shared_value.pow(exp);

res.open().await
})
.await;

assert_eq!(res, expected_res)
}

#[tokio::test]
async fn test_fft() {
let mut rng = thread_rng();
Expand Down
5 changes: 5 additions & 0 deletions src/algebra/scalar/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ impl<C: CurveGroup> Scalar<C> {
}
}

/// Compute the exponentiation of the given scalar
pub fn pow(&self, exp: u64) -> Self {
Scalar::new(self.0.pow([exp]))
}

/// Construct a scalar from the given bytes and reduce modulo the field's modulus
pub fn from_be_bytes_mod_order(bytes: &[u8]) -> Self {
let inner = C::ScalarField::from_be_bytes_mod_order(bytes);
Expand Down

0 comments on commit 07b88a1

Please sign in to comment.