Skip to content

Commit

Permalink
Aggsig signature subtraction function, rust side (#81)
Browse files Browse the repository at this point in the history
* addition of ffi+api functions for aggsig partial sig subtract

* updated aggsig lib function to return more results based on underlying function

* update submodule to latest master

* clean up test result check

* bump version number
  • Loading branch information
yeastplume committed May 16, 2023
1 parent 8022b5a commit e9e4f09
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,7 +1,7 @@
[package]

name = "grin_secp256k1zkp"
version = "0.7.11"
version = "0.7.12"
authors = [ "Grin Developers <mimblewimble@lists.launchpad.net>",
"Dawid Ciężarkiewicz <dpc@ucore.info>",
"Andrew Poelstra <apoelstra@wpsoftware.net>" ]
Expand Down
43 changes: 42 additions & 1 deletion src/aggsig.rs
Expand Up @@ -258,6 +258,38 @@ pub fn add_signatures_single(
Ok(retsig)
}

/// Subtraction of partial signature from a signature
/// Returns: Ok((Signature, None)) on success if the resulting signature has only one possibility
/// Ok((Signature, Signature)) on success if the resulting signature could be one of either possiblity
/// In:
/// sig: completed signature from which to subtact a partial
/// partial_sig: the partial signature to subtract
pub fn subtract_partial_signature(
secp: &Secp256k1,
sig: &Signature,
partial_sig: &Signature,
) -> Result<(Signature, Option<Signature>), Error> {
let mut ret_partsig = Signature::from(ffi::Signature::new());
let mut ret_partsig_alt = Signature::from(ffi::Signature::new());
let retval = unsafe {
ffi::secp256k1_aggsig_subtract_partial_signature(
secp.ctx,
ret_partsig.as_mut_ptr(),
ret_partsig_alt.as_mut_ptr(),
sig.as_ptr(),
partial_sig.as_ptr(),
)
};

match retval {
-1 => Err(Error::SigSubtractionFailure),
1 => Ok((ret_partsig, None)),
2 => Ok((ret_partsig, Some(ret_partsig_alt))),
_ => Err(Error::InvalidSignature)
}
}


/// Manages an instance of an aggsig multisig context, and provides all methods
/// to act on that context
#[derive(Clone, Debug)]
Expand Down Expand Up @@ -396,7 +428,8 @@ mod tests {
add_signatures_single, export_secnonce_single, sign_single, verify_single, verify_batch,
AggSigContext, Secp256k1,
};
use crate::ffi;
use crate::aggsig::subtract_partial_signature;
use crate::ffi;
use crate::key::{PublicKey, SecretKey};
use rand::{thread_rng, Rng};
use crate::ContextFlag;
Expand Down Expand Up @@ -756,6 +789,14 @@ mod tests {
false,
);
assert!(result == true);

// Subtract sig1 from final sig
let (res_sig, res_sig_opt) = subtract_partial_signature(&secp, &final_sig, &sig1).unwrap();
assert!(res_sig == sig2 || res_sig_opt == Some(sig2));

// Subtract sig2 from final sig for good measure
let (res_sig, res_sig_opt) = subtract_partial_signature(&secp, &final_sig, &sig2).unwrap();
assert!(res_sig == sig1 || res_sig_opt == Some(sig1));
}
}
}
10 changes: 9 additions & 1 deletion src/ffi.rs
Expand Up @@ -333,7 +333,15 @@ extern "C" {
num_sigs: size_t,
pubnonce_total: *const PublicKey)
-> c_int;
// EC

pub fn secp256k1_aggsig_subtract_partial_signature(cx: *const Context,
ret_partsig: *mut Signature,
ret_partsig_alt: *mut Signature,
sig: *const Signature,
part_sig: *const Signature)
-> c_int;

// EC
pub fn secp256k1_ec_seckey_verify(cx: *const Context,
sk: *const c_uchar) -> c_int;

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -477,6 +477,8 @@ pub enum Error {
InvalidRangeProof,
/// Error creating partial signature
PartialSigFailure,
/// Failure subtracting two signatures
SigSubtractionFailure,
}

impl Error {
Expand All @@ -493,6 +495,7 @@ impl Error {
Error::IncorrectCommitSum => "secp: invalid pedersen commitment sum",
Error::InvalidRangeProof => "secp: invalid range proof",
Error::PartialSigFailure => "secp: partial sig (aggsig) failure",
Error::SigSubtractionFailure => "secp: subtraction (aggsig) did not result in any valid signatures",
}
}
}
Expand Down

0 comments on commit e9e4f09

Please sign in to comment.