-
Notifications
You must be signed in to change notification settings - Fork 524
Enhancement: Re-enable fillBytes method in ABI and eval.go implementation
#3856
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -2917,72 +2917,16 @@ func opEd25519VerifyBare(cx *EvalContext) error { | |
| return nil | ||
| } | ||
|
|
||
| // leadingZeros needs to be replaced by big.Int.FillBytes | ||
| func leadingZeros(size int, b *big.Int) ([]byte, error) { | ||
| data := b.Bytes() | ||
| if size < len(data) { | ||
| return nil, fmt.Errorf("insufficient buffer size: %d < %d", size, len(data)) | ||
| byteLength := (b.BitLen() + 7) / 8 | ||
| if size < byteLength { | ||
| return nil, fmt.Errorf("insufficient buffer size: %d < %d", size, byteLength) | ||
| } | ||
| if size == len(data) { | ||
| return data, nil | ||
| } | ||
|
|
||
| buf := make([]byte, size) | ||
| copy(buf[size-len(data):], data) | ||
| b.FillBytes(buf) | ||
| return buf, nil | ||
| } | ||
|
|
||
| // polynomial returns x³ - 3x + b. | ||
| // | ||
| // TODO: remove this when go-algorand is updated to go 1.15+ | ||
| func polynomial(curve *elliptic.CurveParams, x *big.Int) *big.Int { | ||
| x3 := new(big.Int).Mul(x, x) | ||
| x3.Mul(x3, x) | ||
|
|
||
| threeX := new(big.Int).Lsh(x, 1) | ||
| threeX.Add(threeX, x) | ||
|
|
||
| x3.Sub(x3, threeX) | ||
| x3.Add(x3, curve.B) | ||
| x3.Mod(x3, curve.P) | ||
|
|
||
| return x3 | ||
| } | ||
|
|
||
| // unmarshalCompressed converts a point, serialized by MarshalCompressed, into an x, y pair. | ||
| // It is an error if the point is not in compressed form or is not on the curve. | ||
| // On error, x = nil. | ||
| // | ||
| // TODO: remove this and replace usage with elliptic.UnmarshallCompressed when go-algorand is | ||
| // updated to go 1.15+ | ||
| func unmarshalCompressed(curve elliptic.Curve, data []byte) (x, y *big.Int) { | ||
| byteLen := (curve.Params().BitSize + 7) / 8 | ||
| if len(data) != 1+byteLen { | ||
| return nil, nil | ||
| } | ||
| if data[0] != 2 && data[0] != 3 { // compressed form | ||
| return nil, nil | ||
| } | ||
| p := curve.Params().P | ||
| x = new(big.Int).SetBytes(data[1:]) | ||
| if x.Cmp(p) >= 0 { | ||
| return nil, nil | ||
| } | ||
| // y² = x³ - 3x + b | ||
| y = polynomial(curve.Params(), x) | ||
| y = y.ModSqrt(y, p) | ||
| if y == nil { | ||
| return nil, nil | ||
| } | ||
| if byte(y.Bit(0)) != data[0]&1 { | ||
| y.Neg(y).Mod(y, p) | ||
| } | ||
| if !curve.IsOnCurve(x, y) { | ||
| return nil, nil | ||
| } | ||
| return | ||
| } | ||
|
|
||
| var ecdsaVerifyCosts = map[byte]int{ | ||
| byte(Secp256k1): 1700, | ||
| byte(Secp256r1): 2500, | ||
|
|
@@ -3070,7 +3014,7 @@ func opEcdsaPkDecompress(cx *EvalContext) error { | |
| return fmt.Errorf("invalid pubkey") | ||
| } | ||
| } else if fs.field == Secp256r1 { | ||
| x, y = unmarshalCompressed(elliptic.P256(), pubkey) | ||
| x, y = elliptic.UnmarshalCompressed(elliptic.P256(), pubkey) | ||
|
Contributor
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. Should we retest the speed of decompress to reconsider opcode cost? Or was our implementation the same as this library function?
Contributor
Author
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. The previous |
||
| if x == nil { | ||
| return fmt.Errorf("invalid compressed pubkey") | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.