-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes_test.go
53 lines (43 loc) · 1.16 KB
/
types_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package chainark
import (
"encoding/hex"
"testing"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/uints"
"github.com/consensys/gnark/test"
)
type IDCircuit struct {
FromBytes LinkageID
Bytes []byte
}
func (c *IDCircuit) Define(api frontend.API) error {
fromU8s := LinkageIDFromU8s(api, uints.NewU8Array(c.Bytes), 128) // from U8s
fromU8s.AssertIsEqual(api, c.FromBytes)
t := fromU8s.IsEqual(api, c.FromBytes)
api.AssertIsEqual(t, 1)
u8s, err := fromU8s.ToBytes(api) // to U8s
if err != nil {
return err
}
for i := 0; i < 32; i++ {
api.AssertIsEqual(u8s[i].Val, c.Bytes[i])
}
return nil
}
func TestLinkageID(t *testing.T) {
assert := test.NewAssert(t)
idHex := "18c4c25dc847bbc76fd3ca67fc4c2028dee5263fddcf01de3faddc20f0462d8f"
idBytes, err := hex.DecodeString(idHex)
assert.NoError(err)
circuit := IDCircuit{
FromBytes: PlaceholderLinkageID(2, 128),
Bytes: idBytes,
}
idFromBytes := LinkageIDFromBytes(idBytes, 128) // from bytes
witness := IDCircuit{
FromBytes: idFromBytes,
}
err = test.IsSolved(&circuit, &witness, ecc.BN254.ScalarField())
assert.NoError(err)
}