Skip to content

math/big: r.Exp(x, 1, m) wrong if r is initially non-zero #22830

@karalabe

Description

@karalabe

For an exponent of 1, big.Int.Exp returns the correct value only for a 0 recipient, and an off-by-one result for all pre-allocated recipients.

package main

import (
	"fmt"
	"math/big"
)

func main() {
	base := new(big.Int)
	base.SetString("84555555300000000000", 10)

	mod := new(big.Int)
	mod.SetString("66666670001111111111", 10)

	fmt.Printf("%v\n", big.NewInt(0).Exp(base, big.NewInt(1), mod))
	fmt.Printf("%v\n", big.NewInt(1).Exp(base, big.NewInt(1), mod))
}

The result in both cases above should be the same, however, they are 17888885298888888889
vs. 17888885298888888888. Playground: https://play.golang.org/p/uSBvGkeGkN

Issue originally found by @guidovranken, no security implication according to @rsc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions