pub struct Secp256r1 {
    /// The underlying raw `[u8; 64]` data of the signature.
    bits: [u8; 64],
}
Expand description

A secp256r1 signature.

Fields

bits: [u8; 64]

The underlying raw [u8; 64] data of the signature.

Implementations

pub fn new() -> Self

Creates a zeroed out instances of a Secp256r1 signature.

Returns

[Secp256r1] - A zero secp256r1 signature.

Examples

use std::crypto::Secp256r1;

fn foo() {
    let new_secp256r1 = Secp256r1::new();
    assert(new_secp256r1.bits()[0] == 0u8);
    assert(new_secp256r1.bits()[63] == 0u8);
}

pub fn bits(self) -> [u8; 64]

Returns the underlying raw [u8; 64] data of the signature.

Returns

  • [[u8; 64]] - The raw data of the signature.

Examples

use std::crypto::Secp256r1;

fn foo() -> {
    let new_secp256r1 = Secp256r1::new();
    assert(new_secp256r1.bits()[0] == 0u8);
}

pub fn recover(
self,
message: Message,
) -> Result<PublicKey, SignatureError>

Recover the public key derived from the private key used to sign a message.
Returns a Result to let the caller choose an error handling strategy.

Additional Information

Follows the Secp256r1 elliptical curve.

Arguments

  • message: [Message] - The signed data.

Returns

  • [Result<PublicKey, SignatureError>] - The recovered public key or an error.

Examples

use std::crypto::{Message, PublicKey, Secp256r1};

fn foo() {
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
    let public_key: PublicKey = PublicKey::from((
        0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E,
        0xC44415635160ACFC87A84300EED97928C949A2D958FC0947C535F7539C59AE75
     ));

    // A recovered public key pair.
    let result_public_key = signature.recover(message);

    assert(result_public_key.is_ok());
    assert(result_public_key.unwrap() == public_key);
}

pub fn address(
self,
message: Message,
) -> Result<Address, SignatureError>

Recover the address derived from the private key used to sign a message.
Returns a Result to let the caller choose an error handling strategy.

Additional Information

Follows the Secp256r1 elliptical curve.

Arguments

  • message: [Message] - The signed data.

Returns

  • [Result<Address, SignatureError>] - The recovered Fuel address or an error.

Examples

use std::crypto::{Message, Secp256r1};

fn foo() {
    let address = Address::from(0x7AAE2D980BE4C3275C72CE5B527FA23FFB97B766966559DD062E2B78FD9D3766);
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);

    // A recovered Fuel address.
    let result_address = signature.address(message);

    assert(result_address.is_ok());
    assert(result_address.unwrap() == address);
}

pub fn evm_address(
self,
message: Message,
) -> Result<EvmAddress, SignatureError>

Recover the EVM address derived from the private key used to sign a message.
Returns a Result to let the caller choose an error handling strategy.

Additional Information

Follows the Secp256r1 elliptical curve.

Arguments

  • message: [Message] - The signed data.

Returns

  • [Result<EvmAddress, SignatureError>] - The recovered evm address or an error.

Examples

use std::{vm::evm::evm_address::EvmAddress, crypto::{Secp256r1, Message}};

fn foo() {
    let evm_address = EvmAddress::from(0x7AAE2D980BE4C3275C72CE5B527FA23FFB97B766966559DD062E2B78FD9D3766);
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);

    // A recovered evm address.
    let result_address = signature.evm_address(message).unwrap();
    assert(result_address.is_ok());
    assert(result_address.unwrap() == evm_address);
}

pub fn verify(
self,
public_key: PublicKey,
message: Message,
) -> Result<(), SignatureError>

Verify that a signature matches given public key.

Arguments

  • public_key: [PublicKey] - The public key to verify against.
  • message: Message - The signed data.

Returns

  • [Result<(), SignatureError>] - An Ok result or an error.

Examples

use std::crypto::{Message, PublicKey, Secp256r1};

fn foo() {
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
    let public_key: PublicKey = PublicKey::from((
        0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E,
        0xC44415635160ACFC87A84300EED97928C949A2D958FC0947C535F7539C59AE75
    ));

    // A valid result
    let result = signature.verify(public_key, message);
    assert(result.is_ok());
}

pub fn verify_address(
self,
address: Address,
message: Message,
) -> Result<(), SignatureError>

Verify that an signature matches given address.

Arguments

  • address: [Address] - The address to verify against.
  • message: Message - The signed data.

Returns

  • [Result<(), SignatureError>] - An Ok result or an error.

Examples

use std::crypto::{Message, Secp256r1};

fn foo() {
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
    let address = Address::from(0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E);

    // A valid result
    let result = signature.verify_address(address, message);
    assert(result.is_ok());
}

pub fn verify_evm_address(
self,
evm_address: EvmAddress,
message: Message,
) -> Result<(), SignatureError>

Verify that an signature matches given evm address.

Arguments

  • evm_address: [EvmAddress] - The evm address to verify against.
  • message: Message - The signed data.

Returns

  • [Result<(), SignatureError>] - An Ok result or an error.

Examples

use std::{crypto::{Message, Secp256r1}, vm::evm::evm_address::EvmAddress};

fn foo() {
    let signature: Secp256r1 = Secp256r1::from((
        0xbd0c9b8792876713afa8bff383eebf31c43437823ed761cc3600d0016de5110c,
        0x44ac566bd156b4fc71a4a4cb2655d3dd360c695edb17dc3b64d611e122fea23d
    ));
    let message: Message = Message::from(0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323);
    let evm_address = EvmAddress::from(0xD73A188181464CC84AE267E45041AEF6AB938F278E636AA1D02D3014C1BEF74E);

    // A valid result
    let result = signature.verify_evm_address(evm_address, message);
    assert(result.is_ok());
}

Trait Implementations

pub fn abi_encode(self, buffer: Buffer) -> Buffer

pub fn abi_decode(refmut buffer: BufferReader) -> Self

pub fn from(bits: B512) -> Self

pub fn from(components: (b256, b256)) -> Self

pub fn from(array: [u8; 64]) -> Self

pub fn try_from(bytes: Bytes) -> Option<Self>

pub fn into(self) -> B512

pub fn into(self) -> (b256, b256)

pub fn into(self) -> Bytes

pub fn eq(self, other: Self) -> bool

pub fn neq(self, other: Self) -> bool

Evaluates if two values of the same type are not equal.

Additional Information

This function is inherited when eq() is implemented.

Arguments

  • other: [Self] - The value of the same type.

Returns

  • [bool] - true if the two values are not equal, otherwise false.

Examples

struct MyStruct {
    val: u64,
}

impl Eq for MyStruct {
    fn eq(self, other: Self) -> bool {
         self.val == other.val
    }
}

fn foo() {
    let struct1 = MyStruct { val: 10 };
    let struct2 = MyStruct { val: 2 };
    let result = struct1 != struct2;
    assert(result);
}

pub fn hash(
self,
refmut state: Hasher,
)