diff --git a/test/built-in/age-zkp.n3s b/test/built-in/age-zkp.n3s new file mode 100644 index 0000000..c86441a --- /dev/null +++ b/test/built-in/age-zkp.n3s @@ -0,0 +1,87 @@ +@prefix xsd: . +@prefix string: . +@prefix math: . +@prefix crypto: . +@prefix log: . +@prefix : . + +# components +:zeroKnowledgeProof a log:Component. +:challengeProof a log:Component. + +# private person data +:Mary :birthDay "2000-01-01"^^xsd:date. +:Pat :birthDay "1970-01-01"^^xsd:date. +:John :birthDay "1960-01-01"^^xsd:date. + +# simulation date +:simulation1 :date "2025-01-11T00:00:00.0Z"^^xsd:dateTime. + +# simulation age +:simulation1 :age "P50Y"^^xsd:duration. + +# prover proving that someone is above some age using a hash and without revealing their age +( _:Name + _:Result + _:Hash + _:BirthDay + _:Date + _:Age + _:Diff + _:Str + _:Context +) log:onNegativeSurface { + () log:onNegativeSurface { + :simulation1 :zeroKnowledgeProof (_:Name _:Result _:Hash). + }. + _:Name :birthDay _:BirthDay. + :simulation1 :date _:Date. + :simulation1 :age _:Age. + (_:Date _:BirthDay) math:difference _:Diff. + ({ _:Diff math:greaterThan _:Age }) string:concatenation _:Str. + _:Str crypto:sha256 _:Hash. + ( + { _:Diff math:greaterThan _:Age } + { _:Result log:equalTo ("proof verified: " _:Name " is more than 50 years old and entitled to work 80% per week")!string:concatenation } + { _:Result log:equalTo ("proof failed: " _:Name " is less than 50 years and not entitled to work 80% per week")!string:concatenation } + ) log:ifThenElseIn (_:Context -1). +}. + +# challenging the proof using the hash +( _:Name + _:Result + _:Hash + _:BirthDay + _:Date + _:Age + _:Diff + _:Str + _:ProofHash + _:Context +) log:onNegativeSurface { + () log:onNegativeSurface { + :simulation1 :challengeProof (_:Name _:Result _:Hash). + }. + _:Name :birthDay _:BirthDay. + :simulation1 :date _:Date. + :simulation1 :age _:Age. + (_:Date _:BirthDay) math:difference _:Diff. + ({ _:Diff math:greaterThan _:Age }) string:concatenation _:Str. + _:Str crypto:sha256 _:ProofHash. + ( + { _:Hash log:equalTo _:ProofHash } + { _:Result log:equalTo ("challenge successful: the proof is valid")!string:concatenation } + { _:Result log:equalTo ("challenge failed: the proof is invalid")!string:concatenation } + ) log:ifThenElseIn (_:Context -1). +}. + +# query +() log:onNegativeSurface { + :simulation1 :zeroKnowledgeProof (:Mary "proof failed: :Mary is less than 50 years and not entitled to work 80% per week" "4f1aa645fc353e36d9fbb3b78d34b97ab34cc1b515dc80276e66ea11acfe2c96"). + :simulation1 :zeroKnowledgeProof (:Pat "proof verified: :Pat is more than 50 years old and entitled to work 80% per week" "5611fb0dfa67e894917f81cf4b24577e4bc89c86c17423ba7fc28181eb368b3b"). + :simulation1 :challengeProof (:Mary "challenge failed: the proof is invalid" "adf23913cd97b2d831b54cf9109c111d205d8dfd777413235bdb1c7c91cf4007"). + :simulation1 :challengeProof (:Pat "challenge successful: the proof is valid" "5611fb0dfa67e894917f81cf4b24577e4bc89c86c17423ba7fc28181eb368b3b"). + () log:onNegativeAnswerSurface { + :test :is true . + } . +} . diff --git a/test/built-in/graph-3-coloring.n3s b/test/built-in/graph-3-coloring.n3s new file mode 100644 index 0000000..6da9492 --- /dev/null +++ b/test/built-in/graph-3-coloring.n3s @@ -0,0 +1,107 @@ +@prefix list: . +@prefix log: . +@prefix : . + +# components +:valid_coloring a log:Component. +:conflict a log:Component. +:permuted_coloring a log:Component. +:challenge_edge a log:Component. +:prover_response a log:Component. +:verify_response a log:Component. + +# graph definition +:a :edge :b. +:b :edge :c. +:c :edge :d. +:d :edge :e. +:e :edge :a. +:a :edge :c. +:b :edge :d. + +# list of allowed colors +true :color :red. +true :color :blue. +true :color :green. + +# check if a coloring is valid for the entire graph (no two connected nodes have the same color) +true :valid_coloring (). +(_:L _:NC _:Rest _:SCOPE) log:onNegativeSurface { + () log:onNegativeSurface { + true :valid_coloring _:L. + }. + _:L list:firstRest (_:NC _:Rest). + ({ _:NC :conflict _:Rest } false true) log:ifThenElseIn (_:SCOPE -1). + true :valid_coloring _:Rest. +}. + +# conflict check: verifies if neighbors have the same color +(_:Node _:Color _:L _:Neighbor _:Any) log:onNegativeSurface { + () log:onNegativeSurface { + (_:Node _:Color) :conflict _:L. + }. + _:L list:firstRest ((_:Neighbor _:Color) _:Any). + _:Node :edge _:Neighbor. +}. + +(_:NC _:L _:Any _:Rest) log:onNegativeSurface { + () log:onNegativeSurface { + _:NC :conflict _:L. + }. + _:L list:firstRest (_:Any _:Rest). + _:NC :conflict _:Rest. +}. + +# Alice's secret valid coloring +true :alice_knows_coloring ((:a :red) (:b :blue) (:c :green) (:d :red) (:e :blue)). + +# prover generates a permuted version of the coloring (hides actual colors) +(_:PermutedColoring _:SecretColoring) log:onNegativeSurface { + () log:onNegativeSurface { + true :permuted_coloring _:PermutedColoring. + }. + true :alice_knows_coloring _:SecretColoring. + _:SecretColoring list:permutation _:PermutedColoring. +}. + +# verifier issues a random edge as a challenge +(_:X _:Y) log:onNegativeSurface { + () log:onNegativeSurface { + true :challenge_edge (_:X _:Y). + }. + _:X :edge _:Y. +}. + +# prover reveals colors for the challenged edge only +(_:X _:Y _:PermutedColoring _:ColorX _:ColorY) log:onNegativeSurface { + () log:onNegativeSurface { + ((_:X _:Y) _:PermutedColoring) :prover_response ((_:X _:ColorX) (_:Y _:ColorY)). + }. + _:PermutedColoring list:member (_:X _:ColorX). + _:PermutedColoring list:member (_:Y _:ColorY). +}. + +# verifier checks if revealed colors are different for the challenged edge +(_:X _:ColorX _:Y _:ColorY _:Result _:SCOPE) log:onNegativeSurface { + () log:onNegativeSurface { + ((_:X _:ColorX) (_:Y _:ColorY)) :verify_response _:Result. + }. + ({ _:ColorX log:notEqualTo _:ColorY } { _:Result log:equalTo :valid } { _:Result log:equalTo :invalid }) log:ifThenElseIn (_:SCOPE -1). +}. + +# query +(_:PermutedColoring _:Edge _:Response _:SCOPE) log:onNegativeSurface { + ( + { + true :permuted_coloring _:PermutedColoring. + true :challenge_edge _:Edge. + (_:Edge _:PermutedColoring) :prover_response _:Response. + } + { + _:Response :verify_response :valid. + } + ) log:forAllIn _:SCOPE. + () log:onNegativeAnswerSurface { + :test :is true. + }. +}.