-
Notifications
You must be signed in to change notification settings - Fork 1
Nem cipher encode and decode. #59
base: main
Are you sure you want to change the base?
Conversation
b8b9f46
to
b88bd99
Compare
b88bd99
to
a1e7f5e
Compare
src/core/nem/NemCrypto.ts
Outdated
* | ||
* @returns Keccak-256 hash | ||
*/ | ||
private static key_derive(shared: Uint8Array, salt: Uint8Array, privateKey: Key, publicKey: Key): number[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we use camel rather than snake case elsewhere?
Also, can the shared
be an internal var as only zero bytes (new Uint8Array(32)) get passed in ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y, don't see reason not to.
src/core/nem/external/nacl-fast.js
Outdated
@@ -651,6 +651,22 @@ | |||
return 0; | |||
} | |||
|
|||
function crypto_shared_key_hash(shared, pk, sk, hash) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shared_key => shared_secret? or perhaps just call it get_scalar_mult_result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this from the old SDK.
All custom methods added crypto_
as a prefix.
maybe call crypto_scalar_mult_result
?
src/core/nem/external/nacl-fast.js
Outdated
@@ -811,6 +827,55 @@ | |||
return 0; | |||
} | |||
|
|||
function unpack(r, p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps export unpackneg
from js script rather than duplicating it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, make sense 👍🏼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a double-check on the code, compare with unpackneg
vs unpack
most of the code is the same but here is the different part.
in unpackneg
if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
in unpack
if (par25519(r[0]) !== (p[31]>>7)) Z(r[0], gf0, r[0]);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah dang, right unpackneg() = unpack AND negate, technically we could extract and/or parametrize this, but not sure if's good idea. cc @Jaguar0625
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, i'd recommend refactoring unpack and unpackneg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gimre-xymcity or could we negate after calling unpackneg?
src/core/nem/NemCrypto.ts
Outdated
const message = payload.slice(48); | ||
|
||
const shared = new Uint8Array(32); | ||
const key = this.key_derive(shared, salt, privateKey, publicKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above comments
src/core/nem/NemCrypto.ts
Outdated
* | ||
* @returns Keccak-256 hash | ||
*/ | ||
private static key_derive(shared: Uint8Array, salt: Uint8Array, privateKey: Key, publicKey: Key): number[] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
y, don't see reason not to.
src/core/nem/external/nacl-fast.js
Outdated
@@ -811,6 +827,55 @@ | |||
return 0; | |||
} | |||
|
|||
function unpack(r, p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup
test/core/nem/NemCrypto.spec.ts
Outdated
expect(Converter.uint8ToUtf8(decrypted)).equal(message); | ||
}); | ||
|
||
it('Encoding throw error if message exceed 976 btyes', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a tests checking that 976 is actually passing ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'bytes' (multiple)
src/core/nem/external/nacl-fast.js
Outdated
@@ -811,6 +827,55 @@ | |||
return 0; | |||
} | |||
|
|||
function unpack(r, p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea, i'd recommend refactoring unpack and unpackneg
src/core/nem/external/nacl-fast.js
Outdated
@@ -811,6 +827,55 @@ | |||
return 0; | |||
} | |||
|
|||
function unpack(r, p) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gimre-xymcity or could we negate after calling unpackneg?
src/core/nem/NemCrypto.ts
Outdated
const salt = payload.slice(0, 32); | ||
// 16 byte for iv | ||
const iv = payload.slice(32, 48); | ||
// 32 byte for payload |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment is wrong
test/BasicVectorTest.template.ts
Outdated
const encoded = salt | ||
? NemCrypto.encode(privateKey, otherPublicKey, clearText, iv, salt) | ||
: encode(privateKey, otherPublicKey, clearText, iv); | ||
|
||
const decoded = salt | ||
? NemCrypto.decode(privateKey, otherPublicKey, Converter.hexToUint8(item.salt + item.iv + item.cipherText)) | ||
: decode(privateKey, otherPublicKey, encoded); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is all this conditional based on salt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this vector test is shared between Symbol and Nem.
salt
is only used in nem
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all the conditionals look awkward to me, especially for tests, so i'd prefer two functions. but ask @gimre-xymcity his opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with what @Jaguar0625 has written, would rather split in 2
test/core/nem/NemCrypto.spec.ts
Outdated
expect(Converter.uint8ToUtf8(decrypted)).equal(message); | ||
}); | ||
|
||
it('Encoding throw error if message exceed 976 btyes', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'bytes' (multiple)
4c79e29
to
102141d
Compare
This reverts commit 102141d.
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please revert "isNegate" as discussed on slack
Added