#Keccak is not working?

1 messages · Page 1 of 1 (latest)

stone oak
#

Hi everyone, I'm trying to generate an Ethereum private and public key using this code (hardcoded strings are for testing):

const pkey = "695695e957585c76623d58dbfdd43f67df98f57fdf83419c49eb18a817f10d84d3d6bb69cc711422c0bd719d43db3542c8a1b7af42d14a1bd29038e53f81aca4"; 
    const skey = "d94e7a6c775d38e8682abe53b97e84b954ca02dc66f048e551340202f9974d33";
    var h = Keccak_256.init(.{});
    var out: [32]u8 = undefined;
    h.update("695695e957585c76623d58dbfdd43f67df98f57fdf83419c49eb18a817f10d84d3d6bb69cc711422c0bd719d43db3542c8a1b7af42d14a1bd29038e53f81aca4");
    h.final(&out);
    std.log.info("{s}", .{skey});
    std.log.info("{s}", .{pkey});
    std.log.info("{s}", .{fmt.fmtSliceHexLower(&out)});

and the output that I got is this:

info: d94e7a6c775d38e8682abe53b97e84b954ca02dc66f048e551340202f9974d33
info: 695695e957585c76623d58dbfdd43f67df98f57fdf83419c49eb18a817f10d84d3d6bb69cc711422c0bd719d43db3542c8a1b7af42d14a1bd29038e53f81aca4
info: e92d53f69a143487a5fafd61e2bfb56fea85fddf7c941dc72819b1e77c4977db

but the hashed public key is wrong, should be "6e4e5948ac7c1c1387ab549d07211b6ab8a77c67ced9d1f0056050c72eb853dc", where is the error? Am I missing something?

signal merlin
#

are you supposed to be hashing the hexadecimal text representation of the key or the actual bytes

stone oak
#

I'm not sure, at the moment I'm passing the string, but I didn't try to use the actual bytes, let me check

#

I changed my code with:

const kp = try ecdsa.KeyPair.create(null);
const pkey = kp.public_key.toUncompressedSec1();
const skey = kp.secret_key.toBytes();
var h = Keccak_256.init(.{});
var out: [32]u8 = undefined;
h.update(&pkey);
h.final(&out);
std.log.info("{s}", .{fmt.fmtSliceHexLower(&skey)});
std.log.info("{s}", .{fmt.fmtSliceHexLower(&pkey)});
std.log.info("{s}", .{fmt.fmtSliceHexLower(&out)});

I am supposedly passing the actual bytes and still doesn't work, these are the results of the test:

info: fffcd0fb9e134c7ed647b38c2fae25da813af8ec3cbc445694a701445c11c1f1
info: 04d7ad95184c452f76f8164b5c2f975b64d1a1956ec3690da8a817bacd74c41ad9a47daaa8bc76fee88fdeded8e45247ba8fa57a6502a8113ecb2d0180beb21bf7
info: 821d0f515fea3f8bcbd23551a4f3a00542a7eee14b8804f177a7a57fc09112d1

the hashed key should be 39a93ef2c594aea23fb86f1428059351a926b71c02f2b5686538d373a6f01431

signal merlin
#

what is Keccak_256 defined as?

stone oak
#

these are the declarations before the main

const std = @import("std");
const fmt = std.fmt;
const crypto = std.crypto;
const Keccak_256 = crypto.hash.sha3.Keccak_256;

const ecdsa = crypto.sign.ecdsa.EcdsaSecp256k1Sha256oSha256;
stone oak
#

So what could be the error?