Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6609831rwb; Tue, 15 Nov 2022 00:43:44 -0800 (PST) X-Google-Smtp-Source: AA0mqf7Fqm+f/AabClkLpoO3uLozruED2uKV1/PFqntsu8ngZPZ/FdnAOXMUhPiDcvkp51Kf1IOm X-Received: by 2002:a17:907:5014:b0:7ab:2559:8bc4 with SMTP id fw20-20020a170907501400b007ab25598bc4mr12377805ejc.682.1668501824087; Tue, 15 Nov 2022 00:43:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668501824; cv=none; d=google.com; s=arc-20160816; b=GFA6tBIPxOkXJRu22kCiFLYD8w/AAnovI6raJNl/CGFXfGcg1HhllskaA/iRialB+g 3iaBDsaZ/eVH1YE+/UxM/r4/hXUoSEItVGAjS9u1ETzp2FAnxXsxLQErXzHr2QiHRc3V t8Ub8+x6DKluYEUegRR1SaP1kiaTb2NmUxhrUg8yR+tzj9H3dWAfxhksLxYDiTgj37UV RXDqco65Wa3Ci0UECTFv2l4QwlCwQm2MdnFYbncjgnIHDfrsP5MLE0YjvWrzLg2/MHe/ SwofwKvfYOGUW6d7/793ov+8HhdVzdHBJkxtApoNOE71H1evMShlgNTsmByIuNnefNnW 7MzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:content-language:references:cc:to:subject:from :user-agent:mime-version:date:message-id; bh=/ILFfExT9eET4dvtfKcKeeJEzTGyjXaeJRNYrR8mBHw=; b=hjtd0U17jJoJNPbffA6TLoJvlINsuozwm8oFEo7npEYaiZKIm6ecU+FAsl4AFGMMnG OrnU2K2yCcoPcVkajSczaSemD1CuHYdmtxYonwgdFdwem/lUgxWqmCtkH+ap3VTX5zmV +Dve6gvSkdsHBgEnPYI7Jn3maGUm5LwaGbczBXCmuf7jU6/wGN85lXbQfDndcvZyATpL sOL436vWzJO5s+QFAHkxmrKNyOoMJk30H7pptzfz5aeqKBoi8sE7kWa+Zy4keu7LVptp DXPK57pnDT/1aP98x4lAcU9LlaOkL/HTIXPYLBZDsli6yKhaLk0rqkLH49XYup/9GBk2 DgLg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fi5-20020a1709073ac500b007ade14a74b9si7060026ejc.660.2022.11.15.00.43.07; Tue, 15 Nov 2022 00:43:44 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232786AbiKOImj (ORCPT + 99 others); Tue, 15 Nov 2022 03:42:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51210 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230254AbiKOImi (ORCPT ); Tue, 15 Nov 2022 03:42:38 -0500 Received: from smtp2-g21.free.fr (smtp2-g21.free.fr [IPv6:2a01:e0c:1:1599::11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B41605582; Tue, 15 Nov 2022 00:42:35 -0800 (PST) Received: from [IPV6:2a01:e35:39f2:1220:da6c:81de:7fd7:e3eb] (unknown [IPv6:2a01:e35:39f2:1220:da6c:81de:7fd7:e3eb]) by smtp2-g21.free.fr (Postfix) with ESMTPS id 29A2120042C; Tue, 15 Nov 2022 09:42:25 +0100 (CET) Message-ID: <9ffb34a9-3cbc-bd0e-7a92-76851a77b9ef@opteya.com> Date: Tue, 15 Nov 2022 09:42:25 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 From: Yann Droneaud Subject: Re: [PATCH v1 3/5] random: add helpers for random numbers with given floor or range To: "Jason A. Donenfeld" Cc: Kees Cook , Greg Kroah-Hartman , Herbert Xu , Theodore Ts'o , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org References: <20221022014403.3881893-1-Jason@zx2c4.com> <20221022014403.3881893-4-Jason@zx2c4.com> <60574e8f-20ff-0996-5558-e9bd35e42681@opteya.com> Content-Language: fr-FR, en-US Organization: OPTEYA In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Hi, Le 14/11/2022 à 19:38, Jason A. Donenfeld a écrit : > On Mon, Nov 14, 2022 at 07:04:13PM +0100, Yann Droneaud wrote: >> I have a bad feeling about this one, and can't help but thinking it's going >> to bite someone: when asked to pick a number *between* 0 and 10, >> I usually think I'm allowed to pick 10 (even if I'm going to answer 7 as it should). > This is one of those bikeshed things you see all over the place, like > whether slices in a language should be [start index, end index] or > [start index, length], or whether arrays should be 0-based or 1-based. > We'll never settle this variety of dispute here. > > But in this case, there are some particular reasons why it must be this > way. Firstly, usage of it this way matches most of the ways the function > is actually used in the kernel, and fits existing semantics. This alone > I find compelling. But also, having all of these functions use half-open > intervals means that each function can take care of its entire range, > without having to resort to using 64-bit arithmetic, and no function is > a complete subset of any other function. So doing it this way makes > these maximally useful too. For get_random_below(), which replaces a modulo, and could have been called get_random_mod(), having an open upper range seems fine. It's already what can be achieved by the % operator. But I believe it's unfortunate get_random_between() cannot be called to get a number up to UINT32_MAX, as get_random_between(0, UINT32_MAX) would be capped to UINT32_MAX - 1. When not a constant, one could hope the function can cope with a maximum that would grow up to and including UINT32_MAX. > So anyway I think the function has to be defined like this. If you'd > like to bikeshed over a different name than "between", though, be my > guest. Maybe you'd like "from" better. But probably "between" is fine, > and with enough good examples (as my conversion patch does) and the > clear succinct documentation comment, we should be good. That the conversion patch [1] that triggered my comment: I find replacing the following rather unpleasing, somewhat uncanny: -get_random_u32_below(1024) + 1 + get_random_u32_between(1, 1024 + 1) I would prefer - get_random_u32_below(1024) + 1 + get_random_u32_between(1, 1024) [1]https://lore.kernel.org/lkml/20221114164558.1180362-4-Jason@zx2c4.com/ Regards. -- Yann Droneaud OPTEYA