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

Fix 0.to_bytes in CSL #488

Closed
klntsky opened this issue Jun 3, 2022 · 1 comment
Closed

Fix 0.to_bytes in CSL #488

klntsky opened this issue Jun 3, 2022 · 1 comment
Assignees
Labels
bug Something isn't working critical stage 4 `Contract`-style interface/dsl

Comments

@klntsky
Copy link
Member

klntsky commented Jun 3, 2022

Quite a while ago I discovered an inconsistency between plutus and cardano-serialization-lib:

const lib = require('@ngua/cardano-serialization-lib-nodejs');
const zero = lib.BigInt.from_str("0");
const zeroData = lib.PlutusData.new_integer(zero);
const one = lib.BigInt.from_str("1");
const oneData = lib.PlutusData.new_integer(one);
console.log('0:', zeroData.to_bytes(), '1:', oneData.to_bytes());

Output:

0: Uint8Array [ 194, 65, 0 ] 1: Uint8Array [ 1 ]

And in Plutus:

import PlutusTx
import PlutusTx.IsData
import Codec.Serialise
import Data.ByteString.Lazy as BL
import Data.ByteString as BS
import qualified Data.Text.Encoding as Text
import qualified Data.ByteString.Base16 as Base16
import Data.Text (Text)

toHexDigest :: BS.ByteString -> Text
toHexDigest = Text.decodeUtf8 . Base16.encode

toHex = toHexDigest . BL.toStrict . serialise . toData

test = [ toHex 0, toHex 1 ]
> test
["00","01"]

So serialization-lib treats zero very specifically for some reason.

This is the source to the problem that hashing any datum that contains Integer 0 gives a completely wrong result.

We really can't monkey-patch this in CTL, because to_bytes call recurses over WASM data on CSL side. So we really need to update our CSL fork and ask @ngua to upload a new version to NPM.

https://github.com/ngua/cardano-serialization-lib

The ask:

  • Identify the source of the problem
  • Check that the fix was not upstreamed yet, and if so, just upgrade CSL. Otherwise:
  • Identify all affected types (Check Int, BigInt, BigNum in CSL)
  • Fix the CBOR encoder so that it outputs results consistent with Plutus.
  • Upstream the fix

Actually we just need to update CSL: this has already been fixed: Emurgo/cardano-serialization-lib#332

@klntsky klntsky added bug Something isn't working stage 4 `Contract`-style interface/dsl critical labels Jun 3, 2022
@ngua ngua moved this to Todo in cardano-transaction-lib Jun 3, 2022
@ngua ngua moved this from Todo to In Progress in cardano-transaction-lib Jun 3, 2022
@ngua ngua moved this from In Progress to Review in cardano-transaction-lib Jun 9, 2022
@ngua
Copy link
Contributor

ngua commented Jun 9, 2022

Closed by #493

@ngua ngua closed this as completed Jun 9, 2022
Repository owner moved this from Review to Done in cardano-transaction-lib Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working critical stage 4 `Contract`-style interface/dsl
Projects
Development

No branches or pull requests

3 participants