Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117057 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49075 invoked from network); 18 Feb 2022 05:13:39 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Feb 2022 05:13:39 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A8860180381 for ; Thu, 17 Feb 2022 22:32:02 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 17 Feb 2022 22:32:02 -0800 (PST) Received: by mail-yb1-f170.google.com with SMTP id v186so17704492ybg.1 for ; Thu, 17 Feb 2022 22:32:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=colopl.co.jp; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=2rEU+Alq1P24V0Tyrd00gCDIpr6PV6sHboFYtTTZrAE=; b=IQGvehoZ+VIoC+dH50lhmeCMc0lvf86nHOT9nEW+qur3zNJr8TzEO4KSSJXoA8V9Ui rD56vg+8idw8hpB0gse0TdhrivpoysamLdv3rRDZ7bT+ckHTybr0nBFBAOd18o6drZzx wJejM+zXfZj193bDS+NhVFsxIhduXwp3wKwhCJW7vpdjLu3oTn1ktIrejV4JPGDhJ6xj qoP8/8eMkQQUKymLiN2QzVV8GUhGBPecD6E7CD8/LfZI+4t2osZWeY9vIhvKt+AGr22J 3CWZ4KaWAOjSVFTfei8gwBXqg1a9OBVC8jcKCPML2IxuPeURVi9lCeg8RrmOarNqZl7s DUHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=2rEU+Alq1P24V0Tyrd00gCDIpr6PV6sHboFYtTTZrAE=; b=CLGkaEkyv/UWyDRWQsJz/15AQCjNkFhVvslf4dR9kfjWDsj1zk+e9XhkqOK2jLq94s eWPRv+dQFW9cks9k8pdXcDTZ6eE0cFuodr6wBSAMQp4rquDbeKpk3NPlNx8x72XHdiwj 6eKEtPs0NAJSbpXjLWs4Bb33qnUCEiTn/MKRWSxYE5WF7srDubjNJJ5Ros46kXxUlEzc xNnb6jV5mDMop6elYyVzUclPJjV/iXaVIapSgmmLY2DWPsCsHOr+Bx9iCzmkgmKjGbld Qd+bLQSIFIckB7gCG+UQ9lXYWdJgSwweKWFkG4+WztcJG4LnFFp+8Avns4nbU4X4AwmD wW/g== X-Gm-Message-State: AOAM532P1thOWYmE9ozuDUot2Avm42d3q2pmJo0rMCgNsSr2mnr+pdV6 lVPP7RcG9TGuwTpusjLruQ11sQFWk8VfAxDt44UloZn23RXV X-Google-Smtp-Source: ABdhPJzRZoEHDFDon5Odm2Dq7kTW5wd0/9fSAPllgIKRY30ePj9BxEEmPS/RGYlL7AhuI810EmvesaVIH84O+MR21AU= X-Received: by 2002:a25:9a44:0:b0:622:4e:958c with SMTP id r4-20020a259a44000000b00622004e958cmr6050650ybo.566.1645165921395; Thu, 17 Feb 2022 22:32:01 -0800 (PST) MIME-Version: 1.0 References: <41a1b458-4941-f34e-f1b4-e25b3298b80a@bastelstu.be> <553ba7ca-3821-c2d9-f88f-b216013a887b@bastelstu.be> <2c667812-88c8-0b7b-3558-561a1348d0b2@bastelstu.be> <5f496cf9-8754-b009-9cb5-b978222b2249@bastelstu.be> In-Reply-To: Date: Fri, 18 Feb 2022 15:31:50 +0900 Message-ID: To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="00000000000057802805d8450a68" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Random Extension 4.0 From: g-kudo@colopl.co.jp (Go Kudo) --00000000000057802805d8450a68 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2022=E5=B9=B42=E6=9C=8817=E6=97=A5(=E6=9C=A8) 19:25 Tim D=C3=BCsterhus : > Hi > > On 2/17/22 08:37, Go Kudo wrote: > > The following points have been fixed: > > > > - `nextByteSize(): int` has been removed from Random\Engine > > - If the width of the RNG is statically defined, it will now be used > > preferentially > > - Added Xoshiro256StarStar > > - Fixed an endianness issue > > > > And updated RFC > > > > https://wiki.php.net/rfc/rng_extension > > > > [...] > > This seems to have solved the whole problem. How about it? > > Awesome, this is feeling much much better now. As you might've seen I've > made some comments on GitHub regarding implementation bugs. > > I have two more conceptional questions: > > ----------- > > 1) > > However I believe you did not answer my question regarding the following > and that's something that should be clear in the RFC and documentation: > > > use Random\Engine\Xoshiro256StarStar; > use Random\Randomizer; > > $g1 =3D new Xoshiro256StarStar(1); > $g2 =3D clone $g1; > $r1 =3D new Randomizer($g1); > $r2 =3D new Randomizer($g2); > > var_dump(\bin2hex($r1->getBytes(8))); // string(16) "c510c70f6daff2b= 3" > var_dump(\bin2hex($r2->getBytes(4) . $r2->getBytes(4))); // > string(16) "c510c70fea4c3647" > > > In this example I get 8 bytes from the randomizer. One time by getting > all 8 bytes at once, the second time by getting 4 bytes and then another > 4 bytes. > > I think that both lines should result in the same output, because in > both cases I am getting 8 bytes, without any other operations that might > affect the engine state in between. As a user I should not be required > to know how the Randomizer works internally (by always getting 8 bytes > from the engine and throwing away unused bytes). > > If you disagree and think this is the correct behavior then this should > be documented accordingly in the RFC. If you agree, then this should be > fixed and a testcase be added. > > ----------- > > 2) > > This ties into (1): Currently any additional bytes returned by the > engine are silently ignored. Either the bytes should be processed in > full, or an error emitted if the returned bytestring is too long. > > Consider the attached test case with a Sha1-based RNG. If I grab 20 > bytes (the length of a SHA-1 hash) from the Randomizer then the > 'generate()' function will be called 3 times, despite it returning > sufficient bytes on the first attempt. If I want to make sure that no > bytes are wasted, then I need to implement a pretty complex construction > (Sha1_2) to always return exactly 8 bytes. > > Best regards > Tim D=C3=BCsterhus Hi Hello. I have been looking into output buffering, but don't know the right way to do it. The buffering works fine if all RNG generation widths are static, but if they are dynamic so complicated. It is possible to solve this problem by allowing generate() itself to specify the size it wants, but this would significantly slow down performance. I've looked at the sample code, but do you really need support for Randomizer? Engine::generate() can output dynamic binaries up to 64 bits. You can use Engine directly, instead of Randomizer::getBytes(). What exactly is the situation where buffering by Randomizer is needed? Also worried that buffering will cut off random numbers at arbitrary sizes. It may cause bias in the generated results. If you don't want to waste the generated values, users can still implement their own buffering structures into Random\Engine following. ```php gen =3D $this->stream($seed); $this->buffer =3D ''; } private function stream($state) { while (true) { $state =3D \sha1($state, true); for ($i =3D 0; $i < \strlen($state); $i++) { yield $state[$i]; } } } public function generate(): string { echo __METHOD__, PHP_EOL; $result =3D ""; for ($i =3D 0; $i < 8; $i++) { $result .=3D $this->gen->current(); $this->gen->next(); } return $result; } public function getBytes(int $length): string { $result =3D ''; while (\strlen($result) < $length) { if ($this->buffer =3D=3D=3D '') { $this->buffer =3D $this->generate(); } $required =3D $length - \strlen($result); $temp =3D \substr($this->buffer, 0, $required); $this->buffer =3D \substr($this->buffer, $required); $result .=3D $temp; } return $result; } } ``` Buffering mechanism that was implemented in a limited way has been reverted recently. Best Regards Go Kudo --00000000000057802805d8450a68--