Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2016 00:56
Show Gist options
  • Save anonymous/95848b6afbda4de8b4d0 to your computer and use it in GitHub Desktop.
Save anonymous/95848b6afbda4de8b4d0 to your computer and use it in GitHub Desktop.
Created using soleditor: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://chriseth.github.io/browser-solidity/?gist=
library HashLadder{
function genPubKey(bytes32[2][32] privKey) returns (bytes32[2][32]){
bytes32[2][32] memory pubKey;
for(uint8 i; i< 32; i++){
bytes32 pa = privKey[i][0];
bytes32 pb = privKey[i][1];
for(uint k; k<258; k++){
pa = sha3(pa);
pb = sha3(pb);
}
pubKey[i] = [pa,pb];
}
return pubKey;
}
function sign_chunk(byte chunk, bytes32[2] privKey) constant returns(bytes32[2]){
bytes32[2] memory pubKey;
uint n = uint8(chunk);
bytes32 a = privKey[0];
bytes32 b = privKey[1];
for(uint i; i < n+1; i++){
a = sha3(a);
}
for(uint j; j < 256-n; j++){
b = sha3(b);
}
return([a,b]);
}
function sign(bytes message, bytes32[2][32] privKey) returns(bytes32[2][32]){
bytes32 hash = sha3(message);
bytes32[2][32] memory sig;
bytes32[2][32] memory key;
for(uint8 i; i<32;i++){
sig[i] = sign_chunk(hash[i],privKey[i]);
}
return (sig);
}
function verify_chunk(byte chunk, bytes32[2] pubKey, bytes32[2] signature) constant returns (bool){
uint a_i;
uint b_i;
bytes32 a = signature[0];
bytes32 b = signature[1];
while(b_i <= 258 && b != pubKey[1]){
b = sha3(b);
b_i++;
}
while(a_i <= 258 && a != pubKey[0]){
a = sha3(a);
a_i++;
}
return (uint(chunk) == b_i - 2 && b_i == 259 - a_i);
//return true;
}
function verify(bytes32 msgHash, bytes32[2][32] pubKey, bytes32[2][32] signature) public returns (bool){
for(uint8 i; i<32; i++){
if(!verify_chunk(msgHash[i],pubKey[i],signature[i])) return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment