sealer: Utils for reading commitments from cache + unit tests
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package commitment
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const treeDFile = "sc-02-data-tree-d.dat"
|
||||
|
||||
// TreeDCommD reads CommD from tree-d
|
||||
func TreeDCommD(cache string) ([32]byte, error) {
|
||||
// Open the tree-d file for reading
|
||||
file, err := os.Open(filepath.Join(cache, treeDFile))
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
defer file.Close() // nolint:errcheck
|
||||
|
||||
// Seek to 32 bytes from the end of the file
|
||||
_, err = file.Seek(-32, io.SeekEnd)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
// Read the last 32 bytes
|
||||
var commD [32]byte
|
||||
_, err = file.Read(commD[:])
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
return commD, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package commitment
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/triplewz/poseidon"
|
||||
ff "github.com/triplewz/poseidon/bls12_381"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
const pauxFile = "p_aux"
|
||||
|
||||
func CommR(commC, commRLast [32]byte) ([32]byte, error) {
|
||||
// reverse commC and commRLast so that endianness is correct
|
||||
for i, j := 0, len(commC)-1; i < j; i, j = i+1, j-1 {
|
||||
commC[i], commC[j] = commC[j], commC[i]
|
||||
commRLast[i], commRLast[j] = commRLast[j], commRLast[i]
|
||||
}
|
||||
|
||||
input_a := new(big.Int)
|
||||
input_a.SetBytes(commC[:])
|
||||
input_b := new(big.Int)
|
||||
input_b.SetBytes(commRLast[:])
|
||||
input := []*big.Int{input_a, input_b}
|
||||
|
||||
cons, err := poseidon.GenPoseidonConstants(3)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
h1, err := poseidon.Hash(input, cons, poseidon.OptimizedStatic)
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
h1element := new(ff.Element).SetBigInt(h1).Bytes()
|
||||
|
||||
// reverse the bytes so that endianness is correct
|
||||
for i, j := 0, len(h1element)-1; i < j; i, j = i+1, j-1 {
|
||||
h1element[i], h1element[j] = h1element[j], h1element[i]
|
||||
}
|
||||
|
||||
return h1element, nil
|
||||
}
|
||||
|
||||
// PAuxCommR reads p_aux and computes CommR
|
||||
func PAuxCommR(cache string) ([32]byte, error) {
|
||||
commCcommRLast, err := os.ReadFile(filepath.Join(cache, pauxFile))
|
||||
if err != nil {
|
||||
return [32]byte{}, err
|
||||
}
|
||||
|
||||
if len(commCcommRLast) != 64 {
|
||||
return [32]byte{}, xerrors.Errorf("invalid commCcommRLast length %d", len(commCcommRLast))
|
||||
}
|
||||
|
||||
var commC, commRLast [32]byte
|
||||
copy(commC[:], commCcommRLast[:32])
|
||||
copy(commRLast[:], commCcommRLast[32:])
|
||||
|
||||
return CommR(commC, commRLast)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package commitment
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCommR(t *testing.T) {
|
||||
var commC = [32]byte{
|
||||
0x09, 0x1e, 0x07, 0x3b, 0x98, 0x2f, 0x66, 0xf0,
|
||||
0x13, 0xc0, 0x26, 0xda, 0x6e, 0x54, 0xd8, 0x7d,
|
||||
0xbf, 0x8b, 0xba, 0x84, 0x8e, 0xf5, 0x7a, 0x55,
|
||||
0x29, 0xc7, 0xe7, 0xf7, 0x2c, 0x82, 0x88, 0x43,
|
||||
}
|
||||
|
||||
var commRLast = [32]byte{
|
||||
0xf0, 0xc5, 0x78, 0x5c, 0x6c, 0x8c, 0xf6, 0x2d,
|
||||
0x96, 0x8b, 0x1e, 0xcd, 0x68, 0xed, 0xb9, 0xd9,
|
||||
0x1e, 0xb9, 0x44, 0x5c, 0x78, 0x58, 0xa6, 0x00,
|
||||
0x26, 0xf8, 0x82, 0x68, 0x60, 0xf7, 0xe7, 0x68,
|
||||
}
|
||||
|
||||
res, err := CommR(commC, commRLast)
|
||||
require.NoError(t, err)
|
||||
|
||||
var expected = [32]byte{
|
||||
0xe6, 0x74, 0xd1, 0x9e, 0x6c, 0xe7, 0xfc, 0xf3,
|
||||
0x3b, 0xbf, 0xd9, 0xb3, 0x43, 0xa0, 0xce, 0xb1,
|
||||
0x2d, 0x28, 0x31, 0xd1, 0xda, 0x54, 0x31, 0x61,
|
||||
0x89, 0x1e, 0xbc, 0xca, 0xd2, 0xc6, 0xdb, 0x01,
|
||||
}
|
||||
|
||||
require.Equal(t, res, expected)
|
||||
}
|
||||
Reference in New Issue
Block a user