Skip to content

Instantly share code, notes, and snippets.

@vmx
Created April 3, 2023 12:47
Show Gist options
  • Save vmx/df023bc733d6f53c4915638061fbb966 to your computer and use it in GitHub Desktop.
Save vmx/df023bc733d6f53c4915638061fbb966 to your computer and use it in GitHub Desktop.
Filecoin replica encoding in Go

Filecoin replica encoding in Go

The encoding of a unsealed data into the replica data with a sector key is simple field additions. The Rust source is at https://github.com/filecoin-project/rust-fil-proofs/blob/fcb22da26b2ac6ea6acafd1da41cc4f098ae332b/storage-proofs-porep/src/encode.rs.

The following is a translation into a Python-like pseudo code, where I add the Go functions to call as comments.

def encode(key_bytes, value_bytes):
    '''`key_bytes` and `value_bytes` are each 32 bytes long.
    '''
    # https://pkg.go.dev/github.com/consensys/gnark-crypto@v0.10.0/ecc/bls12-381/fr#Element.SetBytes
    key = key_bytes.to_field_element()
    value = value.bytes.to_field_element()

    # https://pkg.go.dev/github.com/consensys/gnark-crypto@v0.10.0/ecc/bls12-381/fr#Element.Add
    key += value

decode is the same, except that the value is subtracted from the key.

Some background information. We operate on an elliptic curve called BLS12-381. We are using the Scalar field, in this context often referred as Fr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment