From bf2611a1be30aa9f0bd9774e194cc0e176f6cbd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:09:53 +0900 Subject: [PATCH 01/12] Updated `vite.config.js` so that it matches the `tsconfig.json` --- playground/vite.config.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/playground/vite.config.js b/playground/vite.config.js index 4025d882..5fcc624e 100644 --- a/playground/vite.config.js +++ b/playground/vite.config.js @@ -10,4 +10,7 @@ export default defineConfig({ allow: [ '..' ] } }, + build: { + target: "es2022" + }, }) From 8eb041ce0323df648adefeb1474fb6cf02ab6156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Tue, 28 Nov 2023 12:12:16 +0900 Subject: [PATCH 02/12] Implemented unbiased variant of `Math:rnd` and `Math:gen_rng_unbiased` --- src/interpreter/lib/std.ts | 28 ++++++++++++++++++++++-- src/interpreter/util.ts | 44 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index 40c320e3..3801db0b 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -2,7 +2,7 @@ import { v4 as uuid } from 'uuid'; import seedrandom from 'seedrandom'; import { NUM, STR, FN_NATIVE, FALSE, TRUE, ARR, NULL, BOOL, OBJ, ERROR } from '../value.js'; -import { assertNumber, assertString, assertBoolean, valToJs, jsToVal, assertFunction, assertObject, eq, expectAny, assertArray, reprValue } from '../util.js'; +import { assertNumber, assertString, assertBoolean, valToJs, jsToVal, assertFunction, assertObject, eq, expectAny, assertArray, reprValue, unbiasedRandomIntegerInRange, cryptoGen64, signedNumber32ToBigUint32 } from '../util.js'; import { AiScriptRuntimeError } from '../../error.js'; import type { Value } from '../value.js'; @@ -412,12 +412,19 @@ export const std: Record = { return NUM(Math.random()); }), + 'Math:rnd_unbiased': FN_NATIVE(([min, max]) => { + assertNumber(min); + assertNumber(max); + const array = new BigUint64Array(1); + const result = unbiasedRandomIntegerInRange(min.value, max.value, array, cryptoGen64); + return typeof result === 'number' ? NUM(result) : NULL; + }), + 'Math:gen_rng': FN_NATIVE(([seed]) => { expectAny(seed); if (seed.type !== 'num' && seed.type !== 'str') return NULL; const rng = seedrandom(seed.value.toString()); - return FN_NATIVE(([min, max]) => { if (min && min.type === 'num' && max && max.type === 'num') { return NUM(Math.floor(rng() * (Math.floor(max.value) - Math.ceil(min.value) + 1) + Math.ceil(min.value))); @@ -425,6 +432,23 @@ export const std: Record = { return NUM(rng()); }); }), + + 'Math:gen_rng_unbiased': FN_NATIVE(([seed]) => { + expectAny(seed); + if (seed.type !== 'num' && seed.type !== 'str') return NULL; + + const rng = seedrandom(seed.value.toString()); + return FN_NATIVE(([min, max]) => { + assertNumber(min); + assertNumber(max); + const result = unbiasedRandomIntegerInRange(min.value, max.value, rng, (rng) => { + const upper = signedNumber32ToBigUint32(rng.int32()); + const lower = signedNumber32ToBigUint32(rng.int32()); + return BigInt.asUintN(64, (upper << 32n) | lower); + }); + return typeof result === 'number' ? NUM(result) : NULL; + }); + }), //#endregion //#region Num diff --git a/src/interpreter/util.ts b/src/interpreter/util.ts index 1d60c0f3..2ede1660 100644 --- a/src/interpreter/util.ts +++ b/src/interpreter/util.ts @@ -193,3 +193,47 @@ export function reprValue(value: Value, literalLike = false, processedObjects = return '?'; } + +function clz64(num: number): number { + const q = num / (2 ** 32); + const r = num % (2 ** 32); + const upper = Math.clz32(q); + const lower = Math.clz32(r) + 32; + return upper < 32 ? upper : lower; +} + +export function cryptoGen64(array: BigUint64Array): bigint | null { + if (array.length < 1) return null; + const generated = crypto.getRandomValues(array)[0]; + if (!generated) { + return null; + } + return generated; +} + +export function signedNumber32ToBigUint32(num: number) : bigint { + return BigInt((num & 0x7fffffff) - (num & 0x80000000)); // bitwise operators always treats numbers as 32bit signed integers, but arithmetic operators don't. +} + +export function unbiasedRandomIntegerInRange(min: number, max: number, generator: G, gen64: (generator: G) => bigint | null): number | null { + const ceilMin = Math.ceil(min); + const floorMax = Math.floor(max); + const signedScale = floorMax - ceilMin; + if (signedScale === 0) return 0; + const scale = Math.abs(signedScale); + const scaleSign = Math.sign(signedScale); + if (!Number.isSafeInteger(scale) || !Number.isSafeInteger(ceilMin) || !Number.isSafeInteger(floorMax)) { + return null; + } + const bigScale = BigInt(scale); + const shift = BigInt(clz64(scale)); // scale is already proven to be a safe integer + let result: bigint; + do { + const generated = gen64(generator); + if (!generated) { + return null; + } + result = generated >> shift; + } while (result > bigScale); + return Number(result) * scaleSign + ceilMin; +} From 5ae8288f47e0b973d7fb41b913a73f2b9fee0eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 17:09:31 +0900 Subject: [PATCH 03/12] Updated `aiscript.api.md` --- etc/aiscript.api.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index c430eb08..0593f82f 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -271,6 +271,9 @@ type Continue_2 = NodeBase_2 & { type: 'continue'; }; +// @public (undocumented) +function cryptoGen64(array: BigUint64Array): bigint | null; + declare namespace Cst { export { isStatement_2 as isStatement, @@ -858,6 +861,9 @@ export class Scope { }; } +// @public (undocumented) +function signedNumber32ToBigUint32(num: number): bigint; + // @public (undocumented) type Statement = Definition | Return | Each | For | Loop | Break | Continue | Assign | AddAssign | SubAssign; @@ -918,6 +924,9 @@ type TypeSource = NamedTypeSource | FnTypeSource; // @public (undocumented) type TypeSource_2 = NamedTypeSource_2 | FnTypeSource_2; +// @public (undocumented) +function unbiasedRandomIntegerInRange(min: number, max: number, generator: G, gen64: (generator: G) => bigint | null): number | null; + // @public (undocumented) const unWrapRet: (v: Value) => Value; @@ -941,7 +950,10 @@ declare namespace utils { valToJs, jsToVal, getLangVersion, - reprValue + reprValue, + cryptoGen64, + signedNumber32ToBigUint32, + unbiasedRandomIntegerInRange } } export { utils } From 94c33c259c8866065f20714e228bf821407b78c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 18:58:41 +0900 Subject: [PATCH 04/12] * Refactored `Math:rnd` to use crypto * Refactored `Math:rnd_unbiased` to use crypto * Refactored `Math:gen_rng_unbiased` to use `SeedRandomWrapper` --- src/interpreter/lib/std.ts | 22 ++++---- src/interpreter/util.ts | 44 ---------------- src/utils/random/CryptoGen.ts | 29 +++++++++++ src/utils/random/randomBase.ts | 92 ++++++++++++++++++++++++++++++++++ src/utils/random/seedrandom.ts | 76 ++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+), 56 deletions(-) create mode 100644 src/utils/random/CryptoGen.ts create mode 100644 src/utils/random/randomBase.ts create mode 100644 src/utils/random/seedrandom.ts diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index 3801db0b..b12e0df8 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -2,8 +2,10 @@ import { v4 as uuid } from 'uuid'; import seedrandom from 'seedrandom'; import { NUM, STR, FN_NATIVE, FALSE, TRUE, ARR, NULL, BOOL, OBJ, ERROR } from '../value.js'; -import { assertNumber, assertString, assertBoolean, valToJs, jsToVal, assertFunction, assertObject, eq, expectAny, assertArray, reprValue, unbiasedRandomIntegerInRange, cryptoGen64, signedNumber32ToBigUint32 } from '../util.js'; +import { assertNumber, assertString, assertBoolean, valToJs, jsToVal, assertFunction, assertObject, eq, expectAny, assertArray, reprValue } from '../util.js'; import { AiScriptRuntimeError } from '../../error.js'; +import { CryptoGen } from '../../utils/random/CryptoGen.js'; +import { SeedRandomWrapper } from '../../utils/random/seedrandom.js'; import type { Value } from '../value.js'; export const std: Record = { @@ -407,17 +409,17 @@ export const std: Record = { 'Math:rnd': FN_NATIVE(([min, max]) => { if (min && min.type === 'num' && max && max.type === 'num') { - return NUM(Math.floor(Math.random() * (Math.floor(max.value) - Math.ceil(min.value) + 1) + Math.ceil(min.value))); + const res = CryptoGen.instance.generateRandomIntegerInRange(min.value, max.value); + return res === null ? NULL : NUM(res); } - return NUM(Math.random()); + return NUM(CryptoGen.instance.generateNumber0To1()); }), 'Math:rnd_unbiased': FN_NATIVE(([min, max]) => { assertNumber(min); assertNumber(max); - const array = new BigUint64Array(1); - const result = unbiasedRandomIntegerInRange(min.value, max.value, array, cryptoGen64); - return typeof result === 'number' ? NUM(result) : NULL; + const res = CryptoGen.instance.generateRandomIntegerInRange(min.value, max.value); + return res === null ? NULL : NUM(res); }), 'Math:gen_rng': FN_NATIVE(([seed]) => { @@ -437,15 +439,11 @@ export const std: Record = { expectAny(seed); if (seed.type !== 'num' && seed.type !== 'str') return NULL; - const rng = seedrandom(seed.value.toString()); + const rng = new SeedRandomWrapper(seed.value); return FN_NATIVE(([min, max]) => { assertNumber(min); assertNumber(max); - const result = unbiasedRandomIntegerInRange(min.value, max.value, rng, (rng) => { - const upper = signedNumber32ToBigUint32(rng.int32()); - const lower = signedNumber32ToBigUint32(rng.int32()); - return BigInt.asUintN(64, (upper << 32n) | lower); - }); + const result = rng.generateRandomIntegerInRange(min.value, max.value); return typeof result === 'number' ? NUM(result) : NULL; }); }), diff --git a/src/interpreter/util.ts b/src/interpreter/util.ts index 2ede1660..1d60c0f3 100644 --- a/src/interpreter/util.ts +++ b/src/interpreter/util.ts @@ -193,47 +193,3 @@ export function reprValue(value: Value, literalLike = false, processedObjects = return '?'; } - -function clz64(num: number): number { - const q = num / (2 ** 32); - const r = num % (2 ** 32); - const upper = Math.clz32(q); - const lower = Math.clz32(r) + 32; - return upper < 32 ? upper : lower; -} - -export function cryptoGen64(array: BigUint64Array): bigint | null { - if (array.length < 1) return null; - const generated = crypto.getRandomValues(array)[0]; - if (!generated) { - return null; - } - return generated; -} - -export function signedNumber32ToBigUint32(num: number) : bigint { - return BigInt((num & 0x7fffffff) - (num & 0x80000000)); // bitwise operators always treats numbers as 32bit signed integers, but arithmetic operators don't. -} - -export function unbiasedRandomIntegerInRange(min: number, max: number, generator: G, gen64: (generator: G) => bigint | null): number | null { - const ceilMin = Math.ceil(min); - const floorMax = Math.floor(max); - const signedScale = floorMax - ceilMin; - if (signedScale === 0) return 0; - const scale = Math.abs(signedScale); - const scaleSign = Math.sign(signedScale); - if (!Number.isSafeInteger(scale) || !Number.isSafeInteger(ceilMin) || !Number.isSafeInteger(floorMax)) { - return null; - } - const bigScale = BigInt(scale); - const shift = BigInt(clz64(scale)); // scale is already proven to be a safe integer - let result: bigint; - do { - const generated = gen64(generator); - if (!generated) { - return null; - } - result = generated >> shift; - } while (result > bigScale); - return Number(result) * scaleSign + ceilMin; -} diff --git a/src/utils/random/CryptoGen.ts b/src/utils/random/CryptoGen.ts new file mode 100644 index 00000000..c167c82f --- /dev/null +++ b/src/utils/random/CryptoGen.ts @@ -0,0 +1,29 @@ +import { RandomBase, readBigUintLittleEndian } from './randomBase.js'; + +export class CryptoGen extends RandomBase { + private static _instance: CryptoGen = new CryptoGen(); + public static get instance() : CryptoGen { + return CryptoGen._instance; + } + + private constructor() { + super(); + } + + protected generateBigUintByBytes(bytes: number): bigint { + let u8a = new Uint8Array(Math.ceil(bytes / 8) * 8); + if (u8a.length < 1 || !Number.isSafeInteger(bytes)) return 0n; + u8a = this.generateBytes(u8a.subarray(0, bytes)); + return readBigUintLittleEndian(u8a.buffer) ?? 0n; + } + + public generateBigUintByBits(bits: number): bigint { + if (bits < 1 || !Number.isSafeInteger(bits)) return 0n; + const bytes = Math.ceil(bits / 8); + const wastedBits = BigInt(bytes * 8 - bits); + return this.generateBigUintByBytes(bytes) >> wastedBits; + } + public generateBytes(array: Uint8Array): Uint8Array { + return crypto.getRandomValues(array); + } +} diff --git a/src/utils/random/randomBase.ts b/src/utils/random/randomBase.ts new file mode 100644 index 00000000..3123fd07 --- /dev/null +++ b/src/utils/random/randomBase.ts @@ -0,0 +1,92 @@ +export const safeIntegerBits = Math.ceil(Math.log2(Number.MAX_SAFE_INTEGER)); +export const bigSafeIntegerBits = BigInt(safeIntegerBits); +export const bigMaxSafeIntegerExclusive = 1n << bigSafeIntegerBits; +export const fractionBits = safeIntegerBits - 1; +export const bigFractionBits = BigInt(fractionBits); + +export abstract class RandomBase { + protected abstract generateBigUintByBytes(bytes: number): bigint; + public abstract generateBigUintByBits(bits: number): bigint; + public abstract generateBytes(array: Uint8Array): Uint8Array; + + public generateNumber0To1(): number { + let res = this.generateBigUintByBits(safeIntegerBits); + let exponent = 1022; + let remainingFractionBits = safeIntegerBits - bitsToRepresent(res); + while (remainingFractionBits > 0 && exponent >= safeIntegerBits) { + exponent -= remainingFractionBits; + res <<= BigInt(remainingFractionBits); + res |= this.generateBigUintByBits(remainingFractionBits); + remainingFractionBits = safeIntegerBits - bitsToRepresent(res); + } + if (remainingFractionBits > 0) { + const shift = Math.min(exponent - 1, remainingFractionBits); + res <<= BigInt(shift); + res |= this.generateBigUintByBits(shift); + exponent = Math.max(exponent - shift, 0); + } + return (Number(res) * 0.5 ** safeIntegerBits) * (0.5 ** (1022 - exponent)); + } + + public generateUniform(maxInclusive: bigint): bigint { + if (maxInclusive < 1) return 0n; + const log2 = maxInclusive.toString(2).length; + const bytes = Math.ceil(log2 / 8); + const wastedBits = BigInt(bytes * 8 - log2); + let result: bigint; + do { + result = this.generateBigUintByBytes(bytes) >> wastedBits; + } while (result > maxInclusive); + return result; + } + + public generateRandomIntegerInRange(min: number, max: number): number | null { + const ceilMin = Math.ceil(min); + const floorMax = Math.floor(max); + const signedScale = floorMax - ceilMin; + if (signedScale === 0) return ceilMin; + const scale = Math.abs(signedScale); + const scaleSign = Math.sign(signedScale); + if (!Number.isSafeInteger(scale) || !Number.isSafeInteger(ceilMin) || !Number.isSafeInteger(floorMax)) { + return null; + } + const bigScale = BigInt(scale); + return Number(this.generateUniform(bigScale)) * scaleSign + ceilMin; + } +} + +export function bitsToRepresent(num: bigint): number { + if (num === 0n) return 0; + return num.toString(2).length; +} + +function readSmallBigUintLittleEndian(buffer: ArrayBufferLike): bigint | null { + if (buffer.byteLength === 0) return null; + if (buffer.byteLength < 8) { + const array = new Uint8Array(8); + array.set(new Uint8Array(buffer)); + return new DataView(array.buffer).getBigUint64(0, true); + } + return new DataView(buffer).getBigUint64(0, true); +} + +export function readBigUintLittleEndian(buffer: ArrayBufferLike): bigint | null { + if (buffer.byteLength === 0) return null; + if (buffer.byteLength <= 8) { + return readSmallBigUintLittleEndian(buffer); + } + const dataView = new DataView(buffer); + let pos = 0n; + let res = 0n; + let index = 0; + for (; index < dataView.byteLength - 7; index += 8, pos += 64n) { + const element = dataView.getBigUint64(index, true); + res |= element << pos; + } + if (index < dataView.byteLength) { + const array = new Uint8Array(8); + array.set(new Uint8Array(buffer, index)); + res |= new DataView(array.buffer).getBigUint64(0, true) << pos; + } + return res; +} diff --git a/src/utils/random/seedrandom.ts b/src/utils/random/seedrandom.ts new file mode 100644 index 00000000..5c6b3d5f --- /dev/null +++ b/src/utils/random/seedrandom.ts @@ -0,0 +1,76 @@ +import seedrandom from 'seedrandom'; +import { RandomBase, readBigUintLittleEndian } from './randomBase.js'; + +const seedRandomBlockSize = Int32Array.BYTES_PER_ELEMENT; + +export class SeedRandomWrapper extends RandomBase { + private rng: seedrandom.PRNG; + private buffer: Uint8Array; + private filledBuffer: Uint8Array; + constructor(seed: string | number) { + super(); + this.rng = seedrandom(seed.toString()); + this.buffer = new Uint8Array(seedRandomBlockSize); + this.filledBuffer = new Uint8Array(0); + } + private fillBuffer(): void { + this.buffer.fill(0); + this.buffer = this.fillBufferDirect(this.buffer); + this.filledBuffer = this.buffer; + } + private fillBufferDirect(buffer: Uint8Array): Uint8Array { + if ((buffer.length % seedRandomBlockSize) !== 0) throw new Error(`SeedRandomWrapper.fillBufferDirect should always be called with the buffer with the length a multiple-of-${seedRandomBlockSize}!`); + const length = buffer.length / seedRandomBlockSize; + const dataView = new DataView(buffer.buffer); + let byteOffset = 0; + for (let index = 0; index < length; index++, byteOffset += seedRandomBlockSize) { + dataView.setInt32(byteOffset, this.rng.int32(), false); + } + return buffer; + } + protected generateBigUintByBytes(bytes: number): bigint { + let u8a = new Uint8Array(Math.ceil(bytes / 8) * 8); + if (u8a.length < 1 || !Number.isSafeInteger(bytes)) return 0n; + u8a = this.generateBytes(u8a.subarray(0, bytes)); + return readBigUintLittleEndian(u8a.buffer) ?? 0n; + } + + public generateBigUintByBits(bits: number): bigint { + if (bits < 1 || !Number.isSafeInteger(bits)) return 0n; + const bytes = Math.ceil(bits / 8); + const wastedBits = BigInt(bytes * 8 - bits); + return this.generateBigUintByBytes(bytes) >> wastedBits; + } + + public generateBytes(array: Uint8Array): Uint8Array { + if (array.length < 1) return array; + array.fill(0); + let dst = array; + if (dst.length <= this.filledBuffer.length) { + dst.set(this.filledBuffer.subarray(0, dst.length)); + this.filledBuffer = this.filledBuffer.subarray(dst.length); + return array; + } else { + while (dst.length > 0) { + if (this.filledBuffer.length === 0) { + if (dst.length >= seedRandomBlockSize) { + const df64 = dst.subarray(0, dst.length - (dst.length % seedRandomBlockSize)); + this.fillBufferDirect(df64); + dst = dst.subarray(df64.length); + continue; + } + this.fillBuffer(); + } + if (dst.length <= this.filledBuffer.length) { + dst.set(this.filledBuffer.subarray(0, dst.length)); + this.filledBuffer = this.filledBuffer.subarray(dst.length); + return array; + } + dst.set(this.filledBuffer); + dst = dst.subarray(this.filledBuffer.length); + this.fillBuffer(); + } + return array; + } + } +} From eb7edd3d1969a0ac9f533fbc971f836d8a7e1095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:15:31 +0900 Subject: [PATCH 05/12] Updated `aiscript.api.md` --- etc/aiscript.api.md | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index 0593f82f..c430eb08 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -271,9 +271,6 @@ type Continue_2 = NodeBase_2 & { type: 'continue'; }; -// @public (undocumented) -function cryptoGen64(array: BigUint64Array): bigint | null; - declare namespace Cst { export { isStatement_2 as isStatement, @@ -861,9 +858,6 @@ export class Scope { }; } -// @public (undocumented) -function signedNumber32ToBigUint32(num: number): bigint; - // @public (undocumented) type Statement = Definition | Return | Each | For | Loop | Break | Continue | Assign | AddAssign | SubAssign; @@ -924,9 +918,6 @@ type TypeSource = NamedTypeSource | FnTypeSource; // @public (undocumented) type TypeSource_2 = NamedTypeSource_2 | FnTypeSource_2; -// @public (undocumented) -function unbiasedRandomIntegerInRange(min: number, max: number, generator: G, gen64: (generator: G) => bigint | null): number | null; - // @public (undocumented) const unWrapRet: (v: Value) => Value; @@ -950,10 +941,7 @@ declare namespace utils { valToJs, jsToVal, getLangVersion, - reprValue, - cryptoGen64, - signedNumber32ToBigUint32, - unbiasedRandomIntegerInRange + reprValue } } export { utils } From 26f0c3a4bfd5523970f6cbcb446ee48a8fd1928b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:24:45 +0900 Subject: [PATCH 06/12] * Removed `Math:rnd_unbiased` --- src/interpreter/lib/std.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index 5dc7a5f3..ec6ccb2b 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -416,13 +416,6 @@ export const std: Record = { return NUM(CryptoGen.instance.generateNumber0To1()); }), - 'Math:rnd_unbiased': FN_NATIVE(([min, max]) => { - assertNumber(min); - assertNumber(max); - const res = CryptoGen.instance.generateRandomIntegerInRange(min.value, max.value); - return res === null ? NULL : NUM(res); - }), - 'Math:gen_rng': FN_NATIVE(([seed]) => { expectAny(seed); if (seed.type !== 'num' && seed.type !== 'str') return NULL; From 717629308a9b5326b60c18bd9f59d1fb55f69b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:57:28 +0900 Subject: [PATCH 07/12] * Updated `CHANGELOG.md` * Updated `std-math.md` --- CHANGELOG.md | 2 ++ docs/std-math.md | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab62e6f9..c7b95a02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Fix: チェイン系(インデックスアクセス`[]`、プロパティアクセス`.`、関数呼び出し`()`)と括弧を組み合わせた時に不正な挙動をするバグを修正 - 関数`Str#charcode_at` `Str#to_arr` `Str#to_char_arr` `Str#to_charcode_arr` `Str#to_utf8_byte_arr` `Str#to_unicode_codepoint_arr` `Str:from_unicode_codepoints` `Str:from_utf8_bytes`を追加 - Fix: `Str#codepoint_at`がサロゲートペアに対応していないのを修正 +- Fix: `Math:rnd`が範囲外の値を返す可能性があるのを修正 +- 関数`Math:gen_rng_unbiased`を追加 ## Note バージョン0.16.0に記録漏れがありました。 >- 関数`Str:from_codepoint` `Str#codepoint_at`を追加 diff --git a/docs/std-math.md b/docs/std-math.md index a25b7bec..32ce40eb 100644 --- a/docs/std-math.md +++ b/docs/std-math.md @@ -122,6 +122,11 @@ _min_ および _max_ を渡した場合、_min_ <= x, x <= _max_ の整数、 ### @Math:gen_rng(_seed_: num | str): fn シードから乱数生成機を生成します。 +生成された乱数生成器は 0 <= x, x < 1 の 小数を返します。 + +### @Math:gen_rng_unbiased(_seed_: num | str): @(_min_: num, _max_: num) +シードから _min_ <= x, x <= _max_ の整数を一様分布で生成する乱数生成機を生成します。 +_min_ および _max_ が渡されていない場合はエラーとなります。 ## その他 ### @Math:clz32(_x_: num): num From 27463e647effbd888d735be27ade60d88ac2b397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:04:46 +0900 Subject: [PATCH 08/12] Implemented `Math:gen_rng_chacha20` --- package.json | 2 + src/interpreter/lib/std.ts | 14 +++++ src/utils/random/chacha20.ts | 107 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/utils/random/chacha20.ts diff --git a/package.json b/package.json index 8f4965ed..a387d37b 100644 --- a/package.json +++ b/package.json @@ -56,6 +56,8 @@ "typescript": "5.3.2" }, "dependencies": { + "@types/libsodium-wrappers-sumo": "0.7.8", + "libsodium-wrappers-sumo": "0.7.13", "seedrandom": "3.0.5", "stringz": "2.1.0", "uuid": "9.0.1" diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index ec6ccb2b..bb646214 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -7,6 +7,7 @@ import { AiScriptRuntimeError } from '../../error.js'; import { textDecoder } from '../../const.js'; import { CryptoGen } from '../../utils/random/CryptoGen.js'; import { SeedRandomWrapper } from '../../utils/random/seedrandom.js'; +import { ChaCha20 } from '../../utils/random/chacha20.js'; import type { Value } from '../value.js'; export const std: Record = { @@ -441,6 +442,19 @@ export const std: Record = { return typeof result === 'number' ? NUM(result) : NULL; }); }), + + 'Math:gen_rng_chacha20': FN_NATIVE(async ([seed]) => { + if (seed && seed.type !== 'num' && seed.type !== 'str') return NULL; + await ChaCha20.ready; + const rng = new ChaCha20(typeof seed === 'undefined' ? undefined : seed.value); + return FN_NATIVE(([min, max]) => { + if (min && min.type === 'num' && max && max.type === 'num') { + const result = rng.generateRandomIntegerInRange(min.value, max.value); + return typeof result === 'number' ? NUM(result) : NULL; + } + return NUM(rng.generateNumber0To1()); + }); + }), //#endregion //#region Num diff --git a/src/utils/random/chacha20.ts b/src/utils/random/chacha20.ts new file mode 100644 index 00000000..733c71df --- /dev/null +++ b/src/utils/random/chacha20.ts @@ -0,0 +1,107 @@ +import sodium from 'libsodium-wrappers-sumo'; +import { RandomBase, bigMaxSafeIntegerExclusive, bigSafeIntegerBits, readBigUintLittleEndian, safeIntegerBits } from './randomBase.js'; + +const chacha20BlockSize = 64; +const bigChacha20BlockSize = BigInt(chacha20BlockSize); + +export class ChaCha20 extends RandomBase { + public static ready = sodium.ready; + private key: Uint8Array; + private nonce: Uint8Array; + private buffer: Uint8Array; + private filledBuffer: Uint8Array; + private counter: bigint; + constructor(seed?: string | number | Uint8Array | undefined) { + const keyNonceBytes = sodium.crypto_stream_chacha20_NONCEBYTES + sodium.crypto_stream_chacha20_KEYBYTES; + super(); + let keynonce: Uint8Array; + if (typeof seed === 'undefined') { + keynonce = crypto.getRandomValues(new Uint8Array(keyNonceBytes)); + } else if (typeof seed === 'number') { + const array = new Float64Array([seed]); + keynonce = sodium.crypto_generichash(keyNonceBytes, new Uint8Array(array.buffer)); + } else { + keynonce = sodium.crypto_generichash(keyNonceBytes, seed); + } + this.key = keynonce.subarray(0, sodium.crypto_stream_chacha20_KEYBYTES); + this.nonce = keynonce.subarray(sodium.crypto_stream_chacha20_KEYBYTES); + this.buffer = new Uint8Array(chacha20BlockSize); + this.counter = 0n; + this.filledBuffer = new Uint8Array(0); + } + private fillBuffer(): void { + this.buffer.fill(0); + this.buffer = this.fillBufferDirect(this.buffer); + this.filledBuffer = this.buffer; + } + private fillBufferDirect(buffer: Uint8Array): Uint8Array { + if ((buffer.length % chacha20BlockSize) !== 0) throw new Error('ChaCha20.fillBufferDirect should always be called with the buffer with the length a multiple-of-64!'); + buffer.fill(0); + let blocks = BigInt(buffer.length / chacha20BlockSize); + let counter = this.counter % bigMaxSafeIntegerExclusive; + const newCount = counter + blocks; + const overflow = newCount >> bigSafeIntegerBits; + if (overflow === 0n) { + buffer.set(sodium.crypto_stream_chacha20_xor_ic(buffer, this.nonce, Number(counter), this.key)); + this.counter = newCount; + return buffer; + } + let dst = buffer; + while (dst.length > 0) { + const remainingBlocks = bigMaxSafeIntegerExclusive - counter; + const genBlocks = remainingBlocks > blocks ? blocks : remainingBlocks; + blocks -= genBlocks; + const dbuf = dst.subarray(0, Number(genBlocks * bigChacha20BlockSize)); // safe integers wouldn't lose any precision with multiplying by a power-of-two anyway. + dbuf.set(sodium.crypto_stream_chacha20_xor_ic(dbuf, this.nonce, Number(counter), this.key)); + dst = dst.subarray(dbuf.length); + counter = BigInt.asUintN(safeIntegerBits, counter + genBlocks); + this.counter = counter; + } + return buffer; + } + protected generateBigUintByBytes(bytes: number): bigint { + let u8a = new Uint8Array(Math.ceil(bytes / 8) * 8); + if (u8a.length < 1 || !Number.isSafeInteger(bytes)) return 0n; + u8a = this.generateBytes(u8a.subarray(0, bytes)); + return readBigUintLittleEndian(u8a.buffer) ?? 0n; + } + + public generateBigUintByBits(bits: number): bigint { + if (bits < 1 || !Number.isSafeInteger(bits)) return 0n; + const bytes = Math.ceil(bits / 8); + const wastedBits = BigInt(bytes * 8 - bits); + return this.generateBigUintByBytes(bytes) >> wastedBits; + } + + public generateBytes(array: Uint8Array): Uint8Array { + if (array.length < 1) return array; + array.fill(0); + let dst = array; + if (dst.length <= this.filledBuffer.length) { + dst.set(this.filledBuffer.subarray(0, dst.length)); + this.filledBuffer = this.filledBuffer.subarray(dst.length); + return array; + } else { + while (dst.length > 0) { + if (this.filledBuffer.length === 0) { + if (dst.length >= chacha20BlockSize) { + const df64 = dst.subarray(0, dst.length - (dst.length % chacha20BlockSize)); + this.fillBufferDirect(df64); + dst = dst.subarray(df64.length); + continue; + } + this.fillBuffer(); + } + if (dst.length <= this.filledBuffer.length) { + dst.set(this.filledBuffer.subarray(0, dst.length)); + this.filledBuffer = this.filledBuffer.subarray(dst.length); + return array; + } + dst.set(this.filledBuffer); + dst = dst.subarray(this.filledBuffer.length); + this.fillBuffer(); + } + return array; + } + } +} From a0934f534a7577489af4e4b7b228a7c378a25210 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:06:48 +0900 Subject: [PATCH 09/12] * Refactored `Math:gen_rng_unbiased` to use ChaCha20 * Refactored `Math:rnd` to use `crypto` * Refactored `Math:rnd_unbiased` to use `crypto` --- src/interpreter/lib/std.ts | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index bb646214..866be440 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -417,6 +417,13 @@ export const std: Record = { return NUM(CryptoGen.instance.generateNumber0To1()); }), + 'Math:rnd_unbiased': FN_NATIVE(([min, max]) => { + assertNumber(min); + assertNumber(max); + const res = CryptoGen.instance.generateRandomIntegerInRange(min.value, max.value); + return res === null ? NULL : NUM(res); + }), + 'Math:gen_rng': FN_NATIVE(([seed]) => { expectAny(seed); if (seed.type !== 'num' && seed.type !== 'str') return NULL; @@ -430,20 +437,7 @@ export const std: Record = { }); }), - 'Math:gen_rng_unbiased': FN_NATIVE(([seed]) => { - expectAny(seed); - if (seed.type !== 'num' && seed.type !== 'str') return NULL; - - const rng = new SeedRandomWrapper(seed.value); - return FN_NATIVE(([min, max]) => { - assertNumber(min); - assertNumber(max); - const result = rng.generateRandomIntegerInRange(min.value, max.value); - return typeof result === 'number' ? NUM(result) : NULL; - }); - }), - - 'Math:gen_rng_chacha20': FN_NATIVE(async ([seed]) => { + 'Math:gen_rng_unbiased': FN_NATIVE(async ([seed]) => { if (seed && seed.type !== 'num' && seed.type !== 'str') return NULL; await ChaCha20.ready; const rng = new ChaCha20(typeof seed === 'undefined' ? undefined : seed.value); From a9767956834d69eb3a0df552053f102fecc04ad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:06:48 +0900 Subject: [PATCH 10/12] + Added tests for `Math:gen_rng_unbiased` * Reduced potential random test failure --- test/index.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/index.ts b/test/index.ts index 91954f98..dd2afb68 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2862,13 +2862,28 @@ describe('std', () => { const res = await exe(` @test(seed) { let random = Math:gen_rng(seed) - return random(0 100) + return random() } let seed1 = \`{Util:uuid()}\` let seed2 = \`{Date:year()}\` let test1 = if (test(seed1) == test(seed1)) {true} else {false} let test2 = if (test(seed1) == test(seed2)) {true} else {false} - <: [test1 test2] + <: [test1, test2] + `) + eq(res, ARR([BOOL(true), BOOL(false)])); + }); + + test.concurrent('gen_rng_unbiased', async () => { + const res = await exe(` + @test(seed) { + let random = Math:gen_rng_unbiased(seed) + return random() + } + let seed1 = \`{Util:uuid()}\` + let seed2 = \`{Date:year()}\` + let test1 = if (test(seed1) == test(seed1)) {true} else {false} + let test2 = if (test(seed1) == test(seed2)) {true} else {false} + <: [test1, test2] `) eq(res, ARR([BOOL(true), BOOL(false)])); }); From 3ef118a573030473bb2887ec0868af6cd509339a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Fri, 9 Feb 2024 20:29:30 +0900 Subject: [PATCH 11/12] * `Math:gen_rng` now has 2nd parameter `algorithm` --- CHANGELOG.md | 7 ++++-- src/interpreter/lib/std.ts | 45 ++++++++++---------------------------- src/utils/random/genrng.ts | 42 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 35 deletions(-) create mode 100644 src/utils/random/genrng.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 765efdca..6ec25dba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,9 +8,13 @@ - 文法エラーの表示を改善。理由を詳細に表示するように。 - 複数行のコメントがある時に文法エラーの表示行数がずれる問題を解消しました。 - 実行時エラーの発生位置が表示されるように。 +- 関数`Math:gen_rng`に第二引数`algorithm`をオプション引数として追加。 + - アルゴリズムを`chacha20`、`rc4`、`rc4_legacy`から選べるようになりました。 + - **Breaking Change** `algorithm`を指定しない場合、`chacha20`が選択されます。 - **Breaking Change** パースの都合によりmatch文の構文を変更。パターンの前に`case`キーワードが必要となり、`*`は`default`に変更。 - **Breaking Change** 多くの予約語を追加。これまで変数名等に使えていた名前に影響が出る可能性があります。 - **Breaking Change** 配列及び関数の引数において、空白区切りが使用できなくなりました。`,`または改行が必要です。 +- **Breaking Change** `Math:rnd`が範囲外の値を返す可能性があるのを修正。 # 0.17.0 - `package.json`を修正 @@ -20,8 +24,7 @@ - 関数`Str#charcode_at` `Str#to_arr` `Str#to_char_arr` `Str#to_charcode_arr` `Str#to_utf8_byte_arr` `Str#to_unicode_codepoint_arr` `Str:from_unicode_codepoints` `Str:from_utf8_bytes`を追加 - Fix: `Str#codepoint_at`がサロゲートペアに対応していないのを修正 - 配列の範囲外および非整数のインデックスへの代入でエラーを出すように -- Fix: `Math:rnd`が範囲外の値を返す可能性があるのを修正 -- 関数`Math:gen_rng_unbiased`を追加 + ## Note バージョン0.16.0に記録漏れがありました。 >- 関数`Str:from_codepoint` `Str#codepoint_at`を追加 diff --git a/src/interpreter/lib/std.ts b/src/interpreter/lib/std.ts index 73e1660f..96a42886 100644 --- a/src/interpreter/lib/std.ts +++ b/src/interpreter/lib/std.ts @@ -1,14 +1,12 @@ /* eslint-disable no-empty-pattern */ import { v4 as uuid } from 'uuid'; -import seedrandom from 'seedrandom'; import { NUM, STR, FN_NATIVE, FALSE, TRUE, ARR, NULL, BOOL, OBJ, ERROR } from '../value.js'; import { assertNumber, assertString, assertBoolean, valToJs, jsToVal, assertFunction, assertObject, eq, expectAny, assertArray, reprValue } from '../util.js'; import { AiScriptRuntimeError } from '../../error.js'; import { AISCRIPT_VERSION } from '../../constants.js'; import { textDecoder } from '../../const.js'; import { CryptoGen } from '../../utils/random/CryptoGen.js'; -import { SeedRandomWrapper } from '../../utils/random/seedrandom.js'; -import { ChaCha20 } from '../../utils/random/chacha20.js'; +import { GenerateChaCha20Random, GenerateLegacyRandom, GenerateRC4Random } from '../../utils/random/genrng.js'; import type { Value } from '../value.js'; export const std: Record = { @@ -418,37 +416,18 @@ export const std: Record = { return NUM(CryptoGen.instance.generateNumber0To1()); }), - 'Math:rnd_unbiased': FN_NATIVE(([min, max]) => { - assertNumber(min); - assertNumber(max); - const res = CryptoGen.instance.generateRandomIntegerInRange(min.value, max.value); - return res === null ? NULL : NUM(res); - }), - - 'Math:gen_rng': FN_NATIVE(([seed]) => { + 'Math:gen_rng': FN_NATIVE(async ([seed, algorithm]) => { expectAny(seed); - if (seed.type !== 'num' && seed.type !== 'str') return NULL; - - const rng = seedrandom(seed.value.toString()); - return FN_NATIVE(([min, max]) => { - if (min && min.type === 'num' && max && max.type === 'num') { - return NUM(Math.floor(rng() * (Math.floor(max.value) - Math.ceil(min.value) + 1) + Math.ceil(min.value))); - } - return NUM(rng()); - }); - }), - - 'Math:gen_rng_unbiased': FN_NATIVE(async ([seed]) => { - if (seed && seed.type !== 'num' && seed.type !== 'str') return NULL; - await ChaCha20.ready; - const rng = new ChaCha20(typeof seed === 'undefined' ? undefined : seed.value); - return FN_NATIVE(([min, max]) => { - if (min && min.type === 'num' && max && max.type === 'num') { - const result = rng.generateRandomIntegerInRange(min.value, max.value); - return typeof result === 'number' ? NUM(result) : NULL; - } - return NUM(rng.generateNumber0To1()); - }); + const algo = !algorithm || algorithm.type !== 'str' ? STR('chacha20') : algorithm; + if (seed.type !== 'num' && seed.type !== 'str' && seed.type !== 'null') return NULL; + switch (algo.value) { + case 'rc4_legacy': + return GenerateLegacyRandom(seed); + case 'rc4': + return GenerateRC4Random(seed); + default: + return GenerateChaCha20Random(seed); + } }), //#endregion diff --git a/src/utils/random/genrng.ts b/src/utils/random/genrng.ts new file mode 100644 index 00000000..30bed298 --- /dev/null +++ b/src/utils/random/genrng.ts @@ -0,0 +1,42 @@ +import seedrandom from 'seedrandom'; +import { FN_NATIVE, NULL, NUM } from '../../interpreter/value.js'; +import { assertNumber } from '../../interpreter/util.js'; +import { SeedRandomWrapper } from './seedrandom.js'; +import type { VNativeFn, VNull, Value } from '../../interpreter/value.js'; +import { ChaCha20 } from './chacha20.js'; + +export function GenerateLegacyRandom(seed: Value | undefined) : VNativeFn | VNull { + if (!seed || seed.type !== 'num' && seed.type !== 'str') return NULL; + const rng = seedrandom(seed.value.toString()); + return FN_NATIVE(([min, max]) => { + if (min && min.type === 'num' && max && max.type === 'num') { + return NUM(Math.floor(rng() * (Math.floor(max.value) - Math.ceil(min.value) + 1) + Math.ceil(min.value))); + } + return NUM(rng()); + }); +} + +export function GenerateRC4Random(seed: Value | undefined) : VNativeFn | VNull { + if (!seed || seed.type !== 'num' && seed.type !== 'str') return NULL; + const rng = new SeedRandomWrapper(seed.value); + return FN_NATIVE(([min, max]) => { + if (min && min.type === 'num' && max && max.type === 'num') { + const result = rng.generateRandomIntegerInRange(min.value, max.value); + return typeof result === 'number' ? NUM(result) : NULL; + } + return NUM(rng.generateNumber0To1()); + }); +} + +export async function GenerateChaCha20Random(seed: Value | undefined) : Promise { + if (!seed || seed.type !== 'num' && seed.type !== 'str' && seed.type !== 'null') return NULL; + await ChaCha20.ready; + const rng = new ChaCha20(seed.type === 'null' ? undefined : seed.value); + return FN_NATIVE(([min, max]) => { + if (min && min.type === 'num' && max && max.type === 'num') { + const result = rng.generateRandomIntegerInRange(min.value, max.value); + return typeof result === 'number' ? NUM(result) : NULL; + } + return NUM(rng.generateNumber0To1()); + }); +} From cd0f8d46163865eb3bb9eade5d33eb4435e515bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=A1=E3=83=BC=E3=81=9A=28=EF=BD=A58=EF=BD=A5=29?= =?UTF-8?q?=E3=81=91=E3=83=BC=E3=81=8D?= <31585494+MineCake147E@users.noreply.github.com> Date: Fri, 9 Feb 2024 20:33:16 +0900 Subject: [PATCH 12/12] *Updated `CHANGELOG.md` --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ec25dba..db66fdfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ - **Breaking Change** パースの都合によりmatch文の構文を変更。パターンの前に`case`キーワードが必要となり、`*`は`default`に変更。 - **Breaking Change** 多くの予約語を追加。これまで変数名等に使えていた名前に影響が出る可能性があります。 - **Breaking Change** 配列及び関数の引数において、空白区切りが使用できなくなりました。`,`または改行が必要です。 -- **Breaking Change** `Math:rnd`が範囲外の値を返す可能性があるのを修正。 +- Fix: `Math:rnd`が範囲外の値を返す可能性があるのを修正。 # 0.17.0 - `package.json`を修正