#UInt64, UInt32 `fromFields()` shows surprising behaviour.

4 messages · Page 1 of 1 (latest)

vocal fable
#
console.log(UInt32.fromFields( [Field(0).sub(Field(1))] ).toBigint());
// 28948022309329048855892746252171976963363056481941560715954676764349967630336n

where Field(0).sub(Field(1)) gives p-1 which is expected,
But I can also convert it to UInt64 which succeeds and also prints a value outside the range of uint64 !! is the unexpected part

hollow coyote
#

Hello @vocal fable, thank you a lot for finding this issue. I have tried the example you have sent, and I believe it is about the definition of fromFields() method. The same thing happens when you just create a UInt32 from a Field with a value bigger than 2^32. I guess the checks are missing for the fromFields method of UInt32 type. I am tagging @rancid spire for more context. Thank you again!

console.log(UInt32.fromJSON("28948022309329048855892746252171976963363056481941560715954676764349967630336").toBigInt()); // Throws an error as expected: "UInt32: Expected number between 0 and 2^32 - 1"

console.log(UInt32.fromFields( [ Field.fromJSON("28948022309329048855892746252171976963363056481941560715954676764349967630336") ] ).toBigint()); // Works without a problem
rancid spire
#

Yup fromFields is more an internal method. It doesn't check the input and that's intended.
If you want to use it to create UInt32s, call UInt32.check() on the result to prove that it's 32 bits

hollow coyote
#

Thank you a lot for the response @rancid spire! Is this the case for all fromFields methods on custom types, or is it the intended behaviour only for UInt32 and UInt64?