Skip to content

Commit f2fd733

Browse files
committed
Merge #62: Update to secp 0.26 and secp-sys 0.8
a1cff98 Exclude rust 1.41.1 on macos in CI (Tibo-lg) fa5d316 Update to secp 0.26 and secp-sys 0.8 (Tibo-lg) Pull request description: This PR updates the rust-secp256k1(-sys) dependencies to latest released ones. I tried to mimic what was done upstream. Regarding the trait implementations (`Eq`, `PartialEq`,...), since the sys types in this crate don't seem to have `serialize` functions I felt like it was ok to just derive then but please correct me if that's wrong. ACKs for top commit: apoelstra: ACK a1cff98 Tree-SHA512: 3f98bece371e589866e1ba7820f5e32761008dedcbaaf2bbf06152dca9f967d87611a1b8d1a8e639ba033c4505e460ee5bdbf2b5f3cd2021dbca5cabecd3398d
2 parents cea8ada + a1cff98 commit f2fd733

File tree

10 files changed

+103
-53
lines changed

10 files changed

+103
-53
lines changed

.github/workflows/rust.yml

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,9 @@ jobs:
7474
- 1.41.1
7575
- beta
7676
- stable
77-
target: [ x86_64-unknown-linux-gnu, x86_64-apple-darwin ]
78-
include:
79-
- target: x86_64-unknown-linux-gnu
80-
os: ubuntu-latest
81-
- target: x86_64-apple-darwin
77+
os: [ ubuntu-latest, macos-latest ]
78+
exclude:
79+
- rust: 1.41.1
8280
os: macos-latest
8381
runs-on: ${{ matrix.os }}
8482
steps:
@@ -108,11 +106,9 @@ jobs:
108106
- 1.41.1
109107
- beta
110108
- stable
111-
target: [ x86_64-unknown-linux-gnu, x86_64-apple-darwin ]
112-
include:
113-
- target: x86_64-unknown-linux-gnu
114-
os: ubuntu-latest
115-
- target: x86_64-apple-darwin
109+
os: [ ubuntu-latest, macos-latest ]
110+
exclude:
111+
- rust: 1.41.1
116112
os: macos-latest
117113
runs-on: ${{ matrix.os }}
118114
steps:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use-serde = ["serde", "secp256k1/serde"]
3131
use-rand = ["rand", "secp256k1/rand"]
3232

3333
[dependencies]
34-
secp256k1 = "0.24.0"
34+
secp256k1 = "0.26.0"
3535
secp256k1-zkp-sys = { version = "0.7.0", default-features = false, path = "./secp256k1-zkp-sys" }
3636
rand = { version = "0.8", default-features = false, optional = true }
3737
serde = { version = "1.0", default-features = false, optional = true }

secp256k1-zkp-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ features = [ "recovery", "lowmemory" ]
2323
cc = "1.0.28"
2424

2525
[dependencies]
26-
secp256k1-sys = "0.6.0"
26+
secp256k1-sys = "0.8.0"
2727

2828
[features]
2929
default = ["std"]

secp256k1-zkp-sys/src/zkp.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use core::{fmt, hash};
1+
use core::{
2+
fmt,
3+
hash::{self, Hash},
4+
};
25
use {types::*, Context, PublicKey, Signature};
36

47
/// Rangeproof maximum length
@@ -472,6 +475,8 @@ impl RangeProof {
472475
}
473476

474477
#[repr(C)]
478+
#[derive(Copy, Clone)]
479+
#[cfg_attr(not(fuzzing), derive(Eq, PartialEq, Hash, Ord, PartialOrd))]
475480
pub struct Tag([c_uchar; 32]);
476481
impl_array_newtype!(Tag, c_uchar, 32);
477482
impl_raw_debug!(Tag);
@@ -502,6 +507,7 @@ impl From<Tag> for [u8; 32] {
502507

503508
// TODO: Replace this with ffi::PublicKey?
504509
#[repr(C)]
510+
#[derive(Copy, Clone)]
505511
pub struct PedersenCommitment([c_uchar; 64]);
506512
impl_array_newtype!(PedersenCommitment, c_uchar, 64);
507513
impl_raw_debug!(PedersenCommitment);
@@ -518,6 +524,23 @@ impl Default for PedersenCommitment {
518524
}
519525
}
520526

527+
#[cfg(not(fuzzing))]
528+
impl PartialEq for PedersenCommitment {
529+
fn eq(&self, other: &Self) -> bool {
530+
&self.0[..] == &other.0[..]
531+
}
532+
}
533+
534+
#[cfg(not(fuzzing))]
535+
impl Eq for PedersenCommitment {}
536+
537+
#[cfg(not(fuzzing))]
538+
impl Hash for PedersenCommitment {
539+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
540+
self.0.hash(state);
541+
}
542+
}
543+
521544
/// A ring signature for the "whitelist" scheme.
522545
#[repr(C)]
523546
#[derive(Clone)]
@@ -566,22 +589,37 @@ pub type EcdsaAdaptorNonceFn = Option<
566589
>;
567590

568591
#[repr(C)]
592+
#[derive(Copy, Clone)]
569593
pub struct EcdsaAdaptorSignature([u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]);
570594
impl_array_newtype!(EcdsaAdaptorSignature, u8, ECDSA_ADAPTOR_SIGNATURE_LENGTH);
571595
impl_raw_debug!(EcdsaAdaptorSignature);
572596

573-
impl From<[u8; 162]> for EcdsaAdaptorSignature {
574-
fn from(bytes: [u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]) -> Self {
575-
EcdsaAdaptorSignature(bytes)
597+
impl EcdsaAdaptorSignature {
598+
/// Create a new (zeroed) ecdsa adaptor signature usable for the FFI interface
599+
pub fn new() -> Self {
600+
EcdsaAdaptorSignature([0u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH])
576601
}
577-
}
578602

579-
impl EcdsaAdaptorSignature {
580-
pub fn new() -> EcdsaAdaptorSignature {
581-
EcdsaAdaptorSignature([0; ECDSA_ADAPTOR_SIGNATURE_LENGTH])
603+
/// Create a new ecdsa adaptor signature usable for the FFI interface from raw bytes
604+
///
605+
/// # Safety
606+
///
607+
/// Does not check the validity of the underlying representation. If it is
608+
/// invalid the result may be assertation failures (and process aborts) from
609+
/// the underlying library. You should not use this method except with data
610+
/// that you obtained from the FFI interface of the same version of this
611+
/// library.
612+
pub unsafe fn from_array_unchecked(data: [c_uchar; ECDSA_ADAPTOR_SIGNATURE_LENGTH]) -> Self {
613+
Self(data)
582614
}
615+
}
583616

584-
pub fn as_bytes(&self) -> &[u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH] {
585-
&self.0
617+
#[cfg(not(fuzzing))]
618+
impl PartialEq for EcdsaAdaptorSignature {
619+
fn eq(&self, other: &Self) -> bool {
620+
&self.0[..] == &other.0[..]
586621
}
587622
}
623+
624+
#[cfg(not(fuzzing))]
625+
impl Eq for EcdsaAdaptorSignature {}

src/zkp/ecdsa_adaptor.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct EcdsaAdaptorSignature(ffi::EcdsaAdaptorSignature);
2424

2525
impl fmt::LowerHex for EcdsaAdaptorSignature {
2626
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
for ch in self.0.as_bytes().iter() {
27+
for ch in self.0.as_ref().iter() {
2828
write!(f, "{:02x}", ch)?;
2929
}
3030
Ok(())
@@ -56,7 +56,7 @@ impl ::serde::Serialize for EcdsaAdaptorSignature {
5656
if s.is_human_readable() {
5757
s.collect_str(self)
5858
} else {
59-
s.serialize_bytes(self.0.as_bytes())
59+
s.serialize_bytes(self.0.as_ref())
6060
}
6161
}
6262
}
@@ -102,7 +102,11 @@ impl EcdsaAdaptorSignature {
102102
ECDSA_ADAPTOR_SIGNATURE_LENGTH => {
103103
let mut ret = [0; ECDSA_ADAPTOR_SIGNATURE_LENGTH];
104104
ret[..].copy_from_slice(data);
105-
Ok(EcdsaAdaptorSignature(ffi::EcdsaAdaptorSignature::from(ret)))
105+
unsafe {
106+
Ok(EcdsaAdaptorSignature(
107+
ffi::EcdsaAdaptorSignature::from_array_unchecked(ret),
108+
))
109+
}
106110
}
107111
_ => Err(Error::InvalidEcdsaAdaptorSignature),
108112
}
@@ -169,7 +173,7 @@ impl EcdsaAdaptorSignature {
169173

170174
let res = unsafe {
171175
ffi::secp256k1_ecdsa_adaptor_encrypt(
172-
*secp.ctx(),
176+
secp.ctx().as_ptr(),
173177
&mut adaptor_sig,
174178
sk.as_c_ptr(),
175179
enckey.as_c_ptr(),
@@ -198,7 +202,7 @@ impl EcdsaAdaptorSignature {
198202

199203
let res = unsafe {
200204
ffi::secp256k1_ecdsa_adaptor_encrypt(
201-
*secp.ctx(),
205+
secp.ctx().as_ptr(),
202206
&mut adaptor_sig,
203207
sk.as_c_ptr(),
204208
enckey.as_c_ptr(),
@@ -242,7 +246,7 @@ impl EcdsaAdaptorSignature {
242246

243247
let ret = unsafe {
244248
ffi::secp256k1_ecdsa_adaptor_recover(
245-
*secp.ctx(),
249+
secp.ctx().as_ptr(),
246250
data.as_mut_c_ptr(),
247251
sig.as_c_ptr(),
248252
self.as_c_ptr(),
@@ -267,7 +271,7 @@ impl EcdsaAdaptorSignature {
267271
) -> Result<(), Error> {
268272
let res = unsafe {
269273
ffi::secp256k1_ecdsa_adaptor_verify(
270-
*secp.ctx(),
274+
secp.ctx().as_ptr(),
271275
self.as_c_ptr(),
272276
pubkey.as_c_ptr(),
273277
msg.as_c_ptr(),

src/zkp/generator.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use rand::Rng;
88
///
99
/// Contrary to a [`crate::SecretKey`], the value 0 is also a valid tweak.
1010
/// Values outside secp curve order are invalid tweaks.
11-
#[derive(Default)]
11+
#[derive(Default, Copy, Clone)]
12+
#[cfg_attr(not(fuzzing), derive(Eq, PartialEq))]
1213
pub struct Tweak([u8; constants::SECRET_KEY_SIZE]);
1314
impl_array_newtype!(Tweak, u8, constants::SECRET_KEY_SIZE);
1415

@@ -156,10 +157,10 @@ impl Generator {
156157

157158
let ret = unsafe {
158159
ffi::secp256k1_generator_generate_blinded(
159-
*secp.ctx(),
160+
secp.ctx().as_ptr(),
160161
&mut generator,
161-
tag.into_inner().as_ptr(),
162-
blinding_factor.as_ptr(),
162+
tag.into_inner().as_c_ptr(),
163+
blinding_factor.as_c_ptr(),
163164
)
164165
};
165166
assert_eq!(ret, 1);

src/zkp/pedersen.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ffi::CPtr;
2+
13
use crate::ffi;
24
use crate::{from_hex, Error, Generator, Secp256k1, Signing, Tweak, ZERO_TWEAK};
35
use core::{fmt, slice, str};
@@ -58,9 +60,9 @@ impl PedersenCommitment {
5860

5961
let ret = unsafe {
6062
ffi::secp256k1_pedersen_commit(
61-
*secp.ctx(),
63+
secp.ctx().as_ptr(),
6264
&mut commitment,
63-
blinding_factor.as_ptr(),
65+
blinding_factor.as_c_ptr(),
6466
value,
6567
generator.as_inner(),
6668
)
@@ -141,12 +143,12 @@ pub fn compute_adaptive_blinding_factor<C: Signing>(
141143

142144
let (vbf, gbf) = secrets
143145
.iter_mut()
144-
.map(|(s_v, s_g)| (s_v.as_mut_ptr(), s_g.as_ptr()))
146+
.map(|(s_v, s_g)| (s_v.as_mut_c_ptr(), s_g.as_c_ptr()))
145147
.unzip::<_, _, Vec<_>, Vec<_>>();
146148

147149
let ret = unsafe {
148150
ffi::secp256k1_pedersen_blind_generator_blind_sum(
149-
*secp.ctx(),
151+
secp.ctx().as_ptr(),
150152
values.as_ptr(),
151153
gbf.as_ptr(),
152154
vbf.as_ptr(),
@@ -172,7 +174,13 @@ pub fn verify_commitments_sum_to_equal<C: Signing>(
172174
let b = b.iter().map(|c| &c.0).collect::<Vec<_>>();
173175

174176
let ret = unsafe {
175-
ffi::secp256k1_pedersen_verify_tally(*secp.ctx(), a.as_ptr(), a.len(), b.as_ptr(), b.len())
177+
ffi::secp256k1_pedersen_verify_tally(
178+
secp.ctx().as_ptr(),
179+
a.as_ptr(),
180+
a.len(),
181+
b.as_ptr(),
182+
b.len(),
183+
)
176184
};
177185

178186
ret == 1

src/zkp/rangeproof.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use ffi::CPtr;
2+
13
use crate::ffi::RANGEPROOF_MAX_LENGTH;
24
use crate::from_hex;
35
use crate::Error;
@@ -81,13 +83,13 @@ impl RangeProof {
8183

8284
let ret = unsafe {
8385
ffi::secp256k1_rangeproof_sign(
84-
*secp.ctx(),
86+
secp.ctx().as_ptr(),
8587
proof.as_mut_ptr(),
8688
&mut proof_length,
8789
min_value,
8890
commitment.as_inner(),
89-
commitment_blinding.as_ptr(),
90-
sk.as_ptr(),
91+
commitment_blinding.as_c_ptr(),
92+
sk.as_c_ptr(),
9193
exp,
9294
min_bits as i32,
9395
value,
@@ -123,7 +125,7 @@ impl RangeProof {
123125

124126
let ret = unsafe {
125127
ffi::secp256k1_rangeproof_verify(
126-
*secp.ctx(),
128+
secp.ctx().as_ptr(),
127129
&mut min_value,
128130
&mut max_value,
129131
commitment.as_inner(),
@@ -164,12 +166,12 @@ impl RangeProof {
164166

165167
let ret = unsafe {
166168
ffi::secp256k1_rangeproof_rewind(
167-
*secp.ctx(),
169+
secp.ctx().as_ptr(),
168170
blinding_factor.as_mut_ptr(),
169171
&mut value,
170172
message.as_mut_ptr(),
171173
&mut message_length,
172-
sk.as_ptr(),
174+
sk.as_c_ptr(),
173175
&mut min_value,
174176
&mut max_value,
175177
commitment.as_inner(),

src/zkp/surjection_proof.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct SurjectionProof {
1515
mod with_rand {
1616
use super::*;
1717
use crate::{Signing, Tag, Tweak};
18+
use ffi::CPtr;
1819
use rand::Rng;
1920

2021
impl SurjectionProof {
@@ -49,7 +50,7 @@ mod with_rand {
4950

5051
let ret = unsafe {
5152
ffi::secp256k1_surjectionproof_initialize(
52-
*secp.ctx(),
53+
secp.ctx().as_ptr(),
5354
&mut proof,
5455
&mut domain_index,
5556
domain_tags.as_ptr(),
@@ -70,7 +71,7 @@ mod with_rand {
7071

7172
let ret = unsafe {
7273
ffi::secp256k1_surjectionproof_generate(
73-
*secp.ctx(),
74+
secp.ctx().as_ptr(),
7475
&mut proof,
7576
domain_blinded_tags.as_ptr(),
7677
domain.len(),
@@ -80,8 +81,8 @@ mod with_rand {
8081
.get(domain_index)
8182
.ok_or(Error::CannotProveSurjection)?
8283
.2
83-
.as_ptr(), // TODO: Return dedicated error here?
84-
codomain_blinding_factor.as_ptr(),
84+
.as_c_ptr(), // TODO: Return dedicated error here?
85+
codomain_blinding_factor.as_c_ptr(),
8586
)
8687
};
8788

@@ -168,7 +169,7 @@ impl SurjectionProof {
168169

169170
let ret = unsafe {
170171
ffi::secp256k1_surjectionproof_verify(
171-
*secp.ctx(),
172+
secp.ctx().as_ptr(),
172173
&self.inner,
173174
domain_blinded_tags.as_ptr(),
174175
domain_blinded_tags.len(),

0 commit comments

Comments
 (0)