diff --git a/package-lock.json b/package-lock.json index db90cbb..495140a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fhevmjs", - "version": "0.0.3", + "version": "0.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fhevmjs", - "version": "0.0.3", + "version": "0.0.4", "license": "BSD-3-Clause-Clear", "dependencies": { "crypto-js": "^4.1.1", diff --git a/package.json b/package.json index 9d39840..dc3a1af 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fhevmjs", - "version": "0.0.3", + "version": "0.0.4", "description": "fhEVM SDK for blockchain using TFHE", "main": "lib/node.js", "browser": "lib/web.js", diff --git a/src/sdk/decrypt.test.ts b/src/sdk/decrypt.test.ts index cec332f..7834e13 100644 --- a/src/sdk/decrypt.test.ts +++ b/src/sdk/decrypt.test.ts @@ -19,7 +19,7 @@ describe('decrypt', () => { it('decrypts a Uint8Array value', async () => { const keypair = sodium.crypto_box_keypair(); - const value = 28482; + const value = 10; const ciphertext = sodium.crypto_box_seal(numberToBytes(value), keypair.publicKey); const cleartext = decrypt(keypair, ciphertext); expect(cleartext).toBe(value); diff --git a/src/sdk/index.test.ts b/src/sdk/index.test.ts index 3f1fdbb..682edf8 100644 --- a/src/sdk/index.test.ts +++ b/src/sdk/index.test.ts @@ -123,7 +123,7 @@ describe('token', () => { const kp = instance.getTokenSignature(contractAddress); expect(kp!.publicKey).toBe(publicKey); - const value = 8238290348; + const value = 89290; const ciphertext = sodium.crypto_box_seal(numberToBytes(value), publicKey, 'hex'); const cleartext = instance.decrypt(contractAddress, ciphertext); expect(cleartext).toBe(value); diff --git a/src/utils.ts b/src/utils.ts index f11c6c4..a24bda6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,27 +9,26 @@ export const fromHexString = (hexString: string): Uint8Array => { export const toHexString = (bytes: Uint8Array) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); -export const numberToBytes = (number: number) => { - // you can use constant number of bytes by using 8 or 4 - const len = Math.ceil(Math.log2(number) / 8); - const byteArray = new Uint8Array(len); - - for (let index = 0; index < byteArray.length; index++) { - const byte = number & 0xff; - byteArray[index] = byte; - number = (number - byte) / 256; +export const numberToBytes = (uint32Value: number) => { + const byteArrayLength = Math.ceil(Math.log2(uint32Value + 1) / 8); + const byteArray = new Uint8Array(byteArrayLength); + + for (let i = byteArrayLength - 1; i >= 0; i--) { + byteArray[i] = uint32Value & 0xff; + uint32Value >>= 8; } return byteArray; }; -export const bytesToNumber = (byteArray: Uint8Array) => { - let result = 0; - for (let i = byteArray.length - 1; i >= 0; i--) { - result = result * 256 + byteArray[i]; +export const bytesToNumber = (byteArray: Uint8Array): number => { + let uint32Value = 0; + + for (let i = 0; i < byteArray.length; i++) { + uint32Value |= byteArray[i] << (8 * (byteArray.length - 1 - i)); } - return result; + return uint32Value; }; export const isAddress = function (address: string) {