{"action":"created","comment":{"url":"https://api.github.com/repos/zcash/zcash/pulls/comments/57836346","id":57836346,"diff_hunk":"@@ -0,0 +1,161 @@\n+#include \"NoteEncryption.hpp\"\n+#include \"crypto/sha512.h\"\n+#include <stdexcept>\n+#include \"sodium.h\"\n+#include <boost/static_assert.hpp>\n+#include \"prf.h\"\n+#include \"zerocash/Zerocash.h\"\n+\n+#define NOTEENCRYPTION_CIPHER_KEYSIZE 32\n+\n+void clamp_curve25519(unsigned char *key)\n+{\n+    key[0] &= 248;\n+    key[31] &= 127;\n+    key[31] |= 64;\n+}\n+\n+void KDF(unsigned char K[NOTEENCRYPTION_CIPHER_KEYSIZE],\n+    const uint256 &dhsecret,\n+    const uint256 &epk,\n+    const uint256 &pk_enc,\n+    const uint256 &seed,\n+    unsigned char nonce\n+   )\n+{\n+    unsigned char K_tmp[64];\n+    CSHA512 hasher;\n+\n+    // Trim the first byte from seed\n+    std::vector<unsigned char> first_248(seed.begin() + 1, seed.end());\n+\n+    if (nonce >= 2) {\n+        throw std::logic_error(\"KDF nonce space exceeded\");\n+    }\n+\n+    // First bit is the 'nonce' bit or `i`\n+    // Second bit is zero\n+    first_248[0] &= 0xFC;\n+    first_248[0] |= nonce;\n+\n+    hasher.Write(&first_248[0], 31);\n+    hasher.Write(dhsecret.begin(), crypto_scalarmult_BYTES);\n+    hasher.Write(epk.begin(), crypto_scalarmult_BYTES);\n+    hasher.Write(pk_enc.begin(), crypto_scalarmult_BYTES);\n+    hasher.Finalize(K_tmp);\n+\n+    memcpy(K, K_tmp + 32, 32);\n+}\n+\n+template<size_t MLEN>\n+NoteEncryption<MLEN>::NoteEncryption(uint256 seed) : nonce(0), seed(seed) {\n+    // All of this code assumes crypto_scalarmult_BYTES is 32\n+    // There's no reason that will _ever_ change, but for\n+    // completeness purposes, let's check anyway.\n+    BOOST_STATIC_ASSERT(32 == crypto_scalarmult_BYTES);\n+    BOOST_STATIC_ASSERT(32 == crypto_scalarmult_SCALARBYTES);\n+    BOOST_STATIC_ASSERT(NOTEENCRYPTION_AUTH_BYTES == crypto_aead_chacha20poly1305_ABYTES);\n+\n+    // Create the ephemeral keypair\n+    randombytes_buf(esk.begin(), crypto_scalarmult_SCALARBYTES);\n+    epk = generate_pubkey(esk);\n+}\n+\n+template<size_t MLEN>\n+NoteEncryption<MLEN>::~NoteEncryption() {\n+    sodium_memzero(epk.begin(), crypto_scalarmult_BYTES);\n+    sodium_memzero(esk.begin(), crypto_scalarmult_SCALARBYTES);\n+}\n+\n+template<size_t MLEN>\n+typename NoteEncryption<MLEN>::Ciphertext NoteEncryption<MLEN>::encrypt\n+                                          (const uint256 &pk_enc,\n+                                           const NoteEncryption<MLEN>::Plaintext &message\n+                                          )\n+{\n+    uint256 dhsecret;","path":"src/crypto/NoteEncryption.cpp","position":76,"original_position":76,"commit_id":"0bcfe2998f9afb2bd07dff6e6e721a28b3ae1421","original_commit_id":"0bcfe2998f9afb2bd07dff6e6e721a28b3ae1421","user":{"login":"ebfull","id":3794471,"avatar_url":"https://avatars.githubusercontent.com/u/3794471?v=3","gravatar_id":"","url":"https://api.github.com/users/ebfull","html_url":"https://github.com/ebfull","followers_url":"https://api.github.com/users/ebfull/followers","following_url":"https://api.github.com/users/ebfull/following{/other_user}","gists_url":"https://api.github.com/users/ebfull/gists{/gist_id}","starred_url":"https://api.github.com/users/ebfull/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebfull/subscriptions","organizations_url":"https://api.github.com/users/ebfull/orgs","repos_url":"https://api.github.com/users/ebfull/repos","events_url":"https://api.github.com/users/ebfull/events{/privacy}","received_events_url":"https://api.github.com/users/ebfull/received_events","type":"User","site_admin":false},"body":"I actually don't see much point in zeroing epk/esk anyway, I guess it can't hurt, but there are plenty of other places we don't zero things. It pretty much means nothing and I don't remember why I did it -- probably because if NoteEncryption gets heap allocated for some reason, I would want the data securely destroyed, something I don't usually care about for stack allocated stuff.\r\n\r\n@daira's input on this would be helpful.","created_at":"2016-03-30T04:39:10Z","updated_at":"2016-03-30T04:39:10Z","html_url":"https://github.com/zcash/zcash/pull/804#discussion_r57836346","pull_request_url":"https://api.github.com/repos/zcash/zcash/pulls/804","_links":{"self":{"href":"https://api.github.com/repos/zcash/zcash/pulls/comments/57836346"},"html":{"href":"https://github.com/zcash/zcash/pull/804#discussion_r57836346"},"pull_request":{"href":"https://api.github.com/repos/zcash/zcash/pulls/804"}}},"pull_request":{"url":"https://api.github.com/repos/zcash/zcash/pulls/804","id":64241728,"html_url":"https://github.com/zcash/zcash/pull/804","diff_url":"https://github.com/zcash/zcash/pull/804.diff","patch_url":"https://github.com/zcash/zcash/pull/804.patch","issue_url":"https://api.github.com/repos/zcash/zcash/issues/804","number":804,"state":"open","locked":false,"title":"Implementation and integration of ECIES replacement \"NoteEncryption\"","user":{"login":"ebfull","id":3794471,"avatar_url":"https://avatars.githubusercontent.com/u/3794471?v=3","gravatar_id":"","url":"https://api.github.com/users/ebfull","html_url":"https://github.com/ebfull","followers_url":"https://api.github.com/users/ebfull/followers","following_url":"https://api.github.com/users/ebfull/following{/other_user}","gists_url":"https://api.github.com/users/ebfull/gists{/gist_id}","starred_url":"https://api.github.com/users/ebfull/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebfull/subscriptions","organizations_url":"https://api.github.com/users/ebfull/orgs","repos_url":"https://api.github.com/users/ebfull/repos","events_url":"https://api.github.com/users/ebfull/events{/privacy}","received_events_url":"https://api.github.com/users/ebfull/received_events","type":"User","site_admin":false},"body":"* Implements `NoteEncryption` roughly as defined by our specification. (Some parts of the specification have not been finalized but the current code is intended to reflect what @daira believes will be the final spec details.)\r\n* Removes crypto++ dependency\r\n* Replaces ECIES with NoteEncryption\r\n\r\n**This is incompatible with the testnet blockchain because it changes the structure of `CTransaction` to accommodate the new scheme.**\r\n\r\nI've tried to avoid dragging in changes to the circuit like our h<sub>Sig</sub> modifications, the size of `r`, and even things like ordering of `rho` and `r` in the ciphertext. We can address these in another PR when we're done modifying the spec.\r\n\r\nSupersedes #795 \r\n\r\nCloses #558\r\n\r\nCloses #485\r\n\r\nCloses #515","created_at":"2016-03-26T16:14:51Z","updated_at":"2016-03-30T04:39:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3e6ea88c9c0d3d43a7de67e87f2142516cbeb444","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/zcash/zcash/pulls/804/commits","review_comments_url":"https://api.github.com/repos/zcash/zcash/pulls/804/comments","review_comment_url":"https://api.github.com/repos/zcash/zcash/pulls/comments{/number}","comments_url":"https://api.github.com/repos/zcash/zcash/issues/804/comments","statuses_url":"https://api.github.com/repos/zcash/zcash/statuses/0bcfe2998f9afb2bd07dff6e6e721a28b3ae1421","head":{"label":"ebfull:noteencryption","ref":"noteencryption","sha":"0bcfe2998f9afb2bd07dff6e6e721a28b3ae1421","user":{"login":"ebfull","id":3794471,"avatar_url":"https://avatars.githubusercontent.com/u/3794471?v=3","gravatar_id":"","url":"https://api.github.com/users/ebfull","html_url":"https://github.com/ebfull","followers_url":"https://api.github.com/users/ebfull/followers","following_url":"https://api.github.com/users/ebfull/following{/other_user}","gists_url":"https://api.github.com/users/ebfull/gists{/gist_id}","starred_url":"https://api.github.com/users/ebfull/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebfull/subscriptions","organizations_url":"https://api.github.com/users/ebfull/orgs","repos_url":"https://api.github.com/users/ebfull/repos","events_url":"https://api.github.com/users/ebfull/events{/privacy}","received_events_url":"https://api.github.com/users/ebfull/received_events","type":"User","site_admin":false},"repo":{"id":52047666,"name":"zcash","full_name":"ebfull/zcash","owner":{"login":"ebfull","id":3794471,"avatar_url":"https://avatars.githubusercontent.com/u/3794471?v=3","gravatar_id":"","url":"https://api.github.com/users/ebfull","html_url":"https://github.com/ebfull","followers_url":"https://api.github.com/users/ebfull/followers","following_url":"https://api.github.com/users/ebfull/following{/other_user}","gists_url":"https://api.github.com/users/ebfull/gists{/gist_id}","starred_url":"https://api.github.com/users/ebfull/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebfull/subscriptions","organizations_url":"https://api.github.com/users/ebfull/orgs","repos_url":"https://api.github.com/users/ebfull/repos","events_url":"https://api.github.com/users/ebfull/events{/privacy}","received_events_url":"https://api.github.com/users/ebfull/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ebfull/zcash","description":"zcash","fork":true,"url":"https://api.github.com/repos/ebfull/zcash","forks_url":"https://api.github.com/repos/ebfull/zcash/forks","keys_url":"https://api.github.com/repos/ebfull/zcash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ebfull/zcash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ebfull/zcash/teams","hooks_url":"https://api.github.com/repos/ebfull/zcash/hooks","issue_events_url":"https://api.github.com/repos/ebfull/zcash/issues/events{/number}","events_url":"https://api.github.com/repos/ebfull/zcash/events","assignees_url":"https://api.github.com/repos/ebfull/zcash/assignees{/user}","branches_url":"https://api.github.com/repos/ebfull/zcash/branches{/branch}","tags_url":"https://api.github.com/repos/ebfull/zcash/tags","blobs_url":"https://api.github.com/repos/ebfull/zcash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ebfull/zcash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ebfull/zcash/git/refs{/sha}","trees_url":"https://api.github.com/repos/ebfull/zcash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ebfull/zcash/statuses/{sha}","languages_url":"https://api.github.com/repos/ebfull/zcash/languages","stargazers_url":"https://api.github.com/repos/ebfull/zcash/stargazers","contributors_url":"https://api.github.com/repos/ebfull/zcash/contributors","subscribers_url":"https://api.github.com/repos/ebfull/zcash/subscribers","subscription_url":"https://api.github.com/repos/ebfull/zcash/subscription","commits_url":"https://api.github.com/repos/ebfull/zcash/commits{/sha}","git_commits_url":"https://api.github.com/repos/ebfull/zcash/git/commits{/sha}","comments_url":"https://api.github.com/repos/ebfull/zcash/comments{/number}","issue_comment_url":"https://api.github.com/repos/ebfull/zcash/issues/comments{/number}","contents_url":"https://api.github.com/repos/ebfull/zcash/contents/{+path}","compare_url":"https://api.github.com/repos/ebfull/zcash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ebfull/zcash/merges","archive_url":"https://api.github.com/repos/ebfull/zcash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ebfull/zcash/downloads","issues_url":"https://api.github.com/repos/ebfull/zcash/issues{/number}","pulls_url":"https://api.github.com/repos/ebfull/zcash/pulls{/number}","milestones_url":"https://api.github.com/repos/ebfull/zcash/milestones{/number}","notifications_url":"https://api.github.com/repos/ebfull/zcash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ebfull/zcash/labels{/name}","releases_url":"https://api.github.com/repos/ebfull/zcash/releases{/id}","deployments_url":"https://api.github.com/repos/ebfull/zcash/deployments","created_at":"2016-02-18T23:50:56Z","updated_at":"2016-02-18T23:50:59Z","pushed_at":"2016-03-26T22:44:05Z","git_url":"git://github.com/ebfull/zcash.git","ssh_url":"git@github.com:ebfull/zcash.git","clone_url":"https://github.com/ebfull/zcash.git","svn_url":"https://github.com/ebfull/zcash","homepage":"https://z.cash/","size":36034,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"zc.v0.11.2.latest"}},"base":{"label":"zcash:zc.v0.11.2.latest","ref":"zc.v0.11.2.latest","sha":"9bab3e89a24ffba4f40aadf95b21537cf8db84ed","user":{"login":"zcash","id":16122764,"avatar_url":"https://avatars.githubusercontent.com/u/16122764?v=3","gravatar_id":"","url":"https://api.github.com/users/zcash","html_url":"https://github.com/zcash","followers_url":"https://api.github.com/users/zcash/followers","following_url":"https://api.github.com/users/zcash/following{/other_user}","gists_url":"https://api.github.com/users/zcash/gists{/gist_id}","starred_url":"https://api.github.com/users/zcash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zcash/subscriptions","organizations_url":"https://api.github.com/users/zcash/orgs","repos_url":"https://api.github.com/users/zcash/repos","events_url":"https://api.github.com/users/zcash/events{/privacy}","received_events_url":"https://api.github.com/users/zcash/received_events","type":"Organization","site_admin":false},"repo":{"id":26987049,"name":"zcash","full_name":"zcash/zcash","owner":{"login":"zcash","id":16122764,"avatar_url":"https://avatars.githubusercontent.com/u/16122764?v=3","gravatar_id":"","url":"https://api.github.com/users/zcash","html_url":"https://github.com/zcash","followers_url":"https://api.github.com/users/zcash/followers","following_url":"https://api.github.com/users/zcash/following{/other_user}","gists_url":"https://api.github.com/users/zcash/gists{/gist_id}","starred_url":"https://api.github.com/users/zcash/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zcash/subscriptions","organizations_url":"https://api.github.com/users/zcash/orgs","repos_url":"https://api.github.com/users/zcash/repos","events_url":"https://api.github.com/users/zcash/events{/privacy}","received_events_url":"https://api.github.com/users/zcash/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/zcash/zcash","description":"zcash","fork":false,"url":"https://api.github.com/repos/zcash/zcash","forks_url":"https://api.github.com/repos/zcash/zcash/forks","keys_url":"https://api.github.com/repos/zcash/zcash/keys{/key_id}","collaborators_url":"https://api.github.com/repos/zcash/zcash/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/zcash/zcash/teams","hooks_url":"https://api.github.com/repos/zcash/zcash/hooks","issue_events_url":"https://api.github.com/repos/zcash/zcash/issues/events{/number}","events_url":"https://api.github.com/repos/zcash/zcash/events","assignees_url":"https://api.github.com/repos/zcash/zcash/assignees{/user}","branches_url":"https://api.github.com/repos/zcash/zcash/branches{/branch}","tags_url":"https://api.github.com/repos/zcash/zcash/tags","blobs_url":"https://api.github.com/repos/zcash/zcash/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/zcash/zcash/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/zcash/zcash/git/refs{/sha}","trees_url":"https://api.github.com/repos/zcash/zcash/git/trees{/sha}","statuses_url":"https://api.github.com/repos/zcash/zcash/statuses/{sha}","languages_url":"https://api.github.com/repos/zcash/zcash/languages","stargazers_url":"https://api.github.com/repos/zcash/zcash/stargazers","contributors_url":"https://api.github.com/repos/zcash/zcash/contributors","subscribers_url":"https://api.github.com/repos/zcash/zcash/subscribers","subscription_url":"https://api.github.com/repos/zcash/zcash/subscription","commits_url":"https://api.github.com/repos/zcash/zcash/commits{/sha}","git_commits_url":"https://api.github.com/repos/zcash/zcash/git/commits{/sha}","comments_url":"https://api.github.com/repos/zcash/zcash/comments{/number}","issue_comment_url":"https://api.github.com/repos/zcash/zcash/issues/comments{/number}","contents_url":"https://api.github.com/repos/zcash/zcash/contents/{+path}","compare_url":"https://api.github.com/repos/zcash/zcash/compare/{base}...{head}","merges_url":"https://api.github.com/repos/zcash/zcash/merges","archive_url":"https://api.github.com/repos/zcash/zcash/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/zcash/zcash/downloads","issues_url":"https://api.github.com/repos/zcash/zcash/issues{/number}","pulls_url":"https://api.github.com/repos/zcash/zcash/pulls{/number}","milestones_url":"https://api.github.com/repos/zcash/zcash/milestones{/number}","notifications_url":"https://api.github.com/repos/zcash/zcash/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/zcash/zcash/labels{/name}","releases_url":"https://api.github.com/repos/zcash/zcash/releases{/id}","deployments_url":"https://api.github.com/repos/zcash/zcash/deployments","created_at":"2014-11-22T03:13:10Z","updated_at":"2016-03-29T03:06:27Z","pushed_at":"2016-03-30T00:09:57Z","git_url":"git://github.com/zcash/zcash.git","ssh_url":"git@github.com:zcash/zcash.git","clone_url":"https://github.com/zcash/zcash.git","svn_url":"https://github.com/zcash/zcash","homepage":"https://z.cash/","size":40847,"stargazers_count":650,"watchers_count":650,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":61,"mirror_url":null,"open_issues_count":294,"forks":61,"open_issues":294,"watchers":650,"default_branch":"zc.v0.11.2.latest"}},"_links":{"self":{"href":"https://api.github.com/repos/zcash/zcash/pulls/804"},"html":{"href":"https://github.com/zcash/zcash/pull/804"},"issue":{"href":"https://api.github.com/repos/zcash/zcash/issues/804"},"comments":{"href":"https://api.github.com/repos/zcash/zcash/issues/804/comments"},"review_comments":{"href":"https://api.github.com/repos/zcash/zcash/pulls/804/comments"},"review_comment":{"href":"https://api.github.com/repos/zcash/zcash/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/zcash/zcash/pulls/804/commits"},"statuses":{"href":"https://api.github.com/repos/zcash/zcash/statuses/0bcfe2998f9afb2bd07dff6e6e721a28b3ae1421"}}}}