{"action":"created","comment":{"url":"https://api.github.com/repos/DeDiS/crypto/pulls/comments/25568423","id":25568423,"diff_hunk":"@@ -21,25 +24,274 @@ func HashBench(b *testing.B, hash func() hash.Hash) {\n \n // Benchmark a stream cipher.\n func StreamCipherBench(b *testing.B, keylen int,\n-\t\t\tcipher func([]byte) cipher.Stream) {\n+\tcipher func([]byte) cipher.Stream) {\n \tkey := make([]byte, keylen)\n \tb.SetBytes(1024 * 1024)\n \tdata := make([]byte, 1024)\n \tfor i := 0; i < b.N; i++ {\n \t\tc := cipher(key)\n \t\tfor j := 0; j < 1024; j++ {\n-\t\t\tc.XORKeyStream(data,data)\n+\t\t\tc.XORKeyStream(data, data)\n \t\t}\n \t}\n }\n \n // Benchmark a block cipher operating in counter mode.\n+/*\n+XXX Broken\n func BlockCipherBench(b *testing.B, keylen int,\n-\t\t\tbcipher func([]byte) cipher.Block) {\n+\tbcipher func([]byte) cipher.Block) {\n \tStreamCipherBench(b, keylen, func(key []byte) cipher.Stream {\n \t\tbc := bcipher(key)\n-\t\tiv := make([]byte,bc.BlockSize())\n-\t\treturn cipher.NewCTR(bc,iv)\n+\t\tiv := make([]byte, bc.BlockSize())\n+\t\treturn cipher.NewCTR(bc, iv)\n \t})\n }\n+*/\n+\n+// Compares the bits between two arrays returning the fraction\n+// of differences. If the two arrays are not of the same length\n+// no comparison is made and a -1 is returned.\n+func BitDiff(a, b []byte) float64 {\n+\tif len(a) != len(b) {\n+\t\treturn -1\n+\t}\n+\n+\tmask1 := byte(1)\n+\tmask2 := byte(2)\n+\tmask3 := byte(4)\n+\tmask4 := byte(8)\n+\tmask5 := byte(16)\n+\tmask6 := byte(32)\n+\tmask7 := byte(64)\n+\tmask8 := byte(128)\n+\n+\tcount := 0\n+\tfor i := 0; i < len(a); i++ {\n+\t\tif (a[i] & mask1) != (b[i] & mask1) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask2) != (b[i] & mask2) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask3) != (b[i] & mask3) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask4) != (b[i] & mask4) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask5) != (b[i] & mask5) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask6) != (b[i] & mask6) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask7) != (b[i] & mask7) {\n+\t\t\tcount += 1\n+\t\t}\n+\t\tif (a[i] & mask8) != (b[i] & mask8) {\n+\t\t\tcount += 1\n+\t\t}\n+\t}\n+\n+\treturn float64(count) / float64(len(a)*8)\n+}\n+\n+// Tests a Cipher can encrypt and decrypt\n+func BCHelloWorldHelper(t *testing.T,\n+\tnewCipher func([]byte, ...interface{}) abstract.Cipher,\n+\tn int, bitdiff float64) {\n+\ttext := []byte(\"Hello, World\")\n+\tcryptsize := len(text)\n+\n+\tbc := newCipher(nil)\n+\tkeysize := bc.KeySize()\n+\n+\tnkeys := make([][]byte, n)\n+\tncrypts := make([][]byte, n)\n+\n+\tfor i := range nkeys {\n+\t\tnkeys[i] = make([]byte, keysize)\n+\t\trand.Read(nkeys[i])\n+\t\tbc = newCipher(nkeys[i])\n+\t\tncrypts[i] = make([]byte, cryptsize)\n+\t\tbc.Message(ncrypts[i], text, ncrypts[i])\n+\n+\t\tbc = newCipher(nkeys[i])\n+\t\tdecrypted := make([]byte, len(text))\n+\t\tbc.Message(decrypted, ncrypts[i], ncrypts[i])\n+\t\tif !bytes.Equal(text, decrypted) {\n+\t\t\tt.Log(\"Encryption / Decryption failed\", i)\n+\t\t\tt.FailNow()\n+\t\t}\n+\t}\n+\n+\tfor i := range ncrypts {\n+\t\tfor j := i + 1; j < len(ncrypts); j++ {\n+\t\t\tif bytes.Equal(ncrypts[i], ncrypts[j]) {\n+\t\t\t\tt.Log(\"Different keys result in same encryption\")\n+\t\t\t\tt.FailNow()\n+\t\t\t}\n+\n+\t\t\tres := BitDiff(ncrypts[i], ncrypts[j])\n+\t\t\tif res < bitdiff {\n+\t\t\t\tt.Log(\"Encryptions not sufficiently different:\", res)\n+\t\t\t\tt.FailNow()\n+\t\t\t}\n+\t\t}\n+\t}\n+}\n+\n+// Tests a Cipher:\n+// 1) Encryption / decryption work\n+// 2) Encryption / decryption with different key don't work\n+// 3) Changing a bit in the ciphertext or mac results in failed mac check\n+// 4) Different keys produce sufficiently random output\n+func AuthenticateAndEncrypt(t *testing.T,\n+\tnewCipher func([]byte, ...interface{}) abstract.Cipher,\n+\tn int, bitdiff float64, text []byte) {\n+\tcryptsize := len(text)\n+\tdecrypted := make([]byte, len(text))\n+\n+\tbc := newCipher(nil)\n+\tkeysize := bc.KeySize()\n+\thashsize := bc.HashSize()\n+\tmac := make([]byte, hashsize)\n+\n+\tncrypts := make([][]byte, n)\n+\tnkeys := make([][]byte, n)\n+\tnmacs := make([][]byte, n)\n+\n+\t// Encrypt / decrypt / mac test\n+\tfor i := range nkeys {\n+\t\tnkeys[i] = make([]byte, keysize)","path":"test/cipher.go","position":134,"original_position":167,"commit_id":"18065ee4087b23576742c1bfc0a0fa0a7821d785","original_commit_id":"6f75e384335363c1582f641e745d10697896c6cc","user":{"login":"davidiw","id":73818,"avatar_url":"https://avatars.githubusercontent.com/u/73818?v=3","gravatar_id":"","url":"https://api.github.com/users/davidiw","html_url":"https://github.com/davidiw","followers_url":"https://api.github.com/users/davidiw/followers","following_url":"https://api.github.com/users/davidiw/following{/other_user}","gists_url":"https://api.github.com/users/davidiw/gists{/gist_id}","starred_url":"https://api.github.com/users/davidiw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidiw/subscriptions","organizations_url":"https://api.github.com/users/davidiw/orgs","repos_url":"https://api.github.com/users/davidiw/repos","events_url":"https://api.github.com/users/davidiw/events{/privacy}","received_events_url":"https://api.github.com/users/davidiw/received_events","type":"User","site_admin":false},"body":"I feel mixed about it, because I had the same reaction as I was writing it and similar thoughts during our discussion. I tend to prefer test code that would guide users on how to use the code or that matches my intuition on good coding practice. So if we wanted to really invest the time into making this better, we ought to pass in the keys and other init variables as parameters into the function, but then I feel like I'm starting to do the same level of optimization as I did with the bit comparison :).","created_at":"2015-03-01T16:04:13Z","updated_at":"2015-03-01T16:04:13Z","html_url":"https://github.com/DeDiS/crypto/pull/38#discussion_r25568423","pull_request_url":"https://api.github.com/repos/DeDiS/crypto/pulls/38","_links":{"self":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/comments/25568423"},"html":{"href":"https://github.com/DeDiS/crypto/pull/38#discussion_r25568423"},"pull_request":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/38"}}},"pull_request":{"url":"https://api.github.com/repos/DeDiS/crypto/pulls/38","id":29786526,"html_url":"https://github.com/DeDiS/crypto/pull/38","diff_url":"https://github.com/DeDiS/crypto/pull/38.diff","patch_url":"https://github.com/DeDiS/crypto/pull/38.patch","issue_url":"https://api.github.com/repos/DeDiS/crypto/issues/38","number":38,"state":"open","locked":false,"title":"Cipher fix, cipher test, and a note...","user":{"login":"davidiw","id":73818,"avatar_url":"https://avatars.githubusercontent.com/u/73818?v=3","gravatar_id":"","url":"https://api.github.com/users/davidiw","html_url":"https://github.com/davidiw","followers_url":"https://api.github.com/users/davidiw/followers","following_url":"https://api.github.com/users/davidiw/following{/other_user}","gists_url":"https://api.github.com/users/davidiw/gists{/gist_id}","starred_url":"https://api.github.com/users/davidiw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidiw/subscriptions","organizations_url":"https://api.github.com/users/davidiw/orgs","repos_url":"https://api.github.com/users/davidiw/repos","events_url":"https://api.github.com/users/davidiw/events{/privacy}","received_events_url":"https://api.github.com/users/davidiw/received_events","type":"User","site_admin":false},"body":"I can break this up into individual pull requests if there are any issues with any of the commits.","created_at":"2015-02-23T05:07:23Z","updated_at":"2015-03-01T16:04:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"632d4e57c632ba4c5cbcf29cc506f203abfd08c9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/DeDiS/crypto/pulls/38/commits","review_comments_url":"https://api.github.com/repos/DeDiS/crypto/pulls/38/comments","review_comment_url":"https://api.github.com/repos/DeDiS/crypto/pulls/comments{/number}","comments_url":"https://api.github.com/repos/DeDiS/crypto/issues/38/comments","statuses_url":"https://api.github.com/repos/DeDiS/crypto/statuses/18065ee4087b23576742c1bfc0a0fa0a7821d785","head":{"label":"davidiw:master","ref":"master","sha":"18065ee4087b23576742c1bfc0a0fa0a7821d785","user":{"login":"davidiw","id":73818,"avatar_url":"https://avatars.githubusercontent.com/u/73818?v=3","gravatar_id":"","url":"https://api.github.com/users/davidiw","html_url":"https://github.com/davidiw","followers_url":"https://api.github.com/users/davidiw/followers","following_url":"https://api.github.com/users/davidiw/following{/other_user}","gists_url":"https://api.github.com/users/davidiw/gists{/gist_id}","starred_url":"https://api.github.com/users/davidiw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidiw/subscriptions","organizations_url":"https://api.github.com/users/davidiw/orgs","repos_url":"https://api.github.com/users/davidiw/repos","events_url":"https://api.github.com/users/davidiw/events{/privacy}","received_events_url":"https://api.github.com/users/davidiw/received_events","type":"User","site_admin":false},"repo":{"id":30279412,"name":"crypto","full_name":"davidiw/crypto","owner":{"login":"davidiw","id":73818,"avatar_url":"https://avatars.githubusercontent.com/u/73818?v=3","gravatar_id":"","url":"https://api.github.com/users/davidiw","html_url":"https://github.com/davidiw","followers_url":"https://api.github.com/users/davidiw/followers","following_url":"https://api.github.com/users/davidiw/following{/other_user}","gists_url":"https://api.github.com/users/davidiw/gists{/gist_id}","starred_url":"https://api.github.com/users/davidiw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidiw/subscriptions","organizations_url":"https://api.github.com/users/davidiw/orgs","repos_url":"https://api.github.com/users/davidiw/repos","events_url":"https://api.github.com/users/davidiw/events{/privacy}","received_events_url":"https://api.github.com/users/davidiw/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/davidiw/crypto","description":"Advanced crypto library for the Go language","fork":true,"url":"https://api.github.com/repos/davidiw/crypto","forks_url":"https://api.github.com/repos/davidiw/crypto/forks","keys_url":"https://api.github.com/repos/davidiw/crypto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/davidiw/crypto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/davidiw/crypto/teams","hooks_url":"https://api.github.com/repos/davidiw/crypto/hooks","issue_events_url":"https://api.github.com/repos/davidiw/crypto/issues/events{/number}","events_url":"https://api.github.com/repos/davidiw/crypto/events","assignees_url":"https://api.github.com/repos/davidiw/crypto/assignees{/user}","branches_url":"https://api.github.com/repos/davidiw/crypto/branches{/branch}","tags_url":"https://api.github.com/repos/davidiw/crypto/tags","blobs_url":"https://api.github.com/repos/davidiw/crypto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/davidiw/crypto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/davidiw/crypto/git/refs{/sha}","trees_url":"https://api.github.com/repos/davidiw/crypto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/davidiw/crypto/statuses/{sha}","languages_url":"https://api.github.com/repos/davidiw/crypto/languages","stargazers_url":"https://api.github.com/repos/davidiw/crypto/stargazers","contributors_url":"https://api.github.com/repos/davidiw/crypto/contributors","subscribers_url":"https://api.github.com/repos/davidiw/crypto/subscribers","subscription_url":"https://api.github.com/repos/davidiw/crypto/subscription","commits_url":"https://api.github.com/repos/davidiw/crypto/commits{/sha}","git_commits_url":"https://api.github.com/repos/davidiw/crypto/git/commits{/sha}","comments_url":"https://api.github.com/repos/davidiw/crypto/comments{/number}","issue_comment_url":"https://api.github.com/repos/davidiw/crypto/issues/comments{/number}","contents_url":"https://api.github.com/repos/davidiw/crypto/contents/{+path}","compare_url":"https://api.github.com/repos/davidiw/crypto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/davidiw/crypto/merges","archive_url":"https://api.github.com/repos/davidiw/crypto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/davidiw/crypto/downloads","issues_url":"https://api.github.com/repos/davidiw/crypto/issues{/number}","pulls_url":"https://api.github.com/repos/davidiw/crypto/pulls{/number}","milestones_url":"https://api.github.com/repos/davidiw/crypto/milestones{/number}","notifications_url":"https://api.github.com/repos/davidiw/crypto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/davidiw/crypto/labels{/name}","releases_url":"https://api.github.com/repos/davidiw/crypto/releases{/id}","created_at":"2015-02-04T03:31:51Z","updated_at":"2015-03-01T16:00:55Z","pushed_at":"2015-03-01T16:00:55Z","git_url":"git://github.com/davidiw/crypto.git","ssh_url":"git@github.com:davidiw/crypto.git","clone_url":"https://github.com/davidiw/crypto.git","svn_url":"https://github.com/davidiw/crypto","homepage":null,"size":4634,"stargazers_count":0,"watchers_count":0,"language":"Go","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":"master"}},"base":{"label":"DeDiS:master","ref":"master","sha":"001aeb899ebd9ce6b4cb674bce27edb2708c8df1","user":{"login":"DeDiS","id":3638331,"avatar_url":"https://avatars.githubusercontent.com/u/3638331?v=3","gravatar_id":"","url":"https://api.github.com/users/DeDiS","html_url":"https://github.com/DeDiS","followers_url":"https://api.github.com/users/DeDiS/followers","following_url":"https://api.github.com/users/DeDiS/following{/other_user}","gists_url":"https://api.github.com/users/DeDiS/gists{/gist_id}","starred_url":"https://api.github.com/users/DeDiS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DeDiS/subscriptions","organizations_url":"https://api.github.com/users/DeDiS/orgs","repos_url":"https://api.github.com/users/DeDiS/repos","events_url":"https://api.github.com/users/DeDiS/events{/privacy}","received_events_url":"https://api.github.com/users/DeDiS/received_events","type":"Organization","site_admin":false},"repo":{"id":25253455,"name":"crypto","full_name":"DeDiS/crypto","owner":{"login":"DeDiS","id":3638331,"avatar_url":"https://avatars.githubusercontent.com/u/3638331?v=3","gravatar_id":"","url":"https://api.github.com/users/DeDiS","html_url":"https://github.com/DeDiS","followers_url":"https://api.github.com/users/DeDiS/followers","following_url":"https://api.github.com/users/DeDiS/following{/other_user}","gists_url":"https://api.github.com/users/DeDiS/gists{/gist_id}","starred_url":"https://api.github.com/users/DeDiS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DeDiS/subscriptions","organizations_url":"https://api.github.com/users/DeDiS/orgs","repos_url":"https://api.github.com/users/DeDiS/repos","events_url":"https://api.github.com/users/DeDiS/events{/privacy}","received_events_url":"https://api.github.com/users/DeDiS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/DeDiS/crypto","description":"Advanced crypto library for the Go language","fork":false,"url":"https://api.github.com/repos/DeDiS/crypto","forks_url":"https://api.github.com/repos/DeDiS/crypto/forks","keys_url":"https://api.github.com/repos/DeDiS/crypto/keys{/key_id}","collaborators_url":"https://api.github.com/repos/DeDiS/crypto/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/DeDiS/crypto/teams","hooks_url":"https://api.github.com/repos/DeDiS/crypto/hooks","issue_events_url":"https://api.github.com/repos/DeDiS/crypto/issues/events{/number}","events_url":"https://api.github.com/repos/DeDiS/crypto/events","assignees_url":"https://api.github.com/repos/DeDiS/crypto/assignees{/user}","branches_url":"https://api.github.com/repos/DeDiS/crypto/branches{/branch}","tags_url":"https://api.github.com/repos/DeDiS/crypto/tags","blobs_url":"https://api.github.com/repos/DeDiS/crypto/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/DeDiS/crypto/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/DeDiS/crypto/git/refs{/sha}","trees_url":"https://api.github.com/repos/DeDiS/crypto/git/trees{/sha}","statuses_url":"https://api.github.com/repos/DeDiS/crypto/statuses/{sha}","languages_url":"https://api.github.com/repos/DeDiS/crypto/languages","stargazers_url":"https://api.github.com/repos/DeDiS/crypto/stargazers","contributors_url":"https://api.github.com/repos/DeDiS/crypto/contributors","subscribers_url":"https://api.github.com/repos/DeDiS/crypto/subscribers","subscription_url":"https://api.github.com/repos/DeDiS/crypto/subscription","commits_url":"https://api.github.com/repos/DeDiS/crypto/commits{/sha}","git_commits_url":"https://api.github.com/repos/DeDiS/crypto/git/commits{/sha}","comments_url":"https://api.github.com/repos/DeDiS/crypto/comments{/number}","issue_comment_url":"https://api.github.com/repos/DeDiS/crypto/issues/comments{/number}","contents_url":"https://api.github.com/repos/DeDiS/crypto/contents/{+path}","compare_url":"https://api.github.com/repos/DeDiS/crypto/compare/{base}...{head}","merges_url":"https://api.github.com/repos/DeDiS/crypto/merges","archive_url":"https://api.github.com/repos/DeDiS/crypto/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/DeDiS/crypto/downloads","issues_url":"https://api.github.com/repos/DeDiS/crypto/issues{/number}","pulls_url":"https://api.github.com/repos/DeDiS/crypto/pulls{/number}","milestones_url":"https://api.github.com/repos/DeDiS/crypto/milestones{/number}","notifications_url":"https://api.github.com/repos/DeDiS/crypto/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/DeDiS/crypto/labels{/name}","releases_url":"https://api.github.com/repos/DeDiS/crypto/releases{/id}","created_at":"2014-10-15T12:25:03Z","updated_at":"2015-02-22T23:27:19Z","pushed_at":"2015-02-22T23:27:18Z","git_url":"git://github.com/DeDiS/crypto.git","ssh_url":"git@github.com:DeDiS/crypto.git","clone_url":"https://github.com/DeDiS/crypto.git","svn_url":"https://github.com/DeDiS/crypto","homepage":null,"size":5308,"stargazers_count":23,"watchers_count":23,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":7,"mirror_url":null,"open_issues_count":9,"forks":7,"open_issues":9,"watchers":23,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/38"},"html":{"href":"https://github.com/DeDiS/crypto/pull/38"},"issue":{"href":"https://api.github.com/repos/DeDiS/crypto/issues/38"},"comments":{"href":"https://api.github.com/repos/DeDiS/crypto/issues/38/comments"},"review_comments":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/38/comments"},"review_comment":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/DeDiS/crypto/pulls/38/commits"},"statuses":{"href":"https://api.github.com/repos/DeDiS/crypto/statuses/18065ee4087b23576742c1bfc0a0fa0a7821d785"}}}}