Skip to content

Instantly share code, notes, and snippets.

@lucasvo
Last active April 23, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasvo/8fe0772be2d23948c2061655a9cf60b1 to your computer and use it in GitHub Desktop.
Save lucasvo/8fe0772be2d23948c2061655a9cf60b1 to your computer and use it in GitHub Desktop.
Precise Proofs Example
// ExampleDocument is a protobuf message
document := documents.ExampleDocument{
Value1: 1,
ValueA: "Foo",
ValueB: "Bar",
ValueBytes1: []byte("foobar"),
}
// The FillSalts method is a helper function that fills all fields with 32
// random bytes. SaltedExampleDocument is a protobuf message that has the
// same structure as ExampleDocument but has all `bytes` field types.
salts := documents.SaltedExampleDocument{}
proofs.FillSalts(&salts)
doctree := proofs.NewDocumentTree()
sha256Hash := sha256.New()
doctree.SetHashFunc(sha256Hash)
doctree.FillTree(&document, &salts)
fmt.Printf("Generated tree: %s\n", doctree.String())
// Output:
// Generated tree: DocumentTree with Hash [k4E4F9xgvzDPtCGE0yM1QRguleSxQX6sZ14VTYAYVTk=] and [5] leaves
proof, _ := doctree.CreateProof("ValueA")
proofJson, _ := json.Marshal(proof)
fmt.Println(string(proofJson))
// Output:
// {"property":"ValueA","value":"Foo","salt":"YSJ0pFJ4fk0gYsCOU2zLC1xAcqSDcw7tdV4M5ydlCNw=","hashes":[{"right":"anfIr8Oa4PjWQsf2qFLIGgFBeBphTI+RGBaKp8F6Fw0="},{"left":"B+/DkYDB2vvYAuw9GTbVk7jpxM2vPddxsbhldM1wOus="},{"right":"hCkGp+gqakfRE1aLg4j4mA9eAvKn0LbulLOAKUVLSCg="}]}
valid, _ := doctree.ValidateProof(&proof)
fmt.Printf("Proof validated: %v\n", valid)
// Output:
// Proof validated: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment