Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion service/apinode/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Run(projectManager *project.Manager, db *apidb.DB, sequencerAddr string, in
slog.Error("failed to get project config", "error", err, "project_id", pid)
continue
}
if cfg.ProofType == "movement" {
if cfg.ProofType == "movement" || cfg.ProofType == "sum" {
prevTaskID := tasks[0].TaskID
tasks[len(tasks)-1].PrevTaskID = prevTaskID
}
Expand Down
38 changes: 25 additions & 13 deletions service/apinode/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,19 +170,31 @@ func (s *httpServer) createTask(c *gin.Context) {
var matchedPubkey *ecdsa.PublicKey
var approved bool
for _, r := range recovered {
addr := crypto.PubkeyToAddress(*r.pubkey)
slog.Debug("recovered address", "project_id", req.ProjectID, "address", addr.String())
ok, err := s.db.IsDeviceApproved(req.ProjectID, addr)
if err != nil {
slog.Error("failed to check device permission", "error", err)
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to check device permission")))
return
}
if ok {
approved = true
matchedPubkey = r.pubkey
sig = r.sig
break
if req.ProjectID == "9" {
addr := crypto.PubkeyToAddress(*r.pubkey)
slog.Debug("recovered address", "project_id", req.ProjectID, "address", addr.String())
ok, err := s.db.IsDeviceApproved(req.ProjectID, addr)
if err != nil {
slog.Error("failed to check device permission", "error", err)
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to check device permission")))
return
}
if ok {
approved = true
matchedPubkey = r.pubkey
sig = r.sig
break
}
} else {
addr := crypto.PubkeyToAddress(*r.pubkey)
deviceAddr := gjson.GetBytes(req.Payload, "address").Str

if strings.EqualFold(addr.Hex(), deviceAddr) {
approved = true
matchedPubkey = r.pubkey
sig = r.sig
break
}
}
}
if !approved {
Expand Down
80 changes: 80 additions & 0 deletions vm/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func loadPayload(tasks []*task.Task, projectConfig *project.Config) ([]byte, err
return encodeLivenessPayload(tasks[0], projectConfig)
case "movement":
return encodeMovementPayload(tasks, projectConfig)
case "sum":
return encodeSumPayload(tasks, projectConfig)
default:
return tasks[0].Payload, nil
}
Expand Down Expand Up @@ -168,3 +170,81 @@ func abs(a, b uint64) uint64 {
}
return b - a
}

const SumMaxItems = 2

type ProofOfSumCircuit struct {
PayloadHashs [SumMaxItems][32]uints.U8
Timestamps [SumMaxItems]frontend.Variable
Values [SumMaxItems]frontend.Variable
SigBytes [SumMaxItems][64]uints.U8

PubBytes [SumMaxItems][65]uints.U8
StartTime frontend.Variable

Threshold frontend.Variable `gnark:",public"`
EthAddress frontend.Variable `gnark:",public"`
}

func (circuit *ProofOfSumCircuit) Define(api frontend.API) error { return nil }

func encodeSumPayload(tasks []*task.Task, projectConfig *project.Config) ([]byte, error) {
if len(tasks) != 1 {
return nil, errors.Errorf("invalid tasks len, expect %d, get %d", 1, len(tasks))
}
assignment := ProofOfSumCircuit{}
task := tasks[0]
if task.PrevTask == nil {
return nil, errors.New("sum project miss previous task")
}
lastPayloadHash, _, _, lastData, err := api.HashTask(
&api.CreateTaskReq{
Nonce: task.PrevTask.Nonce,
ProjectID: task.PrevTask.ProjectID.String(),
ProjectVersion: task.PrevTask.ProjectVersion,
Payload: task.PrevTask.Payload,
}, projectConfig)
if err != nil {
return nil, err
}
curPayloadHash, _, _, curData, err := api.HashTask(
&api.CreateTaskReq{
Nonce: task.Nonce,
ProjectID: task.ProjectID.String(),
ProjectVersion: task.ProjectVersion,
Payload: task.Payload,
}, projectConfig)
if err != nil {
return nil, err
}
lastTimestamp := lastData[0].(uint64)
lastValue := lastData[1].(uint64)
lastSig := task.PrevTask.Signature[:64]
curTimestamp := curData[0].(uint64)
curValue := curData[1].(uint64)
curSig := task.Signature[:64]

assignment.PayloadHashs[0] = [32]uints.U8(uints.NewU8Array(lastPayloadHash[:]))
assignment.Timestamps[0] = lastTimestamp
assignment.Values[0] = lastValue
assignment.SigBytes[0] = [64]uints.U8(uints.NewU8Array(lastSig[:]))
assignment.PayloadHashs[1] = [32]uints.U8(uints.NewU8Array(curPayloadHash[:]))
assignment.Timestamps[1] = curTimestamp
assignment.Values[1] = curValue
assignment.SigBytes[1] = [64]uints.U8(uints.NewU8Array(curSig[:]))
assignment.PubBytes[0] = [65]uints.U8(uints.NewU8Array(task.DevicePubKey))
assignment.PubBytes[1] = [65]uints.U8(uints.NewU8Array(task.DevicePubKey))
assignment.Threshold = uint64(10)

pubkey, err := crypto.UnmarshalPubkey(task.DevicePubKey)
if err != nil {
return nil, errors.Wrap(err, "failed to unmarshal pubkey")
}
assignment.EthAddress = crypto.PubkeyToAddress(*pubkey).Big()

witness, err := frontend.NewWitness(&assignment, ecc.BN254.ScalarField())
if err != nil {
return nil, err
}
return witness.MarshalBinary()
}
Loading