Skip to content

Commit

Permalink
Use pre-computed generator tables
Browse files Browse the repository at this point in the history
  • Loading branch information
SethDusek committed Dec 1, 2024
1 parent af69856 commit 98654ba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 6 additions & 0 deletions ergo-chain-types/src/ec_point.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Elliptic curve point.
use derive_more::{From, Into};
use elliptic_curve::ops::MulByGenerator;
use k256::elliptic_curve::group::prime::PrimeCurveAffine;
use k256::elliptic_curve::sec1::ToEncodedPoint;
use k256::{ProjectivePoint, PublicKey, Scalar};
Expand Down Expand Up @@ -116,6 +117,11 @@ pub fn exponentiate(base: &EcPoint, exponent: &Scalar) -> EcPoint {
}
}

/// Raise the generator g to the exponent. This is faster than exponentiate(&generator(), exponent)
pub fn exponentiate_gen(exponent: &Scalar) -> EcPoint {
ProjectivePoint::mul_by_generator(exponent).into()
}

impl ScorexSerializable for EcPoint {
fn scorex_serialize<W: WriteSigmaVlqExt>(&self, w: &mut W) -> ScorexSerializeResult {
let caff = self.0.to_affine();
Expand Down
8 changes: 4 additions & 4 deletions ergotree-interpreter/src/sigma_protocol/dlog_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub mod interactive_prover {
use crate::sigma_protocol::crypto_utils;
use crate::sigma_protocol::wscalar::Wscalar;
use crate::sigma_protocol::{private_input::DlogProverInput, Challenge};
use ergo_chain_types::ec_point::exponentiate_gen;
use ergo_chain_types::{
ec_point::{exponentiate, generator, inverse},
EcPoint,
Expand All @@ -67,7 +68,7 @@ pub mod interactive_prover {
let e: Scalar = challenge.clone().into();
let minus_e = e.negate();
let h_to_e = exponentiate(&public_input.h, &minus_e);
let g_to_z = exponentiate(&generator(), &z);
let g_to_z = exponentiate_gen(&z);
let a = g_to_z * &h_to_e;
(
FirstDlogProverMessage { a: a.into() },
Expand All @@ -80,8 +81,7 @@ pub mod interactive_prover {
/// that leaf to compute the necessary randomness "r" and the commitment "a"
pub fn first_message() -> (Wscalar, FirstDlogProverMessage) {
let r = dlog_group::random_scalar_in_group_range(crypto_utils::secure_rng());
let g = generator();
let a = exponentiate(&g, &r);
let a = exponentiate_gen(&r);
(r.into(), FirstDlogProverMessage { a: a.into() })
}

Expand Down Expand Up @@ -115,7 +115,7 @@ pub mod interactive_prover {
let g = generator();
let h = *proposition.h.clone();
let e: Scalar = challenge.clone().into();
let g_z = exponentiate(&g, second_message.z.as_scalar_ref());
let g_z = exponentiate_gen(second_message.z.as_scalar_ref());
let h_e = exponentiate(&h, &e);
g_z * &inverse(&h_e)
}
Expand Down
5 changes: 2 additions & 3 deletions ergotree-interpreter/src/sigma_protocol/private_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::convert::TryInto;
use std::fmt::Formatter;

use elliptic_curve::ops::MulByGenerator;
use ergo_chain_types::ec_point::exponentiate_gen;
use ergo_chain_types::EcPoint;
use ergotree_ir::serialization::SigmaSerializable;
use ergotree_ir::sigma_protocol::dlog_group;
Expand All @@ -14,7 +14,6 @@ use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
extern crate derive_more;
use derive_more::From;
use k256::elliptic_curve::PrimeField;
use k256::ProjectivePoint;
use num_bigint::BigUint;
use num_traits::ToPrimitive;

Expand Down Expand Up @@ -52,7 +51,7 @@ impl DlogProverInput {
/// Create new DlogProverInput
pub fn new(w: Wscalar) -> DlogProverInput {
Self {
pk: EcPoint::from(ProjectivePoint::mul_by_generator(w.as_scalar_ref())),
pk: exponentiate_gen(w.as_scalar_ref()),
w,
}
}
Expand Down

0 comments on commit 98654ba

Please sign in to comment.