Skip to content
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

Add new crypto features #852

Merged
merged 19 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Rename valid field in public key to isValid
  • Loading branch information
SupunS committed Apr 28, 2021
commit d77a9abe2053acc322b2a1c05a63d749e04a6210
2 changes: 1 addition & 1 deletion docs/language/accounts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ struct AccountKey {
struct PublicKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can PublicKey fields (data, signature algo, isValid..) be updated after the creation of the object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, they are defined as constant fields (i.e. read-only).

let publicKey: [UInt8]
let signatureAlgorithm: SignatureAlgorithm
let valid: Bool
let isValid: Bool

/// Verifies a signature. Checks whether the signature was produced by signing
/// the given tag and data, using this public key and the given hash algorithm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not quite what the verification checks, I suggest updating the text to be more precise:

    ///  Verifies a signature under the given tag, data and public key. It uses the given hash algorithm to hash the tag and data. 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another point, I'm not sure if this is right place to mention that all verifications with an invalid public key will fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be fine to add it here, or we can also add it as a note outside of the code block.

Expand Down
16 changes: 8 additions & 8 deletions runtime/account_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ var accountKeyA = &AccountKey{
PublicKey: &PublicKey{
PublicKey: []byte{1, 2, 3},
SignAlgo: sema.SignatureAlgorithmECDSA_P256,
Valid: false,
IsValid: false,
Validated: true,
},
HashAlgo: sema.HashAlgorithmSHA3_256,
Expand All @@ -284,7 +284,7 @@ var accountKeyB = &AccountKey{
PublicKey: &PublicKey{
PublicKey: []byte{4, 5, 6},
SignAlgo: sema.SignatureAlgorithmECDSA_secp256k1,
Valid: false,
IsValid: false,
Validated: false,
},
HashAlgo: sema.HashAlgorithmSHA3_256,
Expand Down Expand Up @@ -1049,7 +1049,7 @@ func TestPublicKey(t *testing.T) {
assert.Contains(t, err.Error(), "value of type `PublicKey` has no member `validate`")
})

t.Run("valid field", func(t *testing.T) {
t.Run("IsValid", func(t *testing.T) {
for _, validity := range []bool{true, false} {
script := `
pub fun main(): Bool {
Expand All @@ -1058,7 +1058,7 @@ func TestPublicKey(t *testing.T) {
signatureAlgorithm: SignatureAlgorithm.ECDSA_P256
)

return publicKey.valid
return publicKey.isValid
}
`
invoked := false
Expand All @@ -1080,7 +1080,7 @@ func TestPublicKey(t *testing.T) {
}
})

t.Run("valid - publicKey from host env", func(t *testing.T) {
t.Run("IsValid - publicKey from host env", func(t *testing.T) {

storage := newTestAccountKeyStorage()
storage.keys = append(storage.keys, accountKeyA, accountKeyB)
Expand All @@ -1091,7 +1091,7 @@ func TestPublicKey(t *testing.T) {
// Get a public key from host env
let acc = getAccount(0x02)
let publicKey = acc.keys.get(keyIndex: %d)!.publicKey
return publicKey.valid
return publicKey.isValid
}
`, index)

Expand All @@ -1110,11 +1110,11 @@ func TestPublicKey(t *testing.T) {
// If already validated, then the validation func shouldn't get re-invoked
assert.NotEqual(t, key.PublicKey.Validated, invoked)

// If validated, `isValid` should have the same value as `publicKey.Valid`.
// If validated, `isValid` should have the same value as `publicKey.IsValid`.
// Otherwise, it should give the value returned by the `validate()` func.
isValid := validateMethodReturnValue
if key.PublicKey.Validated {
isValid = key.PublicKey.Valid
isValid = key.PublicKey.IsValid
}

assert.Equal(t, cadence.Bool(isValid), value)
Expand Down
4 changes: 2 additions & 2 deletions runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -8172,8 +8172,8 @@ func NewPublicKeyValue(
Functions: functions,
}

// Validate the public key, and initialize 'valid' field.
publicKeyValue.Fields.Set(sema.PublicKeyValidField, validationFunction(publicKeyValue))
// Validate the public key, and initialize 'isValid' field.
publicKeyValue.Fields.Set(sema.PublicKeyIsValidField, validationFunction(publicKeyValue))

return publicKeyValue

Expand Down
6 changes: 3 additions & 3 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2519,15 +2519,15 @@ func NewPublicKeyFromValue(publicKey *interpreter.CompositeValue) *PublicKey {

// `valid` and `validated` fields
var valid, validated bool
validField, validated := publicKey.Fields.Get(sema.PublicKeyValidField)
validField, validated := publicKey.Fields.Get(sema.PublicKeyIsValidField)
if validated {
valid = bool(validField.(interpreter.BoolValue))
}

return &PublicKey{
PublicKey: byteArray,
SignAlgo: SignatureAlgorithm(signAlgoRawValue.ToInt()),
Valid: valid,
IsValid: valid,
Validated: validated,
}
}
Expand Down Expand Up @@ -2572,7 +2572,7 @@ func newPublicKeyValidationFunction(
return func(publicKeyValue *interpreter.CompositeValue) interpreter.BoolValue {
// If the public key is already validated, avoid re-validating, and return the cached result.
if publicKey.Validated {
return interpreter.BoolValue(publicKey.Valid)
return interpreter.BoolValue(publicKey.IsValid)
}

return validatePublicKey(publicKeyValue, runtimeInterface)
Expand Down
8 changes: 4 additions & 4 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5478,7 +5478,7 @@ var AccountKeyType = func() *CompositeType {
const PublicKeyTypeName = "PublicKey"
const PublicKeyPublicKeyField = "publicKey"
const PublicKeySignAlgoField = "signatureAlgorithm"
const PublicKeyValidField = "valid"
const PublicKeyIsValidField = "isValid"
const PublicKeyVerifyFunction = "verify"

const publicKeyKeyFieldDocString = `
Expand All @@ -5489,7 +5489,7 @@ const publicKeySignAlgoFieldDocString = `
The signature algorithm to be used with the key
`

const publicKeyValidFieldDocString = `
const publicKeyIsValidFieldDocString = `
Flag indicating whether the key is valid
`

Expand Down Expand Up @@ -5521,9 +5521,9 @@ var PublicKeyType = func() *CompositeType {
),
NewPublicConstantFieldMember(
publicKeyType,
PublicKeyValidField,
PublicKeyIsValidField,
BoolType,
publicKeyValidFieldDocString,
publicKeyIsValidFieldDocString,
),
NewPublicFunctionMember(
publicKeyType,
Expand Down
2 changes: 1 addition & 1 deletion runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ type AccountKey struct {
type PublicKey struct {
PublicKey []byte
SignAlgo SignatureAlgorithm
Valid bool
IsValid bool
Validated bool
}