Skip to content

Commit

Permalink
Iterate on air private input with ecdsa
Browse files Browse the repository at this point in the history
  • Loading branch information
har777 committed Aug 14, 2024
1 parent 48642b6 commit 239d082
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
51 changes: 49 additions & 2 deletions pkg/runners/zero/zero.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"`
Expand Down Expand Up @@ -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"`
}
16 changes: 8 additions & 8 deletions pkg/vm/builtins/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand All @@ -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
Expand Down

0 comments on commit 239d082

Please sign in to comment.