Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug in utils.leInt2Buff #28

Closed
twister-dev opened this issue Feb 22, 2022 · 1 comment
Closed

bug in utils.leInt2Buff #28

twister-dev opened this issue Feb 22, 2022 · 1 comment

Comments

@twister-dev
Copy link

Description of the Problem

Using leInt2Buff returns inaccurate results depending on the length of the input. I discovered this when constructing a buffer from a small value, eg BigInt(42). When using leInt2Buff(BigInt(42)), the result is Uint8Array(1) [1], whereas the expected result is Uint8Array(1) [42].

My solution in this case is to strictly specify the length passed into leInt2Buff (and to make sure its a multiple of 4). That being said, this issue might go undetected by a consumer of this library, and that could potentially impact the security of a zero knowledge application.

For example, if someone were to write a zk app that uses the pedersenHash of a value that is less than 4 bytes long (up to 2^32). If the primage depends on a Buffer constructed from this function there will be unintentional collisions, because for example leInt2Buff(BigInt(42)) == leInt2Buff(BigInt(216)) == Uint8Array(1) [1] (and there are more repeated patterns up until you exceed 4 bytes, then the pattern changes again).

Steps to Reproduce

Run this script.

const { leInt2Buff } = require('ffjavascript').utils;

const num = BigInt(42);
console.log('num: ', num);
console.log('leInt2Buff len = undefined: ', leInt2Buff(num));
for (let i = 1; i < 33; i++) {
    console.log(`leInt2Buff len = ${i}: `, leInt2Buff(num, i));
}

The truncated results of the above script:

num:  42n
leInt2Buff len = undefined:  Uint8Array(1) [ 1 ]
leInt2Buff len = 1:  Uint8Array(1) [ 1 ]
leInt2Buff len = 2:  Uint8Array(2) [ 0, 1 ]
leInt2Buff len = 3:  Uint8Array(3) [ 0, 1, 1 ]
leInt2Buff len = 4:  Uint8Array(4) [ 42, 0, 0, 0 ]
leInt2Buff len = 5:  Uint8Array(5) [ 42, 0, 0, 0, 1 ]
leInt2Buff len = 6:  Uint8Array(6) [ 42, 0, 0, 0, 0, 1 ]
leInt2Buff len = 7:  Uint8Array(7) [
  42, 0, 0, 0,
   0, 1, 1
]
leInt2Buff len = 8:  Uint8Array(8) [
  42, 0, 0, 0,
   0, 0, 0, 0
]

The pattern of ones at the end of the array continues so on and so forth up until len = 32.

Here's another script, this time comparing 216 vs 42:

const num1 = BigInt(216);
console.log('num1: ', num1);
console.log('leInt2Buff len = undefined: ', leInt2Buff(num1));
console.log('leInt2Buff len = 32: ', leInt2Buff(num1, 32));

const num2 = BigInt(42);
console.log('num2: ', num2);
console.log('leInt2Buff len = undefined: ', leInt2Buff(num2));
console.log('leInt2Buff len = 32: ', leInt2Buff(num2, 32));
num1:  216n
leInt2Buff len = undefined:  Uint8Array(1) [ 1 ]
leInt2Buff len = 32:  Uint8Array(32) [
  216, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0
]
num2:  42n
leInt2Buff len = undefined:  Uint8Array(1) [ 1 ]
leInt2Buff len = 32:  Uint8Array(32) [
  42, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0
]

I ran this script in a local project with the following package.json:

{
  "dependencies": {
    "@ethersproject/bignumber": "^5.5.0",
    "@ethersproject/wallet": "^5.5.0",
    "@types/node": "^17.0.19",
    "circomlibjs": "^0.1.1",
    "ffjavascript": "^0.2.48",
    "typescript": "^4.5.5"
  }
}
NOOMA-42 pushed a commit to NOOMA-42/ffjavascript that referenced this issue Sep 24, 2022
jbaylina added a commit that referenced this issue Oct 25, 2022
fix bug in utils.leInt2Buff #28
@phated
Copy link
Contributor

phated commented Nov 4, 2022

I believe this was solved by #72 - please let us know if you still see a problem.

@phated phated closed this as completed Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants