Skip to content

Commit 62b08ac

Browse files
committed
Optimize From<Challenge>, distinct_token_ids
1 parent c673520 commit 62b08ac

File tree

14 files changed

+35
-47
lines changed

14 files changed

+35
-47
lines changed

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/src/ec_point.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub fn is_identity(ge: &EcPoint) -> bool {
105105

106106
/// Calculates the inverse of the given group element
107107
pub fn inverse(ec: &EcPoint) -> EcPoint {
108-
-ec.clone()
108+
-*ec
109109
}
110110

111111
/// Raises the base GroupElement to the exponent. The result is another GroupElement.
@@ -114,7 +114,7 @@ pub fn exponentiate(base: &EcPoint, exponent: &Scalar) -> EcPoint {
114114
// we treat EC as a multiplicative group, therefore, exponentiate point is multiply.
115115
EcPoint(base.0 * exponent)
116116
} else {
117-
base.clone()
117+
*base
118118
}
119119
}
120120

ergo-lib/src/chain/transaction.rs

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

235235
/// Returns distinct token ids from all given ErgoBoxCandidate's
236-
pub fn distinct_token_ids<I>(output_candidates: I) -> IndexSet<TokenId>
236+
pub fn distinct_token_ids<'a, I>(output_candidates: I) -> IndexSet<TokenId>
237237
where
238-
I: IntoIterator<Item = ErgoBoxCandidate>,
238+
I: IntoIterator<Item = &'a ErgoBoxCandidate>,
239239
{
240-
let token_ids: Vec<TokenId> = output_candidates
240+
let token_ids = output_candidates
241241
.into_iter()
242-
.flat_map(|b| {
243-
b.tokens
244-
.into_iter()
245-
.flatten()
246-
.map(|t| t.token_id)
247-
.collect::<Vec<TokenId>>()
248-
})
249-
.collect();
242+
.flat_map(|b| b.tokens.iter().flatten().map(|t| t.token_id));
243+
250244
IndexSet::<_>::from_iter(token_ids)
251245
}
252246

@@ -264,7 +258,7 @@ impl SigmaSerializable for Transaction {
264258
}
265259

266260
// Serialize distinct ids of tokens in transaction outputs.
267-
let distinct_token_ids = distinct_token_ids(self.output_candidates.clone());
261+
let distinct_token_ids = distinct_token_ids(&self.output_candidates);
268262

269263
// Note that `self.output_candidates` is of type `TxIoVec` which has a max length of
270264
// `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

ergotree-interpreter/src/eval/create_provedlog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl Evaluable for CreateProveDlog {
1616
let value_v = self.input.eval(env, ctx)?;
1717
match value_v {
1818
Value::GroupElement(ecpoint) => {
19-
let prove_dlog = ProveDlog::new((*ecpoint).clone());
19+
let prove_dlog = ProveDlog::new(*ecpoint);
2020
Ok(prove_dlog.into())
2121
}
2222
_ => Err(EvalError::UnexpectedValue(format!(

ergotree-interpreter/src/eval/multiply_group.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Evaluable for MultiplyGroup {
1717

1818
match (&left_v, &right_v) {
1919
(Value::GroupElement(left), Value::GroupElement(right)) => {
20-
Ok(((**left).clone() * right).into())
20+
Ok(((**left) * right).into())
2121
}
2222
_ => Err(EvalError::UnexpectedValue(format!(
2323
"Expected MultiplyGroup input to be GroupElement, got: {0:?}",
@@ -46,7 +46,7 @@ mod tests {
4646
#[test]
4747
fn eval_any(left in any::<EcPoint>(), right in any::<EcPoint>()) {
4848

49-
let expected_mul = left.clone() * &right;
49+
let expected_mul = left * &right;
5050

5151
let expr: Expr = MultiplyGroup {
5252
left: Box::new(Expr::Const(left.into())),

ergotree-interpreter/src/eval/sgroup_elem.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) static GET_ENCODED_EVAL_FN: EvalFn = |_mc, _env, _ctx, obj, _args| {
2222

2323
pub(crate) static NEGATE_EVAL_FN: EvalFn = |_mc, _env, _ctx, obj, _args| {
2424
let negated: EcPoint = match obj {
25-
Value::GroupElement(ec_point) => Ok(-(*ec_point).clone()),
25+
Value::GroupElement(ec_point) => Ok(-(*ec_point)),
2626
_ => Err(EvalError::UnexpectedValue(format!(
2727
"expected obj to be Value::GroupElement, got: {0:?}",
2828
obj
@@ -49,7 +49,7 @@ mod tests {
4949
fn eval_get_encoded() {
5050
let input = force_any_val::<EcPoint>();
5151
let expr: Expr = MethodCall::new(
52-
input.clone().into(),
52+
input.into(),
5353
sgroup_elem::GET_ENCODED_METHOD.clone(),
5454
vec![],
5555
)
@@ -66,13 +66,9 @@ mod tests {
6666
#[test]
6767
fn eval_negate() {
6868
let input = force_any_val::<EcPoint>();
69-
let expr: Expr = MethodCall::new(
70-
input.clone().into(),
71-
sgroup_elem::NEGATE_METHOD.clone(),
72-
vec![],
73-
)
74-
.unwrap()
75-
.into();
69+
let expr: Expr = MethodCall::new(input.into(), sgroup_elem::NEGATE_METHOD.clone(), vec![])
70+
.unwrap()
71+
.into();
7672
assert_eq!(-input, eval_out_wo_ctx::<EcPoint>(&expr))
7773
}
7874
}

ergotree-interpreter/src/sigma_protocol/dht_protocol.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod interactive_prover {
7474
);
7575

7676
// COMPUTE a = g^z*u^(-e) and b = h^z*v^{-e} (where -e here means -e mod q)
77-
let e: Scalar = challenge.clone().into();
77+
let e: Scalar = challenge.into();
7878
let minus_e = e.negate();
7979
let h_to_z = exponentiate(&public_input.h, &z);
8080
let g_to_z = exponentiate(&public_input.g, &z);
@@ -114,7 +114,7 @@ pub mod interactive_prover {
114114
rnd: &Wscalar,
115115
challenge: &Challenge,
116116
) -> SecondDhTupleProverMessage {
117-
let e: Scalar = challenge.clone().into();
117+
let e: Scalar = challenge.into();
118118
// modulo multiplication, no need to explicit mod op
119119
let ew = e.mul(private_input.w.as_scalar_ref());
120120
// modulo addition, no need to explicit mod op
@@ -141,7 +141,7 @@ pub mod interactive_prover {
141141

142142
let z = second_message.z.clone();
143143

144-
let e: Scalar = challenge.clone().into();
144+
let e: Scalar = challenge.into();
145145

146146
use ergo_chain_types::ec_point::{exponentiate, inverse};
147147

ergotree-interpreter/src/sigma_protocol/dlog_protocol.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub mod interactive_prover {
7474
);
7575

7676
//COMPUTE a = g^z*h^(-e) (where -e here means -e mod q)
77-
let e: Scalar = challenge.clone().into();
77+
let e: Scalar = challenge.into();
7878
let minus_e = e.negate();
7979
let h_to_e = exponentiate(&public_input.h, &minus_e);
8080
let g_to_z = exponentiate_gen(&z);
@@ -150,7 +150,7 @@ pub mod interactive_prover {
150150
rnd: Wscalar,
151151
challenge: &Challenge,
152152
) -> SecondDlogProverMessage {
153-
let e: Scalar = challenge.clone().into();
153+
let e: Scalar = challenge.into();
154154
// modulo multiplication, no need to explicit mod op
155155
let ew = e.mul(private_input.w.as_scalar_ref());
156156
// modulo addition, no need to explicit mod op
@@ -168,8 +168,8 @@ pub mod interactive_prover {
168168
challenge: &Challenge,
169169
second_message: &SecondDlogProverMessage,
170170
) -> EcPoint {
171-
let h = *proposition.h.clone();
172-
let e: Scalar = challenge.clone().into();
171+
let h = *proposition.h;
172+
let e: Scalar = challenge.into();
173173
let g_z = exponentiate_gen(second_message.z.as_scalar_ref());
174174
let h_e = exponentiate(&h, &e);
175175
g_z * &inverse(&h_e)

ergotree-interpreter/src/sigma_protocol/prover.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,11 @@ fn prove_to_unchecked<P: Prover + ?Sized>(
216216
// Prover Steps 7: convert the relevant information in the tree (namely, tree structure, node types,
217217
// the statements being proven and commitments at the leaves)
218218
// to a string
219-
let var_name = fiat_shamir_tree_to_bytes(&step6.clone().into())?;
220-
let mut s = var_name;
219+
let mut s = fiat_shamir_tree_to_bytes(&step6.clone().into())?;
221220

222221
// Prover Step 8: compute the challenge for the root of the tree as the Fiat-Shamir hash of s
223222
// and the message being signed.
224-
s.append(&mut message.to_vec());
223+
s.extend_from_slice(message);
225224
let root_challenge: Challenge = fiat_shamir_hash_fn(s.as_slice()).into();
226225
let step8 = step6.with_challenge(root_challenge);
227226
// dbg!(&step8);

0 commit comments

Comments
 (0)