Skip to content

Commit d89f1bb

Browse files
committed
Precompute DlogProverInput.public_image
This avoids repeated multiplications by group element when signing
1 parent 2527351 commit d89f1bb

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
[workspace]
23
resolver = "2"
34
members = [

ergo-chain-types/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ core2 = { workspace = true }
3131
default = ["std", "json"]
3232
arbitrary = ["proptest", "proptest-derive", "std"]
3333
json = ["serde", "serde_json", "serde_with"]
34-
std = ["dep:url", "base16/std", "base64/std", "serde/std"]
34+
std = [
35+
"dep:url",
36+
"base16/std",
37+
"base64/std",
38+
"serde/std",
39+
"k256/precomputed-tables",
40+
"k256/std",
41+
]
3542

3643
[dev-dependencies]

ergo-chain-types/src/ec_point.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use sigma_ser::vlq_encode::{ReadSigmaVlqExt, WriteSigmaVlqExt};
1111
use sigma_ser::{ScorexParsingError, ScorexSerializable, ScorexSerializeResult};
1212

1313
/// Elliptic curve point
14-
#[derive(PartialEq, Clone, Default, From, Into)]
14+
#[derive(PartialEq, Clone, Copy, Default, From, Into)]
1515
#[cfg_attr(
1616
feature = "json",
1717
derive(serde::Serialize, serde::Deserialize),

ergo-lib/src/wallet/deterministic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mod test {
7777
use crate::wallet::Wallet;
7878
fn gen_boxes() -> impl Strategy<Value = (SecretKey, Vec<ErgoBox>)> {
7979
any::<Wscalar>()
80-
.prop_map(|s| SecretKey::DlogSecretKey(DlogProverInput { w: s }))
80+
.prop_map(|s| SecretKey::DlogSecretKey(DlogProverInput::new(s)))
8181
.prop_flat_map(|sk: SecretKey| {
8282
(
8383
Just(sk.clone()),

ergotree-interpreter/src/sigma_protocol/private_input.rs

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use core::convert::TryInto;
33
use core::fmt::Formatter;
44

55
use alloc::vec::Vec;
6+
use elliptic_curve::ops::MulByGenerator;
67
use ergo_chain_types::EcPoint;
78
use ergotree_ir::serialization::SigmaSerializable;
89
use ergotree_ir::sigma_protocol::sigma_boolean::ProveDhTuple;
@@ -13,18 +14,20 @@ use ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
1314
extern crate derive_more;
1415
use derive_more::From;
1516
use k256::elliptic_curve::PrimeField;
17+
use k256::ProjectivePoint;
1618
use num_bigint::BigUint;
1719
use num_traits::ToPrimitive;
1820

1921
use super::wscalar::Wscalar;
2022

2123
/// Secret key of discrete logarithm signature protocol
2224
#[cfg_attr(feature = "json", derive(serde::Serialize, serde::Deserialize))]
23-
#[cfg_attr(feature = "json", serde(transparent))]
24-
#[derive(PartialEq, Eq, Clone, derive_more::From)]
25+
#[cfg_attr(feature = "json", serde(from = "Wscalar", into = "Wscalar"))]
26+
#[derive(PartialEq, Eq, Clone)]
2527
pub struct DlogProverInput {
2628
/// secret key value
2729
pub w: Wscalar,
30+
pk: EcPoint,
2831
}
2932

3033
impl core::fmt::Debug for DlogProverInput {
@@ -34,26 +37,46 @@ impl core::fmt::Debug for DlogProverInput {
3437
}
3538
}
3639

40+
impl From<Wscalar> for DlogProverInput {
41+
fn from(scalar: Wscalar) -> Self {
42+
DlogProverInput::new(scalar)
43+
}
44+
}
45+
46+
impl From<DlogProverInput> for Wscalar {
47+
fn from(prover_input: DlogProverInput) -> Self {
48+
prover_input.w
49+
}
50+
}
51+
3752
impl DlogProverInput {
3853
/// Scalar(secret key) size in bytes
3954
pub const SIZE_BYTES: usize = 32;
4055

56+
/// Create new DlogProverInput
57+
pub fn new(w: Wscalar) -> DlogProverInput {
58+
Self {
59+
pk: EcPoint::from(ProjectivePoint::mul_by_generator(w.as_scalar_ref())),
60+
w,
61+
}
62+
}
4163
/// generates random secret in the range [0, n), where n is DLog group order.
4264
#[cfg(feature = "std")]
4365
pub fn random() -> DlogProverInput {
44-
DlogProverInput {
45-
w: ergotree_ir::sigma_protocol::dlog_group::random_scalar_in_group_range(
46-
super::crypto_utils::secure_rng(),
47-
)
48-
.into(),
49-
}
66+
use ergotree_ir::sigma_protocol::dlog_group;
67+
68+
use crate::sigma_protocol::crypto_utils;
69+
70+
DlogProverInput::new(
71+
dlog_group::random_scalar_in_group_range(crypto_utils::secure_rng()).into(),
72+
)
5073
}
5174

5275
/// Attempts to parse the given byte array as an SEC-1-encoded scalar(secret key).
5376
/// Returns None if the byte array does not contain a big-endian integer in the range [0, modulus).
5477
pub fn from_bytes(bytes: &[u8; DlogProverInput::SIZE_BYTES]) -> Option<DlogProverInput> {
5578
k256::Scalar::from_repr((*bytes).into())
56-
.map(|s| DlogProverInput::from(Wscalar::from(s)))
79+
.map(|s| DlogProverInput::new(Wscalar::from(s)))
5780
.into()
5881
}
5982

@@ -90,12 +113,7 @@ impl DlogProverInput {
90113

91114
/// public key of discrete logarithm signature protocol
92115
pub fn public_image(&self) -> ProveDlog {
93-
// test it, see https://github.com/ergoplatform/sigma-rust/issues/38
94-
let g = ergo_chain_types::ec_point::generator();
95-
ProveDlog::new(ergo_chain_types::ec_point::exponentiate(
96-
&g,
97-
self.w.as_scalar_ref(),
98-
))
116+
ProveDlog::new(self.pk)
99117
}
100118

101119
/// Return true if the secret is 0

0 commit comments

Comments
 (0)