From 417857ec763f4380adf8aa7e72abc3675a5daa7b Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 17:35:59 +0900 Subject: [PATCH 01/10] Fixed typo in error message --- CHANGELOG.md | 6 ++++++ src/operators.ts | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17d38aa..22a4d7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [1.0.8] +This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) +### Fixed +- Fixed typo in error message + ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) @@ -227,6 +232,7 @@ At this version, I've managed to improve test complete time to `79s` -> `2s` by Initial (beta) release. +[1.0.8]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.7...v1.0.8 [1.0.7]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.6...v1.0.7 [1.0.6]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.5...v1.0.6 [1.0.5]: https://github.com/Chia-Mine/clvm-js/compare/v1.0.4...v1.0.5 diff --git a/src/operators.ts b/src/operators.ts index d9d298e..fb19a0b 100644 --- a/src/operators.ts +++ b/src/operators.ts @@ -169,7 +169,7 @@ export type KEYWORDS = keyof typeof KEYWORD_TO_ATOM; export function* args_len(op_name: string, args: SExp){ for(const arg of args.as_iter()){ if(arg.pair){ - throw new EvalError(`${op_name} requires int args"`, arg); + throw new EvalError(`${op_name} requires int args`, arg); } yield (arg.atom as Bytes).length; } From 4797de952d2784d7d9ce22a3beb328822921940a Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 17:47:23 +0900 Subject: [PATCH 02/10] Fixed typo --- src/more_ops.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/more_ops.ts b/src/more_ops.ts index 0e1baa1..245216a 100644 --- a/src/more_ops.ts +++ b/src/more_ops.ts @@ -87,7 +87,7 @@ export function* args_as_int32(op_name: string, args: SExp){ throw new EvalError(`${op_name} requires int32 args`, arg); } else if(arg.atom.length > 4){ - throw new EvalError(`${op_name} requires int32 args (with no leading zeros`, arg); + throw new EvalError(`${op_name} requires int32 args (with no leading zeros)`, arg); } yield arg.as_int(); } From fdcbfdcf9c6299f7d91075d24195680b65e0ff28 Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 17:56:30 +0900 Subject: [PATCH 03/10] Fixed an issue where `op_substr` did not work as expected --- CHANGELOG.md | 1 + src/more_ops.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22a4d7a..eaf6a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) ### Fixed - Fixed typo in error message +- Fixed an issue where `op_substr` did not work as expected. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/more_ops.ts b/src/more_ops.ts index 245216a..2a0173e 100644 --- a/src/more_ops.ts +++ b/src/more_ops.ts @@ -321,7 +321,7 @@ export function op_substr(args: SExp){ throw new EvalError("invalid indices for substr", args); } - const s = s0.subarray(i1, i2); + const s = s0.subarray(i1, i2-i1); const cost = 1; return t(cost, SExp.to(s)); } From ef27d333cc5636db3483a8831b814086908c1c06 Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 18:00:22 +0900 Subject: [PATCH 04/10] Fixed an issue where cost calculation for `op_subtract` was not correct --- CHANGELOG.md | 1 + src/more_ops.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eaf6a32..c60afec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](htt ### Fixed - Fixed typo in error message - Fixed an issue where `op_substr` did not work as expected. +- Fixed an issue where cost calculation for `op_subtract` was not correct. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/more_ops.ts b/src/more_ops.ts index 2a0173e..cd8c6cb 100644 --- a/src/more_ops.ts +++ b/src/more_ops.ts @@ -154,7 +154,7 @@ export function op_subtract(args: SExp){ total += sign * r; sign = BigInt(-1); arg_size += l; - cost += ARITH_COST_PER_BYTE; + cost += ARITH_COST_PER_ARG; } cost += arg_size * ARITH_COST_PER_BYTE; return malloc_cost(cost, SExp.to(total)); From 2e3793df55a6ea852c40df8c918ba253ab8a2185 Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 18:30:58 +0900 Subject: [PATCH 05/10] Fixed `limbs_for_int` return wrong value when argument is `0` --- CHANGELOG.md | 1 + src/casts.ts | 3 +++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c60afec..5369ce5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](htt - Fixed typo in error message - Fixed an issue where `op_substr` did not work as expected. - Fixed an issue where cost calculation for `op_subtract` was not correct. +- Fixed `limbs_for_int` return wrong value when argument is `0`. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/casts.ts b/src/casts.ts index 8c4ad89..a3c4ab5 100644 --- a/src/casts.ts +++ b/src/casts.ts @@ -232,5 +232,8 @@ export function bigint_to_bytes(v: bigint, option?: Partial): By * @param {number} v */ export function limbs_for_int(v: number|bigint): number { + if(v === 0 || v === BigInt(0)){ + return 0; + } return ((v >= 0 ? v : -v).toString(2).length + 7) >> 3; } From 701db46e492ae29cf2689dda95d074daa33d6496 Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 20:45:04 +0900 Subject: [PATCH 06/10] Fixed an issue where `Bytes` comparison returns wrong result in some cases. --- CHANGELOG.md | 1 + src/__type_compatibility__.ts | 44 +++++++++++++++++------ tests/_casts_test.ts | 2 +- tests/_type_compatibility_test.ts | 58 +++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 tests/_type_compatibility_test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5369ce5..ec75ec9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](htt - Fixed an issue where `op_substr` did not work as expected. - Fixed an issue where cost calculation for `op_subtract` was not correct. - Fixed `limbs_for_int` return wrong value when argument is `0`. +- Fixed an issue where `Bytes` comparison returns wrong result in some cases. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/__type_compatibility__.ts b/src/__type_compatibility__.ts index f8aea03..b41f288 100644 --- a/src/__type_compatibility__.ts +++ b/src/__type_compatibility__.ts @@ -4,6 +4,7 @@ import {Word32Array} from "jscrypto/Word32Array"; import {SHA256} from "jscrypto/SHA256"; import {None} from "./__python_types__"; import {G1Element} from "@chiamine/bls-signatures"; +import {bigint_from_bytes} from "./casts"; export function to_hexstr(r: Uint8Array) { return (new Word32Array(r)).toString(); @@ -238,25 +239,46 @@ export class Bytes { * @param other */ public compare(other: Bytes): -1|0|1 { - if(this.length !== other.length){ - return this.length > other.length ? 1 : -1; + if(this.length === 0 && other.length === 0){ + return 0; + } + else if(this.length * other.length === 0){ // Either length of this or other is zero. + return this.length > 0 ? 1 : -1; } const self_raw_byte = this._b; - const dv_self = new DataView(self_raw_byte.buffer, self_raw_byte.byteOffset, self_raw_byte.byteLength); + const self_byteLength = self_raw_byte.byteLength; + const dv_self = new DataView(self_raw_byte.buffer, self_raw_byte.byteOffset, self_byteLength); const other_raw_byte = other.raw(); - const dv_other = new DataView(other_raw_byte.buffer, other_raw_byte.byteOffset, other_raw_byte.byteLength); - - const ui32MaxCount = (this.length / 4) | 0; - for(let i=0;i minByteLength){ // k > minByteLength - 4 ==(optimize)==> minByteLength = minByteLength - 4 + if(k > minByteLength){ + break; + } + const ui32_self = dv_self.getUint32(k); + const ui32_other = dv_other.getUint32(k); if(ui32_self !== ui32_other){ return ui32_self > ui32_other ? 1 : -1; } } - const offset = ui32MaxCount*4; - for(let i=offset;i self_byteLength){ + return -1; + } + else if(k > other_byteLength){ + return 1; + } const ui8_self = dv_self.getUint8(i); const ui8_other = dv_other.getUint8(i); if(ui8_self !== ui8_other){ diff --git a/tests/_casts_test.ts b/tests/_casts_test.ts index 96c7446..b894940 100644 --- a/tests/_casts_test.ts +++ b/tests/_casts_test.ts @@ -184,7 +184,7 @@ describe("bigint_to_bytes", () => { }); test("limbs_for_int", () => { - expect(limbs_for_int(0)).toBe(1); + expect(limbs_for_int(0)).toBe(0); expect(limbs_for_int(1)).toBe(1); expect(limbs_for_int(-255)).toBe(1); expect(limbs_for_int(255)).toBe(1); diff --git a/tests/_type_compatibility_test.ts b/tests/_type_compatibility_test.ts new file mode 100644 index 0000000..e489f81 --- /dev/null +++ b/tests/_type_compatibility_test.ts @@ -0,0 +1,58 @@ +import {Bytes, b, h} from "../src/__type_compatibility__"; + +describe("Bytes", () => { + test("0x00 > b('')", () => { + expect(h("0x00").compare(b(""))).toBe(1); + }); + test("b('') < 0x00", () => { + expect(b("").compare(h("0x00"))).toBe(-1); + }); + test("0x00 < 0x0000", () => { + expect(h("0x00").compare(h("0x0000"))).toBe(-1); + }); + test("0x1000 > 0x10", () => { + expect(h("0x1000").compare(h("0x10"))).toBe(1); + }); + test("0x0010 < 0x10", () => { + expect(h("0x0010").compare(h("0x10"))).toBe(-1); + }); + test("0x1000 < 0x20", () => { + expect(h("0x1000").compare(h("0x20"))).toBe(-1); + }); + test("0x2000 > 0x20", () => { + expect(h("0x2000").compare(h("0x20"))).toBe(1); + }); + test("0x2000 < 0x21", () => { + expect(h("0x2000").compare(h("0x21"))).toBe(-1); + }); + test("0x0011 > 0x0010", () => { + expect(h("0x0011").compare(h("0x0010"))).toBe(1); + }); + test("0x4433221144332211 == 0x4433221144332211", () => { + expect(h("0x4433221144332211").compare(h("0x4433221144332211"))).toBe(0); + }); + test("0x4433221144332211 > 0x4433221144002211", () => { + expect(h("0x4433221144332211").compare(h("0x4433221144002211"))).toBe(1); + }); + test("0x4433221144332211 < 0x4433221144332212", () => { + expect(h("0x4433221144332211").compare(h("0x4433221144332212"))).toBe(-1); + }); + test("0x4433221144332212 > 0x4433221144332211", () => { + expect(h("0x4433221144332212").compare(h("0x4433221144002211"))).toBe(1); + }); + test("0xfedcba9876543210fedcba9876543210fedcba9876543210 == 0xfedcba9876543210fedcba9876543210fedcba9876543210", () => { + expect(h("0xfedcba9876543210fedcba9876543210fedcba9876543210").compare(h("0xfedcba9876543210fedcba9876543210fedcba9876543210"))).toBe(0); + }); + test("0xfedcba9876543210fedcbaAA76543210fedcba9876543210 > 0xfedcba9876543210fedcba9876543210fedcba9876543210", () => { + expect(h("0xfedcba9876543210fedcbaAA76543210fedcba9876543210").compare(h("0xfedcba9876543210fedcba9876543210fedcba9876543210"))).toBe(1); + }); + test("0xfedcba9876543210fedcba9876543210fedcba9876543210 < 0xffdcba987654", () => { + expect(h("0xfedcba9876543210fedcba9876543210fedcba9876543210").compare(h("0xffdcba987654"))).toBe(-1); + }); + test("b('') == b('')", () => { + expect(b("").compare(b(""))).toBe(0); + }); + test("0x414243 == b('ABC')", () => { + expect(h("0x414243").compare(b("ABC"))).toBe(0); + }); +}) From b995e03ebb28c0ee224f1d486f87b4be7b870b09 Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 21:09:55 +0900 Subject: [PATCH 07/10] Fixed an issue where `op_softfork` crashed with argument atom larger than or equal to 53bit --- CHANGELOG.md | 1 + src/more_ops.ts | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec75ec9..8789864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](htt - Fixed an issue where cost calculation for `op_subtract` was not correct. - Fixed `limbs_for_int` return wrong value when argument is `0`. - Fixed an issue where `Bytes` comparison returns wrong result in some cases. +- Fixed an issue where `op_softfork` crashed with argument atom larger than or equal to 53bit. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/more_ops.ts b/src/more_ops.ts index cd8c6cb..f74fa98 100644 --- a/src/more_ops.ts +++ b/src/more_ops.ts @@ -493,7 +493,12 @@ export function op_softfork(args: SExp){ if(!isAtom(a)){ throw new EvalError("softfork requires int args", a); } - const cost = a.as_int(); + const cost_bigint = a.as_bigint(); + if(cost_bigint > BigInt(Number.MAX_SAFE_INTEGER)){ + throw new Error("Cost greater than 2**53-1 is not supported at this time"); + } + + const cost = Number(cost_bigint); if(cost < 1){ throw new EvalError("cost must be > 0", args); } From 29514dbc39c9d15ec7233e642bae8234d227893f Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 21:25:50 +0900 Subject: [PATCH 08/10] Fixed G1Element error message --- CHANGELOG.md | 1 + src/__bls_signatures__/index.ts | 2 +- src/more_ops.ts | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8789864..c4360a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](htt - Fixed `limbs_for_int` return wrong value when argument is `0`. - Fixed an issue where `Bytes` comparison returns wrong result in some cases. - Fixed an issue where `op_softfork` crashed with argument atom larger than or equal to 53bit. +- Fixed G1Element error message. ## [1.0.7] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) diff --git a/src/__bls_signatures__/index.ts b/src/__bls_signatures__/index.ts index d24544c..1f7eeb7 100644 --- a/src/__bls_signatures__/index.ts +++ b/src/__bls_signatures__/index.ts @@ -72,7 +72,7 @@ export function assert_G1Element_valid(bytes: Uint8Array){ const BLSModule = getBLSModule(); const {G1Element} = BLSModule; if(bytes.length !== G1Element.SIZE){ - throw new Error("G1Element: Invalid size"); + throw new Error("Length of bytes object not equal to G1Element::SIZE"); } if((bytes[0] & 0xc0) === 0xc0){ // representing infinity diff --git a/src/more_ops.ts b/src/more_ops.ts index f74fa98..0774bb6 100644 --- a/src/more_ops.ts +++ b/src/more_ops.ts @@ -273,7 +273,8 @@ export function op_point_add(items: SExp){ cost += POINT_ADD_COST_PER_ARG; } catch(e){ - throw new EvalError(`point_add expects blob, got ${_.atom}: ${JSON.stringify(e)}`, items); + const eMsg = e instanceof Error ? e.message : typeof e === "string" ? e : JSON.stringify(e); + throw new EvalError(`point_add expects blob, got ${_}: ${eMsg}`, items); } } return malloc_cost(cost, SExp.to(p)); From e715c654b19d5f7c90fc43419bffeeeb3c24c83b Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 21:29:43 +0900 Subject: [PATCH 09/10] Set versino to 1.0.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1980b6..ba75d23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "clvm", - "version": "1.0.7", + "version": "1.0.8", "author": "ChiaMineJP ", "description": "Javascript implementation of chia lisp", "license": "MIT", From 861eb864614fee3b6255316226d4d6aff1f608cb Mon Sep 17 00:00:00 2001 From: ChiaMineJP Date: Sat, 4 Sep 2021 21:32:40 +0900 Subject: [PATCH 10/10] Fixed CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4360a4..49d502e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [1.0.8] This version is compatible with [`2722c78ddb92f067c5025196f397e4d2955f9053`](https://github.com/Chia-Network/clvm/tree/2722c78ddb92f067c5025196f397e4d2955f9053) of [clvm](https://github.com/Chia-Network/clvm) + ### Fixed - Fixed typo in error message - Fixed an issue where `op_substr` did not work as expected.