Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to secp 0.26 and secp-sys 0.8 #62

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use-serde = ["serde", "secp256k1/serde"]
use-rand = ["rand", "secp256k1/rand"]

[dependencies]
secp256k1 = "0.24.0"
secp256k1 = "0.26.0"
secp256k1-zkp-sys = { version = "0.7.0", default-features = false, path = "./secp256k1-zkp-sys" }
rand = { version = "0.8", default-features = false, optional = true }
serde = { version = "1.0", default-features = false, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion secp256k1-zkp-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ features = [ "recovery", "lowmemory" ]
cc = "1.0.28"

[dependencies]
secp256k1-sys = "0.6.0"
secp256k1-sys = "0.8.0"

[features]
default = ["std"]
Expand Down
58 changes: 48 additions & 10 deletions secp256k1-zkp-sys/src/zkp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use core::{fmt, hash};
use core::{
fmt,
hash::{self, Hash},
};
use {types::*, Context, PublicKey, Signature};

/// Rangeproof maximum length
Expand Down Expand Up @@ -472,6 +475,8 @@ impl RangeProof {
}

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

// TODO: Replace this with ffi::PublicKey?
#[repr(C)]
#[derive(Copy, Clone)]
pub struct PedersenCommitment([c_uchar; 64]);
impl_array_newtype!(PedersenCommitment, c_uchar, 64);
impl_raw_debug!(PedersenCommitment);
Expand All @@ -518,6 +524,23 @@ impl Default for PedersenCommitment {
}
}

#[cfg(not(fuzzing))]
impl PartialEq for PedersenCommitment {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(fuzzing))]
impl Eq for PedersenCommitment {}

#[cfg(not(fuzzing))]
impl Hash for PedersenCommitment {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}

/// A ring signature for the "whitelist" scheme.
#[repr(C)]
#[derive(Clone)]
Expand Down Expand Up @@ -566,22 +589,37 @@ pub type EcdsaAdaptorNonceFn = Option<
>;

#[repr(C)]
#[derive(Copy, Clone)]
pub struct EcdsaAdaptorSignature([u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]);
impl_array_newtype!(EcdsaAdaptorSignature, u8, ECDSA_ADAPTOR_SIGNATURE_LENGTH);
impl_raw_debug!(EcdsaAdaptorSignature);

impl From<[u8; 162]> for EcdsaAdaptorSignature {
fn from(bytes: [u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH]) -> Self {
EcdsaAdaptorSignature(bytes)
impl EcdsaAdaptorSignature {
/// Create a new (zeroed) ecdsa adaptor signature usable for the FFI interface
pub fn new() -> Self {
EcdsaAdaptorSignature([0u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH])
}
}

impl EcdsaAdaptorSignature {
pub fn new() -> EcdsaAdaptorSignature {
EcdsaAdaptorSignature([0; ECDSA_ADAPTOR_SIGNATURE_LENGTH])
/// Create a new ecdsa adaptor signature usable for the FFI interface from raw bytes
///
/// # Safety
///
/// Does not check the validity of the underlying representation. If it is
/// invalid the result may be assertation failures (and process aborts) from
/// the underlying library. You should not use this method except with data
/// that you obtained from the FFI interface of the same version of this
/// library.
pub unsafe fn from_array_unchecked(data: [c_uchar; ECDSA_ADAPTOR_SIGNATURE_LENGTH]) -> Self {
Self(data)
}
}

pub fn as_bytes(&self) -> &[u8; ECDSA_ADAPTOR_SIGNATURE_LENGTH] {
&self.0
#[cfg(not(fuzzing))]
impl PartialEq for EcdsaAdaptorSignature {
fn eq(&self, other: &Self) -> bool {
&self.0[..] == &other.0[..]
}
}

#[cfg(not(fuzzing))]
impl Eq for EcdsaAdaptorSignature {}
18 changes: 11 additions & 7 deletions src/zkp/ecdsa_adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct EcdsaAdaptorSignature(ffi::EcdsaAdaptorSignature);

impl fmt::LowerHex for EcdsaAdaptorSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for ch in self.0.as_bytes().iter() {
for ch in self.0.as_ref().iter() {
write!(f, "{:02x}", ch)?;
}
Ok(())
Expand Down Expand Up @@ -56,7 +56,7 @@ impl ::serde::Serialize for EcdsaAdaptorSignature {
if s.is_human_readable() {
s.collect_str(self)
} else {
s.serialize_bytes(self.0.as_bytes())
s.serialize_bytes(self.0.as_ref())
}
}
}
Expand Down Expand Up @@ -102,7 +102,11 @@ impl EcdsaAdaptorSignature {
ECDSA_ADAPTOR_SIGNATURE_LENGTH => {
let mut ret = [0; ECDSA_ADAPTOR_SIGNATURE_LENGTH];
ret[..].copy_from_slice(data);
Ok(EcdsaAdaptorSignature(ffi::EcdsaAdaptorSignature::from(ret)))
unsafe {
Ok(EcdsaAdaptorSignature(
ffi::EcdsaAdaptorSignature::from_array_unchecked(ret),
))
}
}
_ => Err(Error::InvalidEcdsaAdaptorSignature),
}
Expand Down Expand Up @@ -169,7 +173,7 @@ impl EcdsaAdaptorSignature {

let res = unsafe {
ffi::secp256k1_ecdsa_adaptor_encrypt(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut adaptor_sig,
sk.as_c_ptr(),
enckey.as_c_ptr(),
Expand Down Expand Up @@ -198,7 +202,7 @@ impl EcdsaAdaptorSignature {

let res = unsafe {
ffi::secp256k1_ecdsa_adaptor_encrypt(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut adaptor_sig,
sk.as_c_ptr(),
enckey.as_c_ptr(),
Expand Down Expand Up @@ -242,7 +246,7 @@ impl EcdsaAdaptorSignature {

let ret = unsafe {
ffi::secp256k1_ecdsa_adaptor_recover(
*secp.ctx(),
secp.ctx().as_ptr(),
data.as_mut_c_ptr(),
sig.as_c_ptr(),
self.as_c_ptr(),
Expand All @@ -267,7 +271,7 @@ impl EcdsaAdaptorSignature {
) -> Result<(), Error> {
let res = unsafe {
ffi::secp256k1_ecdsa_adaptor_verify(
*secp.ctx(),
secp.ctx().as_ptr(),
self.as_c_ptr(),
pubkey.as_c_ptr(),
msg.as_c_ptr(),
Expand Down
9 changes: 5 additions & 4 deletions src/zkp/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use rand::Rng;
///
/// Contrary to a [`crate::SecretKey`], the value 0 is also a valid tweak.
/// Values outside secp curve order are invalid tweaks.
#[derive(Default)]
#[derive(Default, Copy, Clone)]
#[cfg_attr(not(fuzzing), derive(Eq, PartialEq))]
pub struct Tweak([u8; constants::SECRET_KEY_SIZE]);
impl_array_newtype!(Tweak, u8, constants::SECRET_KEY_SIZE);

Expand Down Expand Up @@ -156,10 +157,10 @@ impl Generator {

let ret = unsafe {
ffi::secp256k1_generator_generate_blinded(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut generator,
tag.into_inner().as_ptr(),
blinding_factor.as_ptr(),
tag.into_inner().as_c_ptr(),
blinding_factor.as_c_ptr(),
)
};
assert_eq!(ret, 1);
Expand Down
18 changes: 13 additions & 5 deletions src/zkp/pedersen.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ffi::CPtr;

use crate::ffi;
use crate::{from_hex, Error, Generator, Secp256k1, Signing, Tweak, ZERO_TWEAK};
use core::{fmt, slice, str};
Expand Down Expand Up @@ -58,9 +60,9 @@ impl PedersenCommitment {

let ret = unsafe {
ffi::secp256k1_pedersen_commit(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut commitment,
blinding_factor.as_ptr(),
blinding_factor.as_c_ptr(),
value,
generator.as_inner(),
)
Expand Down Expand Up @@ -141,12 +143,12 @@ pub fn compute_adaptive_blinding_factor<C: Signing>(

let (vbf, gbf) = secrets
.iter_mut()
.map(|(s_v, s_g)| (s_v.as_mut_ptr(), s_g.as_ptr()))
.map(|(s_v, s_g)| (s_v.as_mut_c_ptr(), s_g.as_c_ptr()))
.unzip::<_, _, Vec<_>, Vec<_>>();

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

let ret = unsafe {
ffi::secp256k1_pedersen_verify_tally(*secp.ctx(), a.as_ptr(), a.len(), b.as_ptr(), b.len())
ffi::secp256k1_pedersen_verify_tally(
secp.ctx().as_ptr(),
a.as_ptr(),
a.len(),
b.as_ptr(),
b.len(),
)
};

ret == 1
Expand Down
14 changes: 8 additions & 6 deletions src/zkp/rangeproof.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use ffi::CPtr;

use crate::ffi::RANGEPROOF_MAX_LENGTH;
use crate::from_hex;
use crate::Error;
Expand Down Expand Up @@ -81,13 +83,13 @@ impl RangeProof {

let ret = unsafe {
ffi::secp256k1_rangeproof_sign(
*secp.ctx(),
secp.ctx().as_ptr(),
proof.as_mut_ptr(),
&mut proof_length,
min_value,
commitment.as_inner(),
commitment_blinding.as_ptr(),
sk.as_ptr(),
commitment_blinding.as_c_ptr(),
sk.as_c_ptr(),
exp,
min_bits as i32,
value,
Expand Down Expand Up @@ -123,7 +125,7 @@ impl RangeProof {

let ret = unsafe {
ffi::secp256k1_rangeproof_verify(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut min_value,
&mut max_value,
commitment.as_inner(),
Expand Down Expand Up @@ -164,12 +166,12 @@ impl RangeProof {

let ret = unsafe {
ffi::secp256k1_rangeproof_rewind(
*secp.ctx(),
secp.ctx().as_ptr(),
blinding_factor.as_mut_ptr(),
&mut value,
message.as_mut_ptr(),
&mut message_length,
sk.as_ptr(),
sk.as_c_ptr(),
&mut min_value,
&mut max_value,
commitment.as_inner(),
Expand Down
11 changes: 6 additions & 5 deletions src/zkp/surjection_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct SurjectionProof {
mod with_rand {
use super::*;
use crate::{Signing, Tag, Tweak};
use ffi::CPtr;
use rand::Rng;

impl SurjectionProof {
Expand Down Expand Up @@ -49,7 +50,7 @@ mod with_rand {

let ret = unsafe {
ffi::secp256k1_surjectionproof_initialize(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut proof,
&mut domain_index,
domain_tags.as_ptr(),
Expand All @@ -70,7 +71,7 @@ mod with_rand {

let ret = unsafe {
ffi::secp256k1_surjectionproof_generate(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut proof,
domain_blinded_tags.as_ptr(),
domain.len(),
Expand All @@ -80,8 +81,8 @@ mod with_rand {
.get(domain_index)
.ok_or(Error::CannotProveSurjection)?
.2
.as_ptr(), // TODO: Return dedicated error here?
codomain_blinding_factor.as_ptr(),
.as_c_ptr(), // TODO: Return dedicated error here?
codomain_blinding_factor.as_c_ptr(),
)
};

Expand Down Expand Up @@ -168,7 +169,7 @@ impl SurjectionProof {

let ret = unsafe {
ffi::secp256k1_surjectionproof_verify(
*secp.ctx(),
secp.ctx().as_ptr(),
&self.inner,
domain_blinded_tags.as_ptr(),
domain_blinded_tags.len(),
Expand Down
8 changes: 4 additions & 4 deletions src/zkp/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ impl WhitelistSignature {
let mut sig = ffi::WhitelistSignature::default();
let ret = unsafe {
ffi::secp256k1_whitelist_sign(
*secp.ctx(),
secp.ctx().as_ptr(),
&mut sig,
// These two casts are legit because PublicKey has repr(transparent).
online_keys.as_c_ptr() as *const ffi::PublicKey,
offline_keys.as_c_ptr() as *const ffi::PublicKey,
n_keys,
whitelist_key.as_c_ptr(),
online_secret_key.as_ptr(),
summed_secret_key.as_ptr(),
online_secret_key.as_c_ptr(),
summed_secret_key.as_c_ptr(),
key_index,
)
};
Expand All @@ -116,7 +116,7 @@ impl WhitelistSignature {

let ret = unsafe {
ffi::secp256k1_whitelist_verify(
*secp.ctx(),
secp.ctx().as_ptr(),
&self.0,
// These two casts are legit because PublicKey has repr(transparent).
online_keys.as_c_ptr() as *const ffi::PublicKey,
Expand Down