implement state identifiers

andybalholm
Apr 3, 2023, 11:19 PM
MRVLUEYFWRLDK4LDHGJML3FSNXGXPZWW4CJWWZXF5XCMOPHE32PQC

Dependencies

Change contents

  • file addition: state_test.go (----------)
    [6.1]
    package pijul
    import "testing"
    func TestStateWithOneChange(t *testing.T) {
    s := EmptyState
    s = s.Add(mustHashFromBase32("GX2BR74VP5CYZQ3NNXD2UOMDDL7VQOO5EWE3FJXWCYL4RPZB7W6AC"))
    got := s.String()
    want := "L5L7BAORAWMJK627L3SWYN4MFG7AHGRYHXDEVMADSSYLFC2OWNMQC"
    if got != want {
    t.Fatalf("got %s, want %s", got, want)
    }
    }
  • file addition: state.go (----------)
    [6.1]
    package pijul
    import (
    "fmt"
    "filippo.io/edwards25519"
    )
    // A State represents a set of changes. It is calculated from the hashes of
    // the changes, using the Edwards 25519 curve.
    type State struct {
    p edwards25519.Point
    }
    func (s State) String() string {
    return base32Encoding.EncodeToString(append(s.p.Bytes(), 1))
    }
    func StateFromBase32(b32 string) (State, error) {
    b, err := base32Encoding.DecodeString(b32)
    if err != nil {
    return State{}, err
    }
    if len(b) != 33 {
    return State{}, fmt.Errorf("expected 33 bytes, got %d", len(b))
    }
    if b[32] != 1 {
    return State{}, fmt.Errorf("expected Ed25519 state, got type %d", b[32])
    }
    var p edwards25519.Point
    _, err = p.SetBytes(b[:32])
    if err != nil {
    return State{}, err
    }
    return State{p}, nil
    }
    func mustStateFromBase32(b32 string) State {
    s, err := StateFromBase32(b32)
    if err != nil {
    panic(err)
    }
    return s
    }
    // EmptyState is the State that represents the empty set.
    var EmptyState = mustStateFromBase32("LBTGMZTGMZTGMZTGMZTGMZTGMZTGMZTGMZTGMZTGMZTGMZTGMZTAC")
    func (s State) Add(h Hash) State {
    var wideBytes [64]byte
    copy(wideBytes[:], h[:])
    var scalar edwards25519.Scalar
    scalar.SetUniformBytes(wideBytes[:])
    p := s.p
    p.ScalarMult(&scalar, &p)
    return State{p}
    }
  • replacement in pristine.go at line 108
    [3.973][3.973:1027]()
    makeEdge(change, EdgeFlagsBlock, oldBlock, newBlock)
    [3.973]
    [3.1027]
    makeEdge(change, 0, oldBlock, newBlock)
  • edit in go.sum at line 4
    [2.286]
    [2.286]
    filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
    filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
  • edit in go.mod at line 12
    [2.10626]
    [7.11950]
    filippo.io/edwards25519 v1.0.0 // indirect