Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117047 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 16569 invoked from network); 16 Feb 2022 10:21:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Feb 2022 10:21:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 321E518053E for ; Wed, 16 Feb 2022 03:39:33 -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-f178.google.com (mail-yb1-f178.google.com [209.85.219.178]) (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 ; Wed, 16 Feb 2022 03:39:32 -0800 (PST) Received: by mail-yb1-f178.google.com with SMTP id e140so5111095ybh.9 for ; Wed, 16 Feb 2022 03:39:32 -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=3su0emeqHXI+SQrH4UGZdbW4C9vbjwVXABO3OcoyyyQ=; b=EAQJ+Tdcr9gN3POxZMt80VjMX0kX+wtOyU8cjvJ8OgvcgqLt0FxuOF53Rl9qxtQn13 VG4v7YahNtfAZfMPMclI/8EBPp46ONtu04k7b+LkDfGYVy1COTgSxiMcXZT3Hmq2gyNQ 6VhQvgRLObkTH18d3EFISCqa+LFIHUqYwJ200Q9zMkIlH4AJbxfqBUCOhnBLlhoikSux S8hTsdJ0jsmB3UYxbM23cEx30rd+417miTlbRZGdGqFuZz+PGZa+HsbjMWOA1P7B3Aj8 +Bx06814dIyJmblqmEMmwR8gxT2Oa1NwzBgtA9M5Y23MqWWfc4kJAqBuQw9Z0/Wz9MnK tnBQ== 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=3su0emeqHXI+SQrH4UGZdbW4C9vbjwVXABO3OcoyyyQ=; b=Ri6rJzPFaqmfUZ8/QKLWQo+9FFQwX4VvrRGd7fDzpSE5EmpJW/NNam2oDuqiLdsK41 jWOvDMWMx2h3WtlKShjU5yKEdc5iooDY2mLlMU6ynSxgiUQMD4a0TXSNzjDEB8CdaJH8 1m9JyTXcgp34QxBRVPvm9DRVUSAVjpQ4e2xhi4DFasGv1HUAUJDLgxTfdfpVluhBcXdV 9eSMq3zYmMMpUeVl6YZY2N9L6o8NwONxH9UoHoV/UJfqmvTyN7Z+qiqin4UI944HJWDg fXGJpeElCtpZFz8jv66bGdZFio2r8Eimiin1JjucATOpjFtm72mi7+6n45GAPj8l9QMl gI3A== X-Gm-Message-State: AOAM530t3f9+N5AKMix+j82Jo2KoyzBvDJecxNFR6CK2B1VW3KKAJXIe ceaSJbQrggHA2IrYVLzcVYj+4qP8t3p7Icg40vrM X-Google-Smtp-Source: ABdhPJw2PHHnc4GT2RTOZ/oeqHKouIR7IPfD5nxCecmDPBs8hWRz+v7/s5NTjsoBSfAtg1LapXuULqCQEylpR6dyqqE= X-Received: by 2002:a0d:c2c3:0:b0:2d0:f750:1e97 with SMTP id e186-20020a0dc2c3000000b002d0f7501e97mr1996813ywd.136.1645011572318; Wed, 16 Feb 2022 03:39:32 -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: <5f496cf9-8754-b009-9cb5-b978222b2249@bastelstu.be> Date: Wed, 16 Feb 2022 20:39:21 +0900 Message-ID: To: =?UTF-8?Q?Tim_D=C3=BCsterhus?= Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000006b7b6505d8211a78" Subject: Re: [PHP-DEV] [RFC] [Under Discussion] Random Extension 4.0 From: g-kudo@colopl.co.jp (Go Kudo) --0000000000006b7b6505d8211a78 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2022=E5=B9=B42=E6=9C=8816=E6=97=A5(=E6=B0=B4) 20:24 Tim D=C3=BCsterhus : > Hi > > On 2/16/22 12:04, Go Kudo wrote: > > Thanks for the good idea. I changed the NumberGenerator to Engine and > > changed generate() to return a string as suggested. > > Thanks, I've already seen the updated PR and played around with it. This > feels much better now. > > As a test I implemented xoshiro128++ in pure userland (it being a 32 Bit > RNG avoids the signedness issues in PHP userland) and compared it > against the reference implementation in C. > > Find my implementations attached. Both versions give the same results > (little endian encoding): > > $ gcc xoshiro128pp.c ; ./a.out > fa3c872c > $ sapi/cli/php test_rng.php > string(8) "fa3c872c" > > > The main changes since last time are as follows: > > > > - The userland implementation can now specify the width of the random > > number sequence that can be generated > > - Random\Engine::nextByteSize() has been added > > Is the nextByteSize() method actually required? PHP strings already know > their own length. > > > - Random\Engine::generate() now returns a string > > I've looked into your C implementation and it appears it still is > affected by endianness issues. You can't simply memcpy the raw uintXX_t > bytes into the char array. I believe the following should do the trick > to for a consistent little endian encoding: > > bytes[0] =3D (generated >> 0) & 0xff; > bytes[1] =3D (generated >> 8) & 0xff; > bytes[2] =3D (generated >> 16) & 0xff; > bytes[3] =3D (generated >> 24) & 0xff; > > The choice of endianness is arbitrary, but it needs to be consistent for > every platform. > > Likewise when converting back to a number from bytes: > > number =3D (bytes[0] << 0) | (bytes[1] << 8) (bytes[2] << 16) | (bytes[3] > << 24); > > I believe the same issue exists when initializing the XorShift with a > string. > > > I have not yet come to a final conclusion on whether XorShift128Plus > should > > be switched to another RNG. For example, what about implementing > > XorShift128Plus, but adding Xoshiro256** as well? > > > > That would be fine for me as well. But it might make it harder for the > end user to choose an appropriate RNG. > > Best regards > Tim D=C3=BCsterhus Thanks Tim I forgot about the endian problem. Will try to fix it. Thank you very much. > Is the nextByteSize() method actually required? PHP strings already know their own length. This is a convenience of the current implementation. The native implementation does not use strings for performance reasons. This is because there is no way to know the valid width information they generate. I'm trying to think of some good ideas. > That would be fine for me as well. OK, I'm going to add Xoshiro128++ and Xoshiro256**. In order to make PHP easier to maintain, I think it is best not to add more implementations at random, so I will not add any more. --0000000000006b7b6505d8211a78--