ora-io / zkgraph

Template for zkGraph.
https://docs.hyperoracle.io/zkgraph
19 stars 8 forks source link

BigInt loss half-byte in fromBytesBigEndian & fromBytes #48

Closed nom4dv3 closed 1 year ago

nom4dv3 commented 1 year ago

Which branch bignum

What behavior did you observed I have a Bytes with value = 0x00000000000000000000000000000000000000000000000000001925d7b1b0d9 called BigInt.fromBytesBigEndian to convert to BigInt and print it out, got 192d7b1b0d9, missing a '5'.

What is the expected behavior should give me 1925d7b1b0d9

How to reproduce use the default zkgraph (src/), with the following modification:

mapping.ts:

//@ts-ignore
import { require } from "../lib/common/zkwasm";
import { Bytes, Event, BigInt } from "../lib/common/type";

export function handleEvents(events: Event[]): Bytes {
  const source = changetype<Bytes>(events[0].data);
  let reserve0 = source.slice(0, 32);
  let reserve1 = source.slice(32, 64);
  console.log(reserve0.toHexString())
  let r0b = BigInt.fromBytesBigEndian(reserve0);
  console.log(r0b.toHexString())
  let r0 = BigInt.fromString(reserve0.toHexString(), 16);
  console.log(r0.toHexString())
  let r1 = BigInt.fromString(reserve1.toHexString(), 16);
  let bi_price = r1.div(r0)

  let payload = Bytes.fromHexString(bi_price.toString(16)).paddingTo(32, true, 0)
  return payload;
}

$ npm run compile-local && npm run exec-local 0x10d1125

output:

[*] 64 receipts fetched from block 17633573
[*] 1 event matched
        Tx[0]Event[0] |--addr : b4e16d0168e52d35cacd2c6185b44281ec28c9dc 
        Tx[0]Event[0] |--arg#0: 1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1 
        Tx[0]Event[0] |--data : 00000000000000000000000000000000000000000000000000001925d7b1b0d900000000000000000000000000000000000000000000030a5b512f61adb7dc37 

0x00000000000000000000000000000000000000000000000000001925d7b1b0d9
192d7b1b0d9
1925d7b1b0d9
[+] ZKGRAPH STATE OUTPUT: 000000000000000000000000000000000000000000000000000000001ef3858e

Other information you'd like to share It seems BigInt.fromBytes has the same issue.

fewwwww commented 1 year ago

Fixed in #50