{"action":"created","issue":{"url":"https://api.github.com/repos/ethereum/ethereumj/issues/353","repository_url":"https://api.github.com/repos/ethereum/ethereumj","labels_url":"https://api.github.com/repos/ethereum/ethereumj/issues/353/labels{/name}","comments_url":"https://api.github.com/repos/ethereum/ethereumj/issues/353/comments","events_url":"https://api.github.com/repos/ethereum/ethereumj/issues/353/events","html_url":"https://github.com/ethereum/ethereumj/issues/353","id":140000829,"number":353,"title":"Consensus failure; ecrecover precompiled contract","user":{"login":"holiman","id":142290,"avatar_url":"https://avatars.githubusercontent.com/u/142290?v=3","gravatar_id":"","url":"https://api.github.com/users/holiman","html_url":"https://github.com/holiman","followers_url":"https://api.github.com/users/holiman/followers","following_url":"https://api.github.com/users/holiman/following{/other_user}","gists_url":"https://api.github.com/users/holiman/gists{/gist_id}","starred_url":"https://api.github.com/users/holiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/holiman/subscriptions","organizations_url":"https://api.github.com/users/holiman/orgs","repos_url":"https://api.github.com/users/holiman/repos","events_url":"https://api.github.com/users/holiman/events{/privacy}","received_events_url":"https://api.github.com/users/holiman/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2016-03-10T20:32:17Z","updated_at":"2016-03-10T20:50:38Z","closed_at":null,"body":"The ecrecover contract suffers from a lot of the same issues that also affected C++/Python issues reported here: http://martin.swende.se/blog/Devcon1-and-contract-security.html .\r\n\r\nAnyway, here's the code\r\n\r\n        public byte[] execute(byte[] data) {\r\n\r\n            byte[] h = new byte[32];\r\n            byte[] v = new byte[32];\r\n            byte[] r = new byte[32];\r\n            byte[] s = new byte[32];\r\n\r\n            DataWord out = null;\r\n\r\n            try {\r\n                System.arraycopy(data, 0, h, 0, 32);\r\n                System.arraycopy(data, 32, v, 0, 32);\r\n                System.arraycopy(data, 64, r, 0, 32);\r\n\r\n                int sLength = data.length < 128 ? data.length - 96 : 32;\r\n                System.arraycopy(data, 96, s, 0, sLength);\r\n\r\n\r\n                ECKey.ECDSASignature signature = ECKey.ECDSASignature.fromComponents(r, s, v[31]);\r\n\r\n                ECKey key = ECKey.signatureToKey(h, signature.toBase64());\r\n                out = new DataWord(key.getAddress());\r\n            } catch (Throwable any) {\r\n            \tany.printStackTrace();\r\n            }\r\n\r\n            if (out == null) out = new DataWord(0);\r\n\r\n            return out.getData();\r\n        }\r\n    }\r\n\r\n\r\nYou can compare with python here: https://github.com/ethereum/pyethereum/blob/develop/ethereum/specials.py. \r\n\r\nProblems:\r\n- `v`, `r` and `s` are not validated (e.g. if v!=27)\r\n- If error occurs, it will return `new DataWord(0)`, instead of empty byte-array. There's a subtle difference, which can cause consensus fork if the return value is set to overwrite a memory location which is non-zero. The emtpy byte array won't overwrite, whereas the zero-filled bytearray will. "},"comment":{"url":"https://api.github.com/repos/ethereum/ethereumj/issues/comments/195041324","html_url":"https://github.com/ethereum/ethereumj/issues/353#issuecomment-195041324","issue_url":"https://api.github.com/repos/ethereum/ethereumj/issues/353","id":195041324,"user":{"login":"holiman","id":142290,"avatar_url":"https://avatars.githubusercontent.com/u/142290?v=3","gravatar_id":"","url":"https://api.github.com/users/holiman","html_url":"https://github.com/holiman","followers_url":"https://api.github.com/users/holiman/followers","following_url":"https://api.github.com/users/holiman/following{/other_user}","gists_url":"https://api.github.com/users/holiman/gists{/gist_id}","starred_url":"https://api.github.com/users/holiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/holiman/subscriptions","organizations_url":"https://api.github.com/users/holiman/orgs","repos_url":"https://api.github.com/users/holiman/repos","events_url":"https://api.github.com/users/holiman/events{/privacy}","received_events_url":"https://api.github.com/users/holiman/received_events","type":"User","site_admin":false},"created_at":"2016-03-10T20:50:38Z","updated_at":"2016-03-10T20:50:38Z","body":"I'm still trying to get the framework running, exploring it a bit. To fix it properly there should be supporting testcases aswell. Sorry, but I think it's better fixed properly by existing developers. "}}