-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: check SR from batch proofs matches the SR from the DS #14
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,13 +2,18 @@ package prover | |||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"context" | ||||||||||||||
"encoding/json" | ||||||||||||||
"errors" | ||||||||||||||
"fmt" | ||||||||||||||
"math/big" | ||||||||||||||
"net" | ||||||||||||||
"strconv" | ||||||||||||||
"time" | ||||||||||||||
|
||||||||||||||
"github.com/0xPolygon/cdk/config/types" | ||||||||||||||
"github.com/0xPolygon/cdk/log" | ||||||||||||||
"github.com/ethereum/go-ethereum/common" | ||||||||||||||
"github.com/iden3/go-iden3-crypto/poseidon" | ||||||||||||||
) | ||||||||||||||
|
||||||||||||||
var ( | ||||||||||||||
|
@@ -240,13 +245,17 @@ func (p *Prover) CancelProofRequest(proofID string) error { | |||||||||||||
|
||||||||||||||
// WaitRecursiveProof waits for a recursive proof to be generated by the prover | ||||||||||||||
// and returns it. | ||||||||||||||
func (p *Prover) WaitRecursiveProof(ctx context.Context, proofID string) (string, error) { | ||||||||||||||
func (p *Prover) WaitRecursiveProof(ctx context.Context, proofID string) (string, common.Hash, error) { | ||||||||||||||
res, err := p.waitProof(ctx, proofID) | ||||||||||||||
if err != nil { | ||||||||||||||
return "", err | ||||||||||||||
return "", common.Hash{}, err | ||||||||||||||
} | ||||||||||||||
stateRoot, err := GetStateRootFromProof(res.Proof.(*GetProofResponse_RecursiveProof).RecursiveProof) | ||||||||||||||
if err != nil { | ||||||||||||||
return "", common.Hash{}, err | ||||||||||||||
} | ||||||||||||||
resProof := res.Proof.(*GetProofResponse_RecursiveProof) | ||||||||||||||
return resProof.RecursiveProof, nil | ||||||||||||||
return resProof.RecursiveProof, stateRoot, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// WaitFinalProof waits for the final proof to be generated by the prover and | ||||||||||||||
|
@@ -325,3 +334,51 @@ func (p *Prover) call(req *AggregatorMessage) (*ProverMessage, error) { | |||||||||||||
} | ||||||||||||||
return res, nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// GetStateRootFromProof returns the state root from the proof. | ||||||||||||||
func GetStateRootFromProof(proof string) (common.Hash, error) { | ||||||||||||||
type Publics struct { | ||||||||||||||
Publics []string `mapstructure:"publics"` | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
var publics Publics | ||||||||||||||
err := json.Unmarshal([]byte(proof), &publics) | ||||||||||||||
if err != nil { | ||||||||||||||
log.Errorf("Error unmarshalling proof: %v", err) | ||||||||||||||
return common.Hash{}, err | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
var v [8]uint64 | ||||||||||||||
var j = 0 | ||||||||||||||
for i := 19; i < 19+8; i++ { | ||||||||||||||
u64, err := strconv.ParseInt(publics.Publics[i], 10, 64) | ||||||||||||||
if err != nil { | ||||||||||||||
log.Fatal(err) | ||||||||||||||
Comment on lines
+363
to
+364
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick
Suggested change
|
||||||||||||||
} | ||||||||||||||
v[j] = uint64(u64) | ||||||||||||||
j++ | ||||||||||||||
} | ||||||||||||||
bigSR := fea2scalar(v[:]) | ||||||||||||||
hexSR := fmt.Sprintf("%x", bigSR) | ||||||||||||||
if len(hexSR)%2 != 0 { | ||||||||||||||
hexSR = "0" + hexSR | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return common.HexToHash(hexSR), nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// fea2scalar converts array of uint64 values into one *big.Int. | ||||||||||||||
func fea2scalar(v []uint64) *big.Int { | ||||||||||||||
if len(v) != poseidon.NROUNDSF { | ||||||||||||||
return big.NewInt(0) | ||||||||||||||
} | ||||||||||||||
res := new(big.Int).SetUint64(v[0]) | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[1]), 32)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[2]), 64)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[3]), 96)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[4]), 128)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[5]), 160)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[6]), 192)) //nolint:gomnd | ||||||||||||||
res.Add(res, new(big.Int).Lsh(new(big.Int).SetUint64(v[7]), 224)) //nolint:gomnd | ||||||||||||||
return res | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package prover_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/0xPolygon/cdk/aggregator/prover" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const ( | ||
dir = "../../test/vectors/proofs" | ||
) | ||
|
||
type TestStateRoot struct { | ||
Publics []string `mapstructure:"publics"` | ||
} | ||
|
||
func TestCalculateStateRoots(t *testing.T) { | ||
var expectedStateRoots = map[string]string{ | ||
"1871.json": "0x0ed594d8bc0bb38f3190ff25fb1e5b4fe1baf0e2e0c1d7bf3307f07a55d3a60f", | ||
"1872.json": "0xb6aac97ebb0eb2d4a3bdd40cfe49b6a22d42fe7deff1a8fae182a9c11cc8a7b1", | ||
"1873.json": "0x6f88be87a2ad2928a655bbd38c6f1b59ca8c0f53fd8e9e9d5806e90783df701f", | ||
"1874.json": "0x6f88be87a2ad2928a655bbd38c6f1b59ca8c0f53fd8e9e9d5806e90783df701f", | ||
"1875.json": "0xf4a439c5642a182d9e27c8ab82c64b44418ba5fa04c175a013bed452c19908c9"} | ||
|
||
// Read all files in the directory | ||
files, err := os.ReadDir(dir) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
// Read the file | ||
data, err := os.ReadFile(fmt.Sprintf("%s/%s", dir, file.Name())) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Get the state root from the batch proof | ||
fileStateRoot, err := prover.GetStateRootFromProof(string(data)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Get the expected state root | ||
expectedStateRoot, ok := expectedStateRoots[file.Name()] | ||
if !ok { | ||
log.Fatal("Expected state root not found") | ||
} | ||
|
||
// Compare the state roots | ||
require.Equal(t, expectedStateRoot, fileStateRoot.String(), "State roots do not match") | ||
} | ||
} |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract 19 to some constant (probably also 8).