-
Notifications
You must be signed in to change notification settings - Fork 42
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
Signature Subtraction function #67
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
79f6ef6
subtraction function which potentially returns two possible results
yeastplume b022f36
final arg checks + removal of debug printf output
yeastplume 8069aac
revert modifications to main test fike
yeastplume 45cfe0a
tweaks based on feedback
yeastplume a03783f
add case to handle possibility where two provided sigs result in subt…
yeastplume File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -346,6 +346,130 @@ int secp256k1_aggsig_partial_sign(const secp256k1_context* ctx, secp256k1_aggsig | |
return 1; | ||
} | ||
|
||
int secp256k1_aggsig_subtract_partial_signature( | ||
const secp256k1_context* ctx, | ||
unsigned char* result, | ||
unsigned char* result_alt, | ||
const unsigned char *sig64, | ||
const unsigned char *partial64 | ||
) { | ||
secp256k1_scalar tmp, tmp2; | ||
secp256k1_fe noncesum_fe; | ||
secp256k1_ge noncesum_ge; | ||
secp256k1_gej noncesum_gej; | ||
secp256k1_ge noncesum_ge_neg; | ||
secp256k1_gej noncesum_gej_neg; | ||
secp256k1_fe noncepartial_fe; | ||
secp256k1_ge noncepartial_ge; | ||
secp256k1_ge noncepartial_ge_neg; | ||
secp256k1_gej nonceresult_gej; | ||
secp256k1_gej nonceresult_gej_neg; | ||
secp256k1_ge final; | ||
int overflow; | ||
int neg_version_has_quad = 0; | ||
int pos_version_has_quad = 0; | ||
|
||
VERIFY_CHECK(ctx != NULL); | ||
ARG_CHECK(result != NULL); | ||
ARG_CHECK(result_alt != NULL); | ||
ARG_CHECK(sig64 != NULL); | ||
ARG_CHECK(partial64 != NULL); | ||
(void) ctx; | ||
|
||
/* Scalar portion, straightforward scalar subtraction */ | ||
secp256k1_scalar_set_b32(&tmp, sig64 + 32, &overflow); | ||
if (overflow) { | ||
secp256k1_scalar_clear(&tmp); | ||
return 0; | ||
} | ||
secp256k1_scalar_set_b32(&tmp2, partial64 + 32, &overflow); | ||
if (overflow) { | ||
secp256k1_scalar_clear(&tmp); | ||
secp256k1_scalar_clear(&tmp2); | ||
return 0; | ||
} | ||
secp256k1_scalar_negate(&tmp2, &tmp2); | ||
secp256k1_scalar_add(&tmp, &tmp, &tmp2); | ||
|
||
secp256k1_scalar_get_b32(result + 32, &tmp); | ||
secp256k1_scalar_get_b32(result_alt + 32, &tmp); | ||
secp256k1_scalar_clear(&tmp); | ||
secp256k1_scalar_clear(&tmp2); | ||
|
||
/* nonce portion | ||
* Note that we are unable to determine with 100% certainty | ||
* what nonce was originally chosen due to only the x coordinate | ||
* being stored. We can sometimes determine which was correct, but | ||
* may have to return a second possibility. | ||
*/ | ||
|
||
/* Parse nonce sum total and negated version (R, -R) */ | ||
if (!secp256k1_fe_set_b32(&noncesum_fe, sig64)) { | ||
return 0; | ||
} | ||
|
||
/* initialize nonce sum with y value that is a quadratic residue */ | ||
secp256k1_ge_set_xquad(&noncesum_ge, &noncesum_fe); | ||
secp256k1_gej_set_ge(&noncesum_gej, &noncesum_ge); | ||
|
||
/* also initialize negated version -R */ | ||
secp256k1_ge_neg(&noncesum_ge_neg, &noncesum_ge); | ||
secp256k1_gej_set_ge(&noncesum_gej_neg, &noncesum_ge_neg); | ||
|
||
/* Parse provided partial-sig nonce and negate */ | ||
if (!secp256k1_fe_set_b32(&noncepartial_fe, partial64)) { | ||
return 0; | ||
} | ||
|
||
/* Initialize negated version of partial sum, which we're going | ||
to subtract from the nonce total */ | ||
secp256k1_ge_set_xquad(&noncepartial_ge, &noncepartial_fe); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly for these 2. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be in the same file as above |
||
secp256k1_ge_neg(&noncepartial_ge_neg, &noncepartial_ge); | ||
|
||
/* Try positive (Rr = R-Rs) */ | ||
secp256k1_gej_add_ge(&nonceresult_gej, &noncesum_gej, &noncepartial_ge_neg); | ||
|
||
/* Now try neg (Rr = -R-Rs) */ | ||
secp256k1_gej_add_ge(&nonceresult_gej_neg, &noncesum_gej_neg, &noncepartial_ge_neg); | ||
|
||
pos_version_has_quad = secp256k1_gej_has_quad_y_var(&nonceresult_gej); | ||
neg_version_has_quad = secp256k1_gej_has_quad_y_var(&nonceresult_gej_neg); | ||
|
||
/* If ONLY the positive 'version' of Rr (=R-Rs) or only the | ||
negative version of Rr (=-R-Rs) results in a QR, then we know | ||
for certain what the original x value was */ | ||
if (pos_version_has_quad && !neg_version_has_quad) { | ||
secp256k1_ge_set_gej(&final, &nonceresult_gej); | ||
secp256k1_fe_normalize_var(&final.x); | ||
secp256k1_fe_get_b32(result, &final.x); | ||
return 1; | ||
} else if (!pos_version_has_quad && neg_version_has_quad) { | ||
secp256k1_ge_set_gej(&final, &nonceresult_gej_neg); | ||
secp256k1_fe_normalize_var(&final.x); | ||
secp256k1_fe_get_b32(result, &final.x); | ||
return 1; | ||
} else if (pos_version_has_quad && neg_version_has_quad) { | ||
/* if both versions result in a QR, it could have been either, | ||
so we need to return both possibilities and indicate the user | ||
needs to potentially check a second value */ | ||
secp256k1_ge_set_gej(&final, &nonceresult_gej); | ||
secp256k1_fe_normalize_var(&final.x); | ||
secp256k1_fe_get_b32(result, &final.x); | ||
|
||
secp256k1_ge_set_gej(&final, &nonceresult_gej_neg); | ||
secp256k1_fe_normalize_var(&final.x); | ||
secp256k1_fe_get_b32(result_alt, &final.x); | ||
return 2; | ||
} else { | ||
/* if neither result in a QR, then the signature is invalid according | ||
to our construction. Note this should never happen with signatures constructed | ||
via this particular API, and there's a chance that signatures using a different | ||
convention for selecting y coordinates could still return a valid but 'incorrect' | ||
value. */ | ||
return -1; | ||
} | ||
} | ||
|
||
int secp256k1_aggsig_combine_signatures(const secp256k1_context* ctx, secp256k1_aggsig_context* aggctx, unsigned char *sig64, const secp256k1_aggsig_partial_signature *partial, size_t n_sigs) { | ||
size_t i; | ||
secp256k1_scalar s; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what all these 4 calls do. Could you add comments for each? I wish I could just click on the name and be taken to their definition, but sadly I don't have such an editor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have an editor set up for ansi-C code traversal either, but core secp256k1 functions are all documented in
src/field.h
for field elements functions andsrc/group.h
for group elements.secp256k1-zkp/src/field.h
Line 74 in 8d1f5bb
secp256k1-zkp/src/group.h
Line 50 in 8d1f5bb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean you should add comments in the style of "/* Parse nonce sum total and negated version (R, -R) */" for every reader's benefit, so it's clear why you need to call that specific function...