cloudflare / circl

CIRCL: Cloudflare Interoperable Reusable Cryptographic Library
http://blog.cloudflare.com/introducing-circl
Other
1.27k stars 142 forks source link

Bytes() and SetBytes() are not match in BLS12381 G1 and G2 #448

Closed hhhguany closed 1 year ago

hhhguany commented 1 year ago

I did a test and when serializing the G1 generator element variable and then deserializing to another variable, the two variables were matched. But once I perform an operation on the G1 element (e.g. Add), the two variables no longer match. Is this a bug?

// a == x
a := e.G1Generator()
x := new(e.G1)
x.SetBytes(a.Bytes())
// a != x
a := e.G1Generator()
a.Add(a,a)
x := new(e.G1)
x.SetBytes(a.Bytes())
// a != x && x == y
a := e.G1Generator()
a.Add(a,a)
x := new(e.G1)
x.SetBytes(a.Bytes())
y := new(e.G1)
y.SetBytes(x.Bytes())
bwesterb commented 1 year ago

Thanks for reporting. I can't reproduce the issue.

$ git diff
diff --git a/ecc/bls12381/g1_test.go b/ecc/bls12381/g1_test.go
index 2f85211..442c7a3 100644
--- a/ecc/bls12381/g1_test.go
+++ b/ecc/bls12381/g1_test.go
@@ -278,3 +278,13 @@ func TestG1Bytes(t *testing.T) {
                }
        }
 }
+
+func TestIssue448(t *testing.T) {
+       a := G1Generator()
+       a.Add(a, a)
+       x := new(G1)
+       x.SetBytes(a.Bytes())
+       if !x.IsEqual(a) {
+               t.Fatal()
+       }
+}
$ go test -run=Issue448 -v
=== RUN   TestIssue448
--- PASS: TestIssue448 (0.00s)
PASS
ok      github.com/cloudflare/circl/ecc/bls12381    0.099s
armfazh commented 1 year ago

Looking at this execution https://go.dev/play/p/QbdDq5E7o1- all the variables match.

Make sure you have the latest version, or compile with the branch main. Let us know, if you are using a different architecture.

Also use the IsEqual function to compare values. Using operator == is not the right way to compare elements.

hhhguany commented 1 year ago

Thank you, the problem has been resolved. I used the wrong comparison function.