From 3072bf12a97651c780e8deacff07233772d36c09 Mon Sep 17 00:00:00 2001 From: ElijahVlasov Date: Thu, 7 Sep 2023 08:05:31 +0100 Subject: [PATCH] Feature: bit count via math.bits library --- pkg/vm/instruction.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/vm/instruction.go b/pkg/vm/instruction.go index ca7ce0b0a..b9867c694 100644 --- a/pkg/vm/instruction.go +++ b/pkg/vm/instruction.go @@ -2,6 +2,7 @@ package vm import ( "fmt" + "math/bits" f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -155,13 +156,13 @@ func decodeInstructionFlags(instruction *Instruction, flags uint16) error { instruction.DstRegister = Register((flags >> dstRegBit) & 1) instruction.Op0Register = Register((flags >> op0RegBit) & 1) - op1Addr, err := oneHot((flags>>op1ImmBit)&1, (flags>>op1FpBit)&1, (flags>>op1ApBit)&1) + op1Addr, err := oneHot(flags&(1<>pcJumpAbsBit)&1, (flags>>pcJumpRelBit)&1, (flags>>pcJnzBit)&1) + pcUpdate, err := oneHot(flags&(1<>resAddBit)&1, (flags>>resMulBit)&1) + res, err := oneHot(flags&(1<>apAddBit)&1, (flags>>apAdd1Bit)&1) + apUpdate, err := oneHot(flags&(1<>opcodeCallBit)&1, (flags>>opcodeRetBit)&1, (flags>>opcodeAssertEqBit)&1) + opcode, err := oneHot(flags&(1< 1 { + digits := make([]uint8, 0) + bin = bin >> offset - if bit == 1 { - setBit = i + for i := 0; i < int(maxLen); i++ { + digits = append(digits, uint8(bin%2)) + bin >>= 1 } + return 0, fmt.Errorf("decoding wrong sequence of bits: %b", digits) } - if checkSum > 1 { - return 0, fmt.Errorf("decoding wrong sequence of bits: %v", bits) + len := bits.Len16(bin) + + if len == 0 { + return maxLen, nil } - return uint16(setBit), nil + return uint16(len-int(offset)) - 1, nil }