Skip to content

Commit dd05268

Browse files
authored
Stable rust (#47)
* Support stable rust Updated dependencies such that Rust stable 1.53 is now supported. The optimised avx_2 option will NOT rust on stable because there's still an unstable feature on subtle-ng. BUT this feature is actually for doc generation and has been removed from Rust. As soon as subtle-ng merges dalek-cryptography/subtle#85, avx2 will probably be supported on stable as well. The rand dep update breaks the benchmarks, but these will be fixed in the next commit. * Update benchmarks and fmt * Update benchmark code to use new dependency APIs * Run rust-fmt
1 parent e1adaa2 commit dd05268

File tree

11 files changed

+53
-74
lines changed

11 files changed

+53
-74
lines changed

Cargo.toml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ categories = ["cryptography"]
77
homepage = "https://tari.com"
88
readme = "README.md"
99
license = "BSD-3-Clause"
10-
version = "0.9.1"
10+
version = "0.10.0"
1111
edition = "2018"
1212

1313
[dependencies]
1414
tari_utilities = "^0.3"
1515
base64 = "0.10.1"
1616
digest = "0.8.0"
17-
rand = "0.7.2"
17+
rand = { version = "0.8", default-features = false }
1818
clear_on_drop = "=0.2.4"
19-
curve25519-dalek = { version = "2" }
20-
bulletproofs = {version = "2.1.0" , package="tari_bulletproofs"}
21-
merlin = "2.0.0"
19+
curve25519-dalek = { package = "curve25519-dalek-ng", version = "4", default-features = false, features = ["u64_backend", "serde", "alloc"] }
20+
bulletproofs = {version = "4.0.0", package="tari_bulletproofs"}
21+
merlin = { version = "3", default-features = false }
2222
sha2 = "0.8.0"
2323
sha3 = "0.9"
2424
thiserror = "1.0.20"
@@ -33,16 +33,21 @@ libc = { version = "0.2", optional = true }
3333
wasm-bindgen = { version = "^0.2", features = ["serde-serialize"], optional = true }
3434

3535
[dev-dependencies]
36-
criterion = "0.2"
36+
criterion = "0.3.4"
3737
bincode = "1.1.4"
3838

3939
[build-dependencies]
4040
cbindgen = "0.17.0"
4141

4242
[features]
43-
default = []
43+
default = ["no_cc"]
44+
# Note: avx2 still requires a nightly compiler as of 01/07/2021
45+
# The nightly compiler must be older than 2021-06-03 to avoid the
46+
# #![cfg_attr(feature = "nightly", feature(external_doc))]
47+
# ^^^^^^^^^^^^ feature has been removed
48+
# feature error on subtle-ng
4449
avx2 = ["curve25519-dalek/avx2_backend", "bulletproofs/avx2_backend"]
45-
wasm = ["wasm-bindgen", "rand/wasm-bindgen", "rand/getrandom"]
50+
wasm = ["wasm-bindgen", "rand/getrandom"]
4651
ffi = ["libc"]
4752
no_cc_nightly = ["clear_on_drop/nightly"]
4853
no_cc = ["clear_on_drop/no_cc"]

benches/range_proof.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
2121
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2222

23-
use criterion::{criterion_group, Criterion};
23+
use criterion::{criterion_group, BenchmarkId, Criterion};
2424
use rand::{thread_rng, Rng};
2525
use std::time::Duration;
2626
use tari_crypto::{
@@ -40,36 +40,31 @@ fn setup(n: usize) -> (DalekRangeProofService, RistrettoSecretKey, u64, Pedersen
4040
let prover = DalekRangeProofService::new(n, &base).unwrap();
4141
let k = RistrettoSecretKey::random(&mut rng);
4242
let n_max = 1u64 << (n as u64 - 1);
43-
let v = rng.gen_range(1, n_max);
43+
let v = rng.gen_range(1..n_max);
4444
let c = base.commit_value(&k, v);
4545
(prover, k, v, c)
4646
}
4747

4848
pub fn generate_rangeproof(c: &mut Criterion) {
49-
c.bench_function_over_inputs(
50-
"Generate range proofs",
51-
|b, range| {
52-
let (prover, k, v, _) = setup(**range);
49+
let mut group = c.benchmark_group("Generate and validate range proofs");
50+
for input in &[8, 16, 32, 64] {
51+
let parameter_str = format!("{} bytes", input);
52+
// let proof = prover.construct_proof(&k, v).unwrap();
53+
group.bench_with_input(BenchmarkId::new("construct_proof", &parameter_str), input, |b, n| {
54+
let (prover, k, v, _) = setup(*n);
5355
b.iter(move || prover.construct_proof(&k, v).unwrap());
54-
},
55-
&[8, 16, 32, 64],
56-
);
57-
}
58-
59-
pub fn verify_rangeproof_valid(c: &mut Criterion) {
60-
c.bench_function_over_inputs(
61-
"Validate valid range proofs",
62-
|b, range| {
63-
let (prover, k, v, c) = setup(**range);
64-
let proof = prover.construct_proof(&k, v).unwrap();
65-
b.iter(move || assert!(prover.verify(&proof, &c)));
66-
},
67-
&[8, 16, 32, 64],
68-
);
56+
});
57+
group.bench_with_input(BenchmarkId::new("validate_proof", &parameter_str), input, |b, n| {
58+
let (verifier, k, v, c) = setup(*n);
59+
let proof = verifier.construct_proof(&k, v).unwrap();
60+
b.iter(move || assert!(verifier.verify(&proof, &c)));
61+
});
62+
}
63+
group.finish();
6964
}
7065

7166
criterion_group!(
7267
name = range_proofs;
7368
config = Criterion::default().warm_up_time(Duration::from_millis(1_500));
74-
targets = generate_rangeproof, verify_rangeproof_valid
69+
targets = generate_rangeproof
7570
);

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2020-09-11
1+
nightly-2021-06-01

rustfmt.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ comment_width = 120
44
format_strings = true
55
hard_tabs = false
66
imports_layout = "HorizontalVertical"
7-
merge_imports = true
7+
imports_granularity="Crate"
88
match_block_trailing_comma = true
99
max_width = 120
1010
newline_style = "Native"

src/ffi/keys.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ pub unsafe extern "C" fn sign(
6666
msg: *const c_char,
6767
nonce: *mut KeyArray,
6868
signature: *mut KeyArray,
69-
) -> c_int
70-
{
69+
) -> c_int {
7170
if nonce.is_null() || signature.is_null() || priv_key.is_null() || msg.is_null() {
7271
return NULL_POINTER;
7372
}
@@ -98,8 +97,7 @@ pub unsafe extern "C" fn verify(
9897
pub_nonce: *mut KeyArray,
9998
signature: *mut KeyArray,
10099
err_code: *mut c_int,
101-
) -> bool
102-
{
100+
) -> bool {
103101
if pub_key.is_null() || msg.is_null() || pub_nonce.is_null() || signature.is_null() || err_code.is_null() {
104102
if !err_code.is_null() {
105103
*err_code = NULL_POINTER;
@@ -140,8 +138,7 @@ pub unsafe extern "C" fn commitment(
140138
value: *const KeyArray,
141139
spend_key: *const KeyArray,
142140
commitment: *mut KeyArray,
143-
) -> c_int
144-
{
141+
) -> c_int {
145142
if value.is_null() || spend_key.is_null() || spend_key.is_null() {
146143
return NULL_POINTER;
147144
}
@@ -168,8 +165,7 @@ pub unsafe extern "C" fn sign_comsig(
168165
public_nonce: *mut KeyArray,
169166
signature_u: *mut KeyArray,
170167
signature_v: *mut KeyArray,
171-
) -> c_int
172-
{
168+
) -> c_int {
173169
if secret_a.is_null() ||
174170
secret_x.is_null() ||
175171
msg.is_null() ||
@@ -214,8 +210,7 @@ pub unsafe extern "C" fn verify_comsig(
214210
signature_u: *const KeyArray,
215211
signature_v: *const KeyArray,
216212
err_code: *mut c_int,
217-
) -> bool
218-
{
213+
) -> bool {
219214
if commitment.is_null() || msg.is_null() || public_nonce.is_null() || signature_u.is_null() || signature_v.is_null()
220215
{
221216
*err_code = NULL_POINTER;

src/ristretto/dalek_range_proof.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ impl RangeProofService for DalekRangeProofService {
110110
rewind_key: &RistrettoSecretKey,
111111
rewind_blinding_key: &RistrettoSecretKey,
112112
proof_message: &[u8; REWIND_USER_MESSAGE_LENGTH],
113-
) -> Result<Vec<u8>, RangeProofError>
114-
{
113+
) -> Result<Vec<u8>, RangeProofError> {
115114
let mut pt = Transcript::new(b"tari");
116115
let mut full_proof_message = [0u8; REWIND_PROOF_MESSAGE_LENGTH];
117116
full_proof_message[0..REWIND_CHECK_MESSAGE.len()].clone_from_slice(REWIND_CHECK_MESSAGE);
@@ -141,8 +140,7 @@ impl RangeProofService for DalekRangeProofService {
141140
commitment: &PedersenCommitment,
142141
rewind_public_key: &RistrettoPublicKey,
143142
rewind_blinding_public_key: &RistrettoPublicKey,
144-
) -> Result<RewindResult, RangeProofError>
145-
{
143+
) -> Result<RewindResult, RangeProofError> {
146144
let rp = DalekProof::from_bytes(&proof).map_err(|_| RangeProofError::InvalidProof)?;
147145

148146
let mut pt = Transcript::new(b"tari");
@@ -176,8 +174,7 @@ impl RangeProofService for DalekRangeProofService {
176174
commitment: &PedersenCommitment,
177175
rewind_key: &RistrettoSecretKey,
178176
rewind_blinding_key: &RistrettoSecretKey,
179-
) -> Result<FullRewindResult<RistrettoSecretKey>, RangeProofError>
180-
{
177+
) -> Result<FullRewindResult<RistrettoSecretKey>, RangeProofError> {
181178
let rp = DalekProof::from_bytes(&proof).map_err(|_| RangeProofError::InvalidProof)?;
182179

183180
let mut pt = Transcript::new(b"tari");

src/ristretto/musig.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,7 @@ impl<D: Digest> RistrettoMuSig<D> {
270270
pub_key: &RistrettoPublicKey,
271271
secret: &RistrettoSecretKey,
272272
nonce: &RistrettoSecretKey,
273-
) -> Option<RistrettoSchnorr>
274-
{
273+
) -> Option<RistrettoSchnorr> {
275274
let index = self.index_of(pub_key)?;
276275
let pub_nonce = self.get_public_nonce(index)?;
277276
let ai = self.get_musig_scalar(pub_key)?;
@@ -544,8 +543,7 @@ impl SignatureCollection {
544543
r_agg: &RistrettoPublicKey,
545544
p_agg: &RistrettoPublicKey,
546545
m: &MessageHashSlice,
547-
) -> RistrettoSecretKey
548-
{
546+
) -> RistrettoSecretKey {
549547
let e = D::new()
550548
.chain(r_agg.as_bytes())
551549
.chain(p_agg.as_bytes())

src/ristretto/script_commitment.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ impl ScriptCommitmentFactory {
135135
key: &RistrettoSecretKey,
136136
value: u64,
137137
script: &TariScript,
138-
) -> Result<ScriptCommitment, ScriptCommitmentError>
139-
{
138+
) -> Result<ScriptCommitment, ScriptCommitmentError> {
140139
if D::output_size() < 32 {
141140
return Err(ScriptCommitmentError::InvalidDigestLength);
142141
}
@@ -166,8 +165,7 @@ impl ScriptCommitmentFactory {
166165
v: u64,
167166
script: &TariScript,
168167
commitment: &PedersenCommitment,
169-
) -> bool
170-
{
168+
) -> bool {
171169
match self.commit_script::<D>(k, v, script) {
172170
Ok(sc) => commitment == &self.script_to_pedersen(&sc),
173171
_ => false,
@@ -179,8 +177,7 @@ impl ScriptCommitmentFactory {
179177
key: &RistrettoSecretKey,
180178
c: &PedersenCommitment,
181179
s: &TariScript,
182-
) -> Result<RistrettoSecretKey, ScriptCommitmentError>
183-
{
180+
) -> Result<RistrettoSecretKey, ScriptCommitmentError> {
184181
let script_hash = s
185182
.as_hash::<D>()
186183
.map_err(|_| ScriptCommitmentError::InvalidDigestLength)?;

src/ristretto/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ pub struct SignatureSet {
4141
pub fn sign<D: Digest>(
4242
private_key: &RistrettoSecretKey,
4343
message: &[u8],
44-
) -> Result<SignatureSet, SchnorrSignatureError>
45-
{
44+
) -> Result<SignatureSet, SchnorrSignatureError> {
4645
let mut rng = rand::thread_rng();
4746
let (nonce, public_nonce) = RistrettoPublicKey::random_keypair(&mut rng);
4847
let message = D::new().chain(public_nonce.as_bytes()).chain(message).result().to_vec();

src/script/tari_script.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ impl TariScript {
6868
&self,
6969
inputs: &ExecutionStack,
7070
context: &ScriptContext,
71-
) -> Result<StackItem, ScriptError>
72-
{
71+
) -> Result<StackItem, ScriptError> {
7372
// Copy all inputs onto the stack
7473
let mut stack = inputs.clone();
7574

@@ -186,8 +185,7 @@ impl TariScript {
186185
stack: &mut ExecutionStack,
187186
ctx: &ScriptContext,
188187
state: &mut ExecutionState,
189-
) -> Result<(), ScriptError>
190-
{
188+
) -> Result<(), ScriptError> {
191189
use Opcode::*;
192190
use StackItem::*;
193191
match opcode {

0 commit comments

Comments
 (0)