Skip to content

Commit 7f92761

Browse files
authored
Merge pull request #790 from ergoplatform/precomputedtables
Use precomputed tables
2 parents c72f108 + 7bd76af commit 7f92761

File tree

24 files changed

+159
-119
lines changed

24 files changed

+159
-119
lines changed

bindings/ergo-lib-python/src/multi_sig/hints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fn extract_image(py: Python, image: &SigmaBoolean) -> PyResult<Py<PyAny>> {
113113
fn extract_commitment(py: Python, commitment: &FirstProverMessage) -> PyResult<Py<PyAny>> {
114114
match commitment {
115115
FirstProverMessage::FirstDlogProverMessage(ref first_dlog_prover_message) => {
116-
Ok(Py::new(py, EcPoint::from(first_dlog_prover_message.a().clone()))?.into_any())
116+
Ok(Py::new(py, EcPoint::from(*first_dlog_prover_message.a()))?.into_any())
117117
}
118118
FirstProverMessage::FirstDhtProverMessage(_) => Err(PyNotImplementedError::new_err(
119119
"ProveDHTuple is not supported",

bindings/ergo-lib-python/src/sigma_protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl ProveDlog {
1616
}
1717
#[getter]
1818
fn h(&self) -> EcPoint {
19-
(*self.0.h).clone().into()
19+
(*self.0.h).into()
2020
}
2121
fn __repr__(&self) -> String {
2222
format!("{:?}", self.0)

bindings/ergo-lib-python/src/wallet/ext_pub_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl ExtPubKey {
3232
))
3333
}
3434
fn public_key(&self) -> EcPoint {
35-
self.0.public_key.clone().into()
35+
self.0.public_key.into()
3636
}
3737
fn address(&self) -> Address {
3838
address::Address::from(self.0.clone()).into()

bindings/ergo-lib-wasm/src/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ impl UnsignedTransaction {
401401

402402
/// Returns distinct token id from output_candidates as array of byte arrays
403403
pub fn distinct_token_ids(&self) -> Vec<Uint8Array> {
404-
distinct_token_ids(self.0.output_candidates.clone())
404+
distinct_token_ids(&self.0.output_candidates)
405405
.iter()
406406
.map(|id| Uint8Array::from(id.as_ref()))
407407
.collect()

ergo-chain-types/Cargo.toml

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

3744
[dev-dependencies]

ergo-chain-types/src/ec_point.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ use alloc::string::String;
44
use core::convert::TryFrom;
55
use core::ops::{Add, Mul, Neg};
66
use derive_more::{From, Into};
7+
use elliptic_curve::ops::MulByGenerator;
78
use k256::elliptic_curve::group::prime::PrimeCurveAffine;
89
use k256::elliptic_curve::sec1::ToEncodedPoint;
910
use k256::{ProjectivePoint, PublicKey, Scalar};
1011
use sigma_ser::vlq_encode::{ReadSigmaVlqExt, WriteSigmaVlqExt};
1112
use sigma_ser::{ScorexParsingError, ScorexSerializable, ScorexSerializeResult};
1213

1314
/// Elliptic curve point
14-
#[derive(PartialEq, Clone, Default, From, Into)]
15+
#[derive(PartialEq, Clone, Copy, Default, From, Into)]
1516
#[cfg_attr(
1617
feature = "json",
1718
derive(serde::Serialize, serde::Deserialize),
@@ -104,7 +105,7 @@ pub fn is_identity(ge: &EcPoint) -> bool {
104105

105106
/// Calculates the inverse of the given group element
106107
pub fn inverse(ec: &EcPoint) -> EcPoint {
107-
-ec.clone()
108+
-*ec
108109
}
109110

110111
/// Raises the base GroupElement to the exponent. The result is another GroupElement.
@@ -113,10 +114,15 @@ pub fn exponentiate(base: &EcPoint, exponent: &Scalar) -> EcPoint {
113114
// we treat EC as a multiplicative group, therefore, exponentiate point is multiply.
114115
EcPoint(base.0 * exponent)
115116
} else {
116-
base.clone()
117+
*base
117118
}
118119
}
119120

121+
/// Raise the generator g to the exponent. This is faster than exponentiate(&generator(), exponent)
122+
pub fn exponentiate_gen(exponent: &Scalar) -> EcPoint {
123+
ProjectivePoint::mul_by_generator(exponent).into()
124+
}
125+
120126
impl ScorexSerializable for EcPoint {
121127
fn scorex_serialize<W: WriteSigmaVlqExt>(&self, w: &mut W) -> ScorexSerializeResult {
122128
let caff = self.0.to_affine();

ergo-lib/src/chain/transaction.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,20 +234,14 @@ pub enum TransactionSignatureVerificationError {
234234
}
235235

236236
/// Returns distinct token ids from all given ErgoBoxCandidate's
237-
pub fn distinct_token_ids<I>(output_candidates: I) -> IndexSet<TokenId>
237+
pub fn distinct_token_ids<'a, I>(output_candidates: I) -> IndexSet<TokenId>
238238
where
239-
I: IntoIterator<Item = ErgoBoxCandidate>,
239+
I: IntoIterator<Item = &'a ErgoBoxCandidate>,
240240
{
241-
let token_ids: Vec<TokenId> = output_candidates
241+
let token_ids = output_candidates
242242
.into_iter()
243-
.flat_map(|b| {
244-
b.tokens
245-
.into_iter()
246-
.flatten()
247-
.map(|t| t.token_id)
248-
.collect::<Vec<TokenId>>()
249-
})
250-
.collect();
243+
.flat_map(|b| b.tokens.iter().flatten().map(|t| t.token_id));
244+
251245
IndexSet::<_>::from_iter(token_ids)
252246
}
253247

@@ -267,7 +261,7 @@ impl SigmaSerializable for Transaction {
267261
}
268262

269263
// Serialize distinct ids of tokens in transaction outputs.
270-
let distinct_token_ids = distinct_token_ids(self.output_candidates.clone());
264+
let distinct_token_ids = distinct_token_ids(&self.output_candidates);
271265

272266
// Note that `self.output_candidates` is of type `TxIoVec` which has a max length of
273267
// `u16::MAX`. Therefore the following unwrap is safe.

ergo-lib/src/chain/transaction/unsigned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl UnsignedTransaction {
124124

125125
/// Returns distinct token ids from all output_candidates
126126
pub fn distinct_token_ids(&self) -> IndexSet<TokenId> {
127-
distinct_token_ids(self.output_candidates.clone())
127+
distinct_token_ids(&self.output_candidates)
128128
}
129129
}
130130

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()),

ergo-lib/src/wallet/ext_pub_key.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ type HmacSha512 = Hmac<Sha512>;
2828
pub struct ExtPubKey {
2929
/// Parsed public key (EcPoint)
3030
pub public_key: EcPoint,
31-
chain_code: ChainCode,
31+
/// Chain code bytes
32+
pub chain_code: ChainCode,
3233
/// Derivation path for this extended public key
3334
pub derivation_path: DerivationPath,
3435
}

0 commit comments

Comments
 (0)