Skip to content

bug: Error multiplying a twistededwards point by scalar zero #1547

@lucasmenendez

Description

@lucasmenendez

Some of the hints used to perform an efficient ScalarMul operation over TwistedEdwards curve point does not check if the scalar used to the multiplication is zero, causing a division by zero.

Description

Here is a gist with a simple test and the current output: div_zero_test.go

The route of the error is the following:

  1. func (c *curve) ScalarMul(p1 Point, scalar frontend.Variable) Point {
  2. https://github.com/Consensys/gnark/blob/master/std/algebra/native/twistededwards/point.go#L130
  3. https://github.com/Consensys/gnark/blob/master/std/algebra/native/twistededwards/point.go#L245
  4. func halfGCD(mod *big.Int, inputs, outputs []*big.Int) error {
  5. https://github.com/Consensys/gnark-crypto/blob/master/ecc/utils.go#L102

Expected Behavior

If the scalar used to the multiplication is zero, the result should be the Identity Point of the curve.

Possible Fix

Add something similar to this in

glvBasis := new(ecc.Lattice)
:

if inputs[0].Sign() == 0 {
    outputs[0].SetUint64(0) // s1
    outputs[1].SetUint64(0) // s2
    return nil
}

Steps to Reproduce

Check the gists with the test: div_zero_test.go

Context

We are trying to re-encrypt a frontend.Variable with elGamal, which involves to add a encrypted zero. The bug comes when we tried to encrypt a zero value.

Your Environment

  • gnark version used (e.g. v0.8.1, HEAD@develop): HEAD@master
  • gnark-crypto version used: v0.18.0
  • go version (e.g. 1.20.6): v1.24.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions