-
Notifications
You must be signed in to change notification settings - Fork 94
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
[Temporary] Pending bb work for aztec3 #368
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8b5cf9c
update js vk (because we now use UP for merkle hashing)
suyash67 95158a5
Helpers for ECDSA in A3 (#364)
suyash67 602ea19
Change pedersen hash c_bind to use `pedersen_hash::lookup`.
suyash67 d183672
c_binds and other ECDSA related fixes (#407)
suyash67 dee7ba4
chore: align BARRETENBERG_CRYPTO_GENERATOR_PARAMETERS_HACK usage (#411)
ludamad aeb8794
Update join_split test
ludamad c628211
Tweaks to comments
codygunton 21e6487
Add comment for the assertion in bigfield.
suyash67 e39c042
Expanded on ecdsa comment.
suyash67 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "ecdsa.hpp" | ||
#include <barretenberg/ecc/curves/secp256k1/secp256k1.hpp> | ||
|
||
#define WASM_EXPORT __attribute__((visibility("default"))) | ||
|
||
extern "C" { | ||
|
||
WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf) | ||
{ | ||
auto priv_key = from_buffer<secp256k1::fr>(private_key); | ||
secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key; | ||
write(public_key_buf, pub_key); | ||
} | ||
|
||
WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* private_key, | ||
uint8_t* output_sig_r, | ||
uint8_t* output_sig_s, | ||
uint8_t* output_sig_v) | ||
{ | ||
using serialize::write; | ||
auto priv_key = from_buffer<secp256k1::fr>(private_key); | ||
secp256k1::g1::affine_element pub_key = secp256k1::g1::one * priv_key; | ||
crypto::ecdsa::key_pair<secp256k1::fr, secp256k1::g1> key_pair = { priv_key, pub_key }; | ||
|
||
auto sig = crypto::ecdsa::construct_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>( | ||
std::string((char*)message, msg_len), key_pair); | ||
write(output_sig_r, sig.r); | ||
write(output_sig_s, sig.s); | ||
write(output_sig_v, sig.v); | ||
} | ||
|
||
WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* sig_r, | ||
uint8_t const* sig_s, | ||
uint8_t* sig_v, | ||
uint8_t* output_pub_key) | ||
{ | ||
std::array<uint8_t, 32> r, s; | ||
std::copy(sig_r, sig_r + 32, r.begin()); | ||
std::copy(sig_s, sig_s + 32, s.begin()); | ||
const uint8_t v = *sig_v; | ||
|
||
crypto::ecdsa::signature sig = { r, s, v }; | ||
auto recovered_pub_key = | ||
crypto::ecdsa::recover_public_key<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>( | ||
std::string((char*)message, msg_len), sig); | ||
write(output_pub_key, recovered_pub_key); | ||
} | ||
|
||
WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* pub_key, | ||
uint8_t const* sig_r, | ||
uint8_t const* sig_s, | ||
uint8_t const* sig_v) | ||
{ | ||
auto pubk = from_buffer<secp256k1::g1::affine_element>(pub_key); | ||
std::array<uint8_t, 32> r, s; | ||
std::copy(sig_r, sig_r + 32, r.begin()); | ||
std::copy(sig_s, sig_s + 32, s.begin()); | ||
const uint8_t v = *sig_v; | ||
|
||
crypto::ecdsa::signature sig = { r, s, v }; | ||
return crypto::ecdsa::verify_signature<Sha256Hasher, secp256k1::fq, secp256k1::fr, secp256k1::g1>( | ||
std::string((char*)message, msg_len), pubk, sig); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <ecc/curves/secp256k1/secp256k1.hpp> | ||
|
||
#define WASM_EXPORT __attribute__((visibility("default"))) | ||
|
||
extern "C" { | ||
|
||
WASM_EXPORT void ecdsa__compute_public_key(uint8_t const* private_key, uint8_t* public_key_buf); | ||
|
||
WASM_EXPORT void ecdsa__construct_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* private_key, | ||
uint8_t* output_sig_r, | ||
uint8_t* output_sig_s, | ||
uint8_t* output_sig_v); | ||
|
||
WASM_EXPORT void ecdsa__recover_public_key_from_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* sig_r, | ||
uint8_t const* sig_s, | ||
uint8_t* sig_v, | ||
uint8_t* output_pub_key); | ||
|
||
WASM_EXPORT bool ecdsa__verify_signature(uint8_t const* message, | ||
size_t msg_len, | ||
uint8_t const* pub_key, | ||
uint8_t const* sig_r, | ||
uint8_t const* sig_s, | ||
uint8_t const* sig_v); | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include "secp256k1.hpp" | ||
|
||
#define WASM_EXPORT __attribute__((visibility("default"))) | ||
|
||
extern "C" { | ||
|
||
WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result) | ||
{ | ||
auto point = from_buffer<secp256k1::g1::affine_element>(point_buf); | ||
auto scalar = from_buffer<secp256k1::fr>(scalar_buf); | ||
secp256k1::g1::affine_element r = point * scalar; | ||
write(result, r); | ||
} | ||
|
||
WASM_EXPORT void ecc_secp256k1__get_random_scalar_mod_circuit_modulus(uint8_t* result) | ||
{ | ||
barretenberg::fr output = barretenberg::fr::random_element(); | ||
write(result, output); | ||
} | ||
|
||
WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result) | ||
{ | ||
uint512_t bigint_input = from_buffer<uint512_t>(input); | ||
|
||
uint512_t barretenberg_modulus(barretenberg::fr::modulus); | ||
|
||
uint512_t target_output = bigint_input % barretenberg_modulus; | ||
write(result, target_output.lo); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "secp256k1.hpp" | ||
|
||
#define WASM_EXPORT __attribute__((visibility("default"))) | ||
|
||
extern "C" { | ||
|
||
WASM_EXPORT void ecc_secp256k1__mul(uint8_t const* point_buf, uint8_t const* scalar_buf, uint8_t* result); | ||
|
||
WASM_EXPORT void ecc_secp256k1__get_random_scalar_mod_circuit_modulus(uint8_t* result); | ||
|
||
WASM_EXPORT void ecc_secp256k1__reduce512_buffer_mod_circuit_modulus(uint8_t* input, uint8_t* result); | ||
} |
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
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
Oops, something went wrong.
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.
When
!found_root
it makes sense to skip the work below here (sampling a random byte and doing a field negation), but doesn't matter unless we plan to generate a lot of points with this function.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.
This was Kesha's suggestion and I agree with him. If we don't sample a bit and negate the y-coordinate, we're sampling a random point from only "upper" half of the curve points. To really get a "random" point on the curve, we need to randomly change the sign of the
y
coordinate.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.
Yes, this make sense, but my point is that if
found_root
isfalse
, then you don't need to bother sampling a random byte and negating a coordinate, so it would be more efficient to to wrap everything below line 251 in anif (found_root)
block. About half the time thewhile
loop will execute more than once, meaning that if you're generating 2 billion random points of the curve, then you will sample about 1 billion unneeded bytes and do about 1 billion unneeded 256-bit field element negations.