Re: [CFRG] Comment on draft-irtf-cfrg-hash-to-curve-10

Loup Vaillant-David <loup@loup-vaillant.fr> Fri, 23 April 2021 22:20 UTC

Return-Path: <loup@loup-vaillant.fr>
X-Original-To: cfrg@ietfa.amsl.com
Delivered-To: cfrg@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id 3B64B3A1193 for <cfrg@ietfa.amsl.com>; Fri, 23 Apr 2021 15:20:11 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -1.898
X-Spam-Level:
X-Spam-Status: No, score=-1.898 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, RCVD_IN_DNSWL_BLOCKED=0.001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_NONE=0.001] autolearn=ham autolearn_force=no
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2SXIIy2hKfQ0 for <cfrg@ietfa.amsl.com>; Fri, 23 Apr 2021 15:20:09 -0700 (PDT)
Received: from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net [217.70.183.198]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id AE43D3A1196 for <cfrg@ietf.org>; Fri, 23 Apr 2021 15:20:08 -0700 (PDT)
X-Originating-IP: 78.198.246.40
Received: from grey-fade (unknown [78.198.246.40]) (Authenticated sender: loup@loup-vaillant.fr) by relay6-d.mail.gandi.net (Postfix) with ESMTPSA id 347C4C0007; Fri, 23 Apr 2021 22:20:02 +0000 (UTC)
Message-ID: <7307836acc316c31006c999353737f7bc9a155aa.camel@loup-vaillant.fr>
From: Loup Vaillant-David <loup@loup-vaillant.fr>
To: "Riad S. Wahby" <rsw@cs.stanford.edu>, Daira Hopwood <daira@jacaranda.org>
Cc: cfrg@ietf.org
Date: Sat, 24 Apr 2021 00:20:01 +0200
In-Reply-To: <20210423193036.szrrpvg7zbtplkor@muon>
References: <e270e62d-941d-0a87-7dc9-cf80f73b5aeb@jacaranda.org> <108aae2c-576d-ba68-34b8-c539d3fb945d@jacaranda.org> <d2f89438-faeb-47db-97f9-c7ebb394f348@www.fastmail.com> <8c736a71-8ef0-dd8e-1b5a-47cccf1af410@jacaranda.org> <20210422164424.5qwe5msxueqz6rrk@muon> <3360a3c2-9afc-332b-c3c7-6c8c512f8c1b@jacaranda.org> <20210423193036.szrrpvg7zbtplkor@muon>
Content-Type: text/plain; charset="UTF-8"
X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.2
Mime-Version: 1.0
Content-Transfer-Encoding: 7bit
Archived-At: <https://mailarchive.ietf.org/arch/msg/cfrg/zeyg8fvMfVBfNSJ-f8bNca69AuE>
Subject: Re: [CFRG] Comment on draft-irtf-cfrg-hash-to-curve-10
X-BeenThere: cfrg@irtf.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Crypto Forum Research Group <cfrg.irtf.org>
List-Unsubscribe: <https://www.irtf.org/mailman/options/cfrg>, <mailto:cfrg-request@irtf.org?subject=unsubscribe>
List-Archive: <https://mailarchive.ietf.org/arch/browse/cfrg/>
List-Post: <mailto:cfrg@irtf.org>
List-Help: <mailto:cfrg-request@irtf.org?subject=help>
List-Subscribe: <https://www.irtf.org/mailman/listinfo/cfrg>, <mailto:cfrg-request@irtf.org?subject=subscribe>
X-List-Received-Date: Fri, 23 Apr 2021 22:20:11 -0000

Hello,


> But as I said in my prior email, it seems like refactoring Appx. G to
> use divsqrt and adding a few implementations of that function for the
> relevant cases (3 mod 4, 5 mod 8, 9 mod 16, and general, perhaps?) is
> a nice way of cleaning things up. And it seems like the same refactor
> applied to SvdW and Elligator in Appx. G would help, too.

It would help quite a bit.

When I implemented Elligator 2 last year, I tried to read earlier
versions of this draft, and failed. I ended up using the original
instead.

Problem is, the original paper's simple equations involve *two*
exponentiations, while an optimised explicit formula only needs one.
That's a 2x speed difference. But the complexity section G.3.1 is such
that we're easily tempted to just give up and suffer a slower mapping.

For instance: Libsodium. While usually optimised to the brim, it chose
the simpler, slower formula. (It was first added in 2017, before this
draft even existed, but subsequent updates didn't address this.)

Compare the current section G.3.1 with an explicit formula that uses
invsqrt (Taken from my Python code, first gifted by Andrew Moon):

  ufactor = -non_square * sqrtm1
  vfactor = sqrt(ufactor)

  def fast_map_to_curve(r):
    t1 = r**2
    t1 = t1 * non_square
    u  = t1 + fe(1)
    t2 = u**2
    t3 = A**2
    t3 = t3 * t1
    t3 = t3 - t2
    t3 = t3 * A
    t1 = t2 * u
    t1, is_square = invsqrt(t3 * t1)
    u  = r**2
    u  = u * ufactor
    v  = r * vfactor
    if is_square: u = fe(1)
    if is_square: v = fe(1)
    v  = v * t3
    v  = v * t1
    t1 = t1**2
    u  = u * -A
    u  = u * t3
    u  = u * t2
    u  = u * t1
    if is_square != v.is_negative():
        v = -v
    return (u, v)

Quite a bit shorter, and needs fewer variables.

Personally, I'm looking forward to that refactoring. It has the
potential to make implementers' lives significantly easier.

Loup.