diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 7bdd2357..7a0eff81 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -212,7 +212,7 @@ func main() { if err != nil { return err } - airPrivateInputJson, err := json.MarshalIndent(airPrivateInput, "", " ") + airPrivateInputJson, err := json.MarshalIndent(airPrivateInput, "", " ") if err != nil { return err } diff --git a/pkg/runners/zero/zero.go b/pkg/runners/zero/zero.go index 0646de41..99117a16 100644 --- a/pkg/runners/zero/zero.go +++ b/pkg/runners/zero/zero.go @@ -714,12 +714,47 @@ func (runner *ZeroRunner) GetAirPrivateInput(tracePath, memoryPath string) (AirP } case "ecdsa": { + ecdsaRunner, ok := bRunner.Runner.(*builtins.ECDSA) + if !ok { + return AirPrivateInput{}, fmt.Errorf("expected ECDSARunner") + } + + builtinValues := make([]AirPrivateBuiltinECDSA, 0) + for addrOffset, signature := range ecdsaRunner.Signatures { + idx := addrOffset / builtins.CellsPerECDSA + pubKey, err := builtinSegment.Read(addrOffset) + if err != nil { + return AirPrivateInput{}, err + } + msg, err := builtinSegment.Read(addrOffset + 1) + if err != nil { + return AirPrivateInput{}, err + } + + pubKeyBig := big.Int{} + msgBig := big.Int{} + pubKey.Felt.BigInt(&pubKeyBig) + msg.Felt.BigInt(&msgBig) + pubKeyHex := fmt.Sprintf("0x%x", &pubKeyBig) + msgHex := fmt.Sprintf("0x%x", &msgBig) + + rBig := new(big.Int).SetBytes(signature.R[:]) + sBig := new(big.Int).SetBytes(signature.S[:]) + frModulusBig, _ := new(big.Int).SetString("3618502788666131213697322783095070105526743751716087489154079457884512865583", 10) + wBig := new(big.Int).ModInverse(sBig, frModulusBig) + signatureInput := AirPrivateBuiltinECDSASignatureInput{ + R: fmt.Sprintf("0x%x", rBig), + W: fmt.Sprintf("0x%x", wBig), + } + + builtinValues = append(builtinValues, AirPrivateBuiltinECDSA{Index: int(idx), PubKey: pubKeyHex, Msg: msgHex, SignatureInput: signatureInput}) + } + airPrivateInput.Ecdsa = builtinValues } } } } - fmt.Println(airPrivateInput) return airPrivateInput, nil } @@ -728,7 +763,7 @@ type AirPrivateInput struct { MemoryPath string `json:"memory_path"` Pedersen []AirPrivateBuiltinPedersen `json:"pedersen"` RangeCheck []AirPrivateBuiltinRangeCheck `json:"range_check"` - Ecdsa []AirPrivateBuiltinRangeCheck `json:"ecdsa"` + Ecdsa []AirPrivateBuiltinECDSA `json:"ecdsa"` Bitwise []AirPrivateBuiltinBitwise `json:"bitwise"` EcOp []AirPrivateBuiltinEcOp `json:"ec_op"` Keccak []AirPrivateBuiltinKeccak `json:"keccak"` @@ -779,3 +814,15 @@ type AirPrivateBuiltinKeccak struct { InputS6 string `json:"input_s6"` InputS7 string `json:"input_s7"` } + +type AirPrivateBuiltinECDSA struct { + Index int `json:"index"` + PubKey string `json:"pubkey"` + Msg string `json:"msg"` + SignatureInput AirPrivateBuiltinECDSASignatureInput `json:"signature_input"` +} + +type AirPrivateBuiltinECDSASignatureInput struct { + R string `json:"r"` + W string `json:"w"` +} diff --git a/pkg/vm/builtins/ecdsa.go b/pkg/vm/builtins/ecdsa.go index 9b11e6f2..587cf8ad 100644 --- a/pkg/vm/builtins/ecdsa.go +++ b/pkg/vm/builtins/ecdsa.go @@ -12,18 +12,18 @@ import ( const ECDSAName = "ecdsa" const inputCellsPerECDSA = 2 -const cellsPerECDSA = 2 +const CellsPerECDSA = 2 const instancesPerComponentECDSA = 1 type ECDSA struct { - signatures map[uint64]ecdsa.Signature + Signatures map[uint64]ecdsa.Signature ratio uint64 } // verify_ecdsa_signature(message_hash, public_key, sig_r, sig_s) func (e *ECDSA) CheckWrite(segment *memory.Segment, offset uint64, value *memory.MemoryValue) error { - ecdsaIndex := offset % cellsPerECDSA + ecdsaIndex := offset % CellsPerECDSA pubOffset := offset - ecdsaIndex msgOffset := pubOffset + 1 @@ -58,7 +58,7 @@ func (e *ECDSA) CheckWrite(segment *memory.Segment, offset uint64, value *memory } pubKey := &ecdsa.PublicKey{A: key} - sig, ok := e.signatures[pubOffset] + sig, ok := e.Signatures[pubOffset] if !ok { return fmt.Errorf("signature is missing from ECDSA builtin") } @@ -117,8 +117,8 @@ Hint that will call this function looks like this: }, */ func (e *ECDSA) AddSignature(pubOffset uint64, r, s *fp.Element) error { - if e.signatures == nil { - e.signatures = make(map[uint64]ecdsa.Signature) + if e.Signatures == nil { + e.Signatures = make(map[uint64]ecdsa.Signature) } bytes := make([]byte, 0, 64) rBytes := r.Bytes() @@ -132,7 +132,7 @@ func (e *ECDSA) AddSignature(pubOffset uint64, r, s *fp.Element) error { return err } - e.signatures[pubOffset] = sig + e.Signatures[pubOffset] = sig return nil } @@ -141,7 +141,7 @@ func (e *ECDSA) String() string { } func (e *ECDSA) GetAllocatedSize(segmentUsedSize uint64, vmCurrentStep uint64) (uint64, error) { - return getBuiltinAllocatedSize(segmentUsedSize, vmCurrentStep, e.ratio, inputCellsPerECDSA, instancesPerComponentECDSA, cellsPerECDSA) + return getBuiltinAllocatedSize(segmentUsedSize, vmCurrentStep, e.ratio, inputCellsPerECDSA, instancesPerComponentECDSA, CellsPerECDSA) } // recoverY recovers the y and -y coordinate of x. True y can be either y or -y