Skip to content

Commit

Permalink
Merge pull request #15 from ChainSafe/cayman/update-eip2333
Browse files Browse the repository at this point in the history
Update hkdfModR to most recent eip-2333
  • Loading branch information
wemeetagain authored Sep 25, 2020
2 parents 3cacb18 + 4af50f7 commit 42fd6a3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/key-derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ function parentSKToLamportPK(parentSK: Buffer, index: number): Buffer {
}

function hkdfModR(ikm: Buffer, keyInfo: Buffer = Buffer.alloc(0)): Buffer {
const prk = HKDF.extract(
SHA256,
Buffer.concat([ikm, Buffer.alloc(1)]),
Buffer.from("BLS-SIG-KEYGEN-SALT-", "ascii")
);
const okm = HKDF.expand(SHA256, prk, Buffer.concat([keyInfo, Buffer.from([0, 48])]), 48);
const okmBN = new BN(okm, "hex", "be");
const r = new BN("52435875175126190479447740508185965837690552500527637822603658699938581184513");
return Buffer.from(okmBN.mod(r).toArray("be", 32));
let salt = Buffer.from("BLS-SIG-KEYGEN-SALT-", "ascii");
let sk = new BN(0);
while (sk.eqn(0)) {
salt = SHA256.digest(salt);
const prk = HKDF.extract(
SHA256,
Buffer.concat([ikm, Buffer.alloc(1)]),
salt
);
const okm = HKDF.expand(SHA256, prk, Buffer.concat([keyInfo, Buffer.from([0, 48])]), 48);
const okmBN = new BN(okm, "hex", "be");
const r = new BN("52435875175126190479447740508185965837690552500527637822603658699938581184513");
sk = okmBN.mod(r);
}
return Buffer.from(sk.toArray("be", 32));
}

export function deriveChildSK(parentSK: Buffer, index: number): Buffer {
Expand Down
16 changes: 8 additions & 8 deletions test/vectors/test-vectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
"kdf_tests": [
{
"seed": "0xc55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04",
"master_SK": "5399117110774477986698372024995405256382522670366369834617409486544348441851",
"master_SK": "6083874454709270928345386274498605044986640685124978867557563392430687146096",
"child_index": 0,
"child_SK": "11812940737387919040225825939013910852517748782307378293770044673328955938106"
"child_SK": "20397789859736650942317412262472558107875392172444076792671091975210932703118"
},
{
"seed": "0x3141592653589793238462643383279502884197169399375105820974944592",
"master_SK": "36167147331491996618072159372207345412841461318189449162487002442599770291484",
"master_SK": "29757020647961307431480504535336562678282505419141012933316116377660817309383",
"child_index": 3141592653,
"child_SK": "41787458189896526028601807066547832426569899195138584349427756863968330588237"
"child_SK": "25457201688850691947727629385191704516744796114925897962676248250929345014287"
},
{
"seed": "0x0099FF991111002299DD7744EE3355BBDD8844115566CC55663355668888CC00",
"master_SK": "13904094584487173309420026178174172335998687531503061311232927109397516192843",
"master_SK": "27580842291869792442942448775674722299803720648445448686099262467207037398656",
"child_index": 4294967295,
"child_SK": "12482522899285304316694838079579801944734479969002030150864436005368716366140"
"child_SK": "29358610794459428860402234341874281240803786294062035874021252734817515685787"
},
{
"seed": "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
"master_SK": "44010626067374404458092393860968061149521094673473131545188652121635313364506",
"master_SK": "19022158461524446591288038168518313374041767046816487870552872741050760015818",
"child_index": 42,
"child_SK": "4011524214304750350566588165922015929937602165683407445189263506512578573606"
"child_SK": "31372231650479070279774297061823572166496564838472787488249775572789064611981"
}
]
}

0 comments on commit 42fd6a3

Please sign in to comment.