phoreproject / bls

Go implementation of the BLS12-381 pairing
Apache License 2.0
89 stars 31 forks source link

Wrong result with exponentiation/inverse? #16

Open Isolus opened 4 years ago

Isolus commented 4 years ago

I have written some tests to better understand the library. I came across a problem. In my opinion according to the laws of exponentiation ( (a^b)^c = a^(bc) ), all tests should return true since a^(b inv(b)) = a. But the second and fourth don't.

Is there a fault in my reasoning or is this a bug?

    key, err := bls.RandFQ12(rand.Reader)
    if err != nil {
        fmt.Println(err)
        return
    }
    k, err := bls.RandFQ(rand.Reader)
    if err != nil {
        fmt.Println(err)
        return
    }
    kInv, b := k.Inverse()
    if !b {
        fmt.Println("no inverse")
        return
    }
    r := k.Copy()
    r.MulAssign(kInv)

    tmp := key.Exp(k.ToRepr()).Exp(kInv.ToRepr())

    fmt.Println("Test 1: ", r.Equals(bls.FQOne))
    fmt.Println("Test 2: ", key.Equals(tmp))

    g := bls.G1AffineOne.Mul(k.ToRepr())
    h := bls.G2AffineOne.Mul(kInv.ToRepr())
    p := bls.Pairing(g, h)
    palt := bls.Pairing(bls.G1AffineOne.ToProjective(), bls.G2AffineOne.ToProjective())

    fmt.Println("Test 3: ", r.Equals(bls.FQOne))
    fmt.Println("Test 4: ", p.Equals(palt))
    fmt.Println("Test 5: ", p.Equals(palt.Exp(k.ToRepr()).Exp(kInv.ToRepr())))