Skip to content

Commit

Permalink
Merge pull request #12 from zama-ai/fix/fix-array-transform
Browse files Browse the repository at this point in the history
Fix/fix array transform
  • Loading branch information
immortal-tofu authored Jul 2, 2023
2 parents 1c77f34 + 1ebf0ce commit 40df510
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/decrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 13 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 40df510

Please sign in to comment.